Skip to content

Commit

Permalink
Merge pull request #72 from the-events-calendar/feat/reset-hard
Browse files Browse the repository at this point in the history
Support wp argument in reste command to rm WP default dir
  • Loading branch information
borkweb authored Jan 20, 2021
2 parents 3b52cc1 + 270e4c0 commit c153b63
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 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.23] - 2021-01-20
### Changed
- Add an argument, to the `reset` command, to remove the default WordPress (`/_wordpress`) installation directory
using `tric reset wp`.

## [0.5.22] - 2021-01-20
### Added
- Added the `composer-cache` command so the host machine's composer cache can be leveraged within tric containers.
Expand Down
8 changes: 8 additions & 0 deletions src/commands/composer-cache.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Handles the `composer-cache` command.
*
* @var bool $is_help Whether we're handling an `help` request on this command or not.
* @var string $cli_name The current name of tric CLI binary.
* @var \Closure $args The argument map closure, as produced by the `args` function.
*/

namespace Tribe\Test;

Expand All @@ -9,6 +16,7 @@
echo colorize( "example: <light_cyan>{$cli_name} composer-cache</light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} composer-cache unset</light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} composer-cache set /home/person/.cache/composer</light_cyan>\n" );

return;
}

Expand Down
41 changes: 38 additions & 3 deletions src/commands/reset.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
<?php
/**
* Handles the `reset` command to reset tric to its initial state.
*
* @var bool $is_help Whether we're handling an `help` request on this command or not.
* @var string $cli_name The current name of tric CLI binary.
* @var \Closure $args The argument map closure, as produced by the `args` function.
*/

namespace Tribe\Test;

if ( $is_help ) {
echo "Resets the tool to its initial state configured by the env files.\n";
echo "Resets the tool to its initial state configured by the env files.\n" .
"Additionally remove the default WP directory.\n";
echo PHP_EOL;
echo colorize( "usage: <light_cyan>{$cli_name} reset</light_cyan>" );
echo colorize( "usage: <light_cyan>{$cli_name} reset [wp]</light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} reset </light_cyan>\n" );
echo colorize( "example: <light_cyan>{$cli_name} reset wp</light_cyan>\n" );

return;
}

$targets = $args( '...' );

if ( in_array( 'wp', $targets, true ) && is_dir( root( '_wordpress' ) ) ) {
echo "Removing the _wordpress directory ...";

if ( false === rrmdir( root( '_wordpress' ) ) ) {
echo magenta( "\nCould not remove the _wordpress directory; remove it manually.\n" );
exit( 1 );
}

if ( ! mkdir( $wp_dir = root( '_wordpress' ) ) && ! is_dir( $wp_dir ) ) {
echo magenta( "\nCould not create the _wordpress directory; create it manually.\n" );
exit( 1 );
}

echo light_cyan( " done\n" );

return;
}

$run_settings_file = root( '/.env.tric.run' );
echo "Removing {$run_settings_file} ...";

if ( ! file_exists( $run_settings_file ) ) {
echo light_cyan( 'Done' );

return;
}

Expand All @@ -21,4 +56,4 @@
exit( 1 );
}

echo light_cyan( 'Done' );
echo light_cyan( ' done' );
7 changes: 7 additions & 0 deletions src/commands/xdebug.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* Handles the `xdebug` command.
*
* @var bool $is_help Whether we're handling an `help` request on this command or not.
* @var string $cli_name The current name of tric CLI binary.
* @var \Closure $args The argument map closure, as produced by the `args` function.
*/

namespace Tribe\Test;

Expand Down
41 changes: 41 additions & 0 deletions src/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,44 @@ function upper_snake_case( $string ) {
function snake_case( $string ) {
return preg_replace( '/[^\\w_]/', '_', $string ) ?: $string;
}

/**
* Removes a directory and all its contents recursively.
*
* @param string $dir The path to the directory to remove.
*
* @return bool Whether the removal was correctly completed or not.
*/
function rrmdir( $dir ) {
if ( empty( $dir ) || ! file_exists( $dir ) ) {
return true;
}

if ( is_file( $dir ) || is_link( $dir ) ) {
return unlink( $dir );
}

$files = new \RecursiveIteratorIterator (
new \RecursiveDirectoryIterator(
$dir,
\RecursiveDirectoryIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::CHILD_FIRST
);

/** @var \SplFileInfo $fileinfo
*/
foreach ( $files as $fileinfo ) {
if ( $fileinfo->isDir() ) {
if ( rrmdir( $fileinfo->getRealPath() ) === false ) {
return false;
}
} else {
if ( unlink( $fileinfo->getRealPath() ) === false ) {
return false;
}
}
}

return rmdir( $dir );
}
2 changes: 1 addition & 1 deletion tric
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $args = args( [
] );

$cli_name = basename( $argv[0] );
const CLI_VERSION = '0.5.22';
const CLI_VERSION = '0.5.23';

$cli_header = implode( ' - ', [
light_cyan( $cli_name ) . ' version ' . light_cyan( CLI_VERSION ),
Expand Down

0 comments on commit c153b63

Please sign in to comment.