-
Notifications
You must be signed in to change notification settings - Fork 5
/
slic.php
185 lines (162 loc) · 8.73 KB
/
slic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?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';
require_once __DIR__ . '/src/scaffold.php';
require_once __DIR__ . '/src/slic.php';
require_once __DIR__ . '/src/docker.php';
require_once __DIR__ . '/src/notify.php';
require_once __DIR__ . '/src/plugins.php';
require_once __DIR__ . '/src/themes.php';
require_once __DIR__ . '/src/scripts.php';
require_once __DIR__ . '/src/shell.php';
require_once __DIR__ . '/src/wordpress.php';
require_once __DIR__ . '/src/services.php';
require_once __DIR__ . '/src/database.php';
require_once __DIR__ . '/src/project.php';
require_once __DIR__ . '/src/env.php';
require_once __DIR__ . '/src/commands.php';
use StellarWP\Slic\Cache;
use function StellarWP\Slic\args;
use function StellarWP\Slic\cli_header;
use function StellarWP\Slic\colorize;
use function StellarWP\Slic\maybe_prompt_for_repo_update;
use function StellarWP\Slic\maybe_prompt_for_stack_update;
use function StellarWP\Slic\root;
use function StellarWP\Slic\setup_slic_env;
// Set up the argument parsing function.
$args = args( [
'subcommand',
'...',
] );
$cli_name = 'slic';
const CLI_VERSION = '1.8.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 ) ) ) {
// Remove the `-q` flag from the global array of arguments to leave the rest of the commands unchanged.
unset( $argv[ array_search( '-q', $argv ) ] );
$argv = array_values( $argv );
$argc = count( $argv );
// Define a const commands will be able to check for quietness.
define( 'SLIC_QUIET', true );
} else {
echo cli_header( $cli_name, $argc < 2 || $argv[1] === 'help', $cli_header_extra ?? null );
}
define( 'SLIC_ROOT_DIR', __DIR__ );
setup_slic_env( SLIC_ROOT_DIR );
// Start the cache.
global $slic_cache;
$slic_cache = new Cache();
$help_message_template = <<< HELP
» Learn how to use <light_cyan>slic</light_cyan> at <yellow>https://github.com/stellarwp/slic</yellow>
Available commands:
-------------------
<yellow>Popular:</yellow>
<light_cyan>composer</light_cyan> Runs a Composer command in the stack.
<light_cyan>help</light_cyan> Displays this help message.
<light_cyan>here</light_cyan> Sets the current plugins directory to be the one used by slic.
<light_cyan>info</light_cyan> Displays information about the slic tool.
<light_cyan>logs</light_cyan> Displays the current stack logs.
<light_cyan>npm</light_cyan> Runs an npm command in the stack using the version of node specified by .nvmrc.
<light_cyan>phpcbf</light_cyan> Runs PHP Code Beautifier and Fixer within the current <light_cyan>use</light_cyan> target.
<light_cyan>phpcs</light_cyan> Runs PHP_CodeSniffer within the current <light_cyan>use</light_cyan> target.
<light_cyan>restart</light_cyan> Restarts containers in the stack.
<light_cyan>run</light_cyan> Runs a Codeception test in the stack, the equivalent to <light_cyan>'codecept run ...'</light_cyan>.
<light_cyan>shell</light_cyan> Opens a shell in the `slic` container.
<light_cyan>start</light_cyan> Starts containers in the stack.
<light_cyan>stop</light_cyan> Stops containers in the stack.
<light_cyan>use</light_cyan> Sets the plugin to use in the tests.
<light_cyan>using</light_cyan> Returns the current <light_cyan>use</light_cyan> target.
<light_cyan>wp</light_cyan> Runs a wp-cli command or opens a `wp-cli shell` in the stack.
<light_cyan>xdebug</light_cyan> Activates and deactivates XDebug in the stack, returns the current XDebug status or sets its values.
<light_cyan>playwright</light_cyan> Runs Playwright commands in the stack.
Type <light_cyan>{$cli_name} <command> help</light_cyan> for info about each command.
HELP;
$help_advanced_message_template = <<< HELP
<yellow>Advanced:</yellow>
<light_cyan>airplane-mode</light_cyan> Activates or deactivates the airplane-mode plugin.
<light_cyan>build-prompt</light_cyan> Activates or deactivates whether or not composer/npm build prompts should be provided.
<light_cyan>build-stack</light_cyan> Builds the stack containers that require it, or builds a specific service image.
<light_cyan>build-subdir</light_cyan> Activates or deactivates whether or not composer/npm build should apply to sub-directories.
<light_cyan>cache</light_cyan> Activates and deactivates object cache support, returns the current object cache status.
<light_cyan>cc</light_cyan> Runs a Codeception command in the stack, the equivalent of <light_cyan>'codecept ...'</light_cyan>.
<light_cyan>cli</light_cyan> Runs a wp-cli command or opens a `wp-cli shell` in the stack; alias of `wp`.
<light_cyan>composer-cache</light_cyan> Sets or shows the composer cache directory.
<light_cyan>config</light_cyan> Prints the stack configuration as interpolated from the environment.
<light_cyan>debug</light_cyan> Activates or deactivates {$cli_name} debug output or returns the current debug status.
<light_cyan>down</light_cyan> Tears down the stack; alias of `stop`.
<light_cyan>dc</light_cyan> Runs a docker compose command in the stack.
<light_cyan>exec</light_cyan> Runs a bash command in the stack.
<light_cyan>group</light_cyan> Create or remove group of targets for the current plugins directory.
<light_cyan>host-ip</light_cyan> Returns the IP Address of the host machine from the container perspective.
<light_cyan>init</light_cyan> Initializes a plugin for use in slic.
<light_cyan>interactive</light_cyan> Activates or deactivates interactivity of {$cli_name} commands.
<light_cyan>mysql</light_cyan> Opens a mysql shell in the database service.
<light_cyan>ps</light_cyan> Lists the containers part of {$cli_name} stack.
<light_cyan>php-version</light_cyan> Sets or shows the PHP version of the stack.
<light_cyan>reset</light_cyan> Resets {$cli_name} to the initial state as configured by the env files.
<light_cyan>site-cli</light_cyan> Waits for WordPress to be correctly set up to run a wp-cli command in the stack.
<light_cyan>ssh</light_cyan> Opens a shell in the `slic` container; alias of `shell`.
<light_cyan>target</light_cyan> Runs a set of commands on a set of targets.
<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 );
$help_advanced_message = colorize( $help_advanced_message_template );
$is_help = args( [ 'help' ], $args( '...' ), 0 )( 'help', false ) === 'help';
$original_subcommand = $args( 'subcommand' );
$subcommand = $args( 'subcommand', 'help' );
// Both these variables will be used by commands.
$run_settings_file = root( '/.env.slic.run' );
$cli_name = basename( $argv[0] );
if ( 'help' !== $subcommand ) {
maybe_prompt_for_repo_update();
} else {
$help_subcommand = $args( '...' );
if ( $help_subcommand ) {
$subcommand = $help_subcommand[0];
$is_help = true;
}
}
if ( ! in_array( $subcommand, [ 'help', 'update' ] ) ) {
maybe_prompt_for_stack_update();
}
if ( empty( $subcommand ) || $subcommand === 'help' ) {
echo $help_message . PHP_EOL;
if ( $original_subcommand ) {
echo PHP_EOL . $help_advanced_message;
} else {
echo colorize( PHP_EOL . "There are a lot more commands. Use <light_cyan>slic help</light_cyan> to see them all!" . PHP_EOL );
}
maybe_prompt_for_repo_update();
maybe_prompt_for_stack_update();
echo PHP_EOL;
exit( 0 );
}
/*
* Resolve command aliases.
* A map from the user-facing alias to the command that will be actually called.
*/
$aliases = [
'wp' => 'cli',
];
if ( isset( $aliases[ $subcommand ] ) ) {
$subcommand = $aliases[ $subcommand ];
}
$subcommand_file = __DIR__ . '/src/commands/' . $subcommand . '.php';
if ( file_exists( $subcommand_file ) ) {
include_once $subcommand_file;
} else {
echo colorize( "<magenta>Unknown command: {$subcommand}</magenta>" . PHP_EOL . PHP_EOL );
echo $help_message . PHP_EOL;
maybe_prompt_for_repo_update();
maybe_prompt_for_stack_update();
echo PHP_EOL;
exit( 1 );
}
// Add a break line at the end of each command to avoid dirty terminal issues.
echo PHP_EOL;