28b705069ed8d9f01c9534362bdb381b2ab73619
8 * The extension packaging command executed by bin/pharext
10 class Packager
implements Command
15 * Extension source directory
16 * @var pharext\SourceDir
23 public function __construct() {
24 $this->args
= new CliArgs([
25 ["h", "help", "Display this help",
26 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
27 [null, "signature", "Dump signature",
28 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
29 ["v", "verbose", "More output",
30 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
31 ["q", "quiet", "Less output",
32 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
33 ["n", "name", "Extension name",
34 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
35 ["r", "release", "Extension release version",
36 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
37 ["s", "source", "Extension source directory",
38 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
39 ["g", "git", "Use `git ls-files` instead of the standard ignore filter",
40 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
41 ["p", "pecl", "Use PECL package.xml instead of the standard ignore filter",
42 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
43 ["d", "dest", "Destination directory",
44 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
46 ["z", "gzip", "Create additional PHAR compressed with gzip",
47 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
48 ["Z", "bzip", "Create additional PHAR compressed with bzip",
49 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
50 ["S", "sign", "Sign the *.ext.phar with a private key",
51 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
]
57 * @see \pharext\Command::run()
59 public function run($argc, array $argv) {
61 $prog = array_shift($argv);
62 foreach ($this->args
->parse(--$argc, $argv) as $error) {
66 if ($this->args
["help"]) {
71 if ($this->args
["signature"]) {
72 exit($this->signature($prog));
76 if ($this->args
["source"]) {
77 if ($this->args
["pecl"]) {
78 $this->source
= new PeclSourceDir($this, $this->args
["source"]);
79 } elseif ($this->args
["git"]) {
80 $this->source
= new GitSourceDir($this, $this->args
["source"]);
81 } elseif (realpath($this->args
["source"]."/pharext_package.php")) {
82 $this->source
= new PharextSourceDir($this, $this->args
["source"]);
84 $this->source
= new FilteredSourceDir($this, $this->args
["source"]);
87 } catch (\Exception
$e) {
88 $errs[] = $e->getMessage();
91 foreach ($this->args
->validate() as $error) {
96 if (!$this->args
["quiet"]) {
99 foreach ($errs as $err) {
100 $this->error("%s\n", $err);
103 if (!$this->args
["quiet"]) {
109 $this->createPackage();
112 function signature($prog) {
114 $sig = (new Phar(Phar
::running(false)))->getSignature();
115 printf("%s signature of %s\n%s", $sig["hash_type"], $prog,
116 chunk_split($sig["hash"], 64, "\n"));
118 } catch (\Exception
$e) {
119 $this->error("%s\n", $e->getMessage());
125 * Traverses all pharext source files to bundle
128 private function bundle() {
129 foreach (scandir(__DIR__
) as $entry) {
130 if (fnmatch("*.php", $entry)) {
131 yield
"pharext/$entry" => __DIR__
."/$entry";
136 private function askpass($prompt = "Password:") {
137 system("stty -echo", $retval);
139 $this->error("Could not disable echo on the terminal\n");
141 printf("%s ", $prompt);
142 $pass = fgets(STDIN
, 1024);
144 if (substr($pass, -1) == "\n") {
145 $pass = substr($pass, 0, -1);
151 * Creates the extension phar
153 private function createPackage() {
155 $pkgtemp = $this->tempname($pkguniq, "phar");
156 $pkgdesc = "{$this->args->name}-{$this->args->release}";
158 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args
->verbose ?
"\n" : " ");
160 $package = new Phar($pkgtemp);
162 if ($this->args
->sign
) {
163 $this->info("\nUsing private key to sign phar ... \n");
164 $privkey = new Openssl\
PrivateKey(realpath($this->args
->sign
), $this->askpass());
165 $privkey->sign($package);
168 $package->startBuffering();
169 $package->buildFromIterator($this->source
, $this->source
->getBaseDir());
170 $package->buildFromIterator($this->bundle());
171 $package->addFile(__DIR__
."/../pharext_installer.php", "pharext_installer.php");
172 $package->setDefaultStub("pharext_installer.php");
173 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
174 $package->stopBuffering();
176 if (!chmod($pkgtemp, 0777)) {
178 } elseif ($this->args
->verbose
) {
179 $this->info("Created executable phar %s\n", $pkgtemp);
183 if ($this->args
->gzip
) {
184 $this->info("Compressing with gzip ... ");
186 $package->compress(Phar
::GZ
)
187 ->setDefaultStub("pharext_installer.php");
189 } catch (\Exception
$e) {
190 $this->error("%s\n", $e->getMessage());
193 if ($this->args
->bzip
) {
194 $this->info("Compressing with bzip ... ");
196 $package->compress(Phar
::BZ2
)
197 ->setDefaultStub("pharext_installer.php");
199 } catch (\Exception
$e) {
200 $this->error("%s\n", $e->getMessage());
205 } catch (\Exception
$e) {
206 $this->error("%s\n", $e->getMessage());
210 foreach (glob($pkgtemp."*") as $pkgtemp) {
211 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
212 $pkgname = $this->args
->dest
."/". basename($pkgfile);
213 $this->info("Finalizing %s ... ", $pkgname);
214 if (!rename($pkgtemp, $pkgname)) {
219 if ($this->args
->sign
&& isset($privkey)) {
220 $keyname = $this->args
->dest
."/". basename($pkgfile) . ".pubkey";
221 $this->info("Public Key %s ... ", $keyname);
223 $privkey->exportPublicKey($keyname);
225 } catch (\Exception
$e) {
226 $this->error("%s", $e->getMessage());