ConvertShell.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. if (!defined('CHMOD_PUBLIC')) {
  3. define('CHMOD_PUBLIC', 0770);
  4. }
  5. App::uses('AppShell', 'Console/Command');
  6. /**
  7. * uses dos2unix >= 5.0
  8. * console call: dos2unix [-fhkLlqV] [-c convmode] [-o file ...] [-n inputfile outputfile ...]
  9. *
  10. * @cakephp 2.x
  11. * @author Mark Scherer
  12. * @license MIT
  13. * 2011-11-04 ms
  14. */
  15. class ConvertShell extends AppShell {
  16. /**
  17. * predefined options
  18. */
  19. public $modes = array(
  20. 'd2u', 'u2d', 'git', # dos/unix
  21. 'd2m', 'm2d', # dos/mac
  22. 'u2m', 'm2u' # unix/mac
  23. );
  24. /**
  25. * Shell startup, prints info message about dry run.
  26. *
  27. * @return void
  28. */
  29. public function startup() {
  30. parent::startup();
  31. if ($this->params['dry-run']) {
  32. $this->out(__d('cake_console', '<warning>Dry-run mode enabled!</warning>'), 1, Shell::QUIET);
  33. }
  34. if (!$this->_test()) {
  35. $this->out(__d('cake_console', '<warning>dos2unix not available</warning>'), 1, Shell::QUIET);
  36. }
  37. }
  38. /**
  39. * ConvertShell::folder()
  40. *
  41. * @return void
  42. */
  43. public function folder() {
  44. $this->out('Converting folder...');
  45. $folder = APP;
  46. $mode = $this->params['mode'];
  47. if (empty($mode) || !in_array($mode, $this->modes)) {
  48. return $this->error('Invalid mode', 'Please specify d2u, u2d, git (d2u+u2d) ...');
  49. }
  50. if (!empty($this->args)) {
  51. $folder = array_shift($this->args);
  52. $folder = realpath($folder);
  53. }
  54. if (empty($folder)) {
  55. return $this->error('Invalid dir', 'No valid dir given (either absolute or relative to APP)');
  56. }
  57. $this->_convert($folder, $mode);
  58. $this->out('Done!');
  59. }
  60. public function _test() {
  61. # bug - always outputs the system call right away, no way to catch and surpress it
  62. return true;
  63. ob_start();
  64. system('dos2unix -h', $x);
  65. $output = ob_get_contents();
  66. ob_end_clean();
  67. return !empty($output) && $x === 0;
  68. }
  69. public function _convert($dir, $mode, $excludes = array()) {
  70. $Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
  71. RecursiveIteratorIterator::CHILD_FIRST);
  72. foreach ($Iterator as $path) {
  73. $fullPath = $path->__toString();
  74. $continue = false;
  75. foreach ($excludes as $exclude) {
  76. if (strpos($fullPath, $exclude) === 0) {
  77. $continue = true;
  78. break;
  79. }
  80. }
  81. if ($continue) {
  82. continue;
  83. }
  84. if ($path->isDir()) {
  85. continue;
  86. }
  87. if (strpos($fullPath, DS.'.') !== false) {
  88. continue;
  89. }
  90. if (!empty($this->params['verbose'])) {
  91. $this->out('Converting file: '.$fullPath);
  92. }
  93. if (empty($this->params['dry-run'])) {
  94. ob_start();
  95. if ($mode === 'git') {
  96. system('dos2unix --'.'d2u'.' --skipbin '.$fullPath, $x);
  97. system('dos2unix --'.'u2d'.' --skipbin '.$fullPath, $x);
  98. } else {
  99. system('dos2unix --'.$mode.' --skipbin '.$fullPath, $x);
  100. }
  101. $output = ob_get_contents();
  102. ob_end_clean();
  103. }
  104. }
  105. }
  106. /**
  107. * get the option parser
  108. *
  109. * @return ConsoleOptionParser
  110. */
  111. public function getOptionParser() {
  112. $subcommandParser = array(
  113. 'options' => array(
  114. 'mode' => array(
  115. 'short' => 'm',
  116. 'help' => __d('cake_console', 'Mode'),
  117. 'default' => '' # auto detect
  118. ),
  119. 'ext' => array(
  120. 'short' => 'e',
  121. 'help' => __d('cake_console', 'Specify extensions [php|txt|...]'),
  122. 'default' => '',
  123. ),
  124. 'dry-run'=> array(
  125. 'short' => 'd',
  126. 'help' => __d('cake_console', 'Dry run the clear command, no files will actually be deleted. Should be combined with verbose!'),
  127. 'boolean' => true
  128. ),
  129. 'exclude'=> array(
  130. 'short' => 'x',
  131. 'help' => __d('cake_console', 'exclude the following files or folders'),
  132. 'boolean' => true,
  133. 'default' => ''
  134. )
  135. )
  136. );
  137. return parent::getOptionParser()
  138. ->description(__d('cake_console', "The Convert Shell converts files from dos/unix/mac to another system"))
  139. ->addSubcommand('folder', array(
  140. 'help' => __d('cake_console', 'Convert folder recursivly (Tools.Convert folder [options] [path])'),
  141. 'parser' => $subcommandParser
  142. ));
  143. }
  144. }