Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the slic dump command to create dump.sql #183

Merged
merged 12 commits into from
Apr 10, 2024
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-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.

Expand Down
8 changes: 8 additions & 0 deletions includes/polyfills.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// PHP8 str_starts_with() polyfill.
if ( ! function_exists( 'str_starts_with' ) ) {
function str_starts_with( string $haystack, string $needle ): bool {
return 0 === strncmp( $haystack, $needle, strlen( $needle ) );
}
}
4 changes: 3 additions & 1 deletion slic.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// Requires the function files we might need.
require_once __DIR__ . '/includes/polyfills.php';
require_once __DIR__ . '/src/classes/Cache.php';
require_once __DIR__ . '/src/cache.php';
require_once __DIR__ . '/src/utils.php';
Expand Down Expand Up @@ -33,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 ) ) ) {
Expand Down Expand Up @@ -112,6 +113,7 @@
<light_cyan>up</light_cyan> Starts containers in the stack; alias of `start`.
<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.
<light_cyan>update-dump</light_cyan> Updates a SQL dump file. Optionally, installs a specific WordPress version..
HELP;

$help_message = colorize( $help_message_template );
Expand Down
8 changes: 4 additions & 4 deletions src/commands/logs.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace StellarWP\Slic;

/**
* Opens and follows the stack logs.
*
* @var bool $is_help Whether we're handling an `help` request on this command or not.
* @var string $cli_name The current name of the `slic` CLI application.
*/

namespace StellarWP\Slic;

if ( $is_help ) {
$help = <<< HELP
SUMMARY:
Expand All @@ -16,7 +16,7 @@

USAGE:

<yellow>{$cli_name} logs</yell>
<yellow>$cli_name logs</yellow>
HELP;

echo colorize( $help );
Expand Down
111 changes: 111 additions & 0 deletions src/commands/update-dump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace StellarWP\Slic;

/**
* 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.
* @var callable $args The argument map closure, as produced by the `args` function.
* @var string $cli_name The current name of the `slic` CLI application.
*/
if ( $is_help ) {
$help = <<< HELP
SUMMARY:

Updates a SQL dump file. Optionally, installs a specific WordPress version.

USAGE:

<yellow>$cli_name $subcommand <file> [<wp_version>]</yellow>

EXAMPLES:

<light_cyan>$cli_name $subcommand tests/_data/dump.sql</light_cyan>
Update the dump file using slic's currently installed version of WordPress.

<light_cyan>$cli_name $subcommand tests/_data/dump.sql latest</light_cyan>
Update the WordPress version to the latest and update the dump file.

<light_cyan>$cli_name $subcommand tests/_data/dump.sql 6.4.3</light_cyan>
Update the WordPress version to 6.4.3 and update the dump file.
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',
], true );
}

$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 );
35 changes: 35 additions & 0 deletions src/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,38 @@ function ensure_dir( $dir ) {

return realpath( $dir );
}

/**
* Removes trailing directory separators.
*
* @param string $path The directory path.
*
* @return string
*/
function untrailingslashit( $path ) {
return rtrim( $path, DIRECTORY_SEPARATOR );
}

/**
* Appends a directory separator.
*
* @param string $path The directory path.
*
* @return string
*/
function trailingslashit( $path ) {
return untrailingslashit( $path ) . DIRECTORY_SEPARATOR;
}

/**
* Remove any double directory separators.
*
* @example /my-project//tests > /my-project/tests
*
* @param string $path The directory path.
*
* @return string
*/
function remove_double_separators( $path ) {
return str_replace( sprintf( '%s%s', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR ), DIRECTORY_SEPARATOR, $path );
}