|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Dev\Command; |
| 19 | + |
| 20 | +use Symfony\Component\Console\Command\Command; |
| 21 | +use Symfony\Component\Console\Input\ArrayInput; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Input\InputArgument; |
| 24 | +use Symfony\Component\Console\Input\InputOption; |
| 25 | +use Symfony\Component\Console\Output\OutputInterface; |
| 26 | +use Symfony\Component\Yaml\Yaml; |
| 27 | +use RuntimeException; |
| 28 | +use Google\Cloud\Dev\Component; |
| 29 | + |
| 30 | +/** |
| 31 | + * Add a Component |
| 32 | + * @internal |
| 33 | + */ |
| 34 | +class AddVersionCommand extends Command |
| 35 | +{ |
| 36 | + private const OWL_BOT_REGEX='/.*\/\(([\w|]+)\).*/'; |
| 37 | + |
| 38 | + protected function configure() |
| 39 | + { |
| 40 | + $this->setName('add-version') |
| 41 | + ->setDescription('Add a new version to an existing Component') |
| 42 | + ->addArgument('component', InputArgument::REQUIRED, 'Component to add the version to.') |
| 43 | + ->addArgument('version', InputArgument::REQUIRED, 'The new version to add.') |
| 44 | + ->addOption( |
| 45 | + 'no-update-component', |
| 46 | + null, |
| 47 | + InputOption::VALUE_NONE, |
| 48 | + 'Do not run the update-component command after adding the component skeleton' |
| 49 | + ) |
| 50 | + ->addOption( |
| 51 | + 'timeout', |
| 52 | + null, |
| 53 | + InputOption::VALUE_REQUIRED, |
| 54 | + 'The timeout limit for executing commands in seconds. Defaults to 60.', |
| 55 | + 120 |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + private $rootPath; |
| 60 | + |
| 61 | + /** |
| 62 | + * @param string $rootPath The path to the repository root directory.\ |
| 63 | + */ |
| 64 | + public function __construct($rootPath) |
| 65 | + { |
| 66 | + $this->rootPath = realpath($rootPath); |
| 67 | + parent::__construct(); |
| 68 | + } |
| 69 | + |
| 70 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 71 | + { |
| 72 | + $componentName = $input->getArgument('component'); |
| 73 | + $version = $input->getArgument('version'); |
| 74 | + |
| 75 | + // Ensure component exists |
| 76 | + $owlbotFile = sprintf('%s/%s/.OwlBot.yaml', $this->rootPath, $componentName); |
| 77 | + if (!file_exists($owlbotFile)) { |
| 78 | + throw new RuntimeException("Component '$componentName' not found."); |
| 79 | + } |
| 80 | + $output->writeln("Adding new version '$version' to .OwlBot.yaml."); |
| 81 | + $yaml = Yaml::parse(file_get_contents($owlbotFile)); |
| 82 | + foreach ($yaml['deep-copy-regex'] as $i => $deepCopyRegex) { |
| 83 | + if (preg_match(self::OWL_BOT_REGEX, $deepCopyRegex['source'], $matches)) { |
| 84 | + // ensure version doesn't already exist in .OwlBot.yaml before adding it |
| 85 | + if (false !== array_search($version, explode('|', $matches[1]))) { |
| 86 | + $output->writeln("Version '$version' already exists in deep-copy-regex. Skipping... "); |
| 87 | + continue; |
| 88 | + } |
| 89 | + $newVersion = $matches[1] . '|' . $version; |
| 90 | + $yaml['deep-copy-regex'][$i]['source'] = str_replace($matches[1], $newVersion, $matches[0]); |
| 91 | + } |
| 92 | + } |
| 93 | + // Ensure YAML has changed before writing it |
| 94 | + if ($yaml != Yaml::parse(file_get_contents($owlbotFile))) { |
| 95 | + file_put_contents($owlbotFile, Yaml::dump($yaml, 3, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); |
| 96 | + } |
| 97 | + |
| 98 | + // Run "update-component" command to generate the new version and add its sample to the README |
| 99 | + if ($input->getOption('no-update-component')) { |
| 100 | + // nothing left to do |
| 101 | + $output->writeln('Skipping component update: "--no-update-component" flag set'); |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + $args = [ |
| 106 | + 'component' => $componentName, |
| 107 | + '--timeout' => $input->getOption('timeout'), |
| 108 | + ]; |
| 109 | + if (!$this->getApplication()->has('update-component')) { |
| 110 | + throw new \RuntimeException( |
| 111 | + 'Application does not have an update-component command. ' |
| 112 | + . 'Run with --no-update-component to skip this.' |
| 113 | + ); |
| 114 | + } |
| 115 | + $updateCommand = $this->getApplication()->find('update-component'); |
| 116 | + $returnCode = $updateCommand->run(new ArrayInput($args), $output); |
| 117 | + if ($returnCode !== Command::SUCCESS) { |
| 118 | + return $returnCode; |
| 119 | + } |
| 120 | + // Run "add-sample-to-readme" command to ensure our README contains the latest version's sample. |
| 121 | + $addSamplesArgs = ['--component' => [$componentName], '--update' => true]; |
| 122 | + if (!$addSampleCommand = $this->getApplication()->find('add-sample-to-readme')) { |
| 123 | + throw new \RuntimeException('Application does not have an add-samples-to-readme command.'); |
| 124 | + } |
| 125 | + return $addSampleCommand->run(new ArrayInput($addSamplesArgs), $output); |
| 126 | + } |
| 127 | +} |
0 commit comments