Skip to content

Commit

Permalink
Merge pull request #40 from moderntribe/feat/multi-target
Browse files Browse the repository at this point in the history
Add multi-command support
  • Loading branch information
lucatume authored Aug 19, 2020
2 parents 0ab5586 + d3f43b3 commit 196d6ad
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
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.5.4] - 2020-08-18
### Changed

- Add support for multiple commands in the `target` command to allow running a set of commands on a set of targets.

## [0.5.3] - 2020-08-13
### Changed

Expand Down
37 changes: 23 additions & 14 deletions src/commands/target.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Tribe\Test;

if ( $is_help ) {
echo "Runs a command on a set of targets.\n";
echo "Runs a set of commands on a set of targets.\n";
echo PHP_EOL;
echo colorize( "usage: <light_cyan>tric target</light_cyan>\n" );

Expand All @@ -17,33 +17,35 @@

$targets = [];
do {
$last_target = ask( 'Target (return when done): ', null );
$last_target = ask( 'Target (return when done):', null );
if ( $last_target && ensure_valid_target( $last_target, false ) ) {
$targets[] = $last_target;
} else {
continue;
}
} while ( $last_target );
} while ( ! empty( $last_target ) );

$targets = array_unique( $targets );

$command_lines = [];

echo yellow( "\nTargets: " ) . implode( ', ', $targets ) . "\n\n";

// Allow users to enter a command prefixing it with `tric` or not.
do {
$command_line = trim(
preg_replace( '/^\\s*tric/', '', ask( 'Command: ' )
$last_command_line = trim(
preg_replace( '/^\\s*tric/', '', ask( 'Command (return when done):', null )
)
);
} while ( ! $command_line );
if ( ! empty( $last_command_line ) ) {
$command_lines[] = $last_command_line;
}
} while ( ! empty( $last_command_line ) );

echo "\n";
echo yellow( "\nTargets: " ) . implode( ', ', $command_lines ) . "\n\n";

if ( preg_match( '/^n/i', ask(
colorize(
sprintf(
"<bold>Are you sure you want to run</bold> <light_cyan>%s</light_cyan> <bold>on</bold> <light_cyan>%s</light_cyan>?",
$command_line,
"<bold>Are you sure you want to run these commands on</bold> <light_cyan>%s</light_cyan>?",
implode( ', ', $targets )
)
),
Expand All @@ -54,7 +56,14 @@
return;
}

$command = preg_split( '/\\s/', $command_line );
$base_command = array_shift( $command );
foreach ( $command_lines as $command_line ) {
$command = preg_split( '/\\s/', $command_line );
$base_command = array_shift( $command );
$status = execute_command_pool( build_targets_command_pool( $targets, $base_command, $command, [ 'common' ] ) );
if ( 0 !== (int) $status ) {
// If any previous command fails, then exit.
exit( $status );
}
}
exit( $status );

exit( execute_command_pool( build_targets_command_pool( $targets, $base_command, $command, [ 'common' ] ) ) );
4 changes: 2 additions & 2 deletions tric
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $args = args( [
] );

$cli_name = basename( $argv[0] );
const CLI_VERSION = '0.5.3';
const CLI_VERSION = '0.5.4';

$cli_header = implode( ' - ', [
light_cyan( $cli_name ) . ' version ' . light_cyan( CLI_VERSION ),
Expand Down Expand Up @@ -53,7 +53,7 @@ Available commands:
<light_cyan>init</light_cyan> Initializes a plugin for use in tric.
<light_cyan>composer</light_cyan> Runs a Composer command in the stack.
<light_cyan>npm</light_cyan> Runs an npm command in the stack.
<light_cyan>target</light_cyan> Runs a command on a set of targets.
<light_cyan>target</light_cyan> Runs a set of commands on a set of targets.
<light_cyan>xdebug</light_cyan> Activates and deactivates XDebug in the stack, returns the current XDebug status or sets its values.
<light_cyan>airplane-mode</light_cyan> Activates or deactivates the airplane-mode plugin.
<light_cyan>cache</light_cyan> Activates and deactivates object cache support, returns the current object cache status.
Expand Down

0 comments on commit 196d6ad

Please sign in to comment.