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