Skip to content

Commit

Permalink
Add a tric upgrade command that pulls the latest main branch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
borkweb committed Jun 25, 2020
1 parent 664cdbd commit 1702ac2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions src/commands/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tribe\Test;

if ( $is_help ) {
echo "Upgrades tric to the latest version.\n";
echo PHP_EOL;
echo colorize( "usage: <light_cyan>{$cli_name} upgrade</light_cyan>" );
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 );
41 changes: 40 additions & 1 deletion src/tric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );

This comment has been minimized.

Copy link
@lucatume

lucatume Jun 25, 2020

Contributor

This will not work on Windows. No cd there.

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( "<magenta>Version</magenta> <yellow>{$remote_version}</yellow> <magenta>of tric is available! You are currently</magenta>\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;

Expand Down
16 changes: 12 additions & 4 deletions tric
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ),
Expand Down Expand Up @@ -62,6 +63,7 @@ Available commands:
<light_cyan>cli</light_cyan> Runs a wp-cli command in the stack.
<light_cyan>reset</light_cyan> Resets {$cli_name} to the initial state as configured by the env files.
<light_cyan>update</light_cyan> Updates the tool and the images used in its services.
<light_cyan>upgrade</light_cyan> Upgrades the {$cli_name} repo.
<yellow>Info:</yellow>
<light_cyan>build-prompt</light_cyan> Activates or deactivates whether or not composer/npm build prompts should be provided.
Expand Down Expand Up @@ -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':
Expand All @@ -126,6 +133,7 @@ switch ( $subcommand ) {
case 'shell':
case 'up':
case 'update':
case 'upgrade':
case 'use':
case 'using':
case 'xdebug':
Expand Down

0 comments on commit 1702ac2

Please sign in to comment.