Skip to content

Commit 4a74c24

Browse files
authored
feat(dev): add component:execute command (#8875)
1 parent 0980b8f commit 4a74c24

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

dev/google-cloud

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
2323
require __DIR__ . '/vendor/autoload.php';
2424

2525
use Google\Cloud\Dev\Command\ComponentAddVersionCommand;
26+
use Google\Cloud\Dev\Command\ComponentExecuteCommand;
2627
use Google\Cloud\Dev\Command\ComponentInfoCommand;
2728
use Google\Cloud\Dev\Command\ComponentNewCommand;
2829
use Google\Cloud\Dev\Command\ComponentUpdateGencodeCommand;
@@ -48,6 +49,7 @@ $rootDirectory = realpath(__DIR__ . '/../') . '/';
4849
$app = new Application;
4950
$app->add(new ComponentInfoCommand());
5051
$app->add(new ComponentAddVersionCommand($rootDirectory));
52+
$app->add(new ComponentExecuteCommand());
5153
$app->add(new ComponentNewCommand($rootDirectory));
5254
$app->add(new ComponentUpdateGencodeCommand($rootDirectory));
5355
$app->add(new ComponentUpdateReadmeSampleCommand($rootDirectory));
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)