-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathGenerateFlexEndpointCommand.php
200 lines (170 loc) · 8.64 KB
/
GenerateFlexEndpointCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'generate:flex-endpoint', description: 'Generates the json files required by Flex')]
class GenerateFlexEndpointCommand extends Command
{
public const PROVIDER_GITHUB = 'github';
public const PROVIDER_GITLAB = 'gitlab';
protected function configure(): void
{
$this
->addArgument('repository', InputArgument::REQUIRED, 'The name of the repository')
->addArgument('source_branch', InputArgument::REQUIRED, 'The source branch of recipes')
->addArgument('flex_branch', InputArgument::REQUIRED, 'The branch of the target Flex endpoint')
->addArgument('output_directory', InputArgument::REQUIRED, 'The directory where generated files should be stored')
->addArgument('versions_json', InputArgument::OPTIONAL, 'The file where versions of Symfony are described')
->addOption('contrib')
->addOption('provider', null, InputOption::VALUE_REQUIRED, 'Provider of the repository (github or gitlab)', self::PROVIDER_GITHUB)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$repository = $input->getArgument('repository');
$sourceBranch = $input->getArgument('source_branch');
$flexBranch = $input->getArgument('flex_branch');
$outputDir = $input->getArgument('output_directory');
$versionsJson = $input->getArgument('versions_json');
$contrib = $input->getOption('contrib');
$provider = $input->getOption('provider');
$aliases = $recipes = $recipeConflicts = $versions = [];
if ($versionsJson) {
$versions = json_decode(file_get_contents($versionsJson), true);
foreach ($versions['splits'] as $package => $v) {
if (0 === strpos($package, 'symfony/') && '-pack' !== substr($package, -5)) {
$alias = substr($package, 8);
$aliases[$alias] = $package;
$aliases[str_replace('-', '', $alias)] = $package;
}
}
if (isset($versions['splits']['symfony/var-exporter'])) {
// Allow var-exporter v6.2+ to be installed with pre-6.2 apps
$versions['splits']['symfony/var-exporter'] = array_values(array_diff($versions['splits']['symfony/var-exporter'], ['4.2', '4.3', '4.4', '5.0', '5.1', '5.2', '5.3', '5.4', '6.0', '6.1']));
}
}
// stdin usually generated by `git ls-tree HEAD */*/*`
while (false !== $line = fgets(\STDIN)) {
[$tree, $package] = explode("\t", trim($line));
[,, $tree] = explode(' ', $tree);
if (!file_exists($package.'/manifest.json')) {
continue;
}
$manifest = json_decode(file_get_contents($package.'/manifest.json'), true);
$version = substr($package, 1 + strrpos($package, '/'));
$package = substr($package, 0, -1 - \strlen($version));
foreach ($manifest['aliases'] ?? [] as $alias) {
$aliases[$alias] = $package;
$aliases[str_replace('-', '', $alias)] = $package;
}
if (0 === strpos($package, 'symfony/') && '-pack' !== substr($package, -5)) {
$alias = substr($package, 8);
$aliases[$alias] = $package;
$aliases[str_replace('-', '', $alias)] = $package;
}
if ($this->generatePackageJson($package, $version, $manifest, $tree, $outputDir)) {
$recipes[$package][] = $version;
usort($recipes[$package], 'strnatcmp');
if (isset($manifest['conflict'])) {
uksort($manifest['conflict'], 'strnatcmp');
$recipeConflicts[$package][$version] = $manifest['conflict'];
uksort($recipeConflicts[$package], 'strnatcmp');
}
}
}
ksort($aliases, \SORT_NATURAL);
ksort($recipes, \SORT_NATURAL);
ksort($recipeConflicts, \SORT_NATURAL);
switch ($provider) {
case self::PROVIDER_GITHUB:
$baseHost = 'github.com';
$recipeTemplate = sprintf('https://raw.githubusercontent.com/%s/%s/{package_dotted}.{version}.json', $repository, $flexBranch);
$archivedRecipesTemplate = sprintf('https://raw.githubusercontent.com/%s/%s/archived/{package_dotted}/{ref}.json', $repository, $flexBranch);
break;
case self::PROVIDER_GITLAB:
$baseHost = 'gitlab.com';
$recipeTemplate = sprintf('https://gitlab.com/api/v4/projects/%s/repository/files/{package_dotted}.{version}.json/raw?ref=%s', urlencode($repository), $flexBranch);
$archivedRecipesTemplate = sprintf('https://gitlab.com/api/v4/projects/%s/repository/files/archived%%2F{package_dotted}%%2F{ref}.json/raw?ref=%s', urlencode($repository), $flexBranch);
break;
default:
throw new \InvalidArgumentException(sprintf('Unsupported provider "%s".', $provider));
}
file_put_contents($outputDir.'/index.json', json_encode([
'aliases' => $aliases,
'recipes' => $recipes,
'recipe-conflicts' => $recipeConflicts,
'versions' => $versions,
'branch' => $sourceBranch,
'is_contrib' => $contrib,
'_links' => [
'repository' => sprintf('%s/%s', $baseHost, $repository),
'origin_template' => sprintf('{package}:{version}@%s/%s:%s', $baseHost, $repository, $sourceBranch),
'recipe_template' => $recipeTemplate,
'recipe_template_relative' => sprintf('{package_dotted}.{version}.json'),
'archived_recipes_template' => $archivedRecipesTemplate,
'archived_recipes_template_relative' => sprintf('archived/{package_dotted}/{ref}.json'),
],
], \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)."\n");
return 0;
}
private function generatePackageJson(string $package, string $version, array $manifest, string $tree, string $outputDir): bool
{
unset($manifest['aliases']);
$files = [];
$it = new \RecursiveDirectoryIterator($package.'/'.$version);
$it->setFlags($it::SKIP_DOTS | $it::FOLLOW_SYMLINKS | $it::UNIX_PATHS);
foreach (new \RecursiveIteratorIterator($it) as $path => $file) {
$file = substr($path, 1 + \strlen($package.'/'.$version));
if (is_dir($path) || 'manifest.json' === $file) {
continue;
}
if ('post-install.txt' === $file) {
$manifest['post-install-output'] = explode("\n", rtrim(str_replace("\r", '', file_get_contents($path)), "\n"));
continue;
}
if ('Makefile' === $file) {
$manifest['makefile'] = explode("\n", rtrim(str_replace("\r", '', file_get_contents($path)), "\n"));
continue;
}
$contents = file_get_contents($path);
$files[$file] = [
'contents' => preg_match('//u', $contents) ? explode("\n", $contents) : base64_encode($contents),
'executable' => is_executable($path),
];
}
if (!$manifest) {
return false;
}
ksort($files, \SORT_NATURAL);
$contents = json_encode(
[
'manifests' => [
$package => [
'manifest' => $manifest,
'files' => $files,
'ref' => $tree,
],
],
],
\JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES
)."\n";
file_put_contents(sprintf('%s/%s.%s.json', $outputDir, str_replace('/', '.', $package), $version), $contents);
// save another version for the archives
$archivedPath = sprintf('%s/archived/%s/%s.json', $outputDir, str_replace('/', '.', $package), $tree);
if (!file_exists(\dirname($archivedPath))) {
mkdir(\dirname($archivedPath), 0777, true);
}
file_put_contents($archivedPath, $contents);
return true;
}
}