1 | <?php
|
---|
2 | /*
|
---|
3 | * Copyright 2020 Google LLC
|
---|
4 | *
|
---|
5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
---|
6 | * use this file except in compliance with the License. You may obtain a copy of
|
---|
7 | * 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, WITHOUT
|
---|
13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
---|
14 | * License for the specific language governing permissions and limitations under
|
---|
15 | * the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 | namespace Google\Task;
|
---|
19 |
|
---|
20 | use Composer\Script\Event;
|
---|
21 | use InvalidArgumentException;
|
---|
22 | use Symfony\Component\Filesystem\Filesystem;
|
---|
23 | use Symfony\Component\Finder\Finder;
|
---|
24 |
|
---|
25 | class Composer
|
---|
26 | {
|
---|
27 | /**
|
---|
28 | * @param Event $event Composer event passed in for any script method
|
---|
29 | * @param Filesystem $filesystem Optional. Used for testing.
|
---|
30 | */
|
---|
31 | public static function cleanup(
|
---|
32 | Event $event,
|
---|
33 | ?Filesystem $filesystem = null
|
---|
34 | ) {
|
---|
35 | $composer = $event->getComposer();
|
---|
36 | $extra = $composer->getPackage()->getExtra();
|
---|
37 | $servicesToKeep = isset($extra['google/apiclient-services'])
|
---|
38 | ? $extra['google/apiclient-services']
|
---|
39 | : [];
|
---|
40 | if ($servicesToKeep) {
|
---|
41 | $vendorDir = $composer->getConfig()->get('vendor-dir');
|
---|
42 | $serviceDir = sprintf(
|
---|
43 | '%s/google/apiclient-services/src/Google/Service',
|
---|
44 | $vendorDir
|
---|
45 | );
|
---|
46 | if (!is_dir($serviceDir)) {
|
---|
47 | // path for google/apiclient-services >= 0.200.0
|
---|
48 | $serviceDir = sprintf(
|
---|
49 | '%s/google/apiclient-services/src',
|
---|
50 | $vendorDir
|
---|
51 | );
|
---|
52 | }
|
---|
53 | self::verifyServicesToKeep($serviceDir, $servicesToKeep);
|
---|
54 | $finder = self::getServicesToRemove($serviceDir, $servicesToKeep);
|
---|
55 | $filesystem = $filesystem ?: new Filesystem();
|
---|
56 | if (0 !== $count = count($finder)) {
|
---|
57 | $event->getIO()->write(
|
---|
58 | sprintf('Removing %s google services', $count)
|
---|
59 | );
|
---|
60 | foreach ($finder as $file) {
|
---|
61 | $realpath = $file->getRealPath();
|
---|
62 | $filesystem->remove($realpath);
|
---|
63 | $filesystem->remove($realpath . '.php');
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @throws InvalidArgumentException when the service doesn't exist
|
---|
71 | */
|
---|
72 | private static function verifyServicesToKeep(
|
---|
73 | $serviceDir,
|
---|
74 | array $servicesToKeep
|
---|
75 | ) {
|
---|
76 | $finder = (new Finder())
|
---|
77 | ->directories()
|
---|
78 | ->depth('== 0');
|
---|
79 |
|
---|
80 | foreach ($servicesToKeep as $service) {
|
---|
81 | if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) {
|
---|
82 | throw new InvalidArgumentException(
|
---|
83 | sprintf(
|
---|
84 | 'Invalid Google service name "%s"',
|
---|
85 | $service
|
---|
86 | )
|
---|
87 | );
|
---|
88 | }
|
---|
89 | try {
|
---|
90 | $finder->in($serviceDir . '/' . $service);
|
---|
91 | } catch (InvalidArgumentException $e) {
|
---|
92 | throw new InvalidArgumentException(
|
---|
93 | sprintf(
|
---|
94 | 'Google service "%s" does not exist or was removed previously',
|
---|
95 | $service
|
---|
96 | )
|
---|
97 | );
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private static function getServicesToRemove(
|
---|
103 | $serviceDir,
|
---|
104 | array $servicesToKeep
|
---|
105 | ) {
|
---|
106 | // find all files in the current directory
|
---|
107 | return (new Finder())
|
---|
108 | ->directories()
|
---|
109 | ->depth('== 0')
|
---|
110 | ->in($serviceDir)
|
---|
111 | ->exclude($servicesToKeep);
|
---|
112 | }
|
---|
113 | }
|
---|