RoboFile.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php declare(strict_types=1);
  2. if ( ! function_exists('glob_recursive'))
  3. {
  4. // Does not support flag GLOB_BRACE
  5. function glob_recursive($pattern, $flags = 0)
  6. {
  7. $files = glob($pattern, $flags);
  8. foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
  9. {
  10. $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
  11. }
  12. return $files;
  13. }
  14. }
  15. /**
  16. * This is project's console commands configuration for Robo task runner.
  17. *
  18. * @see http://robo.li/
  19. */
  20. class RoboFile extends \Robo\Tasks {
  21. /**
  22. * Directories used by analysis tools
  23. *
  24. * @var array
  25. */
  26. protected $taskDirs = [
  27. 'build/logs',
  28. 'build/pdepend',
  29. 'build/phpdox',
  30. ];
  31. /**
  32. * Directories to remove with the clean task
  33. *
  34. * @var array
  35. */
  36. protected $cleanDirs = [
  37. 'coverage',
  38. 'apiDocumentation',
  39. 'phpdoc',
  40. 'build/logs',
  41. 'build/phpdox',
  42. 'build/pdepend'
  43. ];
  44. /**
  45. * Do static analysis tasks
  46. */
  47. public function analyze()
  48. {
  49. $this->prepare();
  50. $this->lint();
  51. $this->phploc(TRUE);
  52. $this->phpcs(TRUE);
  53. $this->dependencyReport();
  54. $this->phpcpdReport();
  55. }
  56. /**
  57. * Run all tests, generate coverage, generate docs, generate code statistics
  58. */
  59. public function build()
  60. {
  61. $this->analyze();
  62. $this->coverage();
  63. $this->docs();
  64. }
  65. /**
  66. * Cleanup temporary files
  67. */
  68. public function clean()
  69. {
  70. // So the task doesn't complain,
  71. // make any 'missing' dirs to cleanup
  72. array_map(function ($dir) {
  73. if ( ! is_dir($dir))
  74. {
  75. `mkdir -p {$dir}`;
  76. }
  77. }, $this->cleanDirs);
  78. $this->_cleanDir($this->cleanDirs);
  79. $this->_deleteDir($this->cleanDirs);
  80. }
  81. /**
  82. * Run unit tests and generate coverage reports
  83. */
  84. public function coverage()
  85. {
  86. $this->_run(['phpdbg -qrr -- vendor/bin/phpunit -c build']);
  87. }
  88. /**
  89. * Generate documentation with phpdox
  90. */
  91. public function docs()
  92. {
  93. $this->_run(['vendor/bin/phpdox']);
  94. }
  95. /**
  96. * Verify that source files are valid
  97. */
  98. public function lint()
  99. {
  100. $files = $this->getAllSourceFiles();
  101. $chunks = array_chunk($files, (int)`getconf _NPROCESSORS_ONLN`);
  102. foreach($chunks as $chunk)
  103. {
  104. $this->parallelLint($chunk);
  105. }
  106. }
  107. /**
  108. * Run the phpcs tool
  109. *
  110. * @param bool $report - if true, generates reports instead of direct output
  111. */
  112. public function phpcs($report = FALSE)
  113. {
  114. $dir = __DIR__;
  115. $report_cmd_parts = [
  116. 'vendor/bin/phpcs',
  117. "--standard=./build/CodeIgniter",
  118. "--report-checkstyle=./build/logs/phpcs.xml",
  119. ];
  120. $normal_cmd_parts = [
  121. 'vendor/bin/phpcs',
  122. "--standard=./build/CodeIgniter",
  123. ];
  124. $cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
  125. $this->_run($cmd_parts);
  126. }
  127. /**
  128. * Run the phploc tool
  129. *
  130. * @param bool $report - if true, generates reports instead of direct output
  131. */
  132. public function phploc($report = FALSE)
  133. {
  134. // Command for generating reports
  135. $report_cmd_parts = [
  136. 'vendor/bin/phploc',
  137. '--count-tests',
  138. '--log-csv=build/logs/phploc.csv',
  139. '--log-xml=build/logs/phploc.xml',
  140. 'src',
  141. 'tests'
  142. ];
  143. // Command for generating direct output
  144. $normal_cmd_parts = [
  145. 'vendor/bin/phploc',
  146. '--count-tests',
  147. 'src',
  148. 'tests'
  149. ];
  150. $cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
  151. $this->_run($cmd_parts);
  152. }
  153. /**
  154. * Create temporary directories
  155. */
  156. public function prepare()
  157. {
  158. array_map([$this, '_mkdir'], $this->taskDirs);
  159. }
  160. /**
  161. * Lint php files and run unit tests
  162. */
  163. public function test()
  164. {
  165. $this->lint();
  166. $this->taskPhpUnit()
  167. ->configFile('phpunit.xml')
  168. ->run();
  169. $this->_run(["php tests/index.php"]);
  170. }
  171. /**
  172. * Watches for file updates, and automatically runs appropriate actions
  173. */
  174. public function watch()
  175. {
  176. $this->taskWatch()
  177. ->monitor('composer.json', function() {
  178. $this->taskComposerUpdate()->run();
  179. })
  180. ->monitor('src', function () {
  181. $this->taskExec('test')->run();
  182. })
  183. ->monitor('tests', function () {
  184. $this->taskExec('test')->run();
  185. })
  186. ->run();
  187. }
  188. /**
  189. * Create pdepend reports
  190. */
  191. protected function dependencyReport()
  192. {
  193. $cmd_parts = [
  194. 'vendor/bin/pdepend',
  195. '--jdepend-xml=build/logs/jdepend.xml',
  196. '--jdepend-chart=build/pdepend/dependencies.svg',
  197. '--overview-pyramid=build/pdepend/overview-pyramid.svg',
  198. 'src'
  199. ];
  200. $this->_run($cmd_parts);
  201. }
  202. /**
  203. * Get the total list of source files, including tests
  204. *
  205. * @return array
  206. */
  207. protected function getAllSourceFiles()
  208. {
  209. $files = array_merge(
  210. glob_recursive('build/*.php'),
  211. glob_recursive('src/*.php'),
  212. glob_recursive('tests/*.php'),
  213. glob('*.php')
  214. );
  215. sort($files);
  216. return $files;
  217. }
  218. /**
  219. * Run php's linter in one parallel task for the passed chunk
  220. *
  221. * @param array $chunk
  222. */
  223. protected function parallelLint(array $chunk)
  224. {
  225. $task = $this->taskParallelExec()
  226. ->timeout(5)
  227. ->printed(FALSE);
  228. foreach($chunk as $file)
  229. {
  230. $task = $task->process("php -l {$file}");
  231. }
  232. $task->run();
  233. }
  234. /**
  235. * Generate copy paste detector report
  236. */
  237. protected function phpcpdReport()
  238. {
  239. $cmd_parts = [
  240. 'vendor/bin/phpcpd',
  241. '--log-pmd build/logs/pmd-cpd.xml',
  242. 'src'
  243. ];
  244. $this->_run($cmd_parts);
  245. }
  246. /**
  247. * Shortcut for joining an array of command arguments
  248. * and then running it
  249. *
  250. * @param array $cmd_parts - command arguments
  251. * @param string $join_on - what to join the command arguments with
  252. */
  253. protected function _run(array $cmd_parts, $join_on = ' ')
  254. {
  255. $this->taskExec(implode($join_on, $cmd_parts))->run();
  256. }
  257. }