RoboFile.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <?php
  2. require_once __DIR__.'/vendor/autoload.php';
  3. use Symfony\Component\Finder\Finder;
  4. use Robo\Task\Development\GenerateMarkdownDoc as Doc;
  5. class RoboFile extends \Robo\Tasks
  6. {
  7. const STABLE_BRANCH = '2.3';
  8. const REPO_BLOB_URL = 'https://github.com/Codeception/Codeception/blob';
  9. public function release()
  10. {
  11. $this->say("CODECEPTION RELEASE: ".\Codeception\Codecept::VERSION);
  12. $this->update();
  13. $this->buildDocs();
  14. $this->publishDocs();
  15. $this->buildPhar();
  16. $this->buildPhar5();
  17. $this->publishPhar();
  18. $this->publishGit();
  19. $this->publishBase(null, \Codeception\Codecept::VERSION);
  20. $this->versionBump();
  21. $this->update(); //update dependencies after release, because buildPhar5 set them to old versions
  22. }
  23. public function versionBump($version = '')
  24. {
  25. if (!$version) {
  26. $versionParts = explode('.', \Codeception\Codecept::VERSION);
  27. $versionParts[count($versionParts)-1]++;
  28. $version = implode('.', $versionParts);
  29. }
  30. $this->say("Bumping version to $version");
  31. $this->taskReplaceInFile('src/Codeception/Codecept.php')
  32. ->from(\Codeception\Codecept::VERSION)
  33. ->to($version)
  34. ->run();
  35. }
  36. public function update()
  37. {
  38. $this->clean();
  39. $this->taskComposerUpdate()->dir('tests/data/claypit')->run();
  40. $this->taskComposerUpdate()->run();
  41. }
  42. public function changed($change)
  43. {
  44. $this->taskChangelog()
  45. ->version(\Codeception\Codecept::VERSION)
  46. ->change($change)
  47. ->run();
  48. }
  49. protected function server()
  50. {
  51. $this->taskServer(8000)
  52. ->background()
  53. ->dir('tests/data/app')
  54. ->run();
  55. }
  56. public function testPhpbrowser($args = '', $opt = ['test|t' => null])
  57. {
  58. $test = $opt['test'] ? ':'.$opt['test'] : '';
  59. $this->server();
  60. $this->taskCodecept('./codecept')
  61. ->args($args)
  62. ->test('tests/unit/Codeception/Module/PhpBrowserTest.php'.$test)
  63. ->run();
  64. }
  65. public function testRestBrowser($args = '', $opt = ['test|t' => null])
  66. {
  67. $test = $opt['test'] ? ':'.$opt['test'] : '';
  68. $this->taskServer(8010)
  69. ->background()
  70. ->dir('tests/data')
  71. ->run();
  72. $this->taskCodecept('./codecept')
  73. ->test('tests/unit/Codeception/Module/PhpBrowserRestTest.php'.$test)
  74. ->args($args)
  75. ->run();
  76. }
  77. public function testCoverage()
  78. {
  79. $this->server();
  80. $this->taskSymfonyCommand(new \Codeception\Command\Run('run'))
  81. ->arg('suite', 'coverage')
  82. ->run();
  83. }
  84. public function testWebdriver($args = '', $opt = ['test|t' => null])
  85. {
  86. $test = $opt['test'] ? ':'.$opt['test'] : '';
  87. $container = $this->taskDockerRun('davert/selenium-env')
  88. ->detached()
  89. ->publish(4444, 4444)
  90. ->env('APP_PORT', 8000)
  91. ->run();
  92. $this->taskServer(8000)
  93. ->dir('tests/data/app')
  94. ->background()
  95. ->host('0.0.0.0')
  96. ->run();
  97. sleep(3); // wait for selenium to launch
  98. $this->taskCodecept('./codecept')
  99. ->test('tests/web/WebDriverTest.php'.$test)
  100. ->args($args)
  101. ->run();
  102. $this->taskDockerStop($container)->run();
  103. }
  104. public function testLaunchServer($pathToSelenium = '~/selenium-server.jar ')
  105. {
  106. $this->taskExec('java -jar '.$pathToSelenium)
  107. ->background()
  108. ->run();
  109. $this->taskServer(8010)
  110. ->background()
  111. ->dir('tests/data/rest')
  112. ->run();
  113. $this->taskServer(8000)
  114. ->dir('tests/data/app')
  115. ->run();
  116. }
  117. public function testCli()
  118. {
  119. $this->taskSymfonyCommand(new \Codeception\Command\Run('run'))
  120. ->arg('suite', 'cli')
  121. ->run();
  122. $this->taskSymfonyCommand(new \Codeception\Command\Run('run'))
  123. ->arg('suite', 'tests/unit/Codeception/Command')
  124. ->run();
  125. }
  126. private function installDependenciesForPhp54()
  127. {
  128. $this->taskReplaceInFile('composer.json')
  129. ->regex('/"platform": \{.*?\}/')
  130. ->to('"platform": {"php": "5.4.0"}')
  131. ->run();
  132. $this->taskComposerUpdate()->run();
  133. }
  134. private function installDependenciesForPhp70()
  135. {
  136. $this->taskReplaceInFile('composer.json')
  137. ->regex('/"platform": \{.*?\}/')
  138. ->to('"platform": {"php": "7.0.0"}')
  139. ->run();
  140. $this->taskComposerUpdate()->run();
  141. }
  142. private function revertComposerJsonChanges()
  143. {
  144. $this->taskReplaceInFile('composer.json')
  145. ->regex('/"platform": \{.*?\}/')
  146. ->to('"platform": {}')
  147. ->run();
  148. }
  149. /**
  150. * @desc creates codecept.phar
  151. * @throws Exception
  152. */
  153. public function buildPhar()
  154. {
  155. $this->installDependenciesForPhp70();
  156. $this->packPhar('package/codecept.phar');
  157. $this->revertComposerJsonChanges();
  158. }
  159. /**
  160. * @desc creates codecept.phar with Guzzle 5.3 and Symfony 2.8
  161. * @throws Exception
  162. */
  163. public function buildPhar5()
  164. {
  165. if (!file_exists('package/php54')) {
  166. mkdir('package/php54');
  167. }
  168. $this->installDependenciesForPhp54();
  169. $this->packPhar('package/php54/codecept.phar');
  170. $this->revertComposerJsonChanges();
  171. }
  172. private function packPhar($pharFileName)
  173. {
  174. $pharTask = $this->taskPackPhar($pharFileName)
  175. ->compress()
  176. ->stub('package/stub.php');
  177. $finder = Finder::create()
  178. ->ignoreVCS(true)
  179. ->name('*.php')
  180. ->name('*.tpl.dist')
  181. ->name('*.html.dist')
  182. ->in('src');
  183. foreach ($finder as $file) {
  184. $pharTask->addFile('src/'.$file->getRelativePathname(), $file->getRealPath());
  185. }
  186. $finder = Finder::create()
  187. ->ignoreVCS(true)
  188. ->name('*.php')
  189. ->in('ext');
  190. foreach ($finder as $file) {
  191. $pharTask->addFile('ext/'.$file->getRelativePathname(), $file->getRealPath());
  192. }
  193. $finder = Finder::create()->files()
  194. ->ignoreVCS(true)
  195. ->name('*.php')
  196. ->name('*.css')
  197. ->name('*.png')
  198. ->name('*.js')
  199. ->name('*.css')
  200. ->name('*.eot')
  201. ->name('*.svg')
  202. ->name('*.ttf')
  203. ->name('*.wof')
  204. ->name('*.woff')
  205. ->name('*.woff2')
  206. ->name('*.png')
  207. ->name('*.tpl.dist')
  208. ->name('*.html.dist')
  209. ->exclude('videlalvaro')
  210. ->exclude('php-amqplib')
  211. ->exclude('pheanstalk')
  212. ->exclude('phpseclib')
  213. ->exclude('codegyre')
  214. ->exclude('monolog')
  215. ->exclude('phpspec')
  216. ->exclude('squizlabs')
  217. ->exclude('Tests')
  218. ->exclude('tests')
  219. ->exclude('benchmark')
  220. ->exclude('demo')
  221. ->in('vendor');
  222. foreach ($finder as $file) {
  223. $pharTask->addStripped('vendor/'.$file->getRelativePathname(), $file->getRealPath());
  224. }
  225. $pharTask->addFile('autoload.php', 'autoload.php')
  226. ->addFile('codecept', 'package/bin')
  227. ->addFile('shim.php', 'shim.php')
  228. ->addFile('phpunit5-loggers.php', 'phpunit5-loggers.php')
  229. ->run();
  230. $code = $this->taskExec('php ' . $pharFileName)->run()->getExitCode();
  231. if ($code !== 0) {
  232. throw new Exception("There was problem compiling phar");
  233. }
  234. }
  235. /**
  236. * @desc generates modules reference from source files
  237. */
  238. public function buildDocs()
  239. {
  240. $this->say('generating documentation from source files');
  241. $this->buildDocsModules();
  242. $this->buildDocsUtils();
  243. $this->buildDocsCommands();
  244. $this->buildDocsApi();
  245. $this->buildDocsExtensions();
  246. }
  247. public function buildDocsModules()
  248. {
  249. $this->taskCleanDir('docs/modules')->run();
  250. $this->say("Modules");
  251. $modules = Finder::create()->files()->name('*.php')->in(__DIR__ . '/src/Codeception/Module');
  252. foreach ($modules as $module) {
  253. $moduleName = basename(substr($module, 0, -4));
  254. $className = 'Codeception\Module\\' . $moduleName;
  255. $source = "https://github.com/Codeception/Codeception/tree/"
  256. .self::STABLE_BRANCH."/src/Codeception/Module/$moduleName.php";
  257. $this->taskGenDoc('docs/modules/' . $moduleName . '.md')
  258. ->docClass($className)
  259. ->prepend('# '.$moduleName)
  260. ->append('<p>&nbsp;</p><div class="alert alert-warning">Module reference is taken from the source code. <a href="'.$source.'">Help us to improve documentation. Edit module reference</a></div>')
  261. ->processClassSignature(false)
  262. ->processClassDocBlock(function (\ReflectionClass $c, $text) {
  263. return "$text\n\n## Actions";
  264. })->processProperty(false)
  265. ->filterMethods(function (\ReflectionMethod $method) use ($className) {
  266. if ($method->isConstructor() or $method->isDestructor()) {
  267. return false;
  268. }
  269. if (!$method->isPublic()) {
  270. return false;
  271. }
  272. if (strpos($method->name, '_') === 0) {
  273. $doc = $method->getDocComment();
  274. try {
  275. $doc = $doc . $method->getPrototype()->getDocComment();
  276. } catch (\ReflectionException $e) {
  277. }
  278. if (strpos($doc, '@api') === false) {
  279. return false;
  280. }
  281. };
  282. return true;
  283. })->processMethod(function (\ReflectionMethod $method, $text) use ($className, $moduleName) {
  284. $title = "\n### {$method->name}\n";
  285. if (strpos($method->name, '_') === 0) {
  286. $text = str_replace("@api\n", '', $text);
  287. $text = "\n*hidden API method, expected to be used from Helper classes*\n" . $text;
  288. $text = str_replace("{{MODULE_NAME}}", $moduleName, $text);
  289. };
  290. if (!trim($text)) {
  291. return $title . "__not documented__\n";
  292. }
  293. $text = str_replace(
  294. ['@since', '@version'],
  295. [' * `Available since`', ' * `Available since`'],
  296. $text
  297. );
  298. $text = str_replace('@part ', ' * `[Part]` ', $text);
  299. $text = str_replace("@return mixed\n", '', $text);
  300. $text = preg_replace('~@return (.*?)~', ' * `return` $1', $text);
  301. $text = preg_replace("~^@(.*?)([$\s])~", ' * `$1` $2', $text);
  302. return $title . $text;
  303. })->processMethodSignature(false)
  304. ->reorderMethods('ksort')
  305. ->run();
  306. }
  307. }
  308. public function buildDocsUtils()
  309. {
  310. $this->say("Util Classes");
  311. $utils = ['Autoload', 'Fixtures', 'Stub', 'Locator', 'XmlBuilder', 'JsonType', 'HttpCode'];
  312. foreach ($utils as $utilName) {
  313. $className = '\Codeception\Util\\' . $utilName;
  314. $source = self::REPO_BLOB_URL."/".self::STABLE_BRANCH."/src/Codeception/Util/$utilName.php";
  315. $this->documentApiClass('docs/reference/' . $utilName . '.md', $className, $source);
  316. }
  317. }
  318. public function buildDocsApi()
  319. {
  320. $this->say("API Classes");
  321. $apiClasses = ['Codeception\Module', 'Codeception\InitTemplate'];
  322. foreach ($apiClasses as $apiClass) {
  323. $name = (new ReflectionClass($apiClass))->getShortName();
  324. $this->documentApiClass('docs/reference/' . $name . '.md', $apiClass, true);
  325. }
  326. }
  327. public function buildDocsCommands()
  328. {
  329. $this->say("Commands");
  330. $commands = Finder::create()->files()->name('*.php')->depth(0)->in(__DIR__ . '/src/Codeception/Command');
  331. $commandGenerator = $this->taskGenDoc('docs/reference/Commands.md');
  332. foreach ($commands as $command) {
  333. $commandName = basename(substr($command, 0, -4));
  334. $className = '\Codeception\Command\\' . $commandName;
  335. $commandGenerator->docClass($className);
  336. }
  337. $commandGenerator
  338. ->prepend("# Console Commands\n")
  339. ->processClassSignature(function ($r, $text) {
  340. return "## ".$r->getShortName();
  341. })
  342. ->filterMethods(function (ReflectionMethod $r) {
  343. return false;
  344. })
  345. ->run();
  346. }
  347. public function buildDocsExtensions()
  348. {
  349. $this->say('Extensions');
  350. $extensions = Finder::create()->files()->sortByName()->name('*.php')->in(__DIR__ . '/ext');
  351. $extGenerator= $this->taskGenDoc(__DIR__.'/ext/README.md');
  352. foreach ($extensions as $extension) {
  353. $extensionName = basename(substr($extension, 0, -4));
  354. $className = '\Codeception\Extension\\' . $extensionName;
  355. $extGenerator->docClass($className);
  356. }
  357. $extGenerator
  358. ->prepend("# Official Extensions\n")
  359. ->processClassSignature(function (ReflectionClass $r, $text) {
  360. $name = $r->getShortName();
  361. return "## $name\n\n[See Source](" . self::REPO_BLOB_URL."/".self::STABLE_BRANCH. "/ext/$name.php)";
  362. })
  363. ->filterMethods(function (ReflectionMethod $r) {
  364. return false;
  365. })
  366. ->filterProperties(function ($r) {
  367. return false;
  368. })
  369. ->run();
  370. }
  371. /**
  372. * @desc publishes generated phar to codeception.com
  373. */
  374. public function publishPhar()
  375. {
  376. $this->cloneSite();
  377. $version = \Codeception\Codecept::VERSION;
  378. if (strpos($version, self::STABLE_BRANCH) === 0) {
  379. $this->say("publishing to release branch");
  380. copy('../codecept.phar', 'codecept.phar');
  381. if (!is_dir('php54')) {
  382. mkdir('php54');
  383. }
  384. copy('../php54/codecept.phar', 'php5/codecept.phar');
  385. $this->taskExec('git add codecept.phar')->run();
  386. $this->taskExec('git add php5/codecept.phar')->run();
  387. }
  388. $this->taskFileSystemStack()
  389. ->mkdir("releases/$version")
  390. ->mkdir("releases/$version/php54")
  391. ->copy('../codecept.phar', "releases/$version/codecept.phar")
  392. ->copy('../php54/codecept.phar', "releases/$version/php54/codecept.phar")
  393. ->run();
  394. $this->taskGitStack()->add('-A')->run();
  395. $sortByVersion = function (\SplFileInfo $a, \SplFileInfo $b) {
  396. return version_compare($a->getBaseName(), $b->getBaseName());
  397. };
  398. $releases = array_reverse(
  399. iterator_to_array(Finder::create()->depth(0)->directories()->sort($sortByVersion)->in('releases'))
  400. );
  401. $branch = null;
  402. $releaseFile = $this->taskWriteToFile('builds.markdown')
  403. ->line('---')
  404. ->line('layout: page')
  405. ->line('title: Codeception Builds')
  406. ->line('---')
  407. ->line('');
  408. foreach ($releases as $release) {
  409. $releaseName = $release->getBasename();
  410. $downloadUrl = "http://codeception.com/releases/$releaseName/codecept.phar";
  411. list($major, $minor) = explode('.', $releaseName);
  412. if ("$major.$minor" != $branch) {
  413. $branch = "$major.$minor";
  414. $releaseFile->line("\n## $branch");
  415. if ($major < 2) {
  416. $releaseFile->line("*Requires: PHP 5.3 and higher + CURL*\n");
  417. } else {
  418. $releaseFile->line("*Requires: PHP 5.4 and higher + CURL*\n");
  419. }
  420. $releaseFile->line("* **[Download Latest $branch Release]($downloadUrl)**");
  421. }
  422. $versionLine = "* [$releaseName]($downloadUrl)";
  423. if (file_exists("releases/$releaseName/php54/codecept.phar")) {
  424. $downloadUrl = "http://codeception.com/releases/$releaseName/php54/codecept.phar";
  425. if (version_compare($releaseName, '2.3.0', '>=')) {
  426. $versionLine .= ", [for PHP 5.4 - 5.6]($downloadUrl)";
  427. } else {
  428. $versionLine .= ", [for PHP 5.4 or 5.5]($downloadUrl)";
  429. }
  430. }
  431. $releaseFile->line($versionLine);
  432. }
  433. $releaseFile->run();
  434. $this->publishSite();
  435. }
  436. /**
  437. * Updates docs on codeception.com
  438. *
  439. */
  440. public function publishDocs()
  441. {
  442. if (strpos(\Codeception\Codecept::VERSION, self::STABLE_BRANCH) !== 0) {
  443. $this->say("The ".\Codeception\Codecept::VERSION." is not in release branch. Site is not build");
  444. return;
  445. }
  446. $this->say('building site...');
  447. $this->cloneSite();
  448. $this->taskCleanDir('docs')
  449. ->run();
  450. $this->taskFileSystemStack()
  451. ->mkdir('docs/reference')
  452. ->mkdir('docs/modules')
  453. ->run();
  454. chdir('../..');
  455. $this->say('building changelog');
  456. $this->taskWriteToFile('package/site/changelog.markdown')
  457. ->line('---')
  458. ->line('layout: page')
  459. ->line('title: Codeception Changelog')
  460. ->line('---')
  461. ->line('')
  462. ->line(
  463. '<div class="alert alert-warning">Download specific version at <a href="/builds">builds page</a></div>'
  464. )
  465. ->line('')
  466. ->line('# Changelog')
  467. ->line('')
  468. ->line($this->processChangelog())
  469. ->run();
  470. $docs = Finder::create()->files('*.md')->sortByName()->in('docs');
  471. $modules = [];
  472. $api = [];
  473. $reference = [];
  474. foreach ($docs as $doc) {
  475. $newfile = $doc->getFilename();
  476. $name = substr($doc->getBasename(), 0, -3);
  477. $contents = $doc->getContents();
  478. if (strpos($doc->getPathname(), 'docs'.DIRECTORY_SEPARATOR.'modules') !== false) {
  479. $newfile = 'docs/modules/' . $newfile;
  480. $modules[$name] = '/docs/modules/' . $doc->getBasename();
  481. $contents = str_replace('## ', '### ', $contents);
  482. $buttons = [
  483. 'source' => self::REPO_BLOB_URL."/".self::STABLE_BRANCH."/src/Codeception/Module/$name.php"
  484. ];
  485. // building version switcher
  486. foreach (['master', '2.2', '2.1', '2.0', '1.8'] as $branch) {
  487. $buttons[$branch] = self::REPO_BLOB_URL."/$branch/docs/modules/$name.md";
  488. }
  489. $buttonHtml = "\n\n".'<div class="btn-group" role="group" style="float: right" aria-label="...">';
  490. foreach ($buttons as $link => $url) {
  491. if ($link == self::STABLE_BRANCH) {
  492. $link = "<strong>$link</strong>";
  493. }
  494. $buttonHtml.= '<a class="btn btn-default" href="'.$url.'">'.$link.'</a>';
  495. }
  496. $buttonHtml .= '</div>'."\n\n";
  497. $contents = $buttonHtml . $contents;
  498. } elseif (strpos($doc->getPathname(), 'docs'.DIRECTORY_SEPARATOR.'reference') !== false) {
  499. $newfile = 'docs/reference/' . $newfile;
  500. $reference[$name] = '/docs/reference/' . $doc->getBasename();
  501. } else {
  502. $newfile = 'docs/'.$newfile;
  503. $api[substr($name, 3)] = '/docs/'.$doc->getBasename();
  504. }
  505. copy($doc->getPathname(), 'package/site/' . $newfile);
  506. $highlight_languages = implode('|', ['php', 'html', 'bash', 'yaml', 'json', 'xml', 'sql', 'gherkin']);
  507. $contents = preg_replace(
  508. "~```\s?($highlight_languages)\b(.*?)```~ms",
  509. "{% highlight $1 %}\n$2\n{% endhighlight %}",
  510. $contents
  511. );
  512. $contents = str_replace('{% highlight %}', '{% highlight yaml %}', $contents);
  513. $contents = preg_replace("~```\s?(.*?)```~ms", "{% highlight yaml %}\n$1\n{% endhighlight %}", $contents);
  514. // set default language in order not to leave unparsed code inside '```'
  515. $matches = [];
  516. $title = $name;
  517. $contents = "---\nlayout: doc\ntitle: ".($title!="" ? $title." - " : "")
  518. ."Codeception - Documentation\n---\n\n".$contents;
  519. file_put_contents('package/site/' .$newfile, $contents);
  520. }
  521. chdir('package/site');
  522. $guides = array_keys($api);
  523. foreach ($api as $name => $url) {
  524. $filename = substr($url, 6);
  525. $doc = file_get_contents('docs/'.$filename)."\n\n\n";
  526. $i = array_search($name, $guides);
  527. if (isset($guides[$i+1])) {
  528. $next_title = $guides[$i+1];
  529. $next_url = $api[$guides[$i+1]];
  530. $next_url = substr($next_url, 0, -3);
  531. $doc .= "\n* **Next Chapter: [$next_title >]($next_url)**";
  532. }
  533. if (isset($guides[$i-1])) {
  534. $prev_title = $guides[$i-1];
  535. $prev_url = $api[$guides[$i-1]];
  536. $prev_url = substr($prev_url, 0, -3);
  537. $doc .= "\n* **Previous Chapter: [< $prev_title]($prev_url)**";
  538. }
  539. $this->taskWriteToFile('docs/'.$filename)
  540. ->text($doc)
  541. ->run();
  542. }
  543. $guides_list = '';
  544. foreach ($api as $name => $url) {
  545. $url = substr($url, 0, -3);
  546. $name = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1 \\2', $name);
  547. $name = preg_replace('/([a-z\d])([A-Z])/', '\\1 \\2', $name);
  548. $guides_list .= '<li><a href="'.$url.'">'.$name.'</a></li>';
  549. }
  550. file_put_contents('_includes/guides.html', $guides_list);
  551. $this->say("Building Guides index");
  552. $this->taskWriteToFile('_includes/guides.html')
  553. ->text($guides_list)
  554. ->run();
  555. $this->taskWriteToFile('docs/index.html')
  556. ->line('---')
  557. ->line('layout: doc')
  558. ->line('title: Codeception Documentation')
  559. ->line('---')
  560. ->line('')
  561. ->line("<h1>Codeception Documentation Guides</h1>")
  562. ->line('')
  563. ->text($guides_list)
  564. ->run();
  565. /**
  566. * Align modules in two columns like this:
  567. * A D
  568. * B E
  569. * C
  570. */
  571. $modules_cols = 2;
  572. $modules_rows = ceil(count($modules) / $modules_cols);
  573. $module_names_chunked = array_chunk(array_keys($modules), $modules_rows);
  574. $modules_list = '';
  575. for ($i = 0; $i < $modules_rows; $i++) {
  576. for ($j = 0; $j < $modules_cols; $j++) {
  577. if (isset($module_names_chunked[$j][$i])) {
  578. $name = $module_names_chunked[$j][$i];
  579. $url = substr($modules[$name], 0, -3);
  580. $modules_list .= '<li><a href="'.$url.'">'.$name.'</a></li>';
  581. }
  582. }
  583. }
  584. file_put_contents('_includes/modules.html', $modules_list);
  585. $reference_list = '';
  586. foreach ($reference as $name => $url) {
  587. if ($name == 'Commands') {
  588. continue;
  589. }
  590. if ($name == 'Configuration') {
  591. continue;
  592. }
  593. $url = substr($url, 0, -3);
  594. $reference_list .= '<li><a href="'.$url.'">'.$name.'</a></li>';
  595. }
  596. file_put_contents('_includes/reference.html', $reference_list);
  597. $this->say("Writing extensions docs");
  598. $this->taskWriteToFile('_includes/extensions.md')
  599. ->textFromFile(__DIR__.'/ext/README.md')
  600. ->run();
  601. $this->publishSite();
  602. $this->taskExec('git add')->args('.')->run();
  603. }
  604. /**
  605. * @desc creates a new version tag and pushes to github
  606. * @param null $branch
  607. * @param array $opt
  608. */
  609. public function publishGit($branch = null, $opt = ['tag|t' => null])
  610. {
  611. $version = isset($opt['tag']) ? $opt['tag'] : \Codeception\Codecept::VERSION;
  612. $this->say('creating new tag for '.$version);
  613. if (!$branch) {
  614. $branch = explode('.', $version);
  615. array_pop($branch);
  616. $branch = implode('.', $branch);
  617. }
  618. $this->taskExec("git tag $version")->run();
  619. $this->taskExec("git push origin $branch --tags")->run();
  620. }
  621. protected function processChangelog()
  622. {
  623. $sortByVersionDesc = function (\SplFileInfo $a, \SplFileInfo $b) {
  624. $pattern = '/^CHANGELOG-(\d+\.\d+).md$/';
  625. if (preg_match($pattern, $a->getBasename(), $matches1) && preg_match($pattern, $b->getBasename(), $matches2)) {
  626. return version_compare($matches1[1], $matches2[1]) * -1;
  627. }
  628. return 0;
  629. };
  630. $changelogFiles = Finder::create()->name('CHANGELOG-*.md')->in('.')->depth(0)->sort($sortByVersionDesc);
  631. $changelog = '';
  632. foreach ($changelogFiles as $file) {
  633. $changelog .= $file->getContents();
  634. }
  635. //user
  636. $changelog = preg_replace('~\s@([\w-]+)~', ' **[$1](https://github.com/$1)**', $changelog);
  637. //issue
  638. $changelog = preg_replace(
  639. '~#(\d+)~',
  640. '[#$1](https://github.com/Codeception/Codeception/issues/$1)',
  641. $changelog
  642. );
  643. //module
  644. $changelog = preg_replace('~\s\[(\w+)\]\s~', ' **[$1]** ', $changelog);
  645. return $changelog;
  646. }
  647. /**
  648. * @desc cleans all log and temp directories
  649. */
  650. public function clean()
  651. {
  652. $this->taskCleanDir([
  653. 'tests/log',
  654. 'tests/data/claypit/tests/_output',
  655. 'tests/data/included/_log',
  656. 'tests/data/included/jazz/tests/_log',
  657. 'tests/data/included/shire/tests/_log',
  658. ])->run();
  659. $this->taskDeleteDir([
  660. 'tests/data/claypit/c3tmp',
  661. 'tests/data/sandbox'
  662. ])->run();
  663. }
  664. public function buildActors()
  665. {
  666. $build = 'php codecept build';
  667. $this->taskExec($build)->run();
  668. $this->taskExec($build)->args('-c tests/data/claypit')->run();
  669. $this->taskExec($build)->args('-c tests/data/included')->run();
  670. $this->taskExec($build)->args('-c tests/data/included/jazz')->run();
  671. $this->taskExec($build)->args('-c tests/data/included/shire')->run();
  672. $this->taskExec($build)->args('-c tests/data/included/jazz')->run();
  673. }
  674. protected function cloneSite()
  675. {
  676. @mkdir("package/site");
  677. $this->taskExec('git clone')
  678. ->args('git@github.com:Codeception/codeception.github.com.git')
  679. ->args('package/site/')
  680. ->run();
  681. chdir('package/site');
  682. }
  683. protected function publishSite()
  684. {
  685. $this->taskGitStack()
  686. ->add('-A')
  687. ->commit('auto updated documentation')
  688. ->push()
  689. ->run();
  690. chdir('..');
  691. sleep(2);
  692. $this->taskDeleteDir('site')->run();
  693. chdir('..');
  694. $this->say("Site build succesfully");
  695. }
  696. /**
  697. * Publishes Codeception base
  698. * @param null $branch
  699. * @param null $tag
  700. */
  701. public function publishBase($branch = null, $tag = null)
  702. {
  703. if (!$branch) {
  704. $branch = self::STABLE_BRANCH;
  705. }
  706. $this->say("Updating Codeception Base distribution");
  707. $tempBranch = "tmp".uniqid();
  708. $this->taskGitStack()
  709. ->checkout("-b $tempBranch")
  710. ->run();
  711. $this->taskReplaceInFile('composer.json')
  712. ->from('"codeception/codeception"')
  713. ->to('"codeception/base"')
  714. ->run();
  715. $this->taskReplaceInFile('composer.json')
  716. ->regex('~^\s+"facebook\/webdriver".*$~m')
  717. ->to('')
  718. ->run();
  719. $this->taskReplaceInFile('composer.json')
  720. ->regex('~^\s+"guzzlehttp\/guzzle".*$~m')
  721. ->to('')
  722. ->run();
  723. $this->taskComposerUpdate()->run();
  724. $this->taskGitStack()
  725. ->add('composer.json')
  726. ->commit('auto-update')
  727. ->exec("push -f base $tempBranch:$branch")
  728. ->run();
  729. if ($tag) {
  730. $this->taskGitStack()
  731. ->exec("tag -d $tag")
  732. ->exec("push base :refs/tags/$tag")
  733. ->exec("tag $tag")
  734. ->push('base', $tag)
  735. ->run();
  736. }
  737. $this->taskGitStack()
  738. ->checkout('-- composer.json')
  739. ->checkout($branch)
  740. ->exec("branch -D $tempBranch")
  741. ->run();
  742. }
  743. /**
  744. * Checks Codeception code style
  745. * Most useful values for `report` option: `full`, `summary`, `diff`
  746. *
  747. * @param array $opt
  748. */
  749. public function codestyleCheck($opt = ['report|r' => 'summary'])
  750. {
  751. $this->say("Checking code style");
  752. $this->taskExec('php vendor/bin/phpcs')
  753. ->arg('.')
  754. ->arg('--standard=ruleset.xml')
  755. ->arg('--report=' . $opt['report'])
  756. ->arg('--ignore=tests,vendor,package,docs')
  757. ->run();
  758. }
  759. public function codestyleFix()
  760. {
  761. $this->taskExec('php vendor/bin/phpcbf')
  762. ->arg('.')
  763. ->arg('--standard=ruleset.xml')
  764. ->arg('--ignore=tests,vendor,package,docs')
  765. ->run();
  766. }
  767. /**
  768. * @param $file
  769. * @param $className
  770. * @param $source
  771. */
  772. protected function documentApiClass($file, $className, $all = false)
  773. {
  774. $uri = str_replace('\\', '/', $className);
  775. $source = self::REPO_BLOB_URL."/".self::STABLE_BRANCH."/src/$uri.php";
  776. $this->taskGenDoc($file)
  777. ->docClass($className)
  778. ->filterMethods(function (ReflectionMethod $r) use ($all, $className) {
  779. return $all || $r->isPublic();
  780. })
  781. ->append(
  782. '<p>&nbsp;</p><div class="alert alert-warning">Reference is taken from the source code. '
  783. . '<a href="' . $source . '">Help us to improve documentation. Edit module reference</a></div>'
  784. )
  785. ->processPropertySignature(function ($r) {
  786. return "\n#### $" . $r->name. "\n\n";
  787. })
  788. ->processPropertyDocBlock(function ($r, $text) {
  789. $modifiers = implode(' ', \Reflection::getModifierNames($r->getModifiers()));
  790. $text = ' *' . $modifiers . '* **$' . $r->name . "**\n" . $text;
  791. $text = preg_replace("~@(.*?)\s(.*)~", 'type `$2`', $text);
  792. return $text;
  793. })
  794. ->processClassDocBlock(
  795. function (ReflectionClass $r, $text) {
  796. return $text . "\n";
  797. }
  798. )
  799. ->processMethodSignature(function ($r, $text) {
  800. return "#### {$r->name}()\n\n" . ltrim($text, '#');
  801. })
  802. ->processMethodDocBlock(
  803. function (ReflectionMethod $r, $text) use ($file) {
  804. $file = str_replace(__DIR__, '', $r->getFileName());
  805. $source = self::REPO_BLOB_URL."/".self::STABLE_BRANCH. $file;
  806. $line = $r->getStartLine();
  807. $text = preg_replace("~^\s?@(.*?)\s~m", ' * `$1` $2', $text);
  808. $text .= "\n[See source]($source#L$line)";
  809. return "\n" . $text . "\n";
  810. }
  811. )
  812. ->reorderMethods('ksort')
  813. ->run();
  814. }
  815. }