diff --git a/Gulpfile.js b/Gulpfile.js index 70288ea..2fe2690 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -50,7 +50,7 @@ gulp.task('test-cs', function (cb) { // Bundle files as required for plugin distribution.. gulp.task('bundle', ['clean'], function () { console.log('Collecting files for package dist/' + config.name + config.version + ' ...'); - return gulp.src(['**/*.php', '!test/**', '!vendor/**', 'README.md', 'LICENSE.md'], {base: './'}) + return gulp.src(['**/*.php', '!RoboFile.php', '!test/**', '!vendor/**', 'README.md', 'LICENSE.md'], {base: './'}) .pipe(copy('./dist/' + config.name + '.' + config.version + '/' + config.name)); }); diff --git a/RoboFile.php b/RoboFile.php new file mode 100644 index 0000000..d72e8a5 --- /dev/null +++ b/RoboFile.php @@ -0,0 +1,165 @@ + + * + * @package Statify_Blacklist + * @version 1.4.2 + */ + +use Robo\Tasks; + +/** + * Class RoboFile + */ +class RoboFile extends Tasks { + const PROJECT_NAME = 'statify-blacklist'; + + const OPT_TARGET = 'target'; + const OPT_SKIPTEST = 'skipTests'; + const OPT_SKIPSTYLE = 'skipStyle'; + + /** + * Version tag (read from composer.json). + * + * @var string + */ + private $version; + + /** + * Target directory path. + * + * @var string + */ + private $target_dir; + + /** + * Final package name. + * + * @var string + */ + private $final_name; + + /** + * RoboFile constructor + * + * @param array $opts Options. + * + * @return void + */ + public function __construct( $opts = [ self::OPT_TARGET => 'dist' ] ) { + // Read composer configuration and extract version number.. + $composer = json_decode( file_get_contents( __DIR__ . '/composer.json' ) ); + // Extract parameter from options. + $this->version = $composer->version; + $this->target_dir = $opts[ self::OPT_TARGET ]; + $this->final_name = self::PROJECT_NAME . '.' . $this->version; + } + + /** + * Clean up target directory + * + * @param array $opts Options. + * + * @return void + */ + public function clean( + $opts = [ + self::OPT_TARGET => 'dist' + ] + ) { + $this->say( 'Cleaning target directory...' ); + if ( is_dir( $this->target_dir ) ) { + $this->taskCleanDir( [ $this->target_dir ] )->run(); + } + } + + /** + * Run PHPUnit tests + * + * @return void + */ + public function test() { + $this->say( 'Executing PHPUnit tests...' ); + $this->taskPhpUnit()->configFile( __DIR__ . '/phpunit.xml' )->run(); + } + + /** + * Run code style tests + * + * @return void + */ + public function testCS() { + $this->say( 'Executing PHPCS tests...' ); + $this->_exec( __DIR__ . '/vendor/bin/phpcs --standard=phpcs.xml -s' ); + } + + /** + * Build a distributable bundle. + * + * @param array $opts Options. + * + * @return void + */ + public function build( + $opts = [ + self::OPT_TARGET => 'dist', + self::OPT_SKIPTEST => false, + self::OPT_SKIPSTYLE => false, + ] + ) { + $this->clean($opts); + if ( isset( $opts[ self::OPT_SKIPTEST ] ) && true === $opts[ self::OPT_SKIPTEST ] ) { + $this->say( 'Tests skipped' ); + } else { + $this->test(); + } + if ( isset( $opts[ self::OPT_SKIPSTYLE ] ) && true === $opts[ self::OPT_SKIPSTYLE ] ) { + $this->say( 'Style checks skipped' ); + } else { + $this->testCS(); + } + $this->bundle(); + } + + /** + * Bundle global resources. + * + * @return void + */ + private function bundle() { + $this->say( 'Bundling resources...' ); + $this->taskCopyDir( [ + 'inc' => $this->target_dir . '/' . $this->final_name . '/inc', + 'views' => $this->target_dir . '/' . $this->final_name . '/views', + ] )->run(); + $this->_copy( 'statify-blacklist.php', $this->target_dir . '/' . $this->final_name . '/statify-blacklist.php' ); + $this->_copy( 'README.md', $this->target_dir . '/' . $this->final_name . '/README.md' ); + $this->_copy( 'LICENSE.md', $this->target_dir . '/' . $this->final_name . '/LICENSE.md' ); + } + + /** + * Create ZIP package from distribution bundle. + * + * @param array $opts Options. + * + * @return void + */ + public function package( + $opts = [ + self::OPT_TARGET => 'dist', + self::OPT_SKIPTEST => false, + self::OPT_SKIPSTYLE => false, + ] + ) { + $this->build($opts); + $this->say( 'Packaging...' ); + $this->taskPack( $this->target_dir . '/' . $this->final_name . '.zip' ) + ->addDir( '', $this->target_dir . '/' . $this->final_name ) + ->run(); + } +} diff --git a/composer.json b/composer.json index 4d31da0..b41e725 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.4", "php": ">=5.5", + "consolidation/robo": "^1.0.0", "phpunit/phpunit": "*", "phpunit/php-code-coverage": "*", "slowprog/composer-copy-file": "~0.2", @@ -32,6 +33,12 @@ "wp-coding-standards/wpcs": "~0.14" }, "scripts": { + "build": [ + "robo build" + ], + "package": [ + "robo package" + ], "test-all": [ "@test", "@test-cs"