API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
anime
manga
kitsu
anilist
single-user

RoboFile.php 5.5KB

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