FolderSyncShell.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. App::uses('Folder', 'Utility');
  3. App::uses('File', 'Utility');
  4. App::uses('AppShell', 'Console/Command');
  5. /**
  6. * Folder Sync Shell to update all files from location a with new files from location b
  7. *
  8. * @version 1.0
  9. * @cakephp 2.x
  10. * @author Mark Scherer
  11. * @license MIT
  12. * 2012-07-18 ms
  13. */
  14. class FolderSyncShell extends AppShell {
  15. public $sourceFolder = null;
  16. public $targetFolder = null;
  17. public $files = 0;
  18. public $missing = array();
  19. public $updatedFiles = 0;
  20. public $removedFiles = 0;
  21. public $excludes = array('.git', '.svn');
  22. /**
  23. * main
  24. *
  25. * @return void
  26. * 2012-07-18 ms
  27. */
  28. public function main() {
  29. $this->help();
  30. }
  31. /**
  32. * main
  33. *
  34. * @return void
  35. * 2012-07-18 ms
  36. */
  37. public function update() {
  38. $this->sourceFolder = $this->params['source'];
  39. if (empty($this->sourceFolder)) {
  40. $this->sourceFolder = APP;
  41. if (!empty($this->params['plugin'])) {
  42. $this->sourceFolder = CakePlugin::path($this->params['plugin']);
  43. }
  44. }
  45. if ($this->sourceFolder) {
  46. $this->sourceFolder = realpath($this->sourceFolder);
  47. }
  48. if (!empty($this->params['target'])) {
  49. $this->targetFolder = realpath($this->params['target']);
  50. }
  51. if (!$this->sourceFolder || !is_dir($this->sourceFolder)) {
  52. $this->error('Folder not exists', 'Please specify a valid source folder');
  53. }
  54. if (!$this->targetFolder || !is_dir($this->targetFolder)) {
  55. $this->error('Folder not exists', 'Please specify a valid target folder');
  56. }
  57. $this->out('From: '.$this->sourceFolder);
  58. $this->out('To: '.$this->targetFolder);
  59. $excludes = $this->excludes;
  60. $this->_sync($this->sourceFolder, $this->targetFolder, $excludes);
  61. $this->out(__('Files: %s', $this->files));
  62. $this->out();
  63. if (!empty($this->missing)) {
  64. $this->out(__('%s missing files', count($this->missing)));
  65. foreach ($this->missing as $missing) {
  66. $this->out('- '.$missing);
  67. }
  68. $this->out();
  69. }
  70. if ($this->updatedFiles) {
  71. $this->out(__('%s target files updated', $this->updatedFiles));
  72. }
  73. if ($this->removedFiles) {
  74. $this->out(__('%s source files removed', $this->removedFiles));
  75. }
  76. if (!$this->updatedFiles && !$this->removedFiles) {
  77. $this->out('(nothing to do)');
  78. }
  79. }
  80. protected function _sync($from, $to, $excludes = array()) {
  81. $Folder = new Folder($to);
  82. $content = $Folder->read(true, true, true);
  83. foreach ($content[0] as $folder) {
  84. $targetFolder = $folder;
  85. $folder = str_replace($this->targetFolder, '', $targetFolder);
  86. $sourceFolder = $this->sourceFolder . $folder;
  87. $this->_sync($sourceFolder, $targetFolder, $excludes);
  88. }
  89. foreach ($content[1] as $file) {
  90. $targetFile = $file;
  91. $file = str_replace($this->targetFolder, '', $targetFile);
  92. $sourceFile = $this->sourceFolder . $file;
  93. $this->updateFile($targetFile, $sourceFile, $file);
  94. }
  95. }
  96. /**
  97. * @param target
  98. * @param source - does not have to exists
  99. * @return void;
  100. */
  101. protected function updateFile($target, $source, $name = null) {
  102. if (!empty($this->params['verbose'])) {
  103. if (!$name) {
  104. $name = $target;
  105. }
  106. $this->out('- '.$name);
  107. }
  108. $this->files++;
  109. $sourceExists = file_exists($source);
  110. if (!$sourceExists && !empty($this->params['remove'])) {
  111. if (empty($this->params['dry-run']) && !unlink($target)) {
  112. throw new InternalErrorException('no rights');
  113. }
  114. $this->missing[] = $name;
  115. $this->removedFiles++;
  116. return;
  117. } elseif (!$sourceExists) {
  118. $this->missing[] = $name;
  119. return;
  120. }
  121. if (sha1(file_get_contents($source)) === sha1(file_get_contents($target))) {
  122. return;
  123. }
  124. if (empty($this->params['dry-run']) && !copy($source, $target)) {
  125. throw new InternalErrorException('no rights');
  126. }
  127. $this->updatedFiles++;
  128. }
  129. public function help() {
  130. $head = __("Usage: cake FolderSync <command>") . "\n";
  131. $head .= "-----------------------------------------------\n";
  132. $head .= __("Commands:") . "\n\n";
  133. $head .= "\t" . 'update' . "\n\n";
  134. //$head .= "\t" . 'update' . "\n\n";
  135. $this->out($head);
  136. }
  137. public function getOptionParser() {
  138. $subcommandParser = array(
  139. 'options' => array(
  140. 'source'=> array(
  141. 'short' => 's',
  142. 'help' => __d('cake_console', 'source - defaults to app'),
  143. 'default' => '',
  144. ),
  145. 'target'=> array(
  146. 'short' => 't',
  147. 'help' => __d('cake_console', 'target - required'),
  148. 'default' => '',
  149. ),
  150. 'plugin' => array(
  151. 'short' => 'p',
  152. 'help' => __d('cake_console', 'The plugin folder - can only be used with app as source'),
  153. 'default' => '',
  154. ),
  155. 'remove' => array(
  156. 'short' => 'r',
  157. 'help' => __d('cake_console', 'Remove files if source is non-existent'),
  158. 'boolean' => true
  159. ),
  160. 'dry-run'=> array(
  161. 'short' => 'd',
  162. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  163. 'boolean' => true
  164. ),
  165. 'log'=> array(
  166. 'short' => 'l',
  167. 'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
  168. 'boolean' => true
  169. ),
  170. )
  171. );
  172. return parent::getOptionParser()
  173. ->description(__d('cake_console', "..."))
  174. ->addSubcommand('update', array(
  175. 'help' => __d('cake_console', 'Update'),
  176. 'parser' => $subcommandParser
  177. ));
  178. }
  179. }