2017-12-08 19:37:49 +00:00
< ? php
use Robo\Result ;
2019-08-14 16:21:08 +00:00
const BASE = __DIR__ . \DIRECTORY_SEPARATOR ;
const BASE_TEST = BASE . " tests " . \DIRECTORY_SEPARATOR ;
define ( " IS_WIN " , defined ( " PHP_WINDOWS_VERSION_MAJOR " ));
2019-10-17 17:00:56 +00:00
define ( " IS_MAC " , php_uname ( " s " ) === " Darwin " );
2021-07-09 01:29:01 +00:00
define ( " IS_LINUX " , ! IS_WIN && ! IS_MAC );
2021-03-02 00:02:15 +00:00
error_reporting ( 0 );
2019-08-14 16:21:08 +00: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 21:56:05 +00:00
/** Runs the typical test suite
2017-12-22 16:41:54 +00:00
*
* Arguments passed to the task are passed on to PHPUnit . Thus one may , for
2017-12-17 16:37:07 +00:00
* example , run the following command and get the expected results :
2017-12-22 16:41:54 +00:00
*
* ./ robo test -- testsuite TTRSS -- exclude - group slow -- testdox
*
* Please see the PHPUnit documentation for available options .
2020-03-01 20:16:50 +00:00
*/
2017-12-08 19:37:49 +00:00
public function test ( array $args ) : Result {
2019-01-22 22:49:14 +00:00
return $this -> runTests ( escapeshellarg ( \PHP_BINARY ), " typical " , $args );
2017-12-17 15:27:34 +00:00
}
2019-01-25 21:56:05 +00:00
/** Runs the full test suite
2017-12-22 16:41:54 +00:00
*
2018-08-15 13:59:07 +00:00
* This includes pedantic tests which may help to identify problems .
* See help for the " test " task for more details .
2020-03-01 20:16:50 +00:00
*/
2017-12-17 16:37:07 +00:00
public function testFull ( array $args ) : Result {
2019-01-22 22:49:14 +00:00
return $this -> runTests ( escapeshellarg ( \PHP_BINARY ), " full " , $args );
2017-12-17 16:37:07 +00:00
}
2017-12-22 16:41:54 +00:00
/**
* Runs a quick subset of the test suite
*
* See help for the " test " task for more details .
2020-03-01 20:16:50 +00:00
*/
2017-12-17 15:27:34 +00:00
public function testQuick ( array $args ) : Result {
2019-01-22 22:49:14 +00:00
return $this -> runTests ( escapeshellarg ( \PHP_BINARY ), " quick " , $args );
2017-12-08 19:37:49 +00:00
}
2017-12-22 16:41:54 +00:00
/** Produces a code coverage report
*
2017-12-17 16:37:07 +00:00
* By default this task produces an HTML - format coverage report in
2018-08-15 13:59:07 +00:00
* tests / coverage /. Additional reports may be produced by passing
2017-12-17 16:37:07 +00:00
* arguments to this task as one would to PHPUnit .
2017-12-22 16:41:54 +00:00
*
2020-02-02 04:43:46 +00:00
* Robo first tries to use pcov and will fall back first to xdebug then
2020-03-01 15:17:16 +00:00
* phpdbg . Neither pcov nor xdebug need to be enabled to be used ; they
2020-02-02 04:43:46 +00:00
* only need to be present in the extension load path to be used .
2020-03-01 20:16:50 +00:00
*/
2017-12-08 19:37:49 +00:00
public function coverage ( array $args ) : Result {
2018-12-05 22:07:47 +00:00
// run tests with code coverage reporting enabled
$exec = $this -> findCoverageEngine ();
2019-08-14 16:21:08 +00:00
return $this -> runTests ( $exec , " coverage " , array_merge ([ " --coverage-html " , BASE_TEST . " coverage " ], $args ));
2018-12-05 22:07:47 +00:00
}
/** 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 22:28:11 +00:00
*
2018-12-05 22:07:47 +00:00
* See also help for the " coverage " task for more details .
2020-03-01 20:16:50 +00:00
*/
2018-12-05 22:07:47 +00:00
public function coverageFull ( array $args ) : Result {
2017-12-17 15:27:34 +00:00
// run tests with code coverage reporting enabled
$exec = $this -> findCoverageEngine ();
2019-08-14 16:21:08 +00:00
return $this -> runTests ( $exec , " typical " , array_merge ([ " --coverage-html " , BASE_TEST . " coverage " ], $args ));
2018-08-15 13:59:07 +00:00
}
/** Runs the coding standards fixer */
public function clean ( $opts = [ 'demo|d' => false ]) : Result {
2019-08-14 16:21:08 +00:00
$t = $this -> taskExec ( norm ( BASE . " vendor/bin/php-cs-fixer " ));
2018-08-15 13:59:07 +00:00
$t -> arg ( " fix " );
if ( $opts [ 'demo' ]) {
$t -> args ( " --dry-run " , " --diff " ) -> option ( " --diff-format " , " udiff " );
}
return $t -> run ();
2017-12-08 19:37:49 +00:00
}
2017-12-17 15:27:34 +00:00
protected function findCoverageEngine () : string {
2020-02-02 04:43:46 +00: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 21:51:25 +00:00
return " $php -d xdebug.mode=coverage " ;
2020-02-02 04:43:46 +00:00
} elseif ( file_exists ( $dir . " pcov. $ext " )) {
return " $php -d extension=pcov. $ext -d pcov.enabled=1 -d pcov.directory= $code " ;
2020-11-07 00:56:32 +00:00
} elseif ( file_exists ( $dir . " xdebug. $ext " )) {
2021-01-03 21:51:25 +00:00
return " $php -d zend_extension=xdebug. $ext -d xdebug.mode=coverage " ;
2017-12-17 15:27:34 +00:00
} else {
2020-02-02 04:43:46 +00: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 ;
}
2017-12-17 15:27:34 +00:00
}
}
2019-03-25 13:53:06 +00:00
protected function blackhole ( bool $all = false ) : string {
2019-08-14 16:21:08 +00:00
$hole = IS_WIN ? " nul " : " /dev/null " ;
2019-03-25 13:53:06 +00:00
return $all ? " > $hole 2>&1 " : " 2> $hole " ;
}
2020-03-01 20:16:50 +00:00
protected function runTests ( string $executor , string $set , array $args ) : Result {
2018-08-15 13:59:07 +00:00
switch ( $set ) {
case " typical " :
$set = [ " --exclude-group " , " optional " ];
break ;
case " quick " :
$set = [ " --exclude-group " , " optional,slow " ];
break ;
2018-12-05 22:07:47 +00:00
case " coverage " :
2018-12-10 17:39:09 +00:00
$set = [ " --exclude-group " , " optional,coverageOptional " ];
2018-12-05 22:07:47 +00:00
break ;
2018-08-15 13:59:07 +00:00
case " full " :
$set = [];
break ;
default :
throw new \Exception ;
}
2019-08-14 16:21:08 +00: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 12:32:40 +00:00
return $this -> taskExec ( $executor ) -> option ( " -d " , " zend.assertions=1 " ) -> arg ( $execpath ) -> option ( " -c " , $confpath ) -> args ( array_merge ( $set , $args )) -> run ();
2018-08-15 13:59:07 +00:00
}
2021-07-09 02:41:24 +00:00
protected function commitVersion ( ? string $commit ) : array {
2021-07-08 19:55:59 +00:00
$target = $commit ? ? $this -> askDefault ( " Reference commit: " , " HEAD " );
$base = escapeshellarg ( BASE );
$blackhole = $this -> blackhole ();
// get useable version strings from Git
2021-07-09 02:41:24 +00:00
$version = trim ( `git -C $base describe --tags $target $blackhole` );
if ( ! $version ) {
2021-07-08 19:55:59 +00:00
throw new \Exception ( " Commit reference invalid " );
}
2021-07-09 02:41:24 +00:00
return [ $target , $version ];
2021-07-08 19:55:59 +00:00
}
2021-07-09 01:29:01 +00:00
protected function toolExists ( string ... $binary ) : bool {
2021-07-09 02:41:24 +00:00
$blackhole = $this -> blackhole ( IS_WIN );
2021-07-09 01:29:01 +00:00
foreach ( $binary as $bin ) {
if (
2021-07-09 02:41:24 +00:00
( IS_WIN && ( ! exec ( escapeshellarg ( $bin ) . " --help $blackhole " , $junk , $status ) || $status ))
|| ( ! IS_WIN && ( ! exec ( " which " . escapeshellarg ( $bin ) . " $blackhole " , $junk , $status ) || $status ))
2022-09-15 14:12:04 +00:00
) {
2022-01-15 00:28:42 +00:00
return false ;
2021-07-09 01:29:01 +00:00
}
}
return true ;
}
2017-12-22 16:41:54 +00:00
/** Packages a given commit of the software into a release tarball
*
2021-07-08 22:04:15 +00:00
* The commit to package may be any Git tree - ish identifier : a tag , a branch ,
2017-12-17 16:37:07 +00:00
* or any commit hash . If none is provided on the command line , Robo will prompt
2021-05-19 12:51:17 +00:00
* for a commit to package ; the default is " HEAD " .
2017-12-22 16:41:54 +00:00
*
2017-12-17 16:37:07 +00:00
* Note that while it is possible to re - package old versions , the resultant tarball
2017-12-22 16:41:54 +00:00
* may not be equivalent due to subsequent changes in the exclude list , or because
2017-12-17 16:37:07 +00:00
* of new tooling .
2020-03-01 20:16:50 +00:00
*/
2021-07-09 02:41:24 +00:00
public function packageGeneric ( string $commit = null ) : Result {
2021-07-09 01:29:01 +00:00
if ( ! $this -> toolExists ( " git " , " pandoc " )) {
throw new \Exception ( " Git and Pandoc are required in PATH to produce generic release tarballs " );
}
2017-12-08 19:37:49 +00:00
// establish which commit to package
2021-07-09 02:41:24 +00:00
[ $commit , $version ] = $this -> commitVersion ( $commit );
2022-04-04 18:19:53 +00:00
preg_match ( '/^([^-]+)(?:-(\d+)-(\w+))?$/' , $version , $m );
$archVersion = $m [ 1 ] . ( $m [ 2 ] ? " .r $m[2] . $m[3] " : " " );
$baseVersion = $m [ 1 ];
$release = $m [ 2 ];
2021-07-08 19:55:59 +00:00
// name the generic release tarball
$tarball = BASE . " release/ $version /arsse- $version .tar.gz " ;
2017-12-08 19:37:49 +00:00
// 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
2021-07-08 19:55:59 +00:00
$result = $this -> taskExec ( " git worktree add " . escapeshellarg ( $dir ) . " " . escapeshellarg ( $version )) -> dir ( BASE ) -> run ();
2021-05-21 03:38:03 +00:00
if ( $result -> getExitCode () > 0 ) {
return $result ;
}
2021-05-21 10:43:17 +00:00
try {
2022-04-04 18:19:53 +00:00
// Perform Arch-specific tasks
2021-07-17 11:12:24 +00:00
if ( file_exists ( $dir . " dist/arch " )) {
// patch the Arch PKGBUILD file with the correct version string
$t -> addTask ( $this -> taskReplaceInFile ( $dir . " dist/arch/PKGBUILD " ) -> regex ( '/^pkgver=.*$/m' ) -> to ( " pkgver= $archVersion " ));
// patch the Arch PKGBUILD file with the correct source file
$t -> addTask ( $this -> taskReplaceInFile ( $dir . " dist/arch/PKGBUILD " ) -> regex ( '/^source=\("arsse-[^"]+"\)$/m' ) -> to ( 'source=("' . basename ( $tarball ) . '")' ));
}
2022-04-04 18:19:53 +00:00
// perform Debian-specific tasks
if ( file_exists ( $dir . " dist/debian " )) {
// generate the Debian changelog; this also validates our original changelog
$changelog = $this -> changelogParse ( file_get_contents ( $dir . " CHANGELOG " ), $version );
$debianChangelog = $this -> changelogDebian ( $changelog , $version );
// save the Debian-format changelog
$t -> addTask ( $this -> taskWriteToFile ( $dir . " dist/debian/changelog " ) -> text ( $debianChangelog ));
}
// perform RPM-specific tasks
if ( file_exists ( $dir . " dist/rpm " )) {
// patch the spec file with the correct version and release
$t -> addTask ( $this -> taskReplaceInFile ( $dir . " dist/rpm/arsse.spec " ) -> regex ( '/^Version: .*$/m' ) -> to ( " Version: $baseVersion " ));
$t -> addTask ( $this -> taskReplaceInFile ( $dir . " dist/rpm/arsse.spec " ) -> regex ( '/^Release: .*$/m' ) -> to ( " Release: $release " ));
// patch the spec file with the correct tarball name
$t -> addTask ( $this -> taskReplaceInFile ( $dir . " dist/rpm/arsse.spec " ) -> regex ( '/^Source0: .*$/m' ) -> to ( " Source0: arsse- $version .tar.gz " ));
// append the RPM changelog to the spec file
$t -> addTask ( $this -> taskWriteToFile ( $dir . " dist/rpm/arsse.spec " ) -> append ( true ) -> text ( " \n \n %changelog \n " . $this -> changelogRPM ( $changelog , $version )));
}
2021-05-31 16:12:56 +00:00
// save commit description to VERSION file for reference
2021-05-21 10:43:17 +00:00
$t -> addTask ( $this -> taskWriteToFile ( $dir . " VERSION " ) -> text ( $version ));
2022-04-04 18:19:53 +00:00
if ( file_exists ( $dir . " docs " ) || file_exists ( $dir . " manpages " )) {
// perform Composer installation in the temp location with dev dependencies to include Robo and Daux
$t -> addTask ( $this -> taskExec ( " composer install " ) -> arg ( " -q " ) -> dir ( $dir ));
}
2021-07-17 11:12:24 +00:00
if ( file_exists ( $dir . " manpages " )) {
// generate manpages
$t -> addTask ( $this -> taskExec ( " ./robo manpage " ) -> dir ( $dir ));
}
if ( file_exists ( $dir . " docs " )) {
// generate the HTML manual
$t -> addTask ( $this -> taskExec ( " ./robo manual -q " ) -> dir ( $dir ));
}
2021-05-21 10:43:17 +00:00
// perform Composer installation in the temp location for final output
2022-04-04 18:19:53 +00:00
$t -> addTask ( $this -> taskExec ( " composer install " ) -> dir ( $dir ) -> arg ( " --no-dev " ) -> arg ( " -o " ) -> arg ( " --no-scripts " ) -> arg ( " -q " ));
2021-05-21 10:43:17 +00:00
// delete unwanted files
$t -> addTask ( $this -> taskFilesystemStack () -> remove ([
$dir . " .git " ,
$dir . " .gitignore " ,
$dir . " .gitattributes " ,
2021-05-31 16:12:56 +00:00
$dir . " dist/debian/.gitignore " ,
2021-05-21 10:43:17 +00:00
$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 " ,
2021-05-29 19:44:51 +00:00
$dir . " manpages " ,
2021-05-21 10:43:17 +00:00
$dir . " tests " ,
$dir . " vendor-bin " ,
$dir . " vendor/bin " ,
$dir . " robo " ,
$dir . " robo.bat " ,
$dir . " package.json " ,
$dir . " yarn.lock " ,
$dir . " postcss.config.js " ,
]));
2021-05-28 20:23:42 +00:00
$t -> addCode ( function () use ( $dir ) {
// Remove files which lintian complains about; they're otherwise harmless
$files = [];
foreach ( new \CallbackFilterIterator ( new \RecursiveIteratorIterator ( new \RecursiveDirectoryIterator ( $dir . " vendor " , \FilesystemIterator :: CURRENT_AS_PATHNAME | \FilesystemIterator :: SKIP_DOTS )), function ( $v , $k , $i ) {
2021-06-24 15:58:50 +00:00
return preg_match ( '/\/\.git(?:ignore|attributes|modules)$/D' , $v );
2021-05-28 20:23:42 +00:00
}) as $f ) {
$files [] = $f ;
}
return $this -> taskFilesystemStack () -> remove ( $files ) -> run ();
});
2021-05-21 10:43:17 +00:00
// generate a sample configuration file
$t -> addTask ( $this -> taskExec ( escapeshellarg ( \PHP_BINARY ) . " arsse.php conf save-defaults config.defaults.php " ) -> dir ( $dir ));
// remove any existing archive
$t -> addTask ( $this -> taskFilesystemStack () -> remove ( $tarball ));
// package it all up
2021-07-08 19:55:59 +00:00
$t -> addTask ( $this -> taskFilesystemStack () -> mkdir ( dirname ( $tarball )));
2021-05-21 10:43:17 +00:00
$t -> addTask ( $this -> taskPack ( $tarball ) -> addDir ( " arsse " , $dir ));
// execute the collection
$result = $t -> run ();
} finally {
// remove the Git worktree
$this -> taskFilesystemStack () -> remove ( $dir ) -> run ();
$this -> taskExec ( " git worktree prune " ) -> dir ( BASE ) -> run ();
}
2021-05-21 03:38:03 +00:00
return $result ;
2021-05-19 12:51:17 +00:00
}
2021-07-17 04:19:55 +00:00
/** Packages a release tarball into a Debian source package
*
* The commit 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 " .
*/
public function packageDebsrc ( string $commit = null ) : Result {
// establish which commit to package
[ $commit , $version ] = $this -> commitVersion ( $commit );
$tarball = BASE . " release/ $version /arsse- $version .tar.gz " ;
// determine the base version (i.e. x.y.z) and the Debian version (i.e. x.y.z-a)
preg_match ( '/^(\d+(?:\.\d+)+)(?:-(\d+))?/' , $version , $m );
$baseVersion = $m [ 1 ];
$debVersion = $m [ 1 ] . " - " . ( $version === $baseVersion ? " 1 " : $m [ 2 ]);
// start a task collection and create a temporary directory
$t = $this -> collectionBuilder ();
$dir = $t -> tmpDir () . \DIRECTORY_SEPARATOR ;
// build the generic release tarball if it doesn't exist
if ( ! file_exists ( $tarball )) {
$t -> addTask ( $this -> taskExec ( BASE . " robo package:generic $commit " ));
}
$base = $dir . " arsse- $version " . \DIRECTORY_SEPARATOR ;
// start by extracting the tarball
$t -> addCode ( function () use ( $tarball , $dir , $base ) {
// Robo's extract task is broken, so we do it manually
( new \Archive_Tar ( $tarball )) -> extract ( $dir , false );
return $this -> taskFilesystemStack () -> rename ( $dir . " arsse " , $base ) -> run ();
});
// re-pack the tarball using a specific name special to Debian
$t -> addTask ( $this -> taskPack ( $dir . " arsse_ $baseVersion .orig.tar.gz " ) -> addDir ( " arsse- $baseVersion " , $base ));
// pack the debian tarball
2022-04-04 18:19:53 +00:00
$t -> addTask ( $this -> taskPack ( $dir . " arsse_ $debVersion .debian.tar.gz " ) -> addDir ( " debian " , $base . " dist/debian " ));
2021-07-17 04:19:55 +00:00
// generate the DSC file
$t -> addCode ( function () use ( $t , $debVersion , $baseVersion , $dir , $base ) {
try {
$dsc = $this -> generateDebianSourceControl ( $base . " dist/debian/ " , $debVersion , [ $dir . " arsse_ $baseVersion .orig.tar.gz " , $dir . " arsse_ $debVersion .debian.tar.gz " ]);
} catch ( \Exception $e ) {
return new Result ( $t , 1 , $e -> getMessage ());
}
// write the DSC file
return $this -> taskWriteToFile ( $dir . " arsse_ $debVersion .dsc " ) -> text ( $dsc ) -> run ();
});
// delete any existing files
2022-04-04 18:19:53 +00:00
$t -> AddTask ( $this -> taskFilesystemStack () -> remove ([ BASE . " release/ $version /arsse_ $baseVersion .orig.tar.gz " , BASE . " release/ $version /arsse_ $debVersion .debian.tar.gz " , BASE . " release/ $version /arsse_ $debVersion .dsc " ]));
// copy the new files over
2021-07-17 04:19:55 +00:00
$t -> addTask ( $this -> taskFilesystemStack () -> copy ( $dir . " arsse_ $baseVersion .orig.tar.gz " , BASE . " release/ $version /arsse_ $baseVersion .orig.tar.gz " ) -> copy ( $dir . " arsse_ $debVersion .debian.tar.gz " , BASE . " release/ $version /arsse_ $debVersion .debian.tar.gz " ) -> copy ( $dir . " arsse_ $debVersion .dsc " , BASE . " release/ $version /arsse_ $debVersion .dsc " ));
return $t -> run ();
}
2022-04-04 18:19:53 +00:00
/** Packages a given commit of the software and produces all relevant release files
2021-07-09 02:41:24 +00:00
*
* The commit 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 " .
2022-01-15 00:28:42 +00:00
*
2022-04-04 18:19:53 +00:00
* In addition to the release tarball , a Debian source package , Arch PKGBUILD ,
* and RPM spec file are output as well . These are suitable for use with Open
* Build Service instances and with slight modification the Arch User Repository .
* Use for Launchpad PPAs has not been tested .
2021-07-09 02:41:24 +00:00
*/
public function package ( string $commit = null ) : Result {
if ( ! $this -> toolExists ( " git " )) {
throw new \Exception ( " Git is required in PATH to produce packages " );
}
2022-04-04 18:19:53 +00:00
[ $commit , $version ] = $this -> commitVersion ( $commit );
$tarball = BASE . " release/ $version /arsse- $version .tar.gz " ;
// build the generic release tarball
$result = $this -> taskExec ( BASE . " robo package:generic $commit " ) -> run ();
if ( ! $result -> wasSuccessful ()) {
return $result ;
}
// if the generic tarball could be built, try to produce Arch, Debian, and RPM files; these might legitimately not exist in old releases
// start by getting the list of files from the tarball
$archive = new \Archive_Tar ( $tarball );
$filelist = array_flip ( array_column ( $archive -> listContent (), " filename " ));
2021-07-09 02:41:24 +00:00
// start a collection
$t = $this -> collectionBuilder ();
2022-04-04 18:19:53 +00:00
// Produce an Arch PKGBUILD if appropriate
if ( isset ( $filelist [ 'arsse/dist/arch/PKGBUILD' ])) {
$t -> addCode ( function () use ( $tarball , $archive ) {
$dir = dirname ( $tarball ) . \DIRECTORY_SEPARATOR ;
$archive -> extractList ( " arsse/dist/arch/PKGBUILD " , $dir , " arsse/dist/arch/ " , false );
// update the tarball's checksum
$sums = [
'md5' => hash_file ( " md5 " , $tarball ),
];
return $this -> taskReplaceInFile ( $dir . " PKGBUILD " ) -> regex ( '/^md5sums=\("SKIP"\)$/m' ) -> to ( 'md5sums=("' . $sums [ 'md5' ] . '")' ) -> run ();
});
2021-07-09 02:41:24 +00:00
}
2022-04-04 18:19:53 +00:00
// Produce a Debian source package if appropriate
if ( isset ( $filelist [ 'arsse/dist/debian/control' ]) && isset ( $filelist [ 'arsse/dist/debian/source/format' ])) {
$t -> addTask ( $this -> taskExec ( BASE . " robo package:debsrc $commit " ));
2021-07-09 02:41:24 +00:00
}
2022-04-04 18:19:53 +00:00
// Produce an RPM spec file if appropriate
if ( isset ( $filelist [ 'arsse/dist/rpm/arsse.spec' ])) {
$t -> addCode ( function () use ( $tarball , $archive ) {
$dir = dirname ( $tarball ) . \DIRECTORY_SEPARATOR ;
$archive -> extractList ( " arsse/dist/rpm/arsse.spec " , $dir , " arsse/dist/rpm/ " , false );
// perform a do-nothing filesystem operation since we need a Robo task result
return $this -> taskFilesystemStack () -> chmod ( $dir . " arsse.spec " , 0644 ) -> run ();
});
}
return $t -> run ();
2021-07-09 02:41:24 +00:00
}
2019-01-25 21:56:05 +00:00
/** Generates static manual pages in the " manual " directory
2019-09-05 14:21:36 +00:00
*
2019-01-25 21:56:05 +00:00
* The resultant files are suitable for offline viewing and inclusion into release builds
*/
2019-01-22 22:49:14 +00:00
public function manual ( array $args ) : Result {
2019-08-14 16:21:08 +00:00
$execpath = escapeshellarg ( norm ( BASE . " vendor/bin/daux " ));
2019-08-04 20:21:58 +00:00
$t = $this -> collectionBuilder ();
2019-08-14 16:21:08 +00:00
$t -> taskExec ( $execpath ) -> arg ( " generate " ) -> option ( " -d " , BASE . " manual " ) -> args ( $args );
2019-10-11 16:51:32 +00:00
$t -> taskDeleteDir ( BASE . " manual/daux_libraries " );
2019-08-14 16:21:08 +00:00
$t -> taskDeleteDir ( BASE . " manual/theme " );
$t -> taskDeleteDir ( BASE . " manual/themes/src " );
2019-08-04 20:21:58 +00:00
return $t -> run ();
2019-01-22 22:49:14 +00:00
}
2019-01-25 21:56:05 +00:00
2019-08-05 00:49:50 +00:00
/** Serves a live view of the manual using the built-in Web server */
2019-01-25 21:56:05 +00:00
public function manualLive ( array $args ) : Result {
2019-08-14 16:21:08 +00:00
$execpath = escapeshellarg ( norm ( BASE . " vendor/bin/daux " ));
2019-01-25 21:56:05 +00:00
return $this -> taskExec ( $execpath ) -> arg ( " serve " ) -> args ( $args ) -> run ();
}
2019-08-02 15:15:48 +00:00
2019-08-05 00:49:50 +00:00
/** Rebuilds the entire manual theme
2019-09-05 14:21:36 +00:00
*
2019-08-02 15:15:48 +00: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-07-09 01:29:01 +00:00
if ( ! $this -> toolExists ( " yarn " )) {
throw new \Exception ( " Yarn is required in PATH to update the Daux theme " );
}
2021-04-09 23:18:42 +00:00
$postcss = escapeshellarg ( norm ( BASE . " node_modules/.bin/postcss " ));
2019-10-11 16:00:48 +00:00
$themesrc = norm ( BASE . " docs/theme/src/ " ) . \DIRECTORY_SEPARATOR ;
2019-08-14 16:21:08 +00:00
$themeout = norm ( BASE . " docs/theme/arsse/ " ) . \DIRECTORY_SEPARATOR ;
$dauxjs = norm ( BASE . " vendor-bin/daux/vendor/daux/daux.io/themes/daux/js/ " ) . \DIRECTORY_SEPARATOR ;
2019-08-03 02:34:41 +00:00
// start a collection; this stops after the first failure
$t = $this -> collectionBuilder ();
2019-08-04 15:02:33 +00:00
// install dependencies via Yarn
$t -> taskExec ( " yarn install " );
// compile the stylesheet
2021-04-09 23:18:42 +00:00
$t -> taskExec ( $postcss ) -> arg ( $themesrc . " arsse.scss " ) -> option ( " -o " , $themeout . " arsse.css " );
2019-10-11 16:00:48 +00:00
// copy JavaScript files from the Daux theme
foreach ( glob ( $dauxjs . " daux*.js " ) as $file ) {
$t -> taskFilesystemStack () -> copy ( $file , $themeout . basename ( $file ), true );
}
2019-08-04 15:02:33 +00:00
// execute the collection
return $t -> run ();
}
2021-05-20 21:47:02 +00:00
2021-05-29 15:13:19 +00:00
/** Generates the " arsse " command ' s manual page ( UNIX man page )
2021-07-06 01:47:44 +00:00
*
* This requires that the Pandoc document converter be installed and
2021-05-29 15:13:19 +00:00
* available in $PATH .
*/
public function manpage () : Result {
2021-07-09 01:29:01 +00:00
if ( ! $this -> toolExists ( " pandoc " )) {
throw new \Exception ( " Pandoc is required in PATH to generate manual pages " );
}
2021-05-29 20:19:52 +00:00
$t = $this -> collectionBuilder ();
2021-05-29 19:44:51 +00:00
$man = [
'en' => " man1/arsse.1 " ,
];
2021-07-06 01:47:44 +00:00
foreach ( $man as $src => $out ) {
2021-05-29 20:19:52 +00:00
$src = BASE . " manpages/ $src .md " ;
$out = BASE . " dist/man/ $out " ;
$t -> addTask ( $this -> taskFilesystemStack () -> mkdir ( dirname ( $out ), 0755 ));
2021-05-30 02:45:13 +00:00
$t -> addTask ( $this -> taskExec ( " pandoc -s -f markdown-smart -t man -o " . escapeshellarg ( $out ) . " " . escapeshellarg ( $src )));
$t -> addTask ( $this -> taskReplaceInFile ( $out ) -> regex ( '/\.\n(?!\.)/s' ) -> to ( " . " ));
2021-05-29 19:44:51 +00:00
}
2021-05-29 20:19:52 +00:00
return $t -> run ();
2021-05-29 15:13:19 +00:00
}
2021-05-21 03:53:25 +00:00
protected function changelogParse ( string $text , string $targetVersion ) : array {
$lines = preg_split ( '/\r?\n/' , $text );
$version = " " ;
$section = " " ;
$out = [];
$entry = [];
$expected = [ " version " ];
for ( $a = 0 ; $a < sizeof ( $lines );) {
$l = rtrim ( $lines [ $a ++ ]);
2021-06-24 15:58:50 +00:00
if ( in_array ( " version " , $expected ) && preg_match ( '/^Version (\d+(?:\.\d+)*) \(([\d\?]{4}-[\d\?]{2}-[\d\?]{2})\)\s*$/D' , $l , $m )) {
2021-05-21 03:53:25 +00:00
$version = $m [ 1 ];
2021-06-24 15:58:50 +00:00
if ( ! preg_match ( '/^\d{4}-\d{2}-\d{2}$/D' , $m [ 2 ])) {
2021-05-21 03:53:25 +00:00
// uncertain dates are allowed only for the top version, and only if it does not match the target version (otherwise we have forgotten to set the correct date before tagging)
if ( ! $out && $targetVersion !== $version ) {
// use today's date; local time is fine
$date = date ( " Y-m-d " );
} else {
throw new \Exception ( " CHANGELOG: Date at line $a is incomplete " );
}
2021-05-20 21:47:02 +00:00
} else {
2021-05-21 03:53:25 +00:00
$date = $m [ 2 ];
2021-05-20 21:47:02 +00:00
}
2021-05-21 03:53:25 +00:00
if ( $entry ) {
$out [] = $entry ;
2021-07-06 01:47:44 +00:00
}
2021-05-21 03:53:25 +00:00
$entry = [ 'version' => $version , 'date' => $date , 'features' => [], 'fixes' => [], 'changes' => []];
$expected = [ " separator " ];
} elseif ( in_array ( " separator " , $expected ) && preg_match ( '/^=+/' , $l )) {
$length = strlen ( $lines [ $a - 2 ]);
if ( strlen ( $l ) !== $length ) {
throw new \Exception ( " CHANGELOG: Separator at line $a is of incorrect length " );
}
$expected = [ " blank line " ];
$section = " " ;
} elseif ( in_array ( " blank line " , $expected ) && $l === " " ) {
$expected = [
'' => [ " features section " , " fixes section " , " changes section " ],
'features' => [ " fixes section " , " changes section " , " version " ],
'fixes' => [ " changes section " , " version " ],
'changes' => [ " version " ],
][ $section ];
$expected [] = " end-of-file " ;
} elseif ( in_array ( " features section " , $expected ) && $l === " New features: " ) {
$section = " features " ;
$expected = [ " item " ];
} elseif ( in_array ( " fixes section " , $expected ) && $l === " Bug fixes: " ) {
$section = " fixes " ;
$expected = [ " item " ];
} elseif ( in_array ( " changes section " , $expected ) && $l === " Changes: " ) {
$section = " changes " ;
$expected = [ " item " ];
2021-06-24 15:58:50 +00:00
} elseif ( in_array ( " item " , $expected ) && preg_match ( '/^- (\w.*)$/D' , $l , $m )) {
2021-05-21 03:53:25 +00:00
$entry [ $section ][] = $m [ 1 ];
$expected = [ " item " , " continuation " , " blank line " ];
2021-06-24 15:58:50 +00:00
} elseif ( in_array ( " continuation " , $expected ) && preg_match ( '/^ (\w.*)$/D' , $l , $m )) {
2021-05-21 03:53:25 +00:00
$last = sizeof ( $entry [ $section ]) - 1 ;
$entry [ $section ][ $last ] .= " \n " . $m [ 1 ];
2021-05-21 03:38:03 +00:00
} else {
2021-05-21 03:53:25 +00:00
if ( sizeof ( $expected ) > 1 ) {
throw new \Exception ( " CHANGELOG: Expected one of [ " . implode ( " , " , $expected ) . " ] at line $a " );
} else {
throw new \Exception ( " CHANGELOG: Expected " . $expected [ 0 ] . " at line $a " );
}
2021-05-21 03:38:03 +00:00
}
2021-05-21 03:53:25 +00:00
}
if ( ! in_array ( " end-of-file " , $expected )) {
2021-05-21 03:38:03 +00:00
if ( sizeof ( $expected ) > 1 ) {
2021-05-21 03:53:25 +00:00
throw new \Exception ( " CHANGELOG: Expected one of [ " . implode ( " , " , $expected ) . " ] at end of file " );
2021-05-21 03:38:03 +00:00
} else {
2021-05-21 03:53:25 +00:00
throw new \Exception ( " CHANGELOG: Expected " . $expected [ 0 ] . " at end of file " );
2021-05-20 21:47:02 +00:00
}
}
2021-05-21 03:53:25 +00:00
$out [] = $entry ;
return $out ;
2021-05-20 21:47:02 +00:00
}
2021-07-06 01:47:44 +00:00
2021-05-21 03:53:25 +00:00
protected function changelogDebian ( array $log , string $targetVersion ) : string {
$latest = $log [ 0 ][ 'version' ];
$baseVersion = preg_replace ( '/^(\d+(?:\.\d+)*).*/' , " $ 1 " , $targetVersion );
if ( $baseVersion !== $targetVersion && version_compare ( $latest , $baseVersion , " > " )) {
// if the changelog contains an entry for a future version, change its version number to match the target version instead of using the future version
$log [ 0 ][ 'version' ] = $targetVersion ;
2021-07-08 19:55:59 +00:00
$log [ 0 ][ 'distribution' ] = " UNRELEASED " ;
} elseif ( $baseVersion !== $targetVersion ) {
2021-05-21 03:53:25 +00:00
// otherwise synthesize a changelog entry for the changes since the last tag
2021-07-08 19:55:59 +00:00
array_unshift ( $log , [ 'version' => $targetVersion , 'date' => date ( " Y-m-d " ), 'features' => [], 'fixes' => [], 'changes' => [ " Unspecified changes " ], 'distribution' => " UNRELEASED " ]);
2021-05-21 03:38:03 +00:00
}
2021-05-21 03:53:25 +00:00
$out = " " ;
foreach ( $log as $entry ) {
2021-05-22 01:11:22 +00:00
// normalize the version string
2021-06-24 15:58:50 +00:00
preg_match ( '/^(\d+(?:\.\d+)*)(?:-(\d+)-.+)?$/D' , $entry [ 'version' ], $m );
2021-05-22 01:11:22 +00:00
$version = $m [ 1 ] . " - " . ( $m [ 2 ] ? : " 1 " );
// output the entry
2021-07-08 19:55:59 +00:00
$out .= " arsse ( $version ) " . ( $entry [ 'distribution' ] ? ? " unstable " ) . " ; urgency=low \n " ;
2021-05-21 03:53:25 +00:00
if ( $entry [ 'features' ]) {
2021-05-22 01:11:22 +00:00
$out .= " \n " ;
2021-05-21 03:53:25 +00:00
foreach ( $entry [ 'features' ] as $item ) {
$out .= " * " . trim ( preg_replace ( " /^/m " , " " , $item )) . " \n " ;
}
2021-05-21 03:38:03 +00:00
}
2021-05-21 03:53:25 +00:00
if ( $entry [ 'fixes' ]) {
2021-05-22 01:11:22 +00:00
$out .= " \n " ;
2021-05-21 03:53:25 +00:00
foreach ( $entry [ 'fixes' ] as $item ) {
$out .= " * " . trim ( preg_replace ( " /^/m " , " " , $item )) . " \n " ;
}
2021-05-21 03:38:03 +00:00
}
2021-05-21 03:53:25 +00:00
if ( $entry [ 'changes' ]) {
2021-05-22 01:11:22 +00:00
$out .= " \n " ;
2021-05-21 03:53:25 +00:00
foreach ( $entry [ 'changes' ] as $item ) {
$out .= " * " . trim ( preg_replace ( " /^/m " , " " , $item )) . " \n " ;
}
2021-05-21 03:38:03 +00:00
}
2021-07-08 19:55:59 +00:00
$out .= " \n -- J. King <jking@jkingweb.ca> " . \DateTimeImmutable :: createFromFormat ( " Y-m-d " , $entry [ 'date' ], new \DateTimeZone ( " UTC " )) -> format ( " D, d M Y " ) . " 00:00:00 +0000 \n \n " ;
2021-05-21 03:38:03 +00:00
}
2021-05-21 03:53:25 +00:00
return $out ;
2021-05-21 03:38:03 +00:00
}
2021-07-17 04:19:55 +00:00
protected function generateDebianSourceControl ( string $dir , string $version , array $tarballs ) : string {
// read in control file
if ( ! $control = @ file_get_contents ( $dir . " control " )) {
throw new \Exception ( " Unable to read Debian control file " );
}
// read the format
if ( ! $format = @ file_get_contents ( $dir . " source/format " )) {
throw new \Exception ( " Unable to read source format in Debian files " );
}
// read the binary packages from the control file
if ( preg_match_all ( '/^Package:\s*(\S+)/m' , $control , $m )) {
$binary = [];
foreach ( $m [ 1 ] as $pkg ) {
$binary [] = $pkg ;
}
} else {
throw new \Exception ( " No packages defined in Debian control file " );
}
// read the package architectures from the control file
if ( preg_match_all ( '/^Architecture:\s*(\S+)/m' , $control , $m ) || sizeof ( $m [ 1 ]) != sizeof ( $binary )) {
$architecture = [];
foreach ( $m [ 1 ] as $pkg ) {
$architecture [] = preg_replace ( '/\s/' , " " , $pkg );
}
} else {
throw new \Exception ( " Number of architectures defined in Debian control file does not match number of packages " );
}
// read the package sections from the control file
if ( preg_match_all ( '/^Section:\s*(\S+)/m' , $control , $m ) || sizeof ( $m [ 1 ]) != sizeof ( $binary )) {
$section = [];
foreach ( $m [ 1 ] as $pkg ) {
$section [] = $pkg ;
}
} else {
throw new \Exception ( " Number of sections defined in Debian control file does not match number of packages " );
}
// read the package priorities from the control file
if ( preg_match_all ( '/^Priority:\s*(\S+)/m' , $control , $m ) || sizeof ( $m [ 1 ]) != sizeof ( $binary )) {
$priority = [];
foreach ( $m [ 1 ] as $pkg ) {
$priority [] = $pkg ;
}
} else {
throw new \Exception ( " Number of priorities defined in Debian control file does not match number of packages " );
}
// read simple metadata from the control file
$metadata = [];
foreach ([ " Source " , " Maintainer " , " Homepage " , " Standards-Version " , " Vcs-Browser " , " Vcs-Git " ] as $meta ) {
if ( preg_match ( '/^' . $meta . ':\s*(.+)/m' , $control , $m )) {
$metadata [ $meta ] = $m [ 1 ];
} else {
throw new \Exception ( " $meta is not defined in Debian control file " );
}
}
// read build dependencies from control file
if ( preg_match ( '/(?:^|\n)Build-Depends:\s*((?:[^\n]|\n(?= ))+)/s' , $control , $m )) {
$buildDepends = preg_replace ( '/\s/' , " " , $m [ 1 ]);
} else {
$buildDepends = " " ;
}
// trim format
$format = trim ( $format );
// consolidate binaries and package list
$packageList = [];
for ( $a = 0 ; $a < sizeof ( $binary ); $a ++ ) {
$packageList [] = " $binary[$a] deb $section[$a] $priority[$a] arch= $architecture[$a] " ;
}
$packageList = implode ( " \n " , $packageList );
// consolidate package names
$binary = implode ( " , " , $binary );
// consolidate architectures
$architecture = implode ( " , " , array_unique ( $architecture ));
// calculate checksums for files
$fMeta = [];
foreach ( $tarballs as $f ) {
$fMeta [ $f ] = [
'name' => basename ( $f ),
'size' => filesize ( $f ),
'sha1' => hash_file ( " sha1 " , $f ),
'sha256' => hash_file ( " sha256 " , $f ),
'md5' => hash_file ( " md5 " , $f ),
];
}
// consolidate SHA-1 checksums
$sums = [];
foreach ( $fMeta as $data ) {
$sums [] = $data [ 'sha1' ] . " " . $data [ 'size' ] . " " . $data [ 'name' ];
}
$sumsSha1 = implode ( " \n " , $sums );
// consolidate SHA-256 checksums
$sums = [];
foreach ( $fMeta as $data ) {
$sums [] = $data [ 'sha256' ] . " " . $data [ 'size' ] . " " . $data [ 'name' ];
}
$sumsSha256 = implode ( " \n " , $sums );
// consolidate MD5 checksums
$sums = [];
foreach ( $fMeta as $data ) {
$sums [] = $data [ 'md5' ] . " " . $data [ 'size' ] . " " . $data [ 'name' ];
}
$sumsMd5 = implode ( " \n " , $sums );
// return complete file
return <<< DSC_FILE
Format : $format
Source : { $metadata [ 'Source' ]}
Binary : $binary
Architecture : $architecture
Version : $version
Maintainer : { $metadata [ 'Maintainer' ]}
Homepage : { $metadata [ 'Homepage' ]}
Standards - Version : { $metadata [ 'Standards-Version' ]}
Vcs - Browser : { $metadata [ 'Vcs-Browser' ]}
Vcs - Git : { $metadata [ 'Vcs-Git' ]}
Build - Depends : $buildDepends
Package - List :
$packageList
Checksums - Sha1 :
$sumsSha1
Checksums - Sha256 :
$sumsSha256
Files :
$sumsMd5
DSC_FILE ;
}
2021-05-22 01:11:22 +00:00
}