28b705069ed8d9f01c9534362bdb381b2ab73619
[pharext/pharext] / src / pharext / Packager.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6
7 /**
8 * The extension packaging command executed by bin/pharext
9 */
10 class Packager implements Command
11 {
12 use CliCommand;
13
14 /**
15 * Extension source directory
16 * @var pharext\SourceDir
17 */
18 private $source;
19
20 /**
21 * Create the command
22 */
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,
45 "."],
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]
52 ]);
53 }
54
55 /**
56 * @inheritdoc
57 * @see \pharext\Command::run()
58 */
59 public function run($argc, array $argv) {
60 $errs = [];
61 $prog = array_shift($argv);
62 foreach ($this->args->parse(--$argc, $argv) as $error) {
63 $errs[] = $error;
64 }
65
66 if ($this->args["help"]) {
67 $this->header();
68 $this->help($prog);
69 exit;
70 }
71 if ($this->args["signature"]) {
72 exit($this->signature($prog));
73 }
74
75 try {
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"]);
83 } else {
84 $this->source = new FilteredSourceDir($this, $this->args["source"]);
85 }
86 }
87 } catch (\Exception $e) {
88 $errs[] = $e->getMessage();
89 }
90
91 foreach ($this->args->validate() as $error) {
92 $errs[] = $error;
93 }
94
95 if ($errs) {
96 if (!$this->args["quiet"]) {
97 $this->header();
98 }
99 foreach ($errs as $err) {
100 $this->error("%s\n", $err);
101 }
102 printf("\n");
103 if (!$this->args["quiet"]) {
104 $this->help($prog);
105 }
106 exit(1);
107 }
108
109 $this->createPackage();
110 }
111
112 function signature($prog) {
113 try {
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"));
117 return 0;
118 } catch (\Exception $e) {
119 $this->error("%s\n", $e->getMessage());
120 return 2;
121 }
122 }
123
124 /**
125 * Traverses all pharext source files to bundle
126 * @return Generator
127 */
128 private function bundle() {
129 foreach (scandir(__DIR__) as $entry) {
130 if (fnmatch("*.php", $entry)) {
131 yield "pharext/$entry" => __DIR__."/$entry";
132 }
133 }
134 }
135
136 private function askpass($prompt = "Password:") {
137 system("stty -echo", $retval);
138 if ($retval) {
139 $this->error("Could not disable echo on the terminal\n");
140 }
141 printf("%s ", $prompt);
142 $pass = fgets(STDIN, 1024);
143 system("stty echo");
144 if (substr($pass, -1) == "\n") {
145 $pass = substr($pass, 0, -1);
146 }
147 return $pass;
148 }
149
150 /**
151 * Creates the extension phar
152 */
153 private function createPackage() {
154 $pkguniq = uniqid();
155 $pkgtemp = $this->tempname($pkguniq, "phar");
156 $pkgdesc = "{$this->args->name}-{$this->args->release}";
157
158 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
159 try {
160 $package = new Phar($pkgtemp);
161
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);
166 }
167
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();
175
176 if (!chmod($pkgtemp, 0777)) {
177 $this->error(null);
178 } elseif ($this->args->verbose) {
179 $this->info("Created executable phar %s\n", $pkgtemp);
180 } else {
181 $this->info("OK\n");
182 }
183 if ($this->args->gzip) {
184 $this->info("Compressing with gzip ... ");
185 try {
186 $package->compress(Phar::GZ)
187 ->setDefaultStub("pharext_installer.php");
188 $this->info("OK\n");
189 } catch (\Exception $e) {
190 $this->error("%s\n", $e->getMessage());
191 }
192 }
193 if ($this->args->bzip) {
194 $this->info("Compressing with bzip ... ");
195 try {
196 $package->compress(Phar::BZ2)
197 ->setDefaultStub("pharext_installer.php");
198 $this->info("OK\n");
199 } catch (\Exception $e) {
200 $this->error("%s\n", $e->getMessage());
201 }
202 }
203
204 unset($package);
205 } catch (\Exception $e) {
206 $this->error("%s\n", $e->getMessage());
207 exit(4);
208 }
209
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)) {
215 $this->error(null);
216 exit(5);
217 }
218 $this->info("OK\n");
219 if ($this->args->sign && isset($privkey)) {
220 $keyname = $this->args->dest ."/". basename($pkgfile) . ".pubkey";
221 $this->info("Public Key %s ... ", $keyname);
222 try {
223 $privkey->exportPublicKey($keyname);
224 $this->info("OK\n");
225 } catch (\Exception $e) {
226 $this->error("%s", $e->getMessage());
227 }
228 }
229 }
230 }
231 }