-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDist.php
More file actions
144 lines (114 loc) · 4.03 KB
/
Dist.php
File metadata and controls
144 lines (114 loc) · 4.03 KB
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
<?php
namespace Phug\Split\Command;
use Phug\Split;
use Phug\Split\Command\Options\GitProgram;
use SimpleCli\Options\Verbose;
use SimpleCli\SimpleCli;
/**
* Create a distribution directory with each sub-package in it.
*/
class Dist extends Analyze
{
use Verbose, GitProgram;
/**
* @argument
*
* Output directory.
*
* @var string
*/
public $output = 'dist';
/**
* @option git-credentials
*
* Git credentials.
*
* @var string
*/
public $gitCredentials = '';
/**
* @option
*
* Composer API (by default repo.packagist.org URL used).
* %s pattern in the API is replaced by the package full name.
* It can be any local or remote path file_get_contents() is able to get.
*
* @var string
*/
public $api = 'https://repo.packagist.org/p2/%s.json';
/**
* @param Split|SimpleCli $cli
*
* @return bool
*/
public function run(SimpleCli $cli): bool
{
return $this->distribute($this->getSplitCli($cli));
}
protected function info(Split $cli, string $message): void
{
if ($this->verbose) {
$cli->writeLine($message, 'brown');
}
}
/**
* @SuppressWarnings(PHPMD.ErrorControlOperator)
*/
protected function distribute(Split $cli): bool
{
if (!$this->calculatePackagesTree($cli)) {
return false;
}
$this->remove($this->output);
$cli->chdir($this->directory);
@mkdir($this->output, 0777, true);
/** @psalm-var truthy-string|false $output */
$output = @realpath($this->output);
if (!$output) {
return $cli->error('Unable to create output directory.');
}
$this->output = $output;
if (!preg_match('/^\* (.+)$/m', $this->git('branch', [], '2>&1') ?: '', $branch)) {
return $cli->error('You must be on a branch in a git repository to run this command.');
}
$branch = $branch[1];
if (substr($branch, 0, 18) === '(HEAD detached at ') {
$branch = trim(explode("\n", $this->git('describe --contains --all HEAD', [], '2>&1') ?: '')[0]);
}
foreach ($this->getPackages() as $package) {
$this->distributePackage($cli, $package, $branch);
}
$cli->writeLine('Build distributed in '.$this->output, 'light_purple');
return true;
}
protected function distributePackage(Split $cli, array $package, string $branch): bool
{
$cli->chdir($this->directory);
$name = (string) $package['name'];
$data = (array) json_decode((string) file_get_contents(strtr($this->api, ['%s' => $name])), true);
$config = $data['packages'][$name] ?? [];
$config = $config['dev-master'] ?? next($config);
if (!isset($config['source']) || $config['source']['type'] !== 'git') {
return $cli->error("No git source found for the package $name");
}
$cli->writeLine("Build $name", 'light_purple');
$url = $config['source']['url'];
$directory = $this->output."/$name";
$cli->writeLine("git clone $url $directory", 'light_green');
if (strlen($this->gitCredentials)) {
[$protocol, $url] = array_pad(explode('://', $url, 2), 2, '');
$url = $protocol.'://'.$this->gitCredentials.'@'.$url;
}
$cli->gray();
$cli->write($this->git("clone -c advice.detachedHead=false $url $directory", [], '2>&1') ?: '');
$cli->discolor();
$cli->chdir($directory);
$this->info($cli, "git rev-parse --verify origin/$branch");
$branchRevision = trim($this->git("rev-parse --verify origin/$branch", [], '2>&1') ?: '');
$this->info($cli, ' => '.($branchRevision === '' ? 'no revision' : $branchRevision));
$option = preg_match('/^[0-9a-f]+$/i', $branchRevision) ? '' : ' -b';
$cli->writeLine("git checkout$option $branch", 'light_green');
$this->git("checkout$option $branch", [], '2>&1');
return true;
}
}