From c9004938c5553d722cf0bfd73b3863c3a799d42a Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 8 Apr 2024 14:47:21 -0600 Subject: [PATCH 01/12] Fix typo not closing color in logs.php --- src/commands/logs.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commands/logs.php b/src/commands/logs.php index 1f14b3f..a382fb1 100644 --- a/src/commands/logs.php +++ b/src/commands/logs.php @@ -1,13 +1,13 @@ {$cli_name} logs + $cli_name logs HELP; echo colorize( $help ); From d6dcb12b440a251c67a2785705b5d9157e81aefe Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 8 Apr 2024 14:47:39 -0600 Subject: [PATCH 02/12] Add str_starts_with polyfill --- includes/polyfills.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 includes/polyfills.php diff --git a/includes/polyfills.php b/includes/polyfills.php new file mode 100644 index 0000000..2215418 --- /dev/null +++ b/includes/polyfills.php @@ -0,0 +1,8 @@ + Date: Mon, 8 Apr 2024 14:49:16 -0600 Subject: [PATCH 03/12] Add `slic dump` command --- slic.php | 2 + src/commands/dump.php | 144 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/commands/dump.php diff --git a/slic.php b/slic.php index 622b0ba..1213fbd 100644 --- a/slic.php +++ b/slic.php @@ -1,5 +1,6 @@ up Starts containers in the stack; alias of `start`. update Updates the tool and the images used in its services. upgrade Upgrades the {$cli_name} repo. + dump Installs a specific version of WordPress, wipes the database and exports a tests dump.sql. HELP; $help_message = colorize( $help_message_template ); diff --git a/src/commands/dump.php b/src/commands/dump.php new file mode 100644 index 0000000..c44e438 --- /dev/null +++ b/src/commands/dump.php @@ -0,0 +1,144 @@ +$cli_name $subcommand [] [--yes skip confirmation] [--file /custom/path/to/dump.sql] + + EXAMPLES: + + $cli_name $subcommand latest + Export a dump.sql using the latest stable WordPress version. + + $cli_name $subcommand nightly + Export a dump.sql using the nightly WordPress version. + + $cli_name $subcommand 6.4.3 + Export a dump.sql using WordPress version 6.4.3. + + $cli_name $subcommand nightly --yes + Export a dump.sql using the nightly WordPress version and skip any confirmations. + + $cli_name $subcommand 6.4.3 --file /home/my-project/dump.sql + Export a dump.sql to a custom path, using WordPress version 6.4.3. + HELP; + + echo colorize( $help ); + + return; +} + +// Confirm a target has been set or show an error. +slic_target(false); + +// Extract the arguments. +$command = $args( '...' ); +$version = trim( $command[0] ); + +$path = get_project_local_path(); +$file = $path . DIRECTORY_SEPARATOR . 'tests/_data/dump.sql'; +$key = array_search( '--file', $command, true ); + +// Export sql file to a custom location. +if ( $key !== false ) { + $path_key = $key + 1; + + if ( empty( $command[ $path_key ] ) ) { + echo magenta( 'You must provide a path to a sql file after using the --file option, e.g. /home/plugins/my-project/tests/_data/dump-custom.sql' ); + exit ( 1 ); + } + + $file = trim( $command[ $path_key ] ); +} + +if ( ! str_starts_with( $file, $path ) ) { + echo magenta( sprintf( + 'Error: The file sql export path must be under the SLIC_CURRENT_PROJECT directory: %s', + $path ) ); + + echo yellow( sprintf( + '%sHint: use the `%s here` command to export for a different project.', + PHP_EOL, + $cli_name + ) ); + + exit( 1 ); +} + +if ( ! in_array( '--yes', $command, true ) ) { + $confirm = ask( sprintf( + 'Are you sure you want to install WordPress "%s", wipe the tests database and overwrite %s?', + $version, + $file, + ), 'yes' ); + + if ( ! $confirm ) { + echo yellow( 'Aborted.' ); + exit( 0 ); + } +} + +// Replace host path with container path. +$container_path = str_replace( get_project_local_path(), get_project_container_path(), $file ); + +$commands = [ + cli_command( [ + 'cli', + 'cache', + 'clear', + ] ), + cli_command( [ + 'core', + 'update', + '--force', + sprintf( '--version=%s', $version ), + ], true ), + cli_command( [ + 'db', + 'reset', + '--yes', + ] ), + cli_command( [ + 'core', + 'version', + '--extra', + ], true ), + cli_command( [ + 'db', + 'export', + '--add-drop-table', + $container_path, + ] ), +]; + +// Execute the command chain. +foreach ( $commands as $arguments ) { + $result = slic_passive()( $arguments ); + + // 0 is success on command line. + if ( $result === 0 ) { + continue; + } + + echo magenta( sprintf( 'Error: Command Failed: %s', implode( ' ', $arguments ) ) ); + exit ( 1 ); +} + +echo green( sprintf( "Success: Exported to host path '%s'.", $file ) ); + +exit( 0 ); From f40bedd35f8f95fb32b6dc081aee8cd431d9d456 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 8 Apr 2024 14:50:50 -0600 Subject: [PATCH 04/12] Update changelog.md --- changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.md b/changelog.md index 71debf8..be7eb6f 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,9 @@ 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). +# [1.6.0] - 2024-04-08 +* Added - The `slic dump` command to export a raw dump.sql for the current project, using a specific WordPress version, e.g. `slic dump 6.4.3`. + # [1.5.4] - 2024-04-08 * Change - Disable WordPress's automatic updating in slic containers via docker compose `WORDPRESS_CONFIG_EXTRA` defines. See comments in `.env.slic` to customize this behavior. From 73ca16549caa4853b1be418492057621ead8d94d Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 8 Apr 2024 14:58:04 -0600 Subject: [PATCH 05/12] Run ensure_wordpress_installed(); after completion. --- src/commands/dump.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/commands/dump.php b/src/commands/dump.php index c44e438..8a8b70a 100644 --- a/src/commands/dump.php +++ b/src/commands/dump.php @@ -139,6 +139,8 @@ exit ( 1 ); } +ensure_wordpress_installed(); + echo green( sprintf( "Success: Exported to host path '%s'.", $file ) ); exit( 0 ); From b0f3f1a7cb3ce974074754e5f990ca924a3cf1e6 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Mon, 8 Apr 2024 15:07:47 -0600 Subject: [PATCH 06/12] Make default confirm no --- src/commands/dump.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/dump.php b/src/commands/dump.php index 8a8b70a..a9879c0 100644 --- a/src/commands/dump.php +++ b/src/commands/dump.php @@ -85,7 +85,7 @@ 'Are you sure you want to install WordPress "%s", wipe the tests database and overwrite %s?', $version, $file, - ), 'yes' ); + ), 'no' ); if ( ! $confirm ) { echo yellow( 'Aborted.' ); From 9c67d4eb6ef74a20f7a0b8eee251e004b9ff5230 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Tue, 9 Apr 2024 10:27:09 -0600 Subject: [PATCH 07/12] Add some util functions --- src/utils.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/utils.php b/src/utils.php index ce7d53d..bb84259 100644 --- a/src/utils.php +++ b/src/utils.php @@ -645,3 +645,38 @@ function ensure_dir( $dir ) { return realpath( $dir ); } + +/** + * Removes trailing directory separators. + * + * @param string $value + * + * @return string + */ +function untrailingslashit( $value ) { + return rtrim( $value, DIRECTORY_SEPARATOR ); +} + +/** + * Appends a directory separator. + * + * @param string $value + * + * @return string + */ +function trailingslashit( $value ) { + return untrailingslashit( $value ) . DIRECTORY_SEPARATOR; +} + +/** + * Remove any double directory separators. + * + * @example /my-project//tests > /my-project/tests + * + * @param string $value + * + * @return string + */ +function remove_double_separators( $value ) { + return str_replace( sprintf( '%s%s', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR ), DIRECTORY_SEPARATOR, $value ); +} From d1e78f02f997204cd803d3ebda378c070f36ba95 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Tue, 9 Apr 2024 10:35:06 -0600 Subject: [PATCH 08/12] Rename variables --- src/utils.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/utils.php b/src/utils.php index bb84259..56919d6 100644 --- a/src/utils.php +++ b/src/utils.php @@ -649,23 +649,23 @@ function ensure_dir( $dir ) { /** * Removes trailing directory separators. * - * @param string $value + * @param string $path The directory path. * * @return string */ -function untrailingslashit( $value ) { - return rtrim( $value, DIRECTORY_SEPARATOR ); +function untrailingslashit( $path ) { + return rtrim( $path, DIRECTORY_SEPARATOR ); } /** * Appends a directory separator. * - * @param string $value + * @param string $path The directory path. * * @return string */ -function trailingslashit( $value ) { - return untrailingslashit( $value ) . DIRECTORY_SEPARATOR; +function trailingslashit( $path ) { + return untrailingslashit( $path ) . DIRECTORY_SEPARATOR; } /** @@ -673,10 +673,10 @@ function trailingslashit( $value ) { * * @example /my-project//tests > /my-project/tests * - * @param string $value + * @param string $path The directory path. * * @return string */ -function remove_double_separators( $value ) { - return str_replace( sprintf( '%s%s', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR ), DIRECTORY_SEPARATOR, $value ); +function remove_double_separators( $path ) { + return str_replace( sprintf( '%s%s', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR ), DIRECTORY_SEPARATOR, $path ); } From 1b3fd8e1819a63b051100df8db09ce644dea3540 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Tue, 9 Apr 2024 10:35:47 -0600 Subject: [PATCH 09/12] Rename command and update command interface --- slic.php | 2 +- src/commands/dump.php | 146 ----------------------------------- src/commands/update-dump.php | 112 +++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 147 deletions(-) delete mode 100644 src/commands/dump.php create mode 100644 src/commands/update-dump.php diff --git a/slic.php b/slic.php index 1213fbd..21cbce9 100644 --- a/slic.php +++ b/slic.php @@ -113,7 +113,7 @@ up Starts containers in the stack; alias of `start`. update Updates the tool and the images used in its services. upgrade Upgrades the {$cli_name} repo. - dump Installs a specific version of WordPress, wipes the database and exports a tests dump.sql. + update-dump Clears the database and exports a new dump.sql for use with acceptance tests. Optionally, installs a specific WordPress version. HELP; $help_message = colorize( $help_message_template ); diff --git a/src/commands/dump.php b/src/commands/dump.php deleted file mode 100644 index a9879c0..0000000 --- a/src/commands/dump.php +++ /dev/null @@ -1,146 +0,0 @@ -$cli_name $subcommand [] [--yes skip confirmation] [--file /custom/path/to/dump.sql] - - EXAMPLES: - - $cli_name $subcommand latest - Export a dump.sql using the latest stable WordPress version. - - $cli_name $subcommand nightly - Export a dump.sql using the nightly WordPress version. - - $cli_name $subcommand 6.4.3 - Export a dump.sql using WordPress version 6.4.3. - - $cli_name $subcommand nightly --yes - Export a dump.sql using the nightly WordPress version and skip any confirmations. - - $cli_name $subcommand 6.4.3 --file /home/my-project/dump.sql - Export a dump.sql to a custom path, using WordPress version 6.4.3. - HELP; - - echo colorize( $help ); - - return; -} - -// Confirm a target has been set or show an error. -slic_target(false); - -// Extract the arguments. -$command = $args( '...' ); -$version = trim( $command[0] ); - -$path = get_project_local_path(); -$file = $path . DIRECTORY_SEPARATOR . 'tests/_data/dump.sql'; -$key = array_search( '--file', $command, true ); - -// Export sql file to a custom location. -if ( $key !== false ) { - $path_key = $key + 1; - - if ( empty( $command[ $path_key ] ) ) { - echo magenta( 'You must provide a path to a sql file after using the --file option, e.g. /home/plugins/my-project/tests/_data/dump-custom.sql' ); - exit ( 1 ); - } - - $file = trim( $command[ $path_key ] ); -} - -if ( ! str_starts_with( $file, $path ) ) { - echo magenta( sprintf( - 'Error: The file sql export path must be under the SLIC_CURRENT_PROJECT directory: %s', - $path ) ); - - echo yellow( sprintf( - '%sHint: use the `%s here` command to export for a different project.', - PHP_EOL, - $cli_name - ) ); - - exit( 1 ); -} - -if ( ! in_array( '--yes', $command, true ) ) { - $confirm = ask( sprintf( - 'Are you sure you want to install WordPress "%s", wipe the tests database and overwrite %s?', - $version, - $file, - ), 'no' ); - - if ( ! $confirm ) { - echo yellow( 'Aborted.' ); - exit( 0 ); - } -} - -// Replace host path with container path. -$container_path = str_replace( get_project_local_path(), get_project_container_path(), $file ); - -$commands = [ - cli_command( [ - 'cli', - 'cache', - 'clear', - ] ), - cli_command( [ - 'core', - 'update', - '--force', - sprintf( '--version=%s', $version ), - ], true ), - cli_command( [ - 'db', - 'reset', - '--yes', - ] ), - cli_command( [ - 'core', - 'version', - '--extra', - ], true ), - cli_command( [ - 'db', - 'export', - '--add-drop-table', - $container_path, - ] ), -]; - -// Execute the command chain. -foreach ( $commands as $arguments ) { - $result = slic_passive()( $arguments ); - - // 0 is success on command line. - if ( $result === 0 ) { - continue; - } - - echo magenta( sprintf( 'Error: Command Failed: %s', implode( ' ', $arguments ) ) ); - exit ( 1 ); -} - -ensure_wordpress_installed(); - -echo green( sprintf( "Success: Exported to host path '%s'.", $file ) ); - -exit( 0 ); diff --git a/src/commands/update-dump.php b/src/commands/update-dump.php new file mode 100644 index 0000000..337ab7d --- /dev/null +++ b/src/commands/update-dump.php @@ -0,0 +1,112 @@ +$cli_name $subcommand [] + + EXAMPLES: + + $cli_name $subcommand tests/_data/dump.sql + Export a dump.sql using slic's currently installed version of WordPress. + + $cli_name $subcommand tests/_data/dump.sql latest + Export a dump.sql using the latest WordPress version. + + $cli_name $subcommand tests/_data/dump.sql 6.4.3 + Export a dump.sql using WordPress version 6.4.3. + HELP; + + echo colorize( $help ); + + return; +} + +// Confirm a target has been set or show an error. +slic_target( false ); + +// Extract the arguments. +$command = $args( '...' ); +$file = trim( $command[0] ); +$version = trim( $command[1] ?? '' ); + +// Build the path inside the slic container. +$container_path = remove_double_separators( trailingslashit( get_project_container_path() ) . $file ); + +// Run core update if a version was provided, otherwise run core update-db. +if ( $version ) { + $update_command = cli_command( [ + 'core', + 'update', + '--force', + sprintf( '--version=%s', $version ), + ], true ); +} else { + $update_command = cli_command( [ + 'core', + 'update-db', + ] ); +} + +$commands = [ + cli_command( [ + 'cli', + 'cache', + 'clear', + ] ), + $update_command, + cli_command( [ + 'db', + 'reset', + '--yes', + ] ), + cli_command( [ + 'core', + 'version', + '--extra', + ], true ), + cli_command( [ + 'db', + 'export', + '--add-drop-table', + $container_path, + ] ), +]; + +// Execute the command chain. +foreach ( $commands as $arguments ) { + $result = slic_passive()( $arguments ); + + // 0 is success on command line. + if ( $result === 0 ) { + continue; + } + + echo magenta( sprintf( 'Error: Command Failed: %s', implode( ' ', $arguments ) ) ); + exit ( 1 ); +} + +ensure_wordpress_installed(); + +echo green( sprintf( + "Success: Exported to host path '%s'.", + remove_double_separators( trailingslashit( get_project_local_path() ) . $file ) +) ); + +exit( 0 ); From efafb6f1b232cc609f4eded1d1056e04e7150f9f Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Tue, 9 Apr 2024 10:37:38 -0600 Subject: [PATCH 10/12] Update changelog.md --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index be7eb6f..51428c3 100644 --- a/changelog.md +++ b/changelog.md @@ -5,7 +5,7 @@ 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). # [1.6.0] - 2024-04-08 -* Added - The `slic dump` command to export a raw dump.sql for the current project, using a specific WordPress version, e.g. `slic dump 6.4.3`. +* Added - The `slic update-dump` command to export a raw dump.sql for the current project, with an optional WordPress version, e.g. `slic update-dump tests/_data/dump.sql latest`. # [1.5.4] - 2024-04-08 * Change - Disable WordPress's automatic updating in slic containers via docker compose `WORDPRESS_CONFIG_EXTRA` defines. See comments in `.env.slic` to customize this behavior. From 47137a97fcae275eee7a3bcdfd8cb2b89f247b2d Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Tue, 9 Apr 2024 10:41:38 -0600 Subject: [PATCH 11/12] Check wp is installed before running core update-db --- src/commands/update-dump.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/update-dump.php b/src/commands/update-dump.php index 337ab7d..5d12f9b 100644 --- a/src/commands/update-dump.php +++ b/src/commands/update-dump.php @@ -61,7 +61,7 @@ $update_command = cli_command( [ 'core', 'update-db', - ] ); + ], true ); } $commands = [ From 6d30997b8a86e8b6e278d7de73c1c64d8d8151cc Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Wed, 10 Apr 2024 10:36:36 +0200 Subject: [PATCH 12/12] doc(changelog,commands) small wording change, set slic version --- changelog.md | 4 ++-- slic.php | 4 ++-- src/commands/update-dump.php | 11 +++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/changelog.md b/changelog.md index 51428c3..2aa35bb 100644 --- a/changelog.md +++ b/changelog.md @@ -4,8 +4,8 @@ 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). -# [1.6.0] - 2024-04-08 -* Added - The `slic update-dump` command to export a raw dump.sql for the current project, with an optional WordPress version, e.g. `slic update-dump tests/_data/dump.sql latest`. +# [1.6.0] - 2024-04-10 +* Added - The `slic update-dump` command to update a dump file for the current project, with an optional WordPress version update, e.g. `slic update-dump tests/_data/dump.sql latest`. # [1.5.4] - 2024-04-08 * Change - Disable WordPress's automatic updating in slic containers via docker compose `WORDPRESS_CONFIG_EXTRA` defines. See comments in `.env.slic` to customize this behavior. diff --git a/slic.php b/slic.php index 21cbce9..fe057b4 100644 --- a/slic.php +++ b/slic.php @@ -34,7 +34,7 @@ ] ); $cli_name = 'slic'; -const CLI_VERSION = '1.5.4'; +const CLI_VERSION = '1.6.0'; // If the run-time option `-q`, for "quiet", is specified, then do not print the header. if ( in_array( '-q', $argv, true ) || ( in_array( 'exec', $argv, true ) && ! in_array( 'help', $argv, true ) ) ) { @@ -113,7 +113,7 @@ up Starts containers in the stack; alias of `start`. update Updates the tool and the images used in its services. upgrade Upgrades the {$cli_name} repo. - update-dump Clears the database and exports a new dump.sql for use with acceptance tests. Optionally, installs a specific WordPress version. + update-dump Updates a SQL dump file. Optionally, installs a specific WordPress version.. HELP; $help_message = colorize( $help_message_template ); diff --git a/src/commands/update-dump.php b/src/commands/update-dump.php index 5d12f9b..d494361 100644 --- a/src/commands/update-dump.php +++ b/src/commands/update-dump.php @@ -3,8 +3,7 @@ namespace StellarWP\Slic; /** - * Clears the database and exports a new dump.sql for use with acceptance tests. Optionally, installs a specific - * WordPress version. + * Updates a SQL dump file. Optionally, installs a specific WordPress version. * * @var bool $is_help Whether we're handling an `help` request on this command or not. * @var string $subcommand This command. @@ -15,7 +14,7 @@ $help = <<< HELP SUMMARY: - Clears the database and exports a new dump.sql for use with acceptance tests. Optionally, installs a specific WordPress version. + Updates a SQL dump file. Optionally, installs a specific WordPress version. USAGE: @@ -24,13 +23,13 @@ EXAMPLES: $cli_name $subcommand tests/_data/dump.sql - Export a dump.sql using slic's currently installed version of WordPress. + Update the dump file using slic's currently installed version of WordPress. $cli_name $subcommand tests/_data/dump.sql latest - Export a dump.sql using the latest WordPress version. + Update the WordPress version to the latest and update the dump file. $cli_name $subcommand tests/_data/dump.sql 6.4.3 - Export a dump.sql using WordPress version 6.4.3. + Update the WordPress version to 6.4.3 and update the dump file. HELP; echo colorize( $help );