/
PackagesBuilder.php
215 lines (192 loc) · 7.36 KB
/
PackagesBuilder.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* This file is part of composer/satis.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Composer\Satis\Builder;
use Composer\Json\JsonFile;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Util\Filesystem;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Builds the JSON files.
*
* @author James Hautot <james@rezo.net>
*/
class PackagesBuilder extends Builder
{
/** @var string packages.json file name. */
private $filename;
/** @var string included json filename template */
private $includeFileName;
private $writtenIncludeJsons = array();
/**
* Dedicated Packages Constructor.
*
* @param OutputInterface $output The output Interface
* @param string $outputDir The directory where to build
* @param array $config The parameters from ./satis.json
* @param bool $skipErrors Escapes Exceptions if true
*/
public function __construct(OutputInterface $output, $outputDir, $config, $skipErrors)
{
parent::__construct($output, $outputDir, $config, $skipErrors);
$this->filename = $this->outputDir.'/packages.json';
$this->includeFileName = isset($config['include-filename']) ? $config['include-filename'] : 'include/all${sha1}.json';
}
/**
* Builds the JSON stuff of the repository.
*
* @param \Composer\Package\PackageInterface[] $packages List of packages to dump
*/
public function dump(array $packages)
{
$packagesByName = array();
$dumper = new ArrayDumper();
foreach ($packages as $package) {
$packagesByName[$package->getName()][$package->getPrettyVersion()] = $dumper->dump($package);
}
$repo = array('packages' => array());
if (isset($this->config['providers']) && $this->config['providers']) {
$providersUrl = 'p/%package%$%hash%.json';
if (!empty($this->config['homepage'])) {
$repo['providers-url'] = parse_url(rtrim($this->config['homepage'], '/'), PHP_URL_PATH) . '/' . $providersUrl;
} else {
$repo['providers-url'] = $providersUrl;
}
$repo['providers'] = array();
$i = 1;
foreach ($packagesByName as $packageName => $versionPackages) {
foreach ($versionPackages as $version => &$versionPackage) {
$versionPackage['uid'] = $i++;
}
$includes = $this->dumpPackageIncludeJson(
array($packageName => $versionPackages),
str_replace('%package%', $packageName, $providersUrl),
'sha256'
);
$repo['providers'][$packageName] = current($includes);
}
} else {
$repo['includes'] = $this->dumpPackageIncludeJson($packagesByName, 'include/all$%hash%.json');
}
$this->dumpPackagesJson($repo);
$this->pruneIncludeDirectories();
}
/**
* Remove all files matching the includeUrl pattern next to just created include jsons
*/
private function pruneIncludeDirectories()
{
$this->output->writeln("<info>Pruning include directories</info>");
$paths = array();
while ($this->writtenIncludeJsons) {
list($hash, $includesUrl) = array_shift($this->writtenIncludeJsons);
$path = $this->outputDir . '/' . ltrim($includesUrl, '/');
$dirname = dirname($path);
$basename = basename($path);
if (strpos($dirname, '%hash%') !== false) {
throw new \RuntimeException('Refusing to prune when %hash% is in dirname');
}
$pattern = '#^' . str_replace('%hash%', '([0-9a-zA-Z]{' . strlen($hash) . '})', preg_quote($basename, '#')) . '$#';
$paths[$dirname][] = array($pattern, $hash);
}
foreach ($paths as $dirname => $entries) {
foreach (new \DirectoryIterator($dirname) as $file) {
foreach ($entries as $entry) {
list($pattern, $hash) = $entry;
if(preg_match($pattern, $file->getFilename(), $matches) && $matches[1] !== $hash) {
unlink($file->getPathname());
$this->output->writeln('<comment>Deleted ' . $file->getPathname() . '</comment>');
}
}
}
}
}
/**
* Writes includes JSON Files.
*
* @param array $packages List of packages to dump
* @param string $includesUrl The includes url (optionally containing %hash%)
* @param string $hashAlgorithm Hash algorithm {@see hash()}
*
* @return array The object for includes key in packages.json
*/
private function dumpPackageIncludeJson(array $packages, $includesUrl, $hashAlgorithm = 'sha1')
{
$filename = str_replace('%hash%', 'prep', $includesUrl);
$path = $tmpPath = $this->outputDir . '/' . ltrim($filename, '/');
$repoJson = new JsonFile($path);
$contents = $repoJson->encode(array('packages' => $packages)) . "\n";
$hash = hash($hashAlgorithm, $contents);
if (strpos($includesUrl, '%hash%') !== false) {
$this->writtenIncludeJsons[] = array($hash, $includesUrl);
$filename = str_replace('%hash%', $hash, $includesUrl);
if (file_exists($path = $this->outputDir . '/' . ltrim($filename, '/'))) {
// When the file exists, we don't need to override it as we assume,
// the contents satisfy the hash
$path = null;
}
}
if ($path) {
$this->writeToFile($path, $contents);
$this->output->writeln("<info>wrote packages to $path</info>");
}
return array(
$filename => array($hashAlgorithm => $hash)
);
}
/**
* Write to a file
*
* @param string $path
* @param string $contents
*/
private function writeToFile($path, $contents)
{
$dir = dirname($path);
if (!is_dir($dir)) {
if (file_exists($dir)) {
throw new \UnexpectedValueException(
$dir.' exists and is not a directory.'
);
}
if (!@mkdir($dir, 0777, true)) {
throw new \UnexpectedValueException(
$dir.' does not exist and could not be created.'
);
}
}
$retries = 3;
while ($retries--) {
try {
file_put_contents($path, $contents);
break;
} catch (\Exception $e) {
if ($retries) {
usleep(500000);
continue;
}
throw $e;
}
}
}
/**
* Writes the packages.json of the repository.
*
* @param array $repo Repository information.
*/
private function dumpPackagesJson($repo)
{
if (isset($this->config['notify-batch'])) {
$repo['notify-batch'] = $this->config['notify-batch'];
}
$this->output->writeln('<info>Writing packages.json</info>');
$repoJson = new JsonFile($this->filename);
$repoJson->write($repo);
}
}