From 1702ac2056c8690d7dd332f10a9fe9ff9f8eaa9c Mon Sep 17 00:00:00 2001 From: Matthew Batchelder Date: Thu, 25 Jun 2020 09:29:52 -0400 Subject: [PATCH] Add a tric upgrade command that pulls the latest main branch I've added a check that occurs once per day to look for a newer version of `tric`. If a new one exists, it'll prompt the user to run `tric upgrade`. :movie_camera: http://p.tri.be/HeQqIv --- .gitignore | 2 ++ changelog.md | 5 +++++ src/commands/upgrade.php | 22 +++++++++++++++++++++ src/tric.php | 41 +++++++++++++++++++++++++++++++++++++++- tric | 16 ++++++++++++---- 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/commands/upgrade.php diff --git a/.gitignore b/.gitignore index 0a2df5e..5649d20 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ _plugins # Any .build-version file created by the tric cli tool. .build-version +# Any .remote-version file created by the tric cli tool. +.remote-version diff --git a/changelog.md b/changelog.md index b86f109..6394c71 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.4.0] - 2020-06-25 +### Added + +- Added `tric upgrade` which allows you to upgrade `tric` to the latest tagged release. + ## [0.3.0] - 2020-06-25 ### Added diff --git a/src/commands/upgrade.php b/src/commands/upgrade.php new file mode 100644 index 0000000..a04434e --- /dev/null +++ b/src/commands/upgrade.php @@ -0,0 +1,22 @@ +{$cli_name} upgrade" ); + return; +} + +$today = date( 'Y-m-d' ); +chdir( TRIC_ROOT_DIR ); +$status = passthru( 'git checkout main && git pull' ); + +if ( ! $status ) { + unlink( TRIC_ROOT_DIR . '/.remote-version' ); + + $status = passthru( 'php tric update' ); +} + +exit( $status ); diff --git a/src/tric.php b/src/tric.php index f3342e5..8e344ec 100644 --- a/src/tric.php +++ b/src/tric.php @@ -948,10 +948,49 @@ function switch_plugin_branch( $branch, $plugin = null ) { } } +/** + * If tric itself is out of date, prompt to update repo. + */ +function maybe_prompt_for_repo_update() { + $remote_version = null; + $check_date = null; + $cli_version = CLI_VERSION; + $today = date( 'Y-m-d' ); + + if ( file_exists( TRIC_ROOT_DIR . '/.remote-version' ) ) { + list( $check_date, $remote_version ) = explode( ':', file_get_contents( TRIC_ROOT_DIR . '/.remote-version' ) ); + } + + if ( empty( $remote_version ) || empty( $check_date ) || $today > $check_date ) { + $tags = explode( "\n", shell_exec( 'cd ' . TRIC_ROOT_DIR . ' && git ls-remote --tags origin' ) ); + foreach ( $tags as &$tag ) { + $tag_parts = explode( '/', $tag ); + $tag = array_pop( $tag_parts ); + } + + natsort( $tags ); + + $remote_version = array_pop( $tags ); + + file_put_contents( TRIC_ROOT_DIR . '/.remote-version', "{$today}:{$remote_version}" ); + } + + // If the version of the CLI is the same as the most recently built version, bail. + if ( version_compare( $remote_version, $cli_version, '<=' ) ) { + return; + } + + echo magenta( "\n****************************************************************\n\n" ); + echo colorize( "Version {$remote_version} of tric is available! You are currently\n" ); + echo magenta( "running version {$cli_version}. To update, execute the following:\n\n" ); + echo yellow( " tric upgrade\n\n" ); + echo magenta( "****************************************************************\n" ); +} + /** * If tric stack is out of date, prompt for an execution of tric update. */ -function maybe_prompt_for_update() { +function maybe_prompt_for_stack_update() { $build_version = '0.0.1'; $cli_version = CLI_VERSION; diff --git a/tric b/tric index e294854..5992b0e 100755 --- a/tric +++ b/tric @@ -14,7 +14,8 @@ use function Tribe\Test\args; use function Tribe\Test\colorize; use function Tribe\Test\root; use function Tribe\Test\light_cyan; -use function Tribe\Test\maybe_prompt_for_update; +use function Tribe\Test\maybe_prompt_for_repo_update; +use function Tribe\Test\maybe_prompt_for_stack_update; use function Tribe\Test\setup_tric_env; // Set up the argument parsing function. @@ -24,7 +25,7 @@ $args = args( [ ] ); $cli_name = basename( $argv[0] ); -const CLI_VERSION = '0.3.0'; +const CLI_VERSION = '0.4.0'; $cli_header = implode( ' - ', [ light_cyan( $cli_name ) . ' version ' . light_cyan( CLI_VERSION ), @@ -62,6 +63,7 @@ Available commands: cli Runs a wp-cli command in the stack. reset Resets {$cli_name} to the initial state as configured by the env files. update Updates the tool and the images used in its services. +upgrade Upgrades the {$cli_name} repo. Info: build-prompt Activates or deactivates whether or not composer/npm build prompts should be provided. @@ -91,15 +93,20 @@ $subcommand = $args( 'subcommand', 'help' ); $cli_name = basename( $argv[0] ); +if ( 'help' !== $subcommand ) { + maybe_prompt_for_repo_update(); +} + if ( ! in_array( $subcommand, [ 'help', 'update'] ) ) { - maybe_prompt_for_update(); + maybe_prompt_for_stack_update(); } switch ( $subcommand ) { default: case 'help': echo $help_message; - maybe_prompt_for_update(); + maybe_prompt_for_repo_update(); + maybe_prompt_for_stack_update(); break; case 'airplane-mode': case 'build-prompt': @@ -126,6 +133,7 @@ switch ( $subcommand ) { case 'shell': case 'up': case 'update': + case 'upgrade': case 'use': case 'using': case 'xdebug':