-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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: composer cleanup task #1877
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
57f66b5
Composer cleanup task
bshaffer 37fed67
invalid services throw an exception
bshaffer d225226
styles fix
bshaffer fcc41d7
fixes styles again
bshaffer a03ae1f
adds validation for service name
bshaffer 38e0bba
another 'style fix' -_-
bshaffer 975909e
adds tests
bshaffer ebfbda5
make tests php54-compatible
bshaffer 633cfe4
fix those styles
bshaffer 3acb887
fix php 7-specific syntax
bshaffer fae34bd
moves mocks to use phpspec/prophecy
bshaffer 040e599
adds README instructions and adds e2e test
bshaffer 100d230
cs fix
bshaffer 22d8516
rearranges README
bshaffer 4036571
update README one last time
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/* | ||
* Copyright 2020 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. | ||
*/ | ||
|
||
use Composer\Script\Event; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class Google_Task_Composer | ||
{ | ||
/** | ||
* @param Event $event Composer event passed in for any script method | ||
* @param FilesystemInterface $filesystem Optional. Used for testing. | ||
*/ | ||
public static function cleanup( | ||
Event $event, | ||
Filesystem $filesystem = null | ||
) { | ||
$composer = $event->getComposer(); | ||
$extra = $composer->getPackage()->getExtra(); | ||
$servicesToKeep = isset($extra['google/apiclient-services']) ? | ||
$extra['google/apiclient-services'] : []; | ||
if ($servicesToKeep) { | ||
$serviceDir = sprintf( | ||
'%s/google/apiclient-services/src/Google/Service', | ||
$composer->getConfig()->get('vendor-dir') | ||
); | ||
self::verifyServicesToKeep($serviceDir, $servicesToKeep); | ||
$finder = self::getServicesToRemove($serviceDir, $servicesToKeep); | ||
$filesystem = $filesystem ?: new Filesystem(); | ||
if (0 !== $count = count($finder)) { | ||
$event->getIO()->write( | ||
sprintf( | ||
'Removing %s google services', | ||
$count | ||
) | ||
); | ||
foreach ($finder as $file) { | ||
$realpath = $file->getRealPath(); | ||
$filesystem->remove($realpath); | ||
$filesystem->remove($realpath . '.php'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException when the service doesn't exist | ||
*/ | ||
private static function verifyServicesToKeep( | ||
$serviceDir, | ||
array $servicesToKeep | ||
) { | ||
$finder = (new Finder()) | ||
->directories() | ||
->depth('== 0'); | ||
|
||
foreach ($servicesToKeep as $service) { | ||
if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Invalid Google service name "%s"', | ||
$service | ||
) | ||
); | ||
} | ||
try { | ||
$finder->in($serviceDir . '/' . $service); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Google service "%s" does not exist or was removed previously', | ||
$service | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
private static function getServicesToRemove( | ||
$serviceDir, | ||
array $servicesToKeep | ||
) { | ||
// find all files in the current directory | ||
return (new Finder()) | ||
->directories() | ||
->depth('== 0') | ||
->in($serviceDir) | ||
->exclude($servicesToKeep); | ||
} | ||
} |
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,206 @@ | ||
<?php | ||
/* | ||
* Copyright 2020 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. | ||
*/ | ||
|
||
class Google_Task_ComposerTest extends BaseTest | ||
{ | ||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Google service "Foo" does not exist | ||
*/ | ||
public function testInvalidServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent(['Foo'])); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Invalid Google service name "../YouTube" | ||
*/ | ||
public function testRelatePathServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent(['../YouTube'])); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Google service "" does not exist | ||
*/ | ||
public function testEmptyServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent([''])); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage Invalid Google service name "YouTube*" | ||
*/ | ||
public function testWildcardServiceName() | ||
{ | ||
Google_Task_Composer::cleanup($this->createMockEvent(['YouTube*'])); | ||
} | ||
|
||
public function testRemoveServices() | ||
{ | ||
$vendorDir = sys_get_temp_dir() . '/rand-' . rand(); | ||
$serviceDir = sprintf( | ||
'%s/google/apiclient-services/src/Google/Service/', | ||
$vendorDir | ||
); | ||
$dirs = [ | ||
'ServiceToKeep', | ||
'ServiceToDelete1', | ||
'ServiceToDelete2', | ||
]; | ||
$files = [ | ||
'ServiceToKeep/ServiceFoo.php', | ||
'ServiceToKeep.php', | ||
'SomeRandomFile.txt', | ||
'ServiceToDelete1/ServiceFoo.php', | ||
'ServiceToDelete1.php', | ||
'ServiceToDelete2/ServiceFoo.php', | ||
'ServiceToDelete2.php', | ||
]; | ||
foreach ($dirs as $dir) { | ||
@mkdir($serviceDir . $dir, 0777, true); | ||
} | ||
foreach ($files as $file) { | ||
touch($serviceDir . $file); | ||
} | ||
$print = 'Removing 2 google services'; | ||
Google_Task_Composer::cleanup( | ||
$this->createMockEvent(['ServiceToKeep'], $vendorDir, $print), | ||
$this->createMockFilesystem([ | ||
'ServiceToDelete2', | ||
'ServiceToDelete2.php', | ||
'ServiceToDelete1', | ||
'ServiceToDelete1.php', | ||
], $serviceDir) | ||
); | ||
} | ||
|
||
private function createMockFilesystem(array $files, $serviceDir) | ||
{ | ||
$mockFilesystem = $this->prophesize('Symfony\Component\Filesystem\Filesystem'); | ||
foreach ($files as $filename) { | ||
$file = new \SplFileInfo($serviceDir . $filename); | ||
$mockFilesystem->remove($file->getRealPath()) | ||
->shouldBeCalledTimes(1); | ||
} | ||
|
||
return $mockFilesystem->reveal(); | ||
} | ||
|
||
private function createMockEvent( | ||
array $servicesToKeep, | ||
$vendorDir = '', | ||
$print = null | ||
) { | ||
$mockPackage = $this->prophesize('Composer\Package\RootPackage'); | ||
$mockPackage->getExtra() | ||
->shouldBeCalledTimes(1) | ||
->willReturn(['google/apiclient-services' => $servicesToKeep]); | ||
|
||
$mockConfig = $this->prophesize('Composer\Config'); | ||
$mockConfig->get('vendor-dir') | ||
->shouldBeCalledTimes(1) | ||
->willReturn($vendorDir); | ||
|
||
$mockComposer = $this->prophesize('Composer\Composer'); | ||
$mockComposer->getPackage() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockPackage->reveal()); | ||
$mockComposer->getConfig() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockConfig->reveal()); | ||
|
||
$mockEvent = $this->prophesize('Composer\Script\Event'); | ||
$mockEvent->getComposer() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockComposer); | ||
|
||
if ($print) { | ||
$mockIO = $this->prophesize('Composer\IO\ConsoleIO'); | ||
$mockIO->write($print) | ||
->shouldBeCalledTimes(1); | ||
|
||
$mockEvent->getIO() | ||
->shouldBeCalledTimes(1) | ||
->willReturn($mockIO->reveal()); | ||
} | ||
|
||
return $mockEvent->reveal(); | ||
} | ||
|
||
public function testE2E() | ||
{ | ||
$composerJson = json_encode([ | ||
'require' => [ | ||
'google/apiclient' => 'dev-add-composer-cleanup' | ||
], | ||
'scripts' => [ | ||
'post-update-cmd' => 'Google_Task_Composer::cleanup' | ||
], | ||
'extra' => [ | ||
'google/apiclient-services' => [ | ||
'Drive', | ||
'YouTube' | ||
] | ||
] | ||
]); | ||
|
||
$tmpDir = sys_get_temp_dir() . '/test-' . rand(); | ||
$serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service'; | ||
|
||
mkdir($tmpDir); | ||
file_put_contents($tmpDir . '/composer.json', $composerJson); | ||
passthru('composer install -d ' . $tmpDir); | ||
|
||
$this->assertFileExists($serviceDir . '/Drive.php'); | ||
$this->assertFileExists($serviceDir . '/Drive'); | ||
$this->assertFileExists($serviceDir . '/YouTube.php'); | ||
$this->assertFileExists($serviceDir . '/YouTube'); | ||
$this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); | ||
$this->assertFileNotExists($serviceDir . '/YouTubeReporting'); | ||
|
||
$composerJson = json_encode([ | ||
'require' => [ | ||
'google/apiclient' => 'dev-add-composer-cleanup' | ||
], | ||
'scripts' => [ | ||
'post-update-cmd' => 'Google_Task_Composer::cleanup' | ||
], | ||
'extra' => [ | ||
'google/apiclient-services' => [ | ||
'Drive', | ||
'YouTube', | ||
'YouTubeReporting', | ||
] | ||
] | ||
]); | ||
|
||
file_put_contents($tmpDir . '/composer.json', $composerJson); | ||
passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services'); | ||
passthru('composer update -d ' . $tmpDir); | ||
|
||
$this->assertFileExists($serviceDir . '/Drive.php'); | ||
$this->assertFileExists($serviceDir . '/Drive'); | ||
$this->assertFileExists($serviceDir . '/YouTube.php'); | ||
$this->assertFileExists($serviceDir . '/YouTube'); | ||
$this->assertFileExists($serviceDir . '/YouTubeReporting.php'); | ||
$this->assertFileExists($serviceDir . '/YouTubeReporting'); | ||
} | ||
} |
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.
We will need to change this before tagging a new version!