|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace StellarWP\Pup\Commands; |
| 4 | + |
| 5 | +use StellarWP\Pup\App; |
| 6 | +use StellarWP\Pup\Exceptions\BaseException; |
| 7 | +use StellarWP\Pup\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | + |
| 13 | +class Workflow extends Command { |
| 14 | + /** |
| 15 | + * @inheritDoc |
| 16 | + * |
| 17 | + * @return void |
| 18 | + */ |
| 19 | + protected function configure() { |
| 20 | + $this->setName( 'workflow' ) |
| 21 | + ->setAliases( [ 'do' ] ) |
| 22 | + ->addArgument( 'workflow', InputArgument::REQUIRED, 'The workflow you would like to run.' ) |
| 23 | + ->addOption( 'root', null, InputOption::VALUE_REQUIRED, 'Set the root directory for running commands.' ) |
| 24 | + ->setDescription( 'Run a command workflow.' ) |
| 25 | + ->setHelp( 'Run a command workflow.' ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @inheritDoc |
| 30 | + */ |
| 31 | + protected function execute( InputInterface $input, OutputInterface $output ) { |
| 32 | + parent::execute( $input, $output ); |
| 33 | + $config = App::getConfig(); |
| 34 | + $root = $input->getOption( 'root' ); |
| 35 | + $workflow_slug = $input->getArgument( 'workflow' ); |
| 36 | + $io = $this->getIO(); |
| 37 | + $application = $this->getApplication(); |
| 38 | + if ( ! $application ) { |
| 39 | + throw new BaseException( 'Could not run pup.' ); |
| 40 | + } |
| 41 | + |
| 42 | + $collection = $config->getWorkflows(); |
| 43 | + |
| 44 | + if ( $collection->count() === 0 ) { |
| 45 | + $io->writeln( '📣 The .puprc does not have any workflows configured.' ); |
| 46 | + $io->writeln( '💡 If you would like to use workflows, simply add a "<comment>workflows</comment>" property in <comment>.puprc</comment> similar to:' ); |
| 47 | + $io->writeln( '' ); |
| 48 | + $io->writeln( '"workflows": {' ); |
| 49 | + $io->writeln( ' "my-workflow": [' ); |
| 50 | + $io->writeln( ' "composer install",' ); |
| 51 | + $io->writeln( ' "npm run build"' ); |
| 52 | + $io->writeln( ' ]' ); |
| 53 | + $io->writeln( '}' ); |
| 54 | + $io->writeln( '' ); |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + $workflow = $collection->get( $workflow_slug ); |
| 59 | + if ( ! $workflow ) { |
| 60 | + $io->writeln( "<error>The workflow '{$workflow_slug}' does not exist.</error>" ); |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + if ( $root ) { |
| 65 | + chdir( $root ); |
| 66 | + } |
| 67 | + |
| 68 | + $io->writeln( "<comment>Running {$workflow_slug} workflow steps...</comment>" ); |
| 69 | + foreach ( $workflow->getCommands() as $step ) { |
| 70 | + $bail_on_failure = true; |
| 71 | + if ( strpos( $step, '@' ) === 0 ) { |
| 72 | + $bail_on_failure = false; |
| 73 | + $step = substr( $step, 1 ); |
| 74 | + } |
| 75 | + $io->section( "> <fg=cyan>{$step}</>" ); |
| 76 | + system( $step, $result ); |
| 77 | + $io->newLine(); |
| 78 | + |
| 79 | + if ( $result ) { |
| 80 | + $io->writeln( "[FAIL] Workflow step failed: {$step}" ); |
| 81 | + |
| 82 | + if ( $bail_on_failure ) { |
| 83 | + $io->writeln( "<fg=red>Exiting...</>" ); |
| 84 | + return $result; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if ( $root ) { |
| 89 | + chdir( $config->getWorkingDir() ); |
| 90 | + } |
| 91 | + |
| 92 | + $io->writeln( '<info>Workflow complete.</info>' ); |
| 93 | + } |
| 94 | + |
| 95 | + return 0; |
| 96 | + } |
| 97 | +} |
0 commit comments