FolderSyncShell.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $this->out('From: '.$this->sourceFolder);
  59. $this->out('To: '.$this->targetFolder);
  60. $excludes = $this->excludes;
  61. $this->_sync($this->sourceFolder, $this->targetFolder, $excludes);
  62. $this->out(__('Files: %s', $this->files));
  63. $this->out();
  64. if (!empty($this->missing)) {
  65. $this->out(__('%s missing files', count($this->missing)));
  66. foreach ($this->missing as $missing) {
  67. $this->out('- '.$missing);
  68. }
  69. $this->out();
  70. }
  71. if ($this->updatedFiles) {
  72. $this->out(__('%s target files updated', $this->updatedFiles));
  73. }
  74. if ($this->removedFiles) {
  75. $this->out(__('%s source files removed', $this->removedFiles));
  76. }
  77. if (!$this->updatedFiles && !$this->removedFiles) {
  78. $this->out('(nothing to do)');
  79. }
  80. }
  81. protected function _sync($from, $to, $excludes = array()) {
  82. $Folder = new Folder($to);
  83. $content = $Folder->read(true, true, true);
  84. foreach ($content[0] as $folder) {
  85. $targetFolder = $folder;
  86. $folder = str_replace($this->targetFolder, '', $targetFolder);
  87. $sourceFolder = $this->sourceFolder . $folder;
  88. $this->_sync($sourceFolder, $targetFolder, $excludes);
  89. }
  90. foreach ($content[1] as $file) {
  91. $targetFile = $file;
  92. $file = str_replace($this->targetFolder, '', $targetFile);
  93. $sourceFile = $this->sourceFolder . $file;
  94. $this->updateFile($targetFile, $sourceFile, $file);
  95. }
  96. }
  97. /**
  98. * @param target
  99. * @param source - does not have to exists
  100. * @return void;
  101. */
  102. protected function updateFile($target, $source, $name = null) {
  103. if (!empty($this->params['verbose'])) {
  104. if (!$name) {
  105. $name = $target;
  106. }
  107. $this->out('- '.$name);
  108. }
  109. $this->files++;
  110. $sourceExists = file_exists($source);
  111. if (!$sourceExists && !empty($this->params['remove'])) {
  112. if (empty($this->params['dry-run']) && !unlink($target)) {
  113. throw new InternalErrorException('no rights');
  114. }
  115. $this->missing[] = $name;
  116. $this->removedFiles++;
  117. return;
  118. } elseif (!$sourceExists) {
  119. $this->missing[] = $name;
  120. return;
  121. }
  122. if (sha1(file_get_contents($source)) === sha1(file_get_contents($target))) {
  123. return;
  124. }
  125. if (empty($this->params['dry-run']) && !copy($source, $target)) {
  126. throw new InternalErrorException('no rights');
  127. }
  128. $this->updatedFiles++;
  129. }
  130. public function help() {
  131. $head = __("Usage: cake FolderSync <command>") . "\n";
  132. $head .= "-----------------------------------------------\n";
  133. $head .= __("Commands:") . "\n\n";
  134. $head .= "\t" . 'update' . "\n\n";
  135. //$head .= "\t" . 'update' . "\n\n";
  136. $this->out($head);
  137. }
  138. public function getOptionParser() {
  139. $subcommandParser = array(
  140. 'options' => array(
  141. 'source'=> array(
  142. 'short' => 's',
  143. 'help' => __d('cake_console', 'source - defaults to app'),
  144. 'default' => '',
  145. ),
  146. 'target'=> array(
  147. 'short' => 't',
  148. 'help' => __d('cake_console', 'target - required'),
  149. 'default' => '',
  150. ),
  151. 'plugin' => array(
  152. 'short' => 'p',
  153. 'help' => __d('cake_console', 'The plugin folder - can only be used with app as source'),
  154. 'default' => '',
  155. ),
  156. 'remove' => array(
  157. 'short' => 'r',
  158. 'help' => __d('cake_console', 'Remove files if source is non-existent'),
  159. 'boolean' => true
  160. ),
  161. 'dry-run'=> array(
  162. 'short' => 'd',
  163. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  164. 'boolean' => true
  165. ),
  166. 'log'=> array(
  167. 'short' => 'l',
  168. 'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
  169. 'boolean' => true
  170. ),
  171. )
  172. );
  173. return parent::getOptionParser()
  174. ->description(__d('cake_console', "..."))
  175. ->addSubcommand('update', array(
  176. 'help' => __d('cake_console', 'Update'),
  177. 'parser' => $subcommandParser
  178. ));
  179. }
  180. }