Skip to content

Commit

Permalink
Merge pull request #19 from moderntribe/release/0.3.0
Browse files Browse the repository at this point in the history
Release/0.3.0
  • Loading branch information
borkweb authored Jun 25, 2020
2 parents 754a741 + e3c6139 commit e8db832
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .env.tric
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ CLI_VERBOSITY=0
# TRIC_CURRENT_PROJECT=the-events-calendar
# When you `here` at the site level, all selected targets via `use` will have a relative path set.
# TRIC_CURRENT_PROJECT_RELATIVE_PATH=
# When you `use` on a supported subdirectory of a plugin, this stores the subdirectory name.
#TRIC_CURRENT_PROJECT_SUBDIR=
# The GitHub handle of the company to clone plugins from.
TRIC_GITHUB_COMPANY_HANDLE=moderntribe

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ _plugins
# Any .env.tric.* file created to override the tric cli tool configuration or to configure the runs.
.env.tric.local
.env.tric.run

# Any .build-version file created by the tric cli tool.
.build-version
37 changes: 37 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Changelog
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.3.0] - TBD
### Added

- Prompt to `tric update` when container build version are out of sync from the tric version.
- Output npm error log when one is generated.

### Changed

- Adjust pathing of subdirectories within the tric stack so that npm can find a `.git` directory when performing `npm install`.
- Suppress the `fixuid` command output in the npm `docker-entrypoint.sh`.
- Separated out poolable (passive) command functions from realtime command functions to prevent issues with interactivity.

## [0.2.0] - 2020-06-24
### Added

- Added phpcs and phpcbf commands.
- Added parallel processing of commands.

### Changed

- Changed `tric build` to `tric build-stack`.

## [0.1.1] - 2020-05-26
### Changed

- Ensure `.htaccess` file is present in `_wordpress`.

## [0.1.0] - 2020-05-25
### Added

- Initial version
17 changes: 15 additions & 2 deletions containers/npm/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
# This file is just a proxy to call the `npm` binary that will, but, take care of fixing file mode issues before.

# If the `FIXUID` env var is set to `1`, default value, then fix UIDs.
test "${FIXUID:-1}" != "0" && eval "$( fixuid )"
test "${FIXUID:-1}" != "0" && eval "$( fixuid > /dev/null 2>&1 )"

npm --prefix /project "$@"
cd /project/${TRIC_CURRENT_PROJECT_SUBDIR}

npm "$@"

# Output error logs if present.
if compgen -G "/home/node/.npm/_logs/*.log" > /dev/null; then
echo "---------------------------------------"
echo "Error log found. Here are the contents (excluding the overly verbose saveTree lines):"
echo "---------------------------------------"
cat /home/node/.npm/_logs/*.log | grep -v "saveTree"
echo "---------------------------------------"
echo "End of error log"
echo "---------------------------------------"
fi
31 changes: 28 additions & 3 deletions src/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ function tric_stack_array() {
* Executes a docker-compose command in real time, printing the output as produced by the command.
*
* @param array<string> $options A list of options to initialize the wrapper.
* @param bool $is_realtime Whether the command should be run in real time (true) or passively (false).
*
* @return \Closure A closure that will run the process in real time and return the process exit status.
*/
function docker_compose_realtime( array $options = [] ) {
function docker_compose_process( array $options = [], $is_realtime = true ) {
setup_id();

$is_ci = is_ci();
Expand All @@ -182,7 +183,7 @@ function docker_compose_realtime( array $options = [] ) {
$host_ip = host_ip( 'Linux' );
}

return static function ( array $command = [], $prefix = null ) use ( $options, $host_ip, $is_ci ) {
return static function ( array $command = [], $prefix = null ) use ( $options, $host_ip, $is_ci, $is_realtime ) {
$command = 'docker-compose ' . implode( ' ', $options ) . ' ' . implode( ' ', $command );

if ( ! empty( $host_ip ) ) {
Expand All @@ -195,6 +196,30 @@ function docker_compose_realtime( array $options = [] ) {
$command = 'XDE=0 ' . $command;
}

return process_realtime( $command, $prefix );
return $is_realtime ? process_realtime( $command ) : process_passive( $command, $prefix );
};
}

/**
* Executes a docker-compose command in passive mode, printing the output as produced by the command.
*
* This approach is used for commands that can be run in a parallel or forked process without interactivity.
*
* @param array<string> $options A list of options to initialize the wrapper.
*
* @return \Closure A closure that will run the process in real time and return the process exit status.
*/
function docker_compose_passive( array $options = [] ) {
return docker_compose_process( $options, false );
}

/**
* Executes a docker-compose command in real time, printing the output as produced by the command.
*
* @param array<string> $options A list of options to initialize the wrapper.
*
* @return \Closure A closure that will run the process in real time and return the process exit status.
*/
function docker_compose_realtime( array $options = [] ) {
return docker_compose_process( $options, true );
}
28 changes: 27 additions & 1 deletion src/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,38 @@ function process( $command ) {
/**
* Runs a process in realtime, displaying its output.
*
* Realtime processes are done without forking, have no need of prefixes, and support interactivity.
*
* @param string $command The command to run.
* @param string|null $prefix The prefix to place before all output.
*
* @return int The process exit status, `0` means ok.
*/
function process_realtime( $command ) {
debug( "Executing command: {$command}" );

echo PHP_EOL;

setup_terminal();

$clean_command = escapeshellcmd( $command );

passthru( $clean_command, $status );

return (int) $status;
}

/**
* Runs a process passively, displaying its output.
*
* Passive processes are ones that only need to dump their output.
*
* @param string $command The command to run.
* @param string|null $prefix The prefix to place before all output.
*
* @return int The process exit status, `0` means ok.
*/
function process_realtime( $command, $prefix = null ) {
function process_passive( $command, $prefix = null ) {
debug( "Executing command: {$command}" );

echo PHP_EOL;
Expand Down
75 changes: 69 additions & 6 deletions src/tric.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,20 @@ function setup_tric_env( $root_dir ) {
* return value will always be a non empty string.
*/
function tric_target( $require = true ) {
$using = getenv( 'TRIC_CURRENT_PROJECT' );
$using = getenv( 'TRIC_CURRENT_PROJECT' );
$using_subdir = getenv( 'TRIC_CURRENT_PROJECT_SUBDIR' );
$using_full = $using . ( $using_subdir ? '/' . $using_subdir : '' );

if ( $require ) {
return $using;
return $using_full;
}
if ( empty( $using ) ) {

if ( empty( $using_full ) ) {
echo magenta( "Use target not set; use the 'use' sub-command to set it.\n" );
exit( 1 );
}

return trim( $using );
return trim( $using_full );
}

/**
Expand All @@ -199,14 +203,20 @@ function tric_switch_target( $target ) {
$root = root();
$run_settings_file = "{$root}/.env.tric.run";
$target_relative_path = '';
$subdir = '';

if ( tric_here_is_site() ) {
$target_relative_path = get_target_relative_path( $target );
}

if ( false !== strpos( $target, '/' ) ) {
list( $target, $subdir ) = explode( '/', $target );
}

$env_values = [
'TRIC_CURRENT_PROJECT' => $target,
'TRIC_CURRENT_PROJECT_RELATIVE_PATH' => $target_relative_path,
'TRIC_CURRENT_PROJECT_SUBDIR' => $subdir,
];

write_env_file( $run_settings_file, $env_values, true );
Expand Down Expand Up @@ -416,6 +426,17 @@ function github_company_handle() {
return ! empty( $handle ) ? trim( $handle ) : 'moderntribe';
}

/**
* Runs a process in passive mode in tric stack and returns the exit status.
*
* This approach is used when running commands that can be done in parallel or forked processes.
*
* @return \Closure The process closure to start a real-time process using tric stack.
*/
function tric_passive() {
return docker_compose_passive( tric_stack_array() );
}

/**
* Runs a process in tric stack and returns the exit status.
*
Expand Down Expand Up @@ -446,10 +467,18 @@ function teardown_stack() {
*/
function rebuild_stack() {
echo "Building the stack images...\n\n";
tric_realtime()( [ 'build-stack' ] );
tric_realtime()( [ 'build' ] );
write_build_version();
echo light_cyan( "\nStack images built.\n\n" );
}

/**
* Write the current CLI_VERSION to the build-version file
*/
function write_build_version() {
file_put_contents( TRIC_ROOT_DIR . '/.build-version', CLI_VERSION );
}

/**
* Prints information about tric tool.
*/
Expand Down Expand Up @@ -765,7 +794,7 @@ function build_command_pool( string $base_command, array $command, array $sub_di
$prefix = "{$base_command}:" . yellow( $target );
}

$status = tric_realtime()( array_merge( [ 'run', '--rm', $base_command ], $command ), $prefix );
$status = tric_passive()( array_merge( [ 'run', '--rm', $base_command ], $command ), $prefix );

if ( 'target' !== $target ) {
tric_switch_target( $using );
Expand Down Expand Up @@ -918,3 +947,37 @@ function switch_plugin_branch( $branch, $plugin = null ) {
exit( 1 );
}
}

/**
* If tric stack is out of date, prompt for an execution of tric update.
*/
function maybe_prompt_for_update() {
$build_version = '0.0.1';
$cli_version = CLI_VERSION;

if ( file_exists( TRIC_ROOT_DIR . '/.build-version' ) ) {
$build_version = file_get_contents( TRIC_ROOT_DIR . '/.build-version' );
}

// If there isn't a .env.tric.run, this is likely a fresh install. Bail.
if ( ! file_exists( TRIC_ROOT_DIR . '/.env.tric.run' ) ) {
return;
}

// If the version of the CLI is the same as the most recently built version, bail.
if ( version_compare( $build_version, $cli_version, '=' ) ) {
return;
}

echo magenta( "\n****************************************************************\n\n" );
echo yellow( " ____________ ____ __\n" );
echo yellow( " | ____\ \ / / | |\n" );
echo yellow( " | |__ \ \/ / | |\n" );
echo yellow( " | __| \_ _/ | |\n" );
echo yellow( " | | | | | |\n" );
echo yellow( " |__| |__| |__|\n\n" );
echo magenta( "Your tric containers are not up to date with the latest version.\n" );
echo magenta( " To update, please run:\n\n" );
echo yellow( " tric update\n\n" );
echo magenta( "****************************************************************\n" );
}
5 changes: 5 additions & 0 deletions src/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ function write_env_file( $file, array $lines = [], $update = false ) {
return "{$key}={$value}";
}, array_keys( $new_lines ), $new_lines ) );

// If this is the first time creating the .env.tric.run file, assume this is the first run and place the CLI version in `.build-version`.
if ( false !== strpos( $file, '.env.tric.run' ) && ! file_exists( $file ) ) {
write_build_version();
}

$put = file_put_contents( $file, $data );

if ( false === $put ) {
Expand Down
11 changes: 10 additions & 1 deletion src/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static function ( SplFileInfo $file ) {
}
);

$allowed_subdirs = [ 'common' ];
$allowed_subdirs = get_allowed_use_subdirectories();
foreach ( iterator_to_array( $dir ) as $key => $value ) {
$basename = basename( $key );
$dirs[ $basename ] = $value;
Expand All @@ -95,3 +95,12 @@ static function ( SplFileInfo $file ) {

return $dirs;
}

/**
* Returns the list of allowed subdirectories for tric use.
*
* @return array<string> Allowed subdirectories for use.
*/
function get_allowed_use_subdirectories() {
return [ 'common' ];
}
8 changes: 7 additions & 1 deletion tric
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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\setup_tric_env;

// Set up the argument parsing function.
Expand All @@ -23,7 +24,7 @@ $args = args( [
] );

$cli_name = basename( $argv[0] );
const CLI_VERSION = '0.2.0';
const CLI_VERSION = '0.3.0';

$cli_header = implode( ' - ', [
light_cyan( $cli_name ) . ' version ' . light_cyan( CLI_VERSION ),
Expand Down Expand Up @@ -90,10 +91,15 @@ $subcommand = $args( 'subcommand', 'help' );

$cli_name = basename( $argv[0] );

if ( ! in_array( $subcommand, [ 'help', 'update'] ) ) {
maybe_prompt_for_update();
}

switch ( $subcommand ) {
default:
case 'help':
echo $help_message;
maybe_prompt_for_update();
break;
case 'airplane-mode':
case 'build-prompt':
Expand Down
Loading

0 comments on commit e8db832

Please sign in to comment.