7 use pharext\Cli\Args
as CliArgs
;
8 use pharext\Cli\Command
as CliCommand
;
11 * The extension packaging command executed by bin/pharext
13 class Packager
implements Command
18 * Extension source directory
19 * @var pharext\SourceDir
27 private $cleanup = [];
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
,
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
],
67 function __destruct() {
68 foreach ($this->cleanup
as $cleanup) {
69 if (is_dir($cleanup)) {
79 * @see \pharext\Command::run()
81 public function run($argc, array $argv) {
83 $prog = array_shift($argv);
84 foreach ($this->args
->parse(--$argc, $argv) as $error) {
88 if ($this->args
["help"]) {
93 if ($this->args
["signature"]) {
94 exit($this->signature($prog));
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
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);
109 $this->source
= new SourceDir\
Pharext($this, $source);
112 } catch (\Exception
$e) {
113 $errs[] = $e->getMessage();
116 foreach ($this->args
->validate() as $error) {
121 if (!$this->args
["quiet"]) {
122 if (!headers_sent()) {
123 /* only display header, if we didn't generate any output yet */
127 foreach ($errs as $err) {
128 $this->error("%s\n", $err);
131 if (!$this->args
["quiet"]) {
137 $this->createPackage();
141 * Dump program signature
142 * @param string $prog
143 * @return int exit code
145 function signature($prog) {
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"));
151 } catch (\Exception
$e) {
152 $this->error("%s\n", $e->getMessage());
158 * Download remote source
159 * @param string $source
160 * @return string local source
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
) {
172 $context = stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) {
173 switch ($notification) {
174 case STREAM_NOTIFY_CONNECT
:
177 case STREAM_NOTIFY_PROGRESS
:
179 $bytes_pct = $bytes_cur/$bytes_max;
180 $this->debug("\r %3d%% [%s>%s] ",
182 str_repeat("=", round(70*$bytes_pct)),
183 str_repeat(" ", round(70*(1-$bytes_pct)))
187 case STREAM_NOTIFY_COMPLETED
:
188 /* this is not generated, why? */
192 if (!$remote = fopen($source, "r", false, $context)) {
196 $local = new Tempfile("remote");
197 if (!stream_copy_to_stream($remote, $local->getStream())) {
201 $local->closeStream();
205 $this->cleanup
[] = $local;
210 * Extract local archive
211 * @param stirng $source
212 * @return string extracted directory
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;
225 * Localize a possibly remote source
226 * @param string $source
227 * @return string local source directory
229 private function localize($source) {
230 if (!stream_is_local($source)) {
231 $source = $this->download($source);
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));
243 $this->debug("OK\n");
250 * Traverses all pharext source files to bundle
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();
263 * Ask for password on the console
264 * @param string $prompt
265 * @return string password
267 private function askpass($prompt = "Password:") {
268 system("stty -echo", $retval);
270 $this->error("Could not disable echo on the terminal\n");
272 printf("%s ", $prompt);
273 $pass = fgets(STDIN
, 1024);
275 if (substr($pass, -1) == "\n") {
276 $pass = substr($pass, 0, -1);
282 * Creates the extension phar
284 private function createPackage() {
286 $pkgtemp = sprintf("%s/%s.phar", sys_get_temp_dir(), $pkguniq);
287 $pkgdesc = "{$this->args->name}-{$this->args->release}";
289 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
291 $package = new Phar($pkgtemp);
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);
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();
307 if (!chmod($pkgtemp, 0777)) {
309 } elseif ($this->args
->verbose
) {
310 $this->debug("Created executable phar %s\n", $pkgtemp);
314 if ($this->args
->gzip
) {
315 $this->info("Compressing with gzip ... ");
317 $package->compress(Phar
::GZ
)
318 ->setDefaultStub("pharext_installer.php");
320 } catch (\Exception
$e) {
321 $this->error("%s\n", $e->getMessage());
324 if ($this->args
->bzip
) {
325 $this->info("Compressing with bzip ... ");
327 $package->compress(Phar
::BZ2
)
328 ->setDefaultStub("pharext_installer.php");
330 } catch (\Exception
$e) {
331 $this->error("%s\n", $e->getMessage());
336 } catch (\Exception
$e) {
337 $this->error("%s\n", $e->getMessage());
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)) {
350 if ($this->args
->sign
&& isset($privkey)) {
351 $keyname = $this->args
->dest
."/". basename($pkgfile) . ".pubkey";
352 $this->info("Public Key %s ... ", $keyname);
354 $privkey->exportPublicKey($keyname);
356 } catch (\Exception
$e) {
357 $this->error("%s", $e->getMessage());