support git clones and (PECL) package archives as sources
[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 if ($this->args["source"]) {
99 $source = $this->localize($this->args["source"]);
100 if ($this->args["pecl"]) {
101 $this->source = new SourceDir\Pecl($this, $source);
102 } elseif ($this->args["git"]) {
103 $this->source = new SourceDir\Git($this, $source);
104 } else {
105 $this->source = new SourceDir\Pharext($this, $source);
106 }
107 }
108 } catch (\Exception $e) {
109 $errs[] = $e->getMessage();
110 }
111
112 foreach ($this->args->validate() as $error) {
113 $errs[] = $error;
114 }
115
116 if ($errs) {
117 if (!$this->args["quiet"]) {
118 $this->header();
119 }
120 foreach ($errs as $err) {
121 $this->error("%s\n", $err);
122 }
123 printf("\n");
124 if (!$this->args["quiet"]) {
125 $this->help($prog);
126 }
127 exit(1);
128 }
129
130 $this->createPackage();
131 }
132
133 /**
134 * Dump program signature
135 * @param string $prog
136 * @return int exit code
137 */
138 function signature($prog) {
139 try {
140 $sig = (new Phar(Phar::running(false)))->getSignature();
141 printf("%s signature of %s\n%s", $sig["hash_type"], $prog,
142 chunk_split($sig["hash"], 64, "\n"));
143 return 0;
144 } catch (\Exception $e) {
145 $this->error("%s\n", $e->getMessage());
146 return 2;
147 }
148 }
149
150 /**
151 * Download remote source
152 * @param string $source
153 * @return string local source
154 */
155 private function download($source) {
156 if ($this->args["git"]) {
157 $local = $this->newtemp("gitclone");
158 $this->exec("git clone", "git", ["clone", $source, $local]);
159 $source = $local;
160 } else {
161 $this->info("Fetching remote source %s ... ", $source);
162 if (!$remote = fopen($source, "r")) {
163 $this->error(null);
164 exit(2);
165 }
166 $local = new Tempfile("remote");
167 if (!stream_copy_to_stream($remote, $local->getStream())) {
168 $this->error(null);
169 exit(2);
170 }
171 $local->closeStream();
172 $source = $local->getPathname();
173 $this->info("OK\n");
174 }
175
176 $this->cleanup[] = $local;
177 return $source;
178 }
179
180 /**
181 * Extract local archive
182 * @param stirng $source
183 * @return string extracted directory
184 */
185 private function extract($source) {
186 $dest = $this->newtemp("local");
187 $this->info("Extracting to %s ... ", $dest);
188 $archive = new PharData($source);
189 $archive->extractTo($dest);
190 $this->info("OK\n");
191 $this->cleanup[] = $dest;
192 return $dest;
193 }
194
195 /**
196 * Localize a possibly remote source
197 * @param string $source
198 * @return string local source directory
199 */
200 private function localize($source) {
201 if (!stream_is_local($source)) {
202 $source = $this->download($source);
203 }
204 if (!is_dir($source)) {
205 $source = $this->extract($source);
206 if ($this->args["pecl"]) {
207 $this->info("Sanitizing PECL dir ... ");
208 $dirs = glob("$source/*", GLOB_ONLYDIR);
209 $files = array_diff(glob("$source/*"), $dirs);
210 $source = current($dirs);
211 foreach ($files as $file) {
212 rename($file, "$source/" . basename($file));
213 }
214 $this->info("OK\n");
215 }
216 }
217 return $source;
218 }
219
220 /**
221 * Traverses all pharext source files to bundle
222 * @return Generator
223 */
224 private function bundle() {
225 $rdi = new \RecursiveDirectoryIterator(__DIR__);
226 $rii = new \RecursiveIteratorIterator($rdi);
227 for ($rii->rewind(); $rii->valid(); $rii->next()) {
228 yield "pharext/". $rii->getSubPathname() => $rii->key();
229
230 }
231 }
232
233 /**
234 * Ask for password on the console
235 * @param string $prompt
236 * @return string password
237 */
238 private function askpass($prompt = "Password:") {
239 system("stty -echo", $retval);
240 if ($retval) {
241 $this->error("Could not disable echo on the terminal\n");
242 }
243 printf("%s ", $prompt);
244 $pass = fgets(STDIN, 1024);
245 system("stty echo");
246 if (substr($pass, -1) == "\n") {
247 $pass = substr($pass, 0, -1);
248 }
249 return $pass;
250 }
251
252 /**
253 * Creates the extension phar
254 */
255 private function createPackage() {
256 $pkguniq = uniqid();
257 $pkgtemp = $this->tempname($pkguniq, "phar");
258 $pkgdesc = "{$this->args->name}-{$this->args->release}";
259
260 $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
261 try {
262 $package = new Phar($pkgtemp);
263
264 if ($this->args->sign) {
265 $this->info("\nUsing private key to sign phar ... \n");
266 $privkey = new Openssl\PrivateKey(realpath($this->args->sign), $this->askpass());
267 $privkey->sign($package);
268 }
269
270 $package->startBuffering();
271 $package->buildFromIterator($this->source, $this->source->getBaseDir());
272 $package->buildFromIterator($this->bundle(__DIR__));
273 $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php");
274 $package->setDefaultStub("pharext_installer.php");
275 $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
276 $package->stopBuffering();
277
278 if (!chmod($pkgtemp, 0777)) {
279 $this->error(null);
280 } elseif ($this->args->verbose) {
281 $this->info("Created executable phar %s\n", $pkgtemp);
282 } else {
283 $this->info("OK\n");
284 }
285 if ($this->args->gzip) {
286 $this->info("Compressing with gzip ... ");
287 try {
288 $package->compress(Phar::GZ)
289 ->setDefaultStub("pharext_installer.php");
290 $this->info("OK\n");
291 } catch (\Exception $e) {
292 $this->error("%s\n", $e->getMessage());
293 }
294 }
295 if ($this->args->bzip) {
296 $this->info("Compressing with bzip ... ");
297 try {
298 $package->compress(Phar::BZ2)
299 ->setDefaultStub("pharext_installer.php");
300 $this->info("OK\n");
301 } catch (\Exception $e) {
302 $this->error("%s\n", $e->getMessage());
303 }
304 }
305
306 unset($package);
307 } catch (\Exception $e) {
308 $this->error("%s\n", $e->getMessage());
309 exit(4);
310 }
311
312 foreach (glob($pkgtemp."*") as $pkgtemp) {
313 $pkgfile = str_replace($pkguniq, "{$pkgdesc}.ext", $pkgtemp);
314 $pkgname = $this->args->dest ."/". basename($pkgfile);
315 $this->info("Finalizing %s ... ", $pkgname);
316 if (!rename($pkgtemp, $pkgname)) {
317 $this->error(null);
318 exit(5);
319 }
320 $this->info("OK\n");
321 if ($this->args->sign && isset($privkey)) {
322 $keyname = $this->args->dest ."/". basename($pkgfile) . ".pubkey";
323 $this->info("Public Key %s ... ", $keyname);
324 try {
325 $privkey->exportPublicKey($keyname);
326 $this->info("OK\n");
327 } catch (\Exception $e) {
328 $this->error("%s", $e->getMessage());
329 }
330 }
331 }
332 }
333 }