From 7bf1610413407379d747bfd9775ca817f893c61b Mon Sep 17 00:00:00 2001 From: Stephan Huber Date: Fri, 8 Sep 2023 18:25:08 +0200 Subject: [PATCH] feat: add option --json to save the output of parallel runs into a json file --- src/Command/BaseCommand.php | 11 +++++++++-- src/Utilities/ParallelExecutor.php | 16 ++++++++++++++-- src/Utilities/ParallelExecutorRun.php | 16 ++++++++++++---- src/Utilities/PluginDiscovery.php | 10 ++++++---- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/Command/BaseCommand.php b/src/Command/BaseCommand.php index ee189f7b..99297e54 100644 --- a/src/Command/BaseCommand.php +++ b/src/Command/BaseCommand.php @@ -71,6 +71,13 @@ protected function configure() 'Run the command on a given set of blueprints simultaneously', null ) + ->addOption( + 'json', + null, + InputOption::VALUE_OPTIONAL, + 'Save the output as json into given file. (Works only when using variants)', + null + ) ->addOption( 'set', 's', @@ -370,7 +377,7 @@ private function handleVariants($variants, InputInterface $input, OutputInterfac $cmd[] = '-v'; } - $cmd_lines[] = $cmd; + $cmd_lines[$v] = $cmd; $rows[] = [$v, implode(' ', $cmd)]; } @@ -381,7 +388,7 @@ private function handleVariants($variants, InputInterface $input, OutputInterfac if ($input->getOption('force') !== false || $io->confirm('Do you want to run these commands? ', false)) { $io->comment('Running ...'); $executor = new ParallelExecutor($cmd_lines, $output, $input->getOption('num-threads')); - return $executor->execute($input, $output); + return $executor->execute($input, $output, $input->getOption('json')); } diff --git a/src/Utilities/ParallelExecutor.php b/src/Utilities/ParallelExecutor.php index a35187f7..d4e1474a 100644 --- a/src/Utilities/ParallelExecutor.php +++ b/src/Utilities/ParallelExecutor.php @@ -25,8 +25,9 @@ public function __construct($command_lines, OutputInterface $output, $max_simult $this->pool = new PriorityPool(); $this->pool->setMaxSimultaneous($max_simultaneous_processes); - foreach ($command_lines as $cmd) { + foreach ($command_lines as $identifier => $cmd) { $this->add(new ParallelExecutorRun( + $identifier, $cmd, $output instanceof ConsoleOutput ? $output->section() @@ -35,7 +36,7 @@ public function __construct($command_lines, OutputInterface $output, $max_simult } } - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output, ?string $save_as_json) { $progress_section = $output instanceof ConsoleOutput @@ -60,9 +61,16 @@ public function execute(InputInterface $input, OutputInterface $output) $progress->finish(); $style = new SymfonyStyle($input, $output); + $data = []; foreach ($this->pool->getAll() as $run) { if ($run instanceof ParallelExecutorRun) { $style->section(sprintf('Results of `%s`', $run->getCommandLine())); + $data[$run->getIdentifier()] = [ + 'command' => $run->getCommandLine(), + 'exit_code' => $run->getProcess()->getExitCode(), + 'output' => $run->getProcess()->getOutput(), + 'error_output' => $run->getProcess()->getErrorOutput(), + ]; $style->writeln($run->getProcess()->getOutput()); $error = $run->getProcess()->getErrorOutput(); if (!empty($error)) { @@ -72,6 +80,10 @@ public function execute(InputInterface $input, OutputInterface $output) } } + if ($save_as_json) { + file_put_contents($save_as_json, json_encode($data, JSON_PRETTY_PRINT)); + } + return $this->pool->isSuccessful(); } diff --git a/src/Utilities/ParallelExecutorRun.php b/src/Utilities/ParallelExecutorRun.php index 0cbadc9f..ec5cf489 100644 --- a/src/Utilities/ParallelExecutorRun.php +++ b/src/Utilities/ParallelExecutorRun.php @@ -17,8 +17,11 @@ class ParallelExecutorRun extends ProcessRun private $output; private $commandLine; - public function __construct($command_line, ConsoleSectionOutput $output = null) + protected $identifier; + + public function __construct(string $identifier, $command_line, ConsoleSectionOutput $output = null) { + $this->identifier = $identifier; $this->output = $output; $this->commandLine = implode(' ', $command_line); @@ -28,7 +31,7 @@ public function __construct($command_line, ConsoleSectionOutput $output = null) } } - public function addListeners() + public function addListeners(): void { $this->writeln("~ waiting"); @@ -52,13 +55,18 @@ function (RunEvent $event) { ); } - public function writeln($message) + public function writeln($message): void { $this->output->overwrite($this->commandLine . ': ' . $message); } - public function getCommandLine() + public function getCommandLine(): string { return $this->commandLine; } + + public function getIdentifier(): string + { + return $this->identifier; + } } diff --git a/src/Utilities/PluginDiscovery.php b/src/Utilities/PluginDiscovery.php index 8c6af381..45a20dc6 100644 --- a/src/Utilities/PluginDiscovery.php +++ b/src/Utilities/PluginDiscovery.php @@ -167,10 +167,12 @@ public static function discoverFromFabfile( $methods = $config->getMethodFactory(); foreach ($result as $plugin) { - $output->writeln(sprintf( - "Registering found plugin %s ...", - $plugin->getName() - )); + if ($output->isVerbose()) { + $output->writeln(sprintf( + "Registering found plugin %s ...", + $plugin->getName() + )); + } foreach ($plugin->getMethods() as $class_name) { $methods->addMethod(new $class_name($logger)); }