Skip to content

Commit 44b2686

Browse files
authored
Merge pull request #21 from VanOns/feature/VOOS-89
[VOOS-89] Add support for post-import commands
2 parents dff72ae + da0a5ac commit 44b2686

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

config/environment-importer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@
226226
//],
227227
],
228228

229+
/*
230+
|--------------------------------------------------------------------------
231+
| Post-import Commands
232+
|--------------------------------------------------------------------------
233+
|
234+
| Commands that should be run after the environment is imported.
235+
|
236+
*/
237+
238+
'post_import_commands' => [],
239+
229240
/*
230241
|--------------------------------------------------------------------------
231242
| Notifications

src/Commands/ImportEnvironmentCommand.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Spatie\DbDumper\Databases\PostgreSql;
1818
use Spatie\DbDumper\Databases\Sqlite;
1919
use Spatie\DbDumper\Exceptions\CannotSetParameter;
20+
use Symfony\Component\Console\Output\BufferedOutput;
2021
use Symfony\Component\Process\Process;
2122
use VanOns\LaravelEnvironmentImporter\Exceptions\ImportEnvironmentException;
2223
use VanOns\LaravelEnvironmentImporter\Notifications\ImportFailed;
@@ -81,6 +82,7 @@ public function handle(): int
8182
$this->importDatabase();
8283
$this->importFiles();
8384
$this->flushCache();
85+
$this->runPostImportCommands();
8486
$this->finish();
8587
} catch (Exception $e) {
8688
$this->error($e->getMessage());
@@ -890,6 +892,57 @@ protected function flushCache(): void
890892
$this->info('[Cache] Cache flushed.');
891893
}
892894

895+
/**
896+
* Run post import commands.
897+
*/
898+
protected function runPostImportCommands(): void
899+
{
900+
$commands = $this->getConfigValue('post_import_commands', []);
901+
902+
if (empty($commands)) {
903+
return;
904+
}
905+
906+
$this->line('[CMD] Running post import commands...');
907+
908+
foreach ($commands as $key => $value) {
909+
if (is_int($key)) {
910+
$command = $value;
911+
$options = [];
912+
} else {
913+
$command = $key;
914+
$options = $value;
915+
}
916+
917+
$label = is_a($command, Command::class, true)
918+
? class_basename($command)
919+
: $command;
920+
921+
$this->line("[CMD] Running post-import command: \"{$label}\"");
922+
923+
if (is_a($command, Command::class, true)) {
924+
$buffer = new BufferedOutput();
925+
926+
Artisan::call($command, $options, $buffer);
927+
928+
foreach (explode("\n", trim($buffer->fetch())) as $outputLine) {
929+
$this->line(" | {$outputLine}");
930+
}
931+
} elseif (is_string($command)) {
932+
Process::fromShellCommandline($command)
933+
->run(function ($type, $buffer) {
934+
foreach (explode("\n", trim($buffer)) as $outputLine) {
935+
$this->line(" | {$outputLine}");
936+
}
937+
});
938+
} else {
939+
$this->warn("[CMD] Invalid post-import command: \"{$label}\"");
940+
}
941+
}
942+
943+
$this->info('[CMD] Ran post import commands.');
944+
}
945+
893946
/**
894947
* Clean up and finish the import.
895948
*/

0 commit comments

Comments
 (0)