IndentShell.php 8.2 KB

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