FolderSyncShell.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $this->error('Folder not exists', 'Please specify a valid source folder');
  54. }
  55. if (!$this->targetFolder || !is_dir($this->targetFolder)) {
  56. $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. } elseif (!$sourceExists) {
  124. $this->missing[] = $name;
  125. $this->out(' (target missing, skipping)', 1, Shell::VERBOSE);
  126. return;
  127. }
  128. if (sha1(file_get_contents($source)) === sha1(file_get_contents($target))) {
  129. $this->out(' (equal, skipping)', 1, Shell::VERBOSE);
  130. return;
  131. }
  132. if (empty($this->params['dry-run']) && !copy($source, $target)) {
  133. throw new InternalErrorException('no rights');
  134. }
  135. $this->updatedFiles++;
  136. }
  137. public function help() {
  138. $head = __("Usage: cake FolderSync <command>") . "\n";
  139. $head .= "-----------------------------------------------\n";
  140. $head .= __("Commands:") . "\n\n";
  141. $head .= "\t" . 'update' . "\n\n";
  142. //$head .= "\t" . 'update' . "\n\n";
  143. $this->out($head);
  144. }
  145. public function getOptionParser() {
  146. $subcommandParser = array(
  147. 'options' => array(
  148. 'source'=> array(
  149. 'short' => 's',
  150. 'help' => __d('cake_console', 'source - defaults to app'),
  151. 'default' => '',
  152. ),
  153. 'target'=> array(
  154. 'short' => 't',
  155. 'help' => __d('cake_console', 'target - required'),
  156. 'default' => '',
  157. ),
  158. 'plugin' => array(
  159. 'short' => 'p',
  160. 'help' => __d('cake_console', 'The plugin folder - can only be used with app as source'),
  161. 'default' => '',
  162. ),
  163. 'remove' => array(
  164. 'short' => 'r',
  165. 'help' => __d('cake_console', 'Remove files if source is non-existent'),
  166. 'boolean' => true
  167. ),
  168. 'invert' => array(
  169. 'short' => 'i',
  170. 'help' => __d('cake_console', 'Invert direction (target to source)'),
  171. 'boolean' => true
  172. ),
  173. 'dry-run'=> array(
  174. 'short' => 'd',
  175. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  176. 'boolean' => true
  177. ),
  178. 'log'=> array(
  179. 'short' => 'l',
  180. 'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
  181. 'boolean' => true
  182. ),
  183. )
  184. );
  185. return parent::getOptionParser()
  186. ->description(__d('cake_console', "..."))
  187. ->addSubcommand('update', array(
  188. 'help' => __d('cake_console', 'Update'),
  189. 'parser' => $subcommandParser
  190. ));
  191. }
  192. }