-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInstallCommand.php
More file actions
443 lines (349 loc) Β· 16.2 KB
/
Copy pathInstallCommand.php
File metadata and controls
443 lines (349 loc) Β· 16.2 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
declare(strict_types=1);
namespace DrevOps\VortexInstaller\Command;
use DrevOps\VortexInstaller\Prompts\PromptManager;
use DrevOps\VortexInstaller\Utils\Config;
use DrevOps\VortexInstaller\Utils\Downloader;
use DrevOps\VortexInstaller\Utils\Env;
use DrevOps\VortexInstaller\Utils\File;
use DrevOps\VortexInstaller\Utils\Tui;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Run command.
*
* Install command.
*
* @package DrevOps\VortexInstaller\Command
*/
class InstallCommand extends Command {
const ARG_DESTINATION = 'destination';
const OPTION_ROOT = 'root';
const OPTION_NO_INTERACTION = 'no-interaction';
const OPTION_CONFIG = 'config';
const OPTION_QUIET = 'quiet';
const OPTION_URI = 'uri';
/**
* Defines default command name.
*
* @var string
*/
public static $defaultName = 'install';
/**
* Defines the configuration object.
*/
protected Config $config;
/**
* {@inheritdoc}
*/
protected function configure(): void {
$this->setName('Vortex CLI installer');
$this->setDescription('Install Vortex CLI from remote or local repository.');
$this->setHelp(<<<EOF
Interactively install Vortex from the latest stable release into the current directory:
php install destination
Non-interactively install Vortex from the latest stable release into the specified directory:
php install --no-interaction destination
Install Vortex from a stable release into the specified directory:
php install --uri=https://github.com/drevops/vortex.git@stable destination
Install Vortex from a specific release into the specified directory:
php install --uri=https://github.com/drevops/vortex.git@1.2.3 destination
Install Vortex from a specific commit into the specified directory:
php install --uri=https://github.com/drevops/vortex.git@abcd123 destination
EOF
);
$this->addArgument(static::ARG_DESTINATION, InputArgument::OPTIONAL, 'Destination directory. Optional. Defaults to the current directory.');
$this->addOption(static::OPTION_ROOT, NULL, InputOption::VALUE_REQUIRED, 'Path to the root for file path resolution. If not specified, current directory is used.');
$this->addOption(static::OPTION_NO_INTERACTION, 'n', InputOption::VALUE_NONE, 'Do not ask any interactive question.');
$this->addOption(static::OPTION_CONFIG, 'c', InputOption::VALUE_REQUIRED, 'A JSON string with options.');
$this->addOption(static::OPTION_URI, 'l', InputOption::VALUE_REQUIRED, 'Remote or local repository URI with an optional git ref set after @.');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
// @see https://github.com/drevops/vortex/issues/1502
if ($input->getOption('help') || $input->getArgument('destination') == 'help') {
$output->write($this->getHelp());
return Command::SUCCESS;
}
Tui::init($output);
try {
$this->checkRequirements();
$this->resolveOptions($input->getArguments(), $input->getOptions());
Tui::init($output, !$this->config->getNoInteraction());
$pm = new PromptManager($this->config);
static::header();
$pm->prompt();
Tui::list($pm->getResponsesSummary(), 'Installation summary');
if (!$pm->shouldProceed()) {
Tui::info('Aborting project installation. No files were changed.');
return Command::SUCCESS;
}
Tui::info('Starting project installation');
Tui::action(
label: 'β¬οΈ Downloading Vortex',
hint: fn(): string => sprintf('Downloading from "%s" repository at commit "%s"', ...Downloader::parseUri($this->config->get(Config::REPO))),
success: 'Vortex downloaded',
action: function (): void {
$version = (new Downloader())->download($this->config->get(Config::REPO), $this->config->get(Config::REF), $this->config->get(Config::TMP));
$this->config->set(Config::VERSION, $version);
},
);
Tui::action(
label: 'βοΈ Customizing Vortex for your project',
success: 'Vortex was customized for your project',
action: fn() => $pm->process(),
);
Tui::action(
label: 'π₯£ Preparing destination directory',
success: 'Destination directory is ready',
action: fn(): array => $this->prepareDestination(),
);
Tui::action(
label: 'β‘οΈ Copying files to the destination directory',
success: 'Files copied to destination directory',
action: fn() => $this->copyFiles(),
);
Tui::action(
label: 'π¬ Preparing demo content',
success: 'Demo content prepared',
action: fn(): string|array => $this->handleDemo(),
);
// @todo Implement the demo mode.
// $this->handleDemo();
}
catch (\Exception $exception) {
Tui::output()->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
Tui::error('Installation failed with an error: ' . $exception->getMessage());
return Command::FAILURE;
}
static::footer();
return Command::SUCCESS;
}
protected function checkRequirements(): void {
if (passthru('command -v git >/dev/null') === FALSE) {
throw new \RuntimeException('Missing git.');
}
if (passthru('command -v curl >/dev/null') === FALSE) {
throw new \RuntimeException('Missing curl.');
}
if (passthru('command -v tar >/dev/null') === FALSE) {
throw new \RuntimeException('Missing tar.');
}
if (passthru('command -v composer >/dev/null') === FALSE) {
throw new \RuntimeException('Missing Composer.');
}
}
/**
* Instantiate configuration from CLI options and environment variables.
*
* Installer configuration is a set of internal installer variables
* prefixed with "VORTEX_INSTALL_" and used to control the installation. They
* are read from the environment variables with $this->config->get().
*
* For simplicity of naming, internal installer config variables used in
* $this->config->get() match environment variables names.
*
* @param array<mixed> $arguments
* Array of CLI arguments.
* @param array<mixed> $options
* Array of CLI options.
*/
protected function resolveOptions(array $arguments, array $options): void {
$config = isset($options[static::OPTION_CONFIG]) && is_scalar($options[static::OPTION_CONFIG]) ? strval($options[static::OPTION_CONFIG]) : '{}';
$this->config = Config::fromString($config);
$this->config->setQuiet($options[static::OPTION_QUIET]);
$this->config->setNoInteraction($options[static::OPTION_NO_INTERACTION]);
// Set root directory to resolve relative paths.
$root = !empty($options[static::OPTION_ROOT]) && is_scalar($options[static::OPTION_ROOT]) ? strval($options[static::OPTION_ROOT]) : NULL;
if ($root) {
$this->config->set(Config::ROOT, $root);
}
// Set destination directory.
$dst = !empty($arguments['destination']) && is_scalar($arguments[static::ARG_DESTINATION]) ? strval($arguments[static::ARG_DESTINATION]) : NULL;
$dst = $dst ?: Env::get(Config::DST, $this->config->get(Config::DST, $this->config->get(Config::ROOT)));
$dst = File::realpath($dst);
$this->config->set(Config::DST, $dst, TRUE);
// Load values from the destination .env file, if it exists.
if (File::exists($this->config->getDst() . '/.env')) {
Env::putFromDotenv($this->config->getDst() . '/.env');
}
[$repo, $ref] = Downloader::parseUri($options[static::OPTION_URI] ?: 'https://github.com/drevops/vortex.git@stable');
$this->config->set(Config::REPO, $repo);
$this->config->set(Config::REF, $ref);
// Check if the project is a Vortex project.
$this->config->set(Config::IS_VORTEX_PROJECT, File::contains($this->config->getDst() . DIRECTORY_SEPARATOR . 'README.md', '/badge\/Vortex-/'));
// Flag to proceed with installation. If FALSE - the installation will only
// print resolved values and will not proceed.
$this->config->set(Config::PROCEED, TRUE);
// Internal flag to enforce DEMO mode. If not set, the demo mode will be
// discovered automatically.
if (!is_null(Env::get(Config::IS_DEMO))) {
$this->config->set(Config::IS_DEMO, (bool) Env::get(Config::IS_DEMO));
}
// Internal flag to skip processing of the demo mode.
$this->config->set(Config::IS_DEMO_DB_DOWNLOAD_SKIP, (bool) Env::get(Config::IS_DEMO_DB_DOWNLOAD_SKIP, FALSE));
}
protected function prepareDestination(): array {
$messages = [];
$dst = $this->config->getDst();
if (!is_dir($dst)) {
$dst = File::mkdir($dst);
$messages[] = sprintf('Created directory "%s".', $dst);
}
if (!is_readable($dst . '/.git')) {
$messages[] = sprintf('Initialising a new Git repository in directory "%s".', $dst);
passthru(sprintf('git --work-tree="%s" --git-dir="%s/.git" init > /dev/null', $dst, $dst));
if (!File::exists($dst . '/.git')) {
throw new \RuntimeException(sprintf('Unable to initialise Git repository in directory "%s".', $dst));
}
}
return $messages;
}
protected function copyFiles(): void {
$src = $this->config->get(Config::TMP);
$dst = $this->config->getDst();
// Due to the way symlinks can be ordered, we cannot copy files one-by-one
// into destination directory. Instead, we are removing all ignored files
// and empty directories, making the src directory "clean", and then
// recursively copying the whole directory.
$all = File::scandirRecursive($src, File::ignoredPaths(), TRUE);
$files = File::scandirRecursive($src);
$valid_files = File::scandirRecursive($src, File::ignoredPaths());
$dirs = array_diff($all, $valid_files);
$ignored_files = array_diff($files, $valid_files);
foreach ($valid_files as $filename) {
$relative_file = str_replace($src . DIRECTORY_SEPARATOR, '.' . DIRECTORY_SEPARATOR, (string) $filename);
if (File::isInternal($relative_file)) {
unlink($filename);
}
}
// Remove skipped files.
foreach ($ignored_files as $skipped_file) {
if (is_readable($skipped_file)) {
unlink($skipped_file);
}
}
// Remove empty directories.
foreach ($dirs as $dir) {
File::rmdirEmpty($dir);
}
// Src directory is now "clean" - copy it to dst directory.
if (is_dir($src) && !File::dirIsEmpty($src)) {
File::copy($src, $dst);
}
// Special case for .env.local as it may exist.
if (!file_exists($dst . '/.env.local') && file_exists($dst . '/.env.local.example')) {
File::copy($dst . '/.env.local.example', $dst . '/.env.local');
}
}
protected function handleDemo(): array|string {
if (empty($this->config->get(Config::IS_DEMO))) {
return 'Not a demo mode.';
}
if (!empty($this->config->get(Config::IS_DEMO_DB_DOWNLOAD_SKIP))) {
return sprintf('%s is set. Skipping demo database download.', Config::IS_DEMO_DB_DOWNLOAD_SKIP);
}
// Reload variables from destination's .env.
Env::putFromDotenv($this->config->getDst() . '/.env');
$url = Env::get('VORTEX_DB_DOWNLOAD_URL');
if (empty($url)) {
return 'No database download URL provided. Skipping demo database download.';
}
$data_dir = $this->config->getDst() . DIRECTORY_SEPARATOR . Env::get('VORTEX_DB_DIR', './.data');
$db_file = Env::get('VORTEX_DB_FILE', 'db.sql');
if (file_exists($data_dir . DIRECTORY_SEPARATOR . $db_file)) {
return 'Database dump file already exists. Skipping demo database download.';
}
$messages = [];
if (!file_exists($data_dir)) {
$data_dir = File::mkdir($data_dir);
$messages[] = sprintf('Created data directory "%s".', $data_dir);
}
$command = sprintf('curl -s -L "%s" -o "%s/%s"', $url, $data_dir, $db_file);
if (passthru($command) === FALSE) {
throw new \RuntimeException(sprintf('Unable to download demo database from %s.', $url));
}
$messages[] = sprintf('No database dump file was found in "%s" directory.', $data_dir);
$messages[] = sprintf('Downloaded demo database from %s.', $url);
return $messages;
}
protected function header(): void {
$logo_large = <<<EOT
βββ βββ βββββββ βββββββ βββββββββ ββββββββ βββ βββ
βββ βββ βββββββββ ββββββββ βββββββββ ββββββββ ββββββββ
βββ βββ βββ βββ ββββββββ βββ ββββββ ββββββ
ββββ ββββ βββ βββ ββββββββ βββ ββββββ ββββββ
βββββββ βββββββββ βββ βββ βββ ββββββββ ββββ βββ
βββββ βββββββ βββ βββ βββ ββββββββ βββ βββ
Drupal project template
by DrevOps
EOT;
$logo_small = <<<EOT
ββ ββ βββ ββββββββββββββββ ββ
ββ ββββ ββββ ββ β ββ ββββ
ββ ββββ βββββββ β βββββ ββ
ββββ βββββββ ββ β βββββββββββ
Drupal project template
by DrevOps
EOT;
$logo = Tui::terminalWidth() >= 80 ? $logo_large : $logo_small;
$logo = Tui::center($logo, Tui::terminalWidth(), 'β');
Tui::note($logo);
$title = 'Welcome to Vortex interactive installer';
$content = '';
$ref = $this->config->get(Config::REF);
if ($ref == Downloader::REF_STABLE) {
$content .= 'This tool will guide you through installing the latest version of Vortex into your project.' . PHP_EOL;
}
elseif ($ref == Downloader::REF_HEAD) {
$content .= 'This tool will guide you through installing the latest development version of Vortex into your project.' . PHP_EOL;
}
else {
$content .= sprintf('This tool will guide you through installing the version of Vortex into your project at commit "%s".', $ref) . PHP_EOL;
}
$content .= PHP_EOL;
if ($this->config->isVortexProject()) {
$content .= 'It looks like Vortex is already installed into this project.' . PHP_EOL;
$content .= PHP_EOL;
}
if ($this->config->getNoInteraction()) {
$content .= 'Vortex installer will try to discover the settings from the environment and will install configuration relevant to your site.' . PHP_EOL;
$content .= PHP_EOL;
$content .= 'Existing committed files may be modified. You will need to resolve any changes manually.' . PHP_EOL;
$title = 'Welcome to Vortex non-interactive installer';
}
else {
$content .= 'Youβll be asked a few questions to tailor the configuration to your site.' . PHP_EOL;
$content .= 'No changes will be made until you confirm everything at the end.' . PHP_EOL;
$content .= PHP_EOL;
$content .= 'If you proceed, some committed files may be modified after confirmation, and you may need to resolve any changes manually.' . PHP_EOL;
$content .= PHP_EOL;
$content .= 'Press Ctrl+C at any time to exit the installer.' . PHP_EOL;
$content .= 'Press Ctrl+U at any time to go back to the previous step.' . PHP_EOL;
}
Tui::box($content, $title);
}
public function footer(): void {
$output = '';
if ($this->config->isVortexProject()) {
$title = 'Finished updating Vortex πππ';
$output .= 'Please review the changes and commit the required files.';
}
else {
$title = 'Finished installing Vortex πππ';
$output .= 'Next steps:' . PHP_EOL;
$output .= ' cd ' . $this->config->getDst() . PHP_EOL;
$output .= ' git add -A # Add all files.' . PHP_EOL;
$output .= ' git commit -m "Initial commit." # Commit all files.' . PHP_EOL;
$output .= ' ahoy build # Build site.' . PHP_EOL;
$output .= PHP_EOL;
$output .= ' See https://vortex.drevops.com/quickstart';
}
Tui::box($output, $title);
}
}