IndentShell.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. //Configure::write('debug', 1);
  3. if (!defined('TB')) {
  4. define('TB', "\t");
  5. }
  6. if (!defined('NL')) {
  7. define('NL', "\n");
  8. }
  9. if (!defined('CR')) {
  10. define('CR', "\r");
  11. }
  12. App::uses('Folder', 'Utility');
  13. App::uses('AppShell', 'Console/Command');
  14. /**
  15. * Indend Shell
  16. *
  17. * @cakephp 2.0
  18. * @author Mark Scherer
  19. * @license MIT
  20. * 2011-11-04 ms
  21. */
  22. class IndentShell extends AppShell {
  23. protected $changes = null;
  24. public $settings = array(
  25. 'files' => array('php', 'ctp', 'inc', 'tpl'),
  26. 'spacesPerTab' => 4,
  27. 'againWithHalf' => true, # if 4, go again with 2 afterwards
  28. 'test' => false, # just count - without doing anything
  29. 'outputToTmp' => false, # write to filename_.ext
  30. 'debug' => false # add debug info after each line
  31. );
  32. protected $_paths = array();
  33. protected $_files = array();
  34. /**
  35. * Main execution function to indend a folder recursivly
  36. *
  37. * @return void
  38. */
  39. public function folder() {
  40. if (!empty($this->args)) {
  41. if (in_array('test', $this->args)) {
  42. $this->settings['test'] = true;
  43. }
  44. if (!empty($this->args[0]) && $this->args[0] != 'app') {
  45. $folder = $this->args[0];
  46. if ($folder == '/') {
  47. $folder = APP;
  48. }
  49. $folder = realpath($folder);
  50. if (!file_exists($folder)) {
  51. die('folder not exists: ' . $folder . '');
  52. }
  53. $this->_paths[] = $folder;
  54. } elseif ($this->args[0] == 'app') {
  55. $this->_paths[] = APP;
  56. }
  57. if (!empty($this->params['files'])) {
  58. $this->settings['files'] = explode(',', $this->params['files']);
  59. }
  60. $this->out($folder);
  61. $this->out('searching...');
  62. $this->_searchFiles();
  63. $this->out('found: ' . count($this->_files));
  64. if ($this->settings['test']) {
  65. $this->out('TEST DONE');
  66. } else {
  67. $continue = $this->in(__('Modifying files! Continue?'), array('y', 'n'), 'n');
  68. if (strtolower($continue) != 'y' && strtolower($continue) != 'yes') {
  69. die('...aborted');
  70. }
  71. $this->_correctFiles3();
  72. $this->out('DONE');
  73. }
  74. } else {
  75. $this->out('Usage: cake intend folder');
  76. $this->out('"folder" is then intended recursivly');
  77. $this->out('default file types are');
  78. $this->out('['.implode(', ', $this->settings['files']).']');
  79. $this->out('');
  80. $this->out('Specify file types manually:');
  81. $this->out('-files php,js,css');
  82. }
  83. }
  84. public function getOptionParser() {
  85. $subcommandParser = array(
  86. 'options' => array(
  87. 'dry-run'=> array(
  88. 'short' => 'd',
  89. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  90. 'boolean' => true
  91. ),
  92. 'log'=> array(
  93. 'short' => 'l',
  94. 'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
  95. 'boolean' => true
  96. ),
  97. 'interactive'=> array(
  98. 'short' => 'i',
  99. 'help' => __d('cake_console', 'Interactive'),
  100. 'boolean' => true
  101. ),
  102. )
  103. );
  104. return parent::getOptionParser()
  105. ->description(__d('cake_console', "Correct indentation of files"))
  106. ->addSubcommand('folder', array(
  107. 'help' => __d('cake_console', 'Indent all files in a folder'),
  108. 'parser' => $subcommandParser
  109. ));
  110. }
  111. public function _write($file, $text) {
  112. $text = implode(PHP_EOL, $text);
  113. if ($this->settings['outputToTmp']) {
  114. $filename = extractPathInfo('file', $file);
  115. if (mb_substr($filename, -1, 1) == '_') {
  116. return;
  117. }
  118. $file = extractPathInfo('dir', $file).DS.$filename.'_.'.extractPathInfo('ext', $file);
  119. }
  120. return file_put_contents($file, $text);
  121. }
  122. public function _read($file) {
  123. $text = file_get_contents($file);
  124. if (empty($text)) {
  125. return array();
  126. }
  127. $pieces = explode(NL, $text);
  128. return $pieces;
  129. }
  130. /**
  131. * NEW TRY!
  132. * idea: just count spaces and replace those
  133. *
  134. * 2010-09-12 ms
  135. */
  136. public function _correctFiles3() {
  137. foreach ($this->_files as $file) {
  138. $this->changes = false;
  139. $textCorrect = array();
  140. $pieces = $this->_read($file);
  141. foreach ($pieces as $piece) {
  142. $tmp = $this->_process($piece, $this->settings['spacesPerTab']);
  143. if ($this->settings['againWithHalf'] && ($spacesPerTab = $this->settings['spacesPerTab']) % 2 === 0 && $spacesPerTab > 3) {
  144. $tmp = $this->_process($tmp, $spacesPerTab/2);
  145. }
  146. $textCorrect[] = $tmp;
  147. }
  148. if ($this->changes) {
  149. $this->_write($file, $textCorrect);
  150. }
  151. }
  152. }
  153. public function _process($piece, $spacesPerTab) {
  154. $pos = -1;
  155. $spaces = $mod = $tabs = 0;
  156. $debug = '';
  157. $newPiece = $piece;
  158. //TODO
  159. while (mb_substr($piece, $pos+1, 1) === ' ' || mb_substr($piece, $pos+1, 1) === TB) {
  160. $pos++;
  161. }
  162. $piece1 = mb_substr($piece, 0, $pos+1);
  163. $piece1 = str_replace(str_repeat(' ', $spacesPerTab), TB, $piece1, $count);
  164. if ($count > 0) {
  165. $this->changes = true;
  166. }
  167. $piece2 = mb_substr($piece, $pos+1);
  168. $newPiece = $piece1 . $piece2;
  169. $newPiece = rtrim($newPiece) . $debug;
  170. if ($newPiece != $piece || strlen($newPiece) !== strlen($piece)) {
  171. $this->changes = true;
  172. }
  173. return $newPiece;
  174. }
  175. /**
  176. * NEW TRY!
  177. * idea: hardcore replaceing
  178. *
  179. * 2010-09-12 ms
  180. */
  181. public function _correctFiles2() {
  182. foreach ($this->_files as $file) {
  183. $changes = false;
  184. $textCorrect = array();
  185. $pieces = $this->_read($file);
  186. foreach ($pieces as $piece) {
  187. $spaces = $mod = $tabs = 0;
  188. $debug = '';
  189. $newPiece = $piece;
  190. //TODO: make sure no other text is in front of it!!!
  191. $newPiece = str_replace(str_repeat(' ', $this->settings['spacesPerTab']), TB, $newPiece, $count);
  192. if ($count > 0) {
  193. $changes = true;
  194. }
  195. $textCorrect[] = rtrim($newPiece) . $debug;
  196. }
  197. if ($changes) {
  198. $this->_write($file, $textCorrect);
  199. }
  200. }
  201. }
  202. /**
  203. * Old try - sometimes TABS at the beginning are not recogized...
  204. * idea: strip tabs and spaces, remember their amount and add tabs again!
  205. * 2010-09-12 ms
  206. */
  207. public function _correctFiles() {
  208. foreach ($this->_files as $file) {
  209. $changes = false;
  210. $textCorrect = array();
  211. $pieces = $this->_read($file);
  212. foreach ($pieces as $piece) {
  213. $pos = -1;
  214. $spaces = $mod = $tabs = 0;
  215. $debug = '';
  216. $newPiece = trim($piece, CR);
  217. $newPiece = trim($newPiece, NL);
  218. //$debug .= ''.stripos($newPiece, TB);
  219. # detect tabs and whitespaces at the beginning
  220. //while (($pieceOfString = mb_substr($newPiece, 0, 1)) == ' ' || ($pieceOfString = mb_substr($newPiece, 0, 1)) == TB) {
  221. while ((stripos($newPiece, ' ')) === 0 || (stripos($newPiece, TB)) === 0) {
  222. $pieceOfString = mb_substr($newPiece, 0, 1);
  223. if ($pieceOfString === ' ') {
  224. $pos++;
  225. $spaces++;
  226. } elseif ($pieceOfString === TB) {
  227. $pos++;
  228. $spaces += $this->settings['spacesPerTab'];
  229. } else {
  230. die('???');
  231. }
  232. $newPiece = mb_substr($newPiece, 1);
  233. }
  234. if ($pos >= 1) {
  235. $changes = true;
  236. # if only spaces and tabs, we might as well trim the line
  237. //should be done
  238. # now correct
  239. //$newPiece = mb_substr($piece, $pos + 1);
  240. # clear single spaces
  241. /*
  242. if (mb_substr($newPiece, 0, 1) === ' ' && mb_substr($newPiece, 1, 1) !== '*') {
  243. $newPiece = mb_substr($newPiece, 1);
  244. }
  245. */
  246. $mod = $spaces % $this->settings['spacesPerTab'];
  247. $tabs = ($spaces - $mod) / $this->settings['spacesPerTab'];
  248. //$beginning = str_replace(' ', TB, $piece);
  249. $beginning = str_repeat(TB, $tabs);
  250. $beginning .= str_repeat(' ', $mod);
  251. $newPiece = $beginning . trim($newPiece);
  252. } else {
  253. $newPiece = rtrim($newPiece);
  254. }
  255. if ($this->settings['debug']) {
  256. $debug .= ' '. ($changes ? '[MOD]': '[]') .' (SPACES '.$tabs.', POS '.$pos.', TABS '.$tabs.', MOD '.$mod.')';
  257. }
  258. $textCorrect[] = $newPiece . $debug;
  259. }
  260. if ($changes) {
  261. $this->_write($file, $textCorrect);
  262. }
  263. //die();
  264. }
  265. }
  266. /**
  267. * Search files that may contain translateable strings
  268. *
  269. * @return void
  270. * @access private
  271. */
  272. public function _searchFiles() {
  273. foreach ($this->_paths as $path) {
  274. $Folder = new Folder($path);
  275. $files = $Folder->findRecursive('.*\.('.implode('|', $this->settings['files']).')', true);
  276. $this->_files += $files;
  277. }
  278. }
  279. }