|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2026 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\InputInterface; |
| 22 | +use Symfony\Component\Console\Input\InputArgument; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | +use Google\Cloud\Dev\Component; |
| 25 | + |
| 26 | +/** |
| 27 | + * Execute commands for every Component |
| 28 | + * |
| 29 | + * ```bash |
| 30 | + * // execute PHP code directly (don't forget to escape `$` for bash |
| 31 | + * ./dev/google-cloud component:execute "copy('SECURITY.md', \$component->getPath() . '/SECURITY.md');" |
| 32 | + * // execute a PHP file (don't forget to escape `$` for bash |
| 33 | + * ./dev/google-cloud component:execute copy_file.php |
| 34 | + * ``` |
| 35 | + * |
| 36 | + * @internal |
| 37 | + */ |
| 38 | +class ComponentExecuteCommand extends Command |
| 39 | +{ |
| 40 | + protected function configure() |
| 41 | + { |
| 42 | + $this->setName('component:execute') |
| 43 | + ->setDescription('Execute a command for each component') |
| 44 | + ->addArgument('code', InputArgument::REQUIRED, 'Path to a file or PHP code to execute') |
| 45 | + ; |
| 46 | + } |
| 47 | + |
| 48 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 49 | + { |
| 50 | + $code = $input->getArgument('code'); |
| 51 | + $components = Component::getComponents(); |
| 52 | + |
| 53 | + $codeToExecute = file_exists($code) ? file_get_contents($code) : $code; |
| 54 | + if (0 === strpos($codeToExecute, '<?php')) { |
| 55 | + $codeToExecute = substr($codeToExecute, 5); |
| 56 | + } |
| 57 | + $output->writeln('<info>' . $codeToExecute . '</>'); |
| 58 | + foreach ($components as $component) { |
| 59 | + $output->writeln('Executing for ' . $component->getName()); |
| 60 | + eval($codeToExecute); |
| 61 | + } |
| 62 | + |
| 63 | + return 0; |
| 64 | + } |
| 65 | +} |
0 commit comments