ba7de89100a9200623db6b8d1912139802d59e2d
[pharext/pharext] / src / pharext / Packager.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6 use pharext\Cli\Args as CliArgs;
7 use pharext\Cli\Command as CliCommand;
8
9 /**
10 * The extension packaging command executed by bin/pharext
11 */
12 class Packager implements Command
13 {
14 use CliCommand;
15
16 /**
17 * Extension source directory
18 * @var pharext\SourceDir
19 */
20 private $source;
21
22 /**
23 * Cleanups
24 * @var array
25 */
26 private $cleanup = [];
27
28 /**
29 * Create the command
30 */
31 public function __construct() {
32 $this->args = new CliArgs([
33 ["h", "help", "Display this help",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
35 ["v", "verbose", "More output",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
37 ["q", "quiet", "Less output",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
39 ["n", "name", "Extension name",
40 CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
41 ["r", "release", "Extension release version",
42 CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
43 ["s", "source", "Extension source directory",
44 CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
45 ["g", "git", "Use `git ls-tree` to determine file list",
46 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
47 ["p", "pecl", "Use PECL package.xml to determine file list, name and release",
48 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
49 ["d", "dest", "Destination directory",
50 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
51 "."],
52 ["z", "gzip", "Create additional PHAR compressed with gzip",
53 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
54 ["Z", "bzip", "Create additional PHAR compressed with bzip",
55 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
56 ["S", "sign", "Sign the PHAR with a private key",
57 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
58 [null, "signature", "Dump signature",
59 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
60 [null, "license", "Show license",
61 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
62 [null, "version", "Show version",
63 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
64 ]);
65 }
66
67 /**
68 * Perform cleaniup
69 */
70 function __destruct() {
71 foreach ($this->cleanup as $cleanup) {
72 if (is_dir($cleanup)) {
73 $this->rm($cleanup);
74 } elseif (file_exists($cleanup)) {
75 unlink($cleanup);
76 }
77 }
78 }
79
80 /**
81 * @inheritdoc
82 * @see \pharext\Command::run()
83 */
84 public function run($argc, array $argv) {
85 $errs = [];
86 $prog = array_shift($argv);
87 foreach ($this->args->parse(--$argc, $argv) as $error) {
88 $errs[] = $error;
89 }
90
91 if ($this->args["help"]) {
92 $this->header();
93 $this->help($prog);
94 exit;
95 }
96 try {
97 foreach (["signature", "license", "version"] as $opt) {
98 if ($this->args[$opt]) {
99 printf("%s\n", $this->metadata($opt));
100 exit;
101 }
102 }
103 } catch (\Exception $e) {
104 $this->error("%s\n", $e->getMessage());
105 exit(2);
106 }
107
108 try {
109 /* source needs to be evaluated before CliArgs validation,
110 * so e.g. name and version can be overriden and CliArgs
111 * does not complain about missing arguments
112 */
113 if ($this->args["source"]) {
114 $source = $this->localize($this->args["source"]);
115 if ($this->args["pecl"]) {
116 $this->source = new SourceDir\Pecl($this, $source);
117 } elseif ($this->args["git"]) {
118 $this->source = new SourceDir\Git($this, $source);
119 } else {
120 $this->source = new SourceDir\Pharext($this, $source);
121 }
122 }
123 } catch (\Exception $e) {
124 $errs[] = $e->getMessage();
125 }
126
127 foreach ($this->args->validate() as $error) {
128 $errs[] = $error;
129 }
130
131 if ($errs) {
132 if (!$this->args["quiet"]) {
133 $this->header();
134 }
135 foreach ($errs as $err) {
136 $this->error("%s\n", $err);
137 }
138 printf("\n");
139 if (!$this->args["quiet"]) {
140 $this->help($prog);
141 }
142 exit(1);
143 }
144
145 $this->createPackage();
146 }
147
148 /**
149 * Download remote source
150 * @param string $source
151 * @return string local source
152 */
153 private function download($source) {
154 $this->info("Fetching remote source %s ...\n", $source);
155
156 if ($this->args->git) {
157 $task = new Task\GitClone($source);
158 } else {
159 $task = new Task\StreamFetch($source, function($bytes_pct) {
160 $this->debug(" %3d%% [%s>%s] \r",
161 floor($bytes_pct*100),
162 str_repeat("=", round(50*$bytes_pct)),
163 str_repeat(" ", round(50*(1-$bytes_pct)))
164 );
165 });
166 }
167 $local = $task->run($this->args->verbose);
168 $this->debug("\n");
169
170 $this->cleanup[] = $local;
171 return $local;
172 }
173
174 /**
175 * Extract local archive
176 * @param stirng $source
177 * @return string extracted directory
178 */
179 private function extract($source) {
180 $this->debug("Extracting %s ...\n", $source);
181
182 $task = new Task\Extract($source);
183 $dest = $task->run($this->args->verbose);
184
185 $this->cleanup[] = $dest;
186 return $dest;
187 }
188
189 /**
190 * Localize a possibly remote source
191 * @param string $source
192 * @return string local source directory
193 */
194 private function localize($source) {
195 if (!stream_is_local($source)) {
196 $source = $this->download($source);
197 $this->cleanup[] = $source;
198 }
199 $source = realpath($source);
200 if (!is_dir($source)) {
201 $source = $this->extract($source);
202 $this->cleanup[] = $source;
203
204 if ($this->args->pecl) {
205 $this->debug("Sanitizing PECL dir ...\n");
206 $source = (new Task\PeclFixup($source))->run($this->args->verbose);
207 }
208 }
209 return $source;
210 }
211
212 /**
213 * Creates the extension phar
214 */
215 private function createPackage() {
216 try {
217 $meta = array_merge($this->metadata(), [
218 "date" => date("Y-m-d"),
219 "name" => $this->args->name,
220 "release" => $this->args->release,
221 "license" => @file_get_contents(current(glob($this->source->getBaseDir()."/LICENSE*"))),
222 "stub" => "pharext_installer.php",
223 ]);
224 $file = (new Task\PharBuild($this->source, $meta))->run();
225
226 if ($this->args->sign) {
227 $this->info("Using private key to sign phar ...\n");
228 $pass = (new Task\Askpass)->run($this->args->verbose);
229 $sign = new Task\PharSign($file, $this->args->sign, $pass);
230 $pkey = $sign->run($this->args->verbose);
231 }
232
233 } catch (\Exception $e) {
234 $this->error("%s\n", $e->getMessage());
235 exit(4);
236 }
237
238 if ($this->args->gzip) {
239 try {
240 $gzip = (new Task\PharCompress($file, Phar::GZ))->run();
241 $move = new Task\PharRename($gzip, $this->args->dest, $this->args->name ."-". $this->args->release);
242 $name = $move->run($this->args->verbose);
243
244 $this->info("Created gzipped phar %s\n", $name);
245
246 if ($this->args->sign) {
247 $sign = new Task\PharSign($name, $this->args->sign, $pass);
248 $sign->run($this->args->verbose)->exportPublicKey($name.".pubkey");
249 }
250
251 } catch (\Exception $e) {
252 $this->warn("%s\n", $e->getMessage());
253 }
254 }
255
256 if ($this->args->bzip) {
257 try {
258 $bzip = (new Task\PharCompress($file, Phar::BZ2))->run();
259 $move = new Task\PharRename($bzip, $this->args->dest, $this->args->name ."-". $this->args->release);
260 $name = $move->run($this->args->verbose);
261
262 $this->info("Created bzipped phar %s\n", $name);
263
264 if ($this->args->sign) {
265 $sign = new Task\PharSign($name, $this->args->sign, $pass);
266 $sign->run($this->args->verbose)->exportPublicKey($name.".pubkey");
267 }
268
269 } catch (\Exception $e) {
270 $this->warn("%s\n", $e->getMessage());
271 }
272 }
273
274 try {
275 $move = new Task\PharRename($file, $this->args->dest, $this->args->name ."-". $this->args->release);
276 $name = $move->run($this->args->verbose);
277
278 $this->info("Created executable phar %s\n", $name);
279
280 if (isset($pkey)) {
281 $pkey->exportPublicKey($name.".pubkey");
282 }
283
284 } catch (\Exception $e) {
285 $this->error("%s\n", $e->getMessage());
286 exit(4);
287 }
288 }
289 }