d3e4000a3f45c505829b336c8528f8a40e531553
[pharext/pharext] / src / pharext / Packager.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6 use PharData;
7 use pharext\Cli\Args as CliArgs;
8 use pharext\Cli\Command as CliCommand;
9
10 /**
11 * The extension packaging command executed by bin/pharext
12 */
13 class Packager implements Command
14 {
15 use CliCommand;
16
17 /**
18 * Extension source directory
19 * @var pharext\SourceDir
20 */
21 private $source;
22
23 /**
24 * Cleanups
25 * @var array
26 */
27 private $cleanup = [];
28
29 /**
30 * Create the command
31 */
32 public function __construct() {
33 $this->args = new CliArgs([
34 ["h", "help", "Display this help",
35 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
36 ["v", "verbose", "More output",
37 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
38 ["q", "quiet", "Less output",
39 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
40 ["n", "name", "Extension name",
41 CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
42 ["r", "release", "Extension release version",
43 CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
44 ["s", "source", "Extension source directory",
45 CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
46 ["g", "git", "Use `git ls-tree` to determine file list",
47 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
48 ["p", "pecl", "Use PECL package.xml to determine file list, name and release",
49 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
50 ["d", "dest", "Destination directory",
51 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
52 "."],
53 ["z", "gzip", "Create additional PHAR compressed with gzip",
54 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
55 ["Z", "bzip", "Create additional PHAR compressed with bzip",
56 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
57 ["S", "sign", "Sign the PHAR with a private key",
58 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
59 [null, "signature", "Dump signature",
60 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
61 ]);
62 }
63
64 /**
65 * Perform cleaniup
66 */
67 function __destruct() {
68 foreach ($this->cleanup as $cleanup) {
69 if (is_dir($cleanup)) {
70 $this->rm($cleanup);
71 } else {
72 unlink($cleanup);
73 }
74 }
75 }
76
77 /**
78 * @inheritdoc
79 * @see \pharext\Command::run()
80 */
81 public function run($argc, array $argv) {
82 $errs = [];
83 $prog = array_shift($argv);
84 foreach ($this->args->parse(--$argc, $argv) as $error) {
85 $errs[] = $error;
86 }
87
88 if ($this->args["help"]) {
89 $this->header();
90 $this->help($prog);
91 exit;
92 }
93 if ($this->args["signature"]) {
94 exit($this->signature($prog));
95 }
96
97 try {
98 /* source needs to be evaluated before CliArgs validation,
99 * so e.g. name and version can be overriden and CliArgs
100 * does not complain about missing arguments
101 */
102 if ($this->args["source"]) {
103 $source = $this->localize($this->args["source"]);
104 if ($this->args["pecl"]) {
105 $this->source = new SourceDir\Pecl($this, $source);
106 } elseif ($this->args["git"]) {
107 $this->source = new SourceDir\Git($this, $source);
108 } else {
109 $this->source = new SourceDir\Pharext($this, $source);
110 }
111 }
112 } catch (\Exception $e) {
113 $errs[] = $e->getMessage();
114 }
115
116 foreach ($this->args->validate() as $error) {
117 $errs[] = $error;
118 }
119
120 if ($errs) {
121 if (!$this->args["quiet"]) {
122 if (!headers_sent()) {
123 /* only display header, if we didn't generate any output yet */
124 $this->header();
125 }
126 }
127 foreach ($errs as $err) {
128 $this->error("%s\n", $err);
129 }
130 printf("\n");
131 if (!$this->args["quiet"]) {
132 $this->help($prog);
133 }
134 exit(1);
135 }
136
137 $this->createPackage();
138 }
139
140 /**
141 * Dump program signature
142 * @param string $prog
143 * @return int exit code
144 */
145 function signature($prog) {
146 try {
147 $sig = (new Phar(Phar::running(false)))->getSignature();
148 printf("%s signature of %s\n%s", $sig["hash_type"], $prog,
149 chunk_split($sig["hash"], 64, "\n"));
150 return 0;
151 } catch (\Exception $e) {
152 $this->error("%s\n", $e->getMessage());
153 return 2;
154 }
155 }
156
157 /**
158 * Download remote source
159 * @param string $source
160 * @return string local source
161 */
162 private function download($source) {
163 $this->info("Fetching remote source %s ... ", $source);
164 if ($this->args["git"]) {
165 $local = new Tempdir("gitclone");
166 $cmd = new ExecCmd("git", $this->args->verbose);
167 $cmd->run(["clone", $source, $local]);
168 if (!$this->args->verbose) {
169 $this->info("OK\n");
170 }
171 } else {
172 $context = stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) {
173 switch ($notification) {
174 case STREAM_NOTIFY_CONNECT:
175 $this->debug("\n");
176 break;
177 case STREAM_NOTIFY_PROGRESS:
178 if ($bytes_max) {
179 $bytes_pct = $bytes_cur/$bytes_max;
180 $this->debug("\r %3d%% [%s>%s] ",
181 $bytes_pct*100,
182 str_repeat("=", round(70*$bytes_pct)),
183 str_repeat(" ", round(70*(1-$bytes_pct)))
184 );
185 }
186 break;
187 case STREAM_NOTIFY_COMPLETED:
188 /* this is not generated, why? */
189 break;
190 }
191 }]);
192 if (!$remote = fopen($source, "r", false, $context)) {
193 $this->error(null);
194 exit(2);
195 }
196 $local = new Tempfile("remote");
197 if (!stream_copy_to_stream($remote, $local->getStream())) {
198 $this->error(null);
199 exit(2);
200 }
201 $local->closeStream();
202 $this->info("OK\n");
203 }
204
205 $this->cleanup[] = $local;
206 return $local;
207 }
208
209 /**
210 * Extract local archive
211 * @param stirng $source
212 * @return string extracted directory
213 */
214 private function extract($source) {
215 $dest = new Tempdir("local");
216 $this->debug("Extracting %s to %s ... ", $source, $dest);
217 $archive = new PharData($source);
218 $archive->extractTo($dest);
219 $this->debug("OK\n");
220 $this->cleanup[] = $dest;
221 return $dest;
222 }
223
224 /**
225 * Localize a possibly remote source
226 * @param string $source
227 * @return string local source directory
228 */
229 private function localize($source) {
230 if (!stream_is_local($source)) {
231 $source = $this->download($source);
232 }
233 if (!is_dir($source)) {
234 $source = $this->extract($source);
235 if ($this->args["pecl"]) {
236 $this->debug("Sanitizing PECL dir ... ");
237 $dirs = glob("$source/*", GLOB_ONLYDIR);
238 $files = array_diff(glob("$source/*"), $dirs);
239 $source = current($dirs);
240 foreach ($files as $file) {
241 rename($file, "$source/" . basename($file));
242 }
243 $this->debug("OK\n");
244 }
245 }
246 return $source;
247 }
248
249 /**
250 * Traverses all pharext source files to bundle
251 * @return Generator
252 */
253 private function bundle() {
254 $rdi = new \RecursiveDirectoryIterator(__DIR__);
255 $rii = new \RecursiveIteratorIterator($rdi);
256 for ($rii->rewind(); $rii->valid(); $rii->next()) {
257 yield "pharext/". $rii->getSubPathname() => $rii->key();
258
259 }
260 }
261
262 /**
263 * Ask for password on the console
264 * @param string $prompt
265 * @return string password
266 */
267 private function askpass($prompt = "Password:") {
268 system("stty -echo", $retval);
269 if ($retval) {
270 $this->error("Could not disable echo on the terminal\n");
271 }
272 printf("%s ", $prompt);
273 $pass = fgets(STDIN, 1024);
274 system("stty echo");
275 if (substr($pass, -1) == "\n") {
276 $pass = substr($pass, 0, -1);
277 }
278 return $pass;
279 }
280
281 /**
282 * Creates the extension phar
283 */
284 private function createPackage() {
285 $pkguniq = uniqid();
286 $pkgtemp = sprintf("%s/%s.phar", sys_get_temp_dir(), $pkguniq);
287 $pkgdesc = "{$this->args->name}-{$this->args->release}";
288
289 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
290 try {
291 $package = new Phar($pkgtemp);
292
293 if ($this->args->sign) {
294 $this->info("\nUsing private key to sign phar ... \n");
295 $privkey = new Openssl\PrivateKey(realpath($this->args->sign), $this->askpass());
296 $privkey->sign($package);
297 }
298
299 $package->startBuffering();
300 $package->buildFromIterator($this->source, $this->source->getBaseDir());
301 $package->buildFromIterator($this->bundle(__DIR__));
302 $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php");
303 $package->setDefaultStub("pharext_installer.php");
304 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
305 $package->stopBuffering();
306
307 if (!chmod($pkgtemp, 0777)) {
308 $this->error(null);
309 } elseif ($this->args->verbose) {
310 $this->debug("Created executable phar %s\n", $pkgtemp);
311 } else {
312 $this->info("OK\n");
313 }
314 if ($this->args->gzip) {
315 $this->info("Compressing with gzip ... ");
316 try {
317 $package->compress(Phar::GZ)
318 ->setDefaultStub("pharext_installer.php");
319 $this->info("OK\n");
320 } catch (\Exception $e) {
321 $this->error("%s\n", $e->getMessage());
322 }
323 }
324 if ($this->args->bzip) {
325 $this->info("Compressing with bzip ... ");
326 try {
327 $package->compress(Phar::BZ2)
328 ->setDefaultStub("pharext_installer.php");
329 $this->info("OK\n");
330 } catch (\Exception $e) {
331 $this->error("%s\n", $e->getMessage());
332 }
333 }
334
335 unset($package);
336 } catch (\Exception $e) {
337 $this->error("%s\n", $e->getMessage());
338 exit(4);
339 }
340
341 foreach (glob($pkgtemp."*") as $pkgtemp) {
342 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
343 $pkgname = $this->args->dest ."/". basename($pkgfile);
344 $this->info("Finalizing %s ... ", $pkgname);
345 if (!rename($pkgtemp, $pkgname)) {
346 $this->error(null);
347 exit(5);
348 }
349 $this->info("OK\n");
350 if ($this->args->sign && isset($privkey)) {
351 $keyname = $this->args->dest ."/". basename($pkgfile) . ".pubkey";
352 $this->info("Public Key %s ... ", $keyname);
353 try {
354 $privkey->exportPublicKey($keyname);
355 $this->info("OK\n");
356 } catch (\Exception $e) {
357 $this->error("%s", $e->getMessage());
358 }
359 }
360 }
361 }
362 }