FolderSyncShell.php 5.5 KB

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