add ExecCmd and Tempdir
[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 if ($this->args["git"]) {
164 $this->info("Cloning %s ... ", $source);
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 $this->info("Fetching remote source %s ... ", $source);
173 if (!$remote = fopen($source, "r")) {
174 $this->error(null);
175 exit(2);
176 }
177 $local = new Tempfile("remote");
178 if (!stream_copy_to_stream($remote, $local->getStream())) {
179 $this->error(null);
180 exit(2);
181 }
182 $local->closeStream();
183 $this->info("OK\n");
184 }
185
186 $this->cleanup[] = $local;
187 return $local->getPathname();
188 }
189
190 /**
191 * Extract local archive
192 * @param stirng $source
193 * @return string extracted directory
194 */
195 private function extract($source) {
196 $dest = new Tempdir("local");
197 if ($this->args->verbose) {
198 $this->info("Extracting to %s ... ", $dest);
199 }
200 $archive = new PharData($source);
201 $archive->extractTo($dest);
202 $this->info("OK\n");
203 $this->cleanup[] = $dest;
204 return $dest;
205 }
206
207 /**
208 * Localize a possibly remote source
209 * @param string $source
210 * @return string local source directory
211 */
212 private function localize($source) {
213 if (!stream_is_local($source)) {
214 $source = $this->download($source);
215 }
216 if (!is_dir($source)) {
217 $source = $this->extract($source);
218 if ($this->args["pecl"]) {
219 $this->info("Sanitizing PECL dir ... ");
220 $dirs = glob("$source/*", GLOB_ONLYDIR);
221 $files = array_diff(glob("$source/*"), $dirs);
222 $source = current($dirs);
223 foreach ($files as $file) {
224 rename($file, "$source/" . basename($file));
225 }
226 $this->info("OK\n");
227 }
228 }
229 return $source;
230 }
231
232 /**
233 * Traverses all pharext source files to bundle
234 * @return Generator
235 */
236 private function bundle() {
237 $rdi = new \RecursiveDirectoryIterator(__DIR__);
238 $rii = new \RecursiveIteratorIterator($rdi);
239 for ($rii->rewind(); $rii->valid(); $rii->next()) {
240 yield "pharext/". $rii->getSubPathname() => $rii->key();
241
242 }
243 }
244
245 /**
246 * Ask for password on the console
247 * @param string $prompt
248 * @return string password
249 */
250 private function askpass($prompt = "Password:") {
251 system("stty -echo", $retval);
252 if ($retval) {
253 $this->error("Could not disable echo on the terminal\n");
254 }
255 printf("%s ", $prompt);
256 $pass = fgets(STDIN, 1024);
257 system("stty echo");
258 if (substr($pass, -1) == "\n") {
259 $pass = substr($pass, 0, -1);
260 }
261 return $pass;
262 }
263
264 /**
265 * Creates the extension phar
266 */
267 private function createPackage() {
268 $pkguniq = uniqid();
269 $pkgtemp = $this->tempname($pkguniq, "phar");
270 $pkgdesc = "{$this->args->name}-{$this->args->release}";
271
272 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
273 try {
274 $package = new Phar($pkgtemp);
275
276 if ($this->args->sign) {
277 $this->info("\nUsing private key to sign phar ... \n");
278 $privkey = new Openssl\PrivateKey(realpath($this->args->sign), $this->askpass());
279 $privkey->sign($package);
280 }
281
282 $package->startBuffering();
283 $package->buildFromIterator($this->source, $this->source->getBaseDir());
284 $package->buildFromIterator($this->bundle(__DIR__));
285 $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php");
286 $package->setDefaultStub("pharext_installer.php");
287 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
288 $package->stopBuffering();
289
290 if (!chmod($pkgtemp, 0777)) {
291 $this->error(null);
292 } elseif ($this->args->verbose) {
293 $this->info("Created executable phar %s\n", $pkgtemp);
294 } else {
295 $this->info("OK\n");
296 }
297 if ($this->args->gzip) {
298 $this->info("Compressing with gzip ... ");
299 try {
300 $package->compress(Phar::GZ)
301 ->setDefaultStub("pharext_installer.php");
302 $this->info("OK\n");
303 } catch (\Exception $e) {
304 $this->error("%s\n", $e->getMessage());
305 }
306 }
307 if ($this->args->bzip) {
308 $this->info("Compressing with bzip ... ");
309 try {
310 $package->compress(Phar::BZ2)
311 ->setDefaultStub("pharext_installer.php");
312 $this->info("OK\n");
313 } catch (\Exception $e) {
314 $this->error("%s\n", $e->getMessage());
315 }
316 }
317
318 unset($package);
319 } catch (\Exception $e) {
320 $this->error("%s\n", $e->getMessage());
321 exit(4);
322 }
323
324 foreach (glob($pkgtemp."*") as $pkgtemp) {
325 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
326 $pkgname = $this->args->dest ."/". basename($pkgfile);
327 $this->info("Finalizing %s ... ", $pkgname);
328 if (!rename($pkgtemp, $pkgname)) {
329 $this->error(null);
330 exit(5);
331 }
332 $this->info("OK\n");
333 if ($this->args->sign && isset($privkey)) {
334 $keyname = $this->args->dest ."/". basename($pkgfile) . ".pubkey";
335 $this->info("Public Key %s ... ", $keyname);
336 try {
337 $privkey->exportPublicKey($keyname);
338 $this->info("OK\n");
339 } catch (\Exception $e) {
340 $this->error("%s", $e->getMessage());
341 }
342 }
343 }
344 }
345 }