FolderSyncShell.php 5.6 KB

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