Arsse/RoboFile.php

292 lines
12 KiB
PHP
Raw Normal View History

<?php
use Robo\Result;
2019-08-14 12:21:08 -04:00
const BASE = __DIR__.\DIRECTORY_SEPARATOR;
const BASE_TEST = BASE."tests".\DIRECTORY_SEPARATOR;
define("IS_WIN", defined("PHP_WINDOWS_VERSION_MAJOR"));
2019-10-17 13:00:56 -04:00
define("IS_MAC", php_uname("s") === "Darwin");
2021-03-01 19:02:15 -05:00
error_reporting(0);
2019-08-14 12:21:08 -04:00
function norm(string $path): string {
$out = realpath($path);
if (!$out) {
$out = str_replace(["/", "\\"], \DIRECTORY_SEPARATOR, $path);
}
return $out;
}
class RoboFile extends \Robo\Tasks {
2019-01-25 16:56:05 -05:00
/** Runs the typical test suite
2017-12-22 11:41:54 -05:00
*
* Arguments passed to the task are passed on to PHPUnit. Thus one may, for
2017-12-17 11:37:07 -05:00
* example, run the following command and get the expected results:
2017-12-22 11:41:54 -05:00
*
* ./robo test --testsuite TTRSS --exclude-group slow --testdox
*
* Please see the PHPUnit documentation for available options.
2020-03-01 15:16:50 -05:00
*/
public function test(array $args): Result {
return $this->runTests(escapeshellarg(\PHP_BINARY), "typical", $args);
}
2019-01-25 16:56:05 -05:00
/** Runs the full test suite
2017-12-22 11:41:54 -05:00
*
2018-08-15 09:59:07 -04:00
* This includes pedantic tests which may help to identify problems.
* See help for the "test" task for more details.
2020-03-01 15:16:50 -05:00
*/
2017-12-17 11:37:07 -05:00
public function testFull(array $args): Result {
return $this->runTests(escapeshellarg(\PHP_BINARY), "full", $args);
2017-12-17 11:37:07 -05:00
}
2017-12-22 11:41:54 -05:00
/**
* Runs a quick subset of the test suite
*
* See help for the "test" task for more details.
2020-03-01 15:16:50 -05:00
*/
public function testQuick(array $args): Result {
return $this->runTests(escapeshellarg(\PHP_BINARY), "quick", $args);
}
2017-12-22 11:41:54 -05:00
/** Produces a code coverage report
*
2017-12-17 11:37:07 -05:00
* By default this task produces an HTML-format coverage report in
2018-08-15 09:59:07 -04:00
* tests/coverage/. Additional reports may be produced by passing
2017-12-17 11:37:07 -05:00
* arguments to this task as one would to PHPUnit.
2017-12-22 11:41:54 -05:00
*
2020-02-01 23:43:46 -05:00
* Robo first tries to use pcov and will fall back first to xdebug then
2020-03-01 10:17:16 -05:00
* phpdbg. Neither pcov nor xdebug need to be enabled to be used; they
2020-02-01 23:43:46 -05:00
* only need to be present in the extension load path to be used.
2020-03-01 15:16:50 -05:00
*/
public function coverage(array $args): Result {
// run tests with code coverage reporting enabled
$exec = $this->findCoverageEngine();
2019-08-14 12:21:08 -04:00
return $this->runTests($exec, "coverage", array_merge(["--coverage-html", BASE_TEST."coverage"], $args));
}
/** Produces a code coverage report, with redundant tests
*
* Depending on the environment, some tests that normally provide
* coverage may be skipped, while working alternatives are normally
* suppressed for reasons of time. This coverage report will try to
* run all tests which may cover code.
2018-12-05 17:28:11 -05:00
*
* See also help for the "coverage" task for more details.
2020-03-01 15:16:50 -05:00
*/
public function coverageFull(array $args): Result {
// run tests with code coverage reporting enabled
$exec = $this->findCoverageEngine();
2019-08-14 12:21:08 -04:00
return $this->runTests($exec, "typical", array_merge(["--coverage-html", BASE_TEST."coverage"], $args));
2018-08-15 09:59:07 -04:00
}
/** Runs the coding standards fixer */
public function clean($opts = ['demo|d' => false]): Result {
2019-08-14 12:21:08 -04:00
$t = $this->taskExec(norm(BASE."vendor/bin/php-cs-fixer"));
2018-08-15 09:59:07 -04:00
$t->arg("fix");
if ($opts['demo']) {
$t->args("--dry-run", "--diff")->option("--diff-format", "udiff");
}
return $t->run();
}
protected function findCoverageEngine(): string {
2020-02-01 23:43:46 -05:00
$dir = rtrim(ini_get("extension_dir"), "/").\DIRECTORY_SEPARATOR;
$ext = IS_WIN ? "dll" : (IS_MAC ? "dylib" : "so");
$php = escapeshellarg(\PHP_BINARY);
$code = escapeshellarg(BASE."lib");
if (extension_loaded("pcov")) {
return "$php -d pcov.enabled=1 -d pcov.directory=$code";
} elseif (extension_loaded("xdebug")) {
2021-01-03 16:51:25 -05:00
return "$php -d xdebug.mode=coverage";
2020-02-01 23:43:46 -05:00
} elseif (file_exists($dir."pcov.$ext")) {
return "$php -d extension=pcov.$ext -d pcov.enabled=1 -d pcov.directory=$code";
2020-11-06 19:56:32 -05:00
} elseif (file_exists($dir."xdebug.$ext")) {
2021-01-03 16:51:25 -05:00
return "$php -d zend_extension=xdebug.$ext -d xdebug.mode=coverage";
} else {
2020-02-01 23:43:46 -05:00
if (IS_WIN) {
$dbg = dirname(\PHP_BINARY)."\\phpdbg.exe";
$dbg = file_exists($dbg) ? $dbg : "";
} else {
$dbg = trim(`which phpdbg 2>/dev/null`);
}
if ($dbg) {
return escapeshellarg($dbg)." -qrr";
} else {
return $php;
}
}
}
2019-03-25 09:53:06 -04:00
protected function blackhole(bool $all = false): string {
2019-08-14 12:21:08 -04:00
$hole = IS_WIN ? "nul" : "/dev/null";
2019-03-25 09:53:06 -04:00
return $all ? ">$hole 2>&1" : "2>$hole";
}
2020-03-01 15:16:50 -05:00
protected function runTests(string $executor, string $set, array $args): Result {
2018-08-15 09:59:07 -04:00
switch ($set) {
case "typical":
$set = ["--exclude-group", "optional"];
break;
case "quick":
$set = ["--exclude-group", "optional,slow"];
break;
case "coverage":
2018-12-10 12:39:09 -05:00
$set = ["--exclude-group", "optional,coverageOptional"];
break;
2018-08-15 09:59:07 -04:00
case "full":
$set = [];
break;
default:
throw new \Exception;
}
2019-08-14 12:21:08 -04:00
$execpath = norm(BASE."vendor-bin/phpunit/vendor/phpunit/phpunit/phpunit");
$confpath = realpath(BASE_TEST."phpunit.dist.xml") ?: norm(BASE_TEST."phpunit.xml");
$this->taskServer(8000)->host("localhost")->dir(BASE_TEST."docroot")->rawArg("-n")->arg(BASE_TEST."server.php")->rawArg($this->blackhole())->background()->run();
2019-09-12 08:32:40 -04:00
return $this->taskExec($executor)->option("-d", "zend.assertions=1")->arg($execpath)->option("-c", $confpath)->args(array_merge($set, $args))->run();
2018-08-15 09:59:07 -04:00
}
2017-12-22 11:41:54 -05:00
/** Packages a given commit of the software into a release tarball
*
2017-12-17 11:37:07 -05:00
* The version to package may be any Git tree-ish identifier: a tag, a branch,
* or any commit hash. If none is provided on the command line, Robo will prompt
* for a commit to package; the default is "HEAD".
2017-12-22 11:41:54 -05:00
*
2017-12-17 11:37:07 -05:00
* Note that while it is possible to re-package old versions, the resultant tarball
2017-12-22 11:41:54 -05:00
* may not be equivalent due to subsequent changes in the exclude list, or because
2017-12-17 11:37:07 -05:00
* of new tooling.
2020-03-01 15:16:50 -05:00
*/
2017-12-17 11:37:07 -05:00
public function package(string $version = null): Result {
// establish which commit to package
$version = $version ?? $this->askDefault("Commit to package:", "HEAD");
2019-08-14 12:21:08 -04:00
$archive = BASE."arsse-$version.tar.gz";
// start a collection
$t = $this->collectionBuilder();
// create a temporary directory
$dir = $t->tmpDir().\DIRECTORY_SEPARATOR;
// create a Git worktree for the selected commit in the temp location
$t->taskExec("git worktree add ".escapeshellarg($dir)." ".escapeshellarg($version))
->completion($this->taskFilesystemStack()->remove($dir))
->completion($this->taskExec("git worktree prune"));
2021-05-19 11:27:21 -04:00
// patch the Arch PKGBUILD file with the correct version string
$t->addCode(function () use ($dir) {
$ver = trim(preg_replace('/^([^-]+)-(\d+)-(\w+)$/', "$1.r$2.$3", `git -C "$dir" describe --tags`));
return $this->taskReplaceInFile($dir."dist/arch/PKGBUILD")->regex('/^pkgver=.*$/m')->to("pkgver=$ver")->run();
});
// patch the Arch PKGBUILD file with the correct source file
$t->addCode(function () use ($dir, $archive) {
$tar = basename($archive);
return $this->taskReplaceInFile($dir."dist/arch/PKGBUILD")->regex('/^source=\("arsse-[^"]+"\)$/m')->to("source=(\"$tar\")")->run();
});
// perform Composer installation in the temp location with dev dependencies
$t->taskComposerInstall()->dir($dir);
// generate the manual
$t->taskExec(escapeshellarg($dir."robo")." manual")->dir($dir);
// perform Composer installation in the temp location for final output
$t->taskComposerInstall()->dir($dir)->noDev()->optimizeAutoloader()->arg("--no-scripts");
// delete unwanted files
$t->taskFilesystemStack()->remove([
$dir.".git",
$dir.".gitignore",
$dir.".gitattributes",
$dir."composer.json",
$dir."composer.lock",
$dir.".php_cs.dist",
$dir."phpdoc.dist.xml",
$dir."build.xml",
$dir."RoboFile.php",
$dir."CONTRIBUTING.md",
$dir."docs",
$dir."tests",
$dir."vendor-bin",
2019-08-04 21:59:25 -04:00
$dir."vendor/bin",
$dir."robo",
$dir."robo.bat",
$dir."package.json",
2021-04-09 19:18:42 -04:00
$dir."yarn.lock",
$dir."postcss.config.js",
]);
// generate a sample configuration file
$t->taskExec(escapeshellarg(\PHP_BINARY)." arsse.php conf save-defaults config.defaults.php")->dir($dir);
// remove any existing archive
$t->taskFilesystemStack()->remove($archive);
// package it all up
$t->taskPack($archive)->addDir("arsse", $dir);
// execute the collection
return $t->run();
}
/** Packages a given commit of the software into an Arch package
*
* The version to package may be any Git tree-ish identifier: a tag, a branch,
* or any commit hash. If none is provided on the command line, Robo will prompt
* for a commit to package; the default is "HEAD".
*
* Note that while it is possible to re-package old versions, the resultant tarball
* may not be equivalent due to subsequent changes in the exclude list, or because
* of new tooling.
*/
public function packageArch(string $version = null): Result {
// establish which commit to package
$version = $version ?? $this->askDefault("Commit to package:", "HEAD");
$archive = BASE."arsse-$version.tar.gz";
// start a collection
$t = $this->collectionBuilder();
// create a tarball
$t->addCode(function() use ($version) {
return $this->package($version);
});
2021-05-19 11:27:21 -04:00
// extract the PKGBUILD from the just-created archive and build it
$t->addCode(function() use ($archive) {
// because Robo doesn't support extracting a single file we have to do it ourselves
(new \Archive_Tar($archive))->extractList("arsse/dist/arch/PKGBUILD", BASE, "arsse/dist/arch/", false);
return $this->taskFilesystemStack()->touch(BASE."PKGBUILD")->run();
})->completion($this->taskFilesystemStack()->remove(BASE."PKGBUILD"));
$t->taskExec("makepkg -Ccf")->dir(BASE);
return $t->run();
}
2019-01-25 16:56:05 -05:00
/** Generates static manual pages in the "manual" directory
2019-09-05 10:21:36 -04:00
*
2019-01-25 16:56:05 -05:00
* The resultant files are suitable for offline viewing and inclusion into release builds
*/
public function manual(array $args): Result {
2019-08-14 12:21:08 -04:00
$execpath = escapeshellarg(norm(BASE."vendor/bin/daux"));
2019-08-04 16:21:58 -04:00
$t = $this->collectionBuilder();
2019-08-14 12:21:08 -04:00
$t->taskExec($execpath)->arg("generate")->option("-d", BASE."manual")->args($args);
2019-10-11 12:51:32 -04:00
$t->taskDeleteDir(BASE."manual/daux_libraries");
2019-08-14 12:21:08 -04:00
$t->taskDeleteDir(BASE."manual/theme");
$t->taskDeleteDir(BASE."manual/themes/src");
2019-08-04 16:21:58 -04:00
return $t->run();
}
2019-01-25 16:56:05 -05:00
/** Serves a live view of the manual using the built-in Web server */
2019-01-25 16:56:05 -05:00
public function manualLive(array $args): Result {
2019-08-14 12:21:08 -04:00
$execpath = escapeshellarg(norm(BASE."vendor/bin/daux"));
2019-01-25 16:56:05 -05:00
return $this->taskExec($execpath)->arg("serve")->args($args)->run();
}
2019-08-02 11:15:48 -04:00
/** Rebuilds the entire manual theme
2019-09-05 10:21:36 -04:00
*
2019-08-02 11:15:48 -04:00
* This requires Node and Yarn to be installed, and only needs to be done when
* Daux's theme changes
*/
public function manualTheme(array $args): Result {
2021-04-09 19:18:42 -04:00
$postcss = escapeshellarg(norm(BASE."node_modules/.bin/postcss"));
2019-10-11 12:00:48 -04:00
$themesrc = norm(BASE."docs/theme/src/").\DIRECTORY_SEPARATOR;
2019-08-14 12:21:08 -04:00
$themeout = norm(BASE."docs/theme/arsse/").\DIRECTORY_SEPARATOR;
$dauxjs = norm(BASE."vendor-bin/daux/vendor/daux/daux.io/themes/daux/js/").\DIRECTORY_SEPARATOR;
// start a collection; this stops after the first failure
$t = $this->collectionBuilder();
// install dependencies via Yarn
$t->taskExec("yarn install");
// compile the stylesheet
2021-04-09 19:18:42 -04:00
$t->taskExec($postcss)->arg($themesrc."arsse.scss")->option("-o", $themeout."arsse.css");
2019-10-11 12:00:48 -04:00
// copy JavaScript files from the Daux theme
foreach (glob($dauxjs."daux*.js") as $file) {
$t->taskFilesystemStack()->copy($file, $themeout.basename($file), true);
}
// execute the collection
return $t->run();
}
2017-12-22 11:41:54 -05:00
}