-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dev): add command to update composer deps #6773
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
34b0920
add command to update composer deps
bshaffer 5feeea7
add --local option
bshaffer ddbca4f
fix cs
bshaffer cb8a1bc
remove bump-dep
bshaffer 43e6b65
add tests
bshaffer 0483388
set COMPOSER_ROOT_VERSION for composer install
bshaffer 4b1e7da
remove -q
bshaffer 00f48d1
ensure we can see reason composer fails
bshaffer d958ea4
Apply suggestions from code review
bshaffer b58d90c
allow specifying a component, fix add
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,9 @@ | |
} | ||
}, | ||
"archive": { | ||
"exclude": ["/ext"] | ||
"exclude": [ | ||
"/ext" | ||
] | ||
}, | ||
"bin": [ | ||
"bin/google-cloud-debugger" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Dev\Command; | ||
|
||
use Google\Cloud\Dev\Composer; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
/** | ||
* Update package dependencies | ||
* | ||
* @internal | ||
*/ | ||
class UpdateDepsCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this->setName('update-deps') | ||
->setDescription('update a dependency across all components') | ||
->addArgument('package', InputArgument::REQUIRED, 'Package name to update, e.g. "google/gax"') | ||
->addArgument('version', InputArgument::OPTIONAL, 'Package version to update to, e.g. "1.4.0"', '') | ||
->addOption('component', 'c', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'bumps deps for the specified component/file') | ||
->addOption('bump', '', InputOption::VALUE_NONE, 'Bump to latest version of the package') | ||
->addOption('add', '', InputOption::VALUE_OPTIONAL, 'Adds the dep if it doesn\'t exist (--add=dev for require-dev)', false) | ||
->addOption('local', '', InputOption::VALUE_NONE, 'Add a link to the local component') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$package = $input->getArgument('package'); | ||
if (!$version = $input->getArgument('version')) { | ||
if (!$input->getOption('bump')) { | ||
throw new \InvalidArgumentException('You must either supply a package version or the --bump flag'); | ||
} | ||
if (!$version = Composer::getLatestVersion($package)) { | ||
throw new \InvalidArgumentException('Could not find a version for ' . $package); | ||
} | ||
} elseif ($input->getOption('bump')) { | ||
throw new \InvalidArgumentException('You cannot supply both a package version and the --bump flag'); | ||
} | ||
|
||
$projectRoot = realpath(__DIR__ . '/../../../'); | ||
$result = (new Finder())->files()->in($projectRoot)->depth('<= 1')->name('composer.json'); | ||
$paths = array_map(fn ($file) => $file->getPathname(), iterator_to_array($result)); | ||
sort($paths); | ||
|
||
$componentPath = $input->getOption('local') ? $this->getComponentName($paths, $package) : null; | ||
$updateCount = 0; | ||
foreach ($input->getOption('component') ?: $paths as $jsonFile) { | ||
if (is_dir($jsonFile) && file_exists($jsonFile . '/composer.json')) { | ||
$jsonFile .= '/composer.json'; | ||
} | ||
$composerJson = json_decode(file_get_contents($jsonFile), true); | ||
$require = 'require'; | ||
if (!isset($composerJson['require'][$package])) { | ||
if (isset($composerJson['require-dev'][$package])) { | ||
$require = 'require-dev'; | ||
} elseif (false === $input->getOption('add')) { | ||
continue; | ||
} elseif ('dev' === $input->getOption('add')) { | ||
$require = 'require-dev'; | ||
} | ||
} | ||
$composerJson[$require][$package] = $version; | ||
if ($input->getOption('local')) { | ||
$composerJson['repositories'] ??= []; | ||
$composerJson['repositories'][] = [ | ||
'type' => 'path', | ||
'url' => '../' . $componentPath, | ||
'options' => [ | ||
'versions' => [$package => $version], | ||
] | ||
]; | ||
} | ||
$output->writeln(sprintf('Updated <info>%s</>', basename(dirname($jsonFile)))); | ||
file_put_contents($jsonFile, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"); | ||
$updateCount++; | ||
} | ||
$output->writeln("Updated <fg=white>$updateCount packages</> to use <comment>$package</>: <info>$version</>"); | ||
|
||
return 0; | ||
} | ||
|
||
private function getComponentName(array $paths, string $package): string | ||
{ | ||
foreach ($paths as $path) { | ||
$composerJson = json_decode(file_get_contents($path), true); | ||
if (isset($composerJson['name']) && $composerJson['name'] === $package) { | ||
return basename(dirname($path)); | ||
} | ||
} | ||
|
||
throw new \InvalidArgumentException('Component not found for package ' . $package); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Dev\Tests\Unit\Command; | ||
|
||
use Google\Cloud\Dev\Command\UpdateDepsCommand; | ||
use Google\Cloud\Dev\Composer; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* @group dev | ||
*/ | ||
class UpdateDepsCommandTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideUpdateDeps | ||
*/ | ||
public function testUpdateDeps(array $cmdOptions, array $json, array $expectedJson) | ||
{ | ||
$tmpFile = sys_get_temp_dir() . '/composer.json'; | ||
file_put_contents($tmpFile, json_encode($json)); | ||
$cmdOptions['--component'] = [$tmpFile]; | ||
$commandTester = new CommandTester(new UpdateDepsCommand()); | ||
$commandTester->execute($cmdOptions); | ||
|
||
$this->assertEquals($expectedJson, json_decode(file_get_contents($tmpFile), true)); | ||
} | ||
|
||
public function testBumpWithVersionThrowsException() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('You cannot supply both a package version and the --bump flag'); | ||
|
||
$commandTester = new CommandTester(new UpdateDepsCommand()); | ||
$commandTester->execute([ | ||
'package' => 'google/gax', | ||
'version' => '1.2.3', | ||
'--bump' => true, | ||
]); | ||
} | ||
|
||
public function testNoBumpOrVersionThrowsException() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('You must either supply a package version or the --bump flag'); | ||
|
||
$commandTester = new CommandTester(new UpdateDepsCommand()); | ||
$commandTester->execute([ | ||
'package' => 'google/gax', | ||
]); | ||
} | ||
|
||
public function testInvalidComponentWithLocalThrowsException() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Component not found for package google/foo'); | ||
|
||
$commandTester = new CommandTester(new UpdateDepsCommand()); | ||
$commandTester->execute([ | ||
'package' => 'google/foo', | ||
'version' => '1.2.3', | ||
'--local' => true, | ||
]); | ||
} | ||
|
||
public function provideUpdateDeps() | ||
{ | ||
return [ | ||
// Update package (require) | ||
[ | ||
['package' => 'google/gax', 'version' => '4.5.6'], | ||
['require' => ['google/gax' => '1.2.3']], | ||
['require' => ['google/gax' => '4.5.6']], | ||
], | ||
// Update package (require-dev) | ||
[ | ||
['package' => 'google/gax', 'version' => '4.5.6'], | ||
['require-dev' => ['google/gax' => '1.2.3']], | ||
['require-dev' => ['google/gax' => '4.5.6']], | ||
], | ||
// Update package (doesn't exist) | ||
[ | ||
['package' => 'google/gax', 'version' => '4.5.6'], | ||
[], | ||
[], | ||
], | ||
// Update package with add (require) | ||
[ | ||
['package' => 'google/gax', 'version' => '4.5.6', '--add' => true], | ||
[], | ||
['require' => ['google/gax' => '4.5.6']], | ||
], | ||
// Update package with add (require-dev) | ||
[ | ||
['package' => 'google/gax', 'version' => '4.5.6', '--add' => 'dev'], | ||
[], | ||
['require-dev' => ['google/gax' => '4.5.6']], | ||
], | ||
// Update package with bump (require-dev) | ||
[ | ||
['package' => 'google/gax', '--bump' => true], | ||
['require' => ['google/gax' => '1.2.3']], | ||
['require' => ['google/gax' => Composer::getLatestVersion('google/gax')]], | ||
], | ||
// Update package with local | ||
[ | ||
['package' => 'google/cloud-core', 'version' => '1.100', '--local' => true], | ||
['require' => ['google/cloud-core' => '1.2.3']], | ||
[ | ||
'require' => ['google/cloud-core' => '1.100'], | ||
'repositories' => [ | ||
[ | ||
'type' => 'path', | ||
'url' => '../Core', | ||
'options' => ['versions' => ['google/cloud-core' => '1.100']], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated to this PR, but should this not be
exit
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, because we want to run all the tests even if one fails, so we can see if the others succeed or not. By sending the error in
FAILED_FILE
, we ensure that we will exit below.