IndentShell.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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->_correctFiles();
  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 _correctFiles() {
  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. $tmp = $this->_processSpaceErrors($tmp, 1);
  119. $textCorrect[] = $tmp;
  120. }
  121. if ($this->changes) {
  122. $this->_write($file, $textCorrect);
  123. }
  124. }
  125. }
  126. /**
  127. * @return string
  128. */
  129. protected function _process($piece, $spacesPerTab) {
  130. $pos = -1;
  131. $spaces = $mod = $tabs = 0;
  132. $debug = '';
  133. $newPiece = $piece;
  134. if ($spacesPerTab) {
  135. //TODO
  136. while (mb_substr($piece, $pos+1, 1) === ' ' || mb_substr($piece, $pos + 1, 1) === TB) {
  137. $pos++;
  138. }
  139. $piece1 = mb_substr($piece, 0, $pos + 1);
  140. $piece1 = str_replace(str_repeat(' ', $spacesPerTab), TB, $piece1, $count);
  141. if ($count > 0) {
  142. $this->changes = true;
  143. }
  144. $piece2 = mb_substr($piece, $pos+1);
  145. $newPiece = $piece1 . $piece2;
  146. }
  147. $newPiece = rtrim($newPiece) . $debug;
  148. if ($newPiece != $piece || strlen($newPiece) !== strlen($piece)) {
  149. $this->changes = true;
  150. }
  151. return $newPiece;
  152. }
  153. /**
  154. * NEW TRY!
  155. * idea: hardcore replaceing
  156. *
  157. * @deprecated
  158. * 2010-09-12 ms
  159. */
  160. protected function _processSpaceErrors($piece) {
  161. $space = 1;
  162. $newPiece = $piece;
  163. if (mb_substr($piece, 0, $space) === ' ' && mb_substr($piece, $space, 1) === TB) {
  164. $newPiece = mb_substr($piece, $space);
  165. }
  166. if ($newPiece != $piece || strlen($newPiece) !== strlen($piece)) {
  167. $this->changes = true;
  168. }
  169. return $newPiece;
  170. }
  171. /**
  172. * Old try - sometimes TABS at the beginning are not recogized...
  173. * idea: strip tabs and spaces, remember their amount and add tabs again!
  174. *
  175. * @deprecated
  176. * 2010-09-12 ms
  177. */
  178. protected function _correctFilesTry() {
  179. foreach ($this->_files as $file) {
  180. $changes = false;
  181. $textCorrect = array();
  182. $pieces = $this->_read($file);
  183. foreach ($pieces as $piece) {
  184. $pos = -1;
  185. $spaces = $mod = $tabs = 0;
  186. $debug = '';
  187. $newPiece = trim($piece, CR);
  188. $newPiece = trim($newPiece, NL);
  189. //$debug .= ''.stripos($newPiece, TB);
  190. # detect tabs and whitespaces at the beginning
  191. //while (($pieceOfString = mb_substr($newPiece, 0, 1)) === ' ' || ($pieceOfString = mb_substr($newPiece, 0, 1)) == TB) {
  192. while ((stripos($newPiece, ' ')) === 0 || (stripos($newPiece, TB)) === 0) {
  193. $pieceOfString = mb_substr($newPiece, 0, 1);
  194. if ($pieceOfString === ' ') {
  195. $pos++;
  196. $spaces++;
  197. } elseif ($pieceOfString === TB) {
  198. $pos++;
  199. $spaces += $this->settings['spacesPerTab'];
  200. } else {
  201. die('???');
  202. }
  203. $newPiece = mb_substr($newPiece, 1);
  204. }
  205. if ($pos >= 1) {
  206. $changes = true;
  207. # if only spaces and tabs, we might as well trim the line
  208. //should be done
  209. # now correct
  210. //$newPiece = mb_substr($piece, $pos + 1);
  211. # clear single spaces
  212. /*
  213. if (mb_substr($newPiece, 0, 1) === ' ' && mb_substr($newPiece, 1, 1) !== '*') {
  214. $newPiece = mb_substr($newPiece, 1);
  215. }
  216. */
  217. $mod = $spaces % $this->settings['spacesPerTab'];
  218. $tabs = ($spaces - $mod) / $this->settings['spacesPerTab'];
  219. //$beginning = str_replace(' ', TB, $piece);
  220. $beginning = str_repeat(TB, $tabs);
  221. $beginning .= str_repeat(' ', $mod);
  222. $newPiece = $beginning . trim($newPiece);
  223. } else {
  224. $newPiece = rtrim($newPiece);
  225. }
  226. if ($this->settings['debug']) {
  227. $debug .= ' '. ($changes ? '[MOD]': '[]') .' (SPACES '.$tabs.', POS '.$pos.', TABS '.$tabs.', MOD '.$mod.')';
  228. }
  229. $textCorrect[] = $newPiece . $debug;
  230. }
  231. if ($changes) {
  232. $this->_write($file, $textCorrect);
  233. }
  234. //die();
  235. }
  236. }
  237. /**
  238. * Search files that may contain translateable strings
  239. *
  240. * @return void
  241. */
  242. protected function _searchFiles() {
  243. foreach ($this->_paths as $path) {
  244. $Folder = new Folder($path);
  245. $files = $Folder->findRecursive('.*\.('.implode('|', $this->settings['files']).')', true);
  246. foreach ($files as $file) {
  247. if (strpos($file, DS . 'Vendor' . DS) !== false) {
  248. continue;
  249. }
  250. $this->_files[] = $file;
  251. }
  252. }
  253. }
  254. public function getOptionParser() {
  255. $subcommandParser = array(
  256. 'options' => array(
  257. 'dry-run'=> array(
  258. 'short' => 'd',
  259. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  260. 'boolean' => true
  261. ),
  262. 'log'=> array(
  263. 'short' => 'l',
  264. 'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
  265. 'boolean' => true
  266. ),
  267. 'interactive'=> array(
  268. 'short' => 'i',
  269. 'help' => __d('cake_console', 'Interactive'),
  270. 'boolean' => true
  271. ),
  272. 'spaces'=> array(
  273. 'short' => 's',
  274. 'help' => __d('cake_console', 'Spaces per Tab'),
  275. 'default' => '4',
  276. ),
  277. 'extensions'=> array(
  278. 'short' => 'e',
  279. 'help' => __d('cake_console', 'Extensions (comma-separated)'),
  280. 'default' => '',
  281. ),
  282. )
  283. );
  284. return parent::getOptionParser()
  285. ->description(__d('cake_console', "Correct indentation of files"))
  286. ->addSubcommand('folder', array(
  287. 'help' => __d('cake_console', 'Indent all files in a folder'),
  288. 'parser' => $subcommandParser
  289. ));
  290. }
  291. }