FolderSyncShell.php 5.7 KB

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