ConvertShell.php 3.8 KB

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