From 8a35182d8159e61e51dc5f19570805cfb5a1a6d4 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 29 Dec 2017 13:36:13 +0100 Subject: [PATCH] Added SVN deployment tasks --- RoboFile.php | 241 ++++++++++++++++++++++++++++++++++++++++++++++++-- composer.json | 3 + 2 files changed, 235 insertions(+), 9 deletions(-) diff --git a/RoboFile.php b/RoboFile.php index d72e8a5..14d94b8 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -11,6 +11,7 @@ * @version 1.4.2 */ +use Robo\Exception\TaskException; use Robo\Tasks; /** @@ -18,6 +19,7 @@ use Robo\Tasks; */ class RoboFile extends Tasks { const PROJECT_NAME = 'statify-blacklist'; + const SVN_URL = 'https://plugins.svn.wordpress.org/statify-blacklist'; const OPT_TARGET = 'target'; const OPT_SKIPTEST = 'skipTests'; @@ -67,14 +69,13 @@ class RoboFile extends Tasks { * * @return void */ - public function clean( - $opts = [ - self::OPT_TARGET => 'dist' - ] - ) { + 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(); + if ( is_dir( $this->target_dir . '/' . $this->final_name ) ) { + $this->_deleteDir( [ $this->target_dir . '/' . $this->final_name ] ); + } + if ( is_file( $this->target_dir . '/' . $this->final_name . '.zip' ) ) { + $this->_remove( $this->target_dir . '/' . $this->final_name . '.zip' ); } } @@ -112,7 +113,7 @@ class RoboFile extends Tasks { self::OPT_SKIPSTYLE => false, ] ) { - $this->clean($opts); + $this->clean( $opts ); if ( isset( $opts[ self::OPT_SKIPTEST ] ) && true === $opts[ self::OPT_SKIPTEST ] ) { $this->say( 'Tests skipped' ); } else { @@ -156,10 +157,232 @@ class RoboFile extends Tasks { self::OPT_SKIPSTYLE => false, ] ) { - $this->build($opts); + $this->build( $opts ); $this->say( 'Packaging...' ); $this->taskPack( $this->target_dir . '/' . $this->final_name . '.zip' ) ->addDir( '', $this->target_dir . '/' . $this->final_name ) ->run(); } + + /** + * Deploy development version (trunk). + * + * @param array $opts Options. + * + * @return void + * @throws TaskException On errors. + */ + public function deployTrunk( + $opts = [ + self::OPT_TARGET => 'dist', + self::OPT_SKIPTEST => false, + self::OPT_SKIPSTYLE => false, + ] + ) { + // First execute build job. + $this->build( $opts ); + + // Prepare VCS, either checkout or update local copy. + $this->prepareVCS(); + + $this->say( 'Preparing deployment directory...' ); + $this->updateVCStrunk(); + + // Update remote repository. + $this->say( 'Deploying...' ); + $this->commitVCS( + '--force trunk/*', + 'Updated ' . self::PROJECT_NAME . ' trunk' + ); + } + + /** + * Deploy current version tag. + * + * @param array $opts Options. + * + * @return void + * @throws TaskException On errors. + */ + public function deployTag( + $opts = [ + self::OPT_TARGET => 'dist', + self::OPT_SKIPTEST => false, + self::OPT_SKIPSTYLE => false, + ] + ) { + // First execute build job. + $this->build( $opts ); + + // Prepare VCS, either checkout or update local copy. + $this->prepareVCS(); + + $this->say( 'Preparing deployment directory...' ); + $this->updateVCStag(); + + // Update remote repository. + $this->say( 'Deploying...' ); + $this->commitVCS( + 'tags/' . $this->version, + 'Updated ' . self::PROJECT_NAME . ' v' . $this->version + ); + } + + /** + * Deploy current version tag. + * + * @param array $opts Options. + * + * @return void + * @throws TaskException On errors. + */ + public function deployReadme( + $opts = [ + self::OPT_TARGET => 'dist', + self::OPT_SKIPTEST => false, + self::OPT_SKIPSTYLE => false, + ] + ) { + // First execute build job. + $this->build( $opts ); + + // Prepare VCS, either checkout or update local copy. + $this->prepareVCS(); + + $this->updateVCSreadme(); + + // Update remote repository. + $this->say( 'Deploying...' ); + $this->commitVCS( + '--force trunk/README.md', + 'Updated ' . self::PROJECT_NAME . ' ReadMe' + ); + } + + /** + * Deploy current version tag and trunk. + * + * @param array $opts Options. + * + * @return void + * @throws TaskException On errors. + */ + public function deployAll( + $opts = [ + self::OPT_TARGET => 'dist', + self::OPT_SKIPTEST => false, + self::OPT_SKIPSTYLE => false, + ] + ) { + // First execute build job. + $this->build( $opts ); + + // Prepare VCS, either checkout or update local copy. + $this->prepareVCS(); + + $this->say( 'Preparing deployment directory...' ); + $this->updateVCStrunk(); + $this->updateVCStag(); + + // Update remote repository. + $this->say( 'Deploying...' ); + $this->commitVCS( + [ + '--force trunk/*', + '--force tags/' . $this->version, + ], + 'Updated ' . self::PROJECT_NAME . ' v' . $this->version + ); + } + + /** + * Prepare VCS direcory. + * + * Checkout or update local copy of SVN repository. + * + * @return void + * @throws TaskException On errors. + */ + private function prepareVCS() { + if ( is_dir( $this->target_dir . '/svn' ) ) { + $this->taskSvnStack() + ->stopOnFail() + ->dir( $this->target_dir . '/svn/statify-blacklist' ) + ->update() + ->run(); + } else { + $this->_mkdir( $this->target_dir . '/svn' ); + $this->taskSvnStack() + ->dir( $this->target_dir . '/svn' ) + ->checkout( self::SVN_URL ) + ->run(); + } + } + + /** + * Commit VCS changes + * + * @param string|array $to_add Files to add. + * @param string $msg Commit message. + * + * @return void + * @throws TaskException On errors. + */ + private function commitVCS( $to_add, $msg ) { + $task = $this->taskSvnStack() + ->stopOnFail() + ->dir( $this->target_dir . '/svn/statify-blacklist' ); + + if ( is_array( $to_add ) ) { + foreach ( $to_add as $ta ) { + $task = $task->add( $ta ); + } + } else { + $task = $task->add( $to_add ); + } + + $task->commit( $msg )->run(); + } + + /** + * Update SVN readme file. + * + * @return void + */ + private function updateVCSreadme() { + $trunk_dir = $this->target_dir . '/svn/statify-blacklist/trunk'; + $this->_copy( $this->target_dir . '/' . $this->final_name . 'README.md', $trunk_dir . 'README.md' ); + } + + /** + * Update SVN development version (trunk). + * + * @return void + */ + private function updateVCStrunk() { + // Clean trunk directory. + $trunk_dir = $this->target_dir . '/svn/statify-blacklist/trunk'; + $this->taskCleanDir( $trunk_dir )->run(); + + // Copy built bundle to trunk. + $this->taskCopyDir( [ $this->target_dir . '/' . $this->final_name => $trunk_dir ] )->run(); + } + + /** + * Update current SVN version tag. + * + * @return void + */ + private function updateVCStag() { + // Clean tag directory if it exists. + $tag_dir = $this->target_dir . '/svn/statify-blacklist/tags/' . $this->version; + if ( is_dir( $tag_dir ) ) { + $this->taskCleanDir( $this->target_dir . '/svn/statify-blacklist/tags/' . $this->version )->run(); + } else { + $this->_mkdir( $tag_dir ); + } + + // Copy built bundle to trunk. + $this->taskCopyDir( [ $this->target_dir . '/' . $this->final_name => $tag_dir ] )->run(); + } } diff --git a/composer.json b/composer.json index b41e725..3742519 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,9 @@ "package": [ "robo package" ], + "deploy": [ + "robo deploy:all" + ], "test-all": [ "@test", "@test-cs"