support for running .ext.phars without ext/phar
[pharext/pharext] / src / pharext / Packager.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6 use pharext\Cli\Args as CliArgs;
7 use pharext\Cli\Command as CliCommand;
8 use pharext\Exception;
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 ["b", "branch", "Checkout this tag/branch",
49 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
50 ["p", "pecl", "Use PECL package.xml to determine file list, name and release",
51 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
52 ["d", "dest", "Destination directory",
53 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
54 "."],
55 ["z", "gzip", "Create additional PHAR compressed with gzip",
56 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
57 ["Z", "bzip", "Create additional PHAR compressed with bzip",
58 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
59 ["S", "sign", "Sign the PHAR with a private key",
60 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
61 ["E", "zend", "Mark as Zend Extension",
62 CliArgs::OPTIONAL|CliARgs::SINGLE|CliArgs::NOARG],
63 [null, "signature", "Show pharext signature",
64 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
65 [null, "license", "Show pharext license",
66 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
67 [null, "version", "Show pharext version",
68 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
69 ]);
70 }
71
72 /**
73 * Perform cleaniup
74 */
75 function __destruct() {
76 foreach ($this->cleanup as $cleanup) {
77 $cleanup->run();
78 }
79 }
80
81 /**
82 * @inheritdoc
83 * @see \pharext\Command::run()
84 */
85 public function run($argc, array $argv) {
86 $errs = [];
87 $prog = array_shift($argv);
88 foreach ($this->args->parse(--$argc, $argv) as $error) {
89 $errs[] = $error;
90 }
91
92 if ($this->args["help"]) {
93 $this->header();
94 $this->help($prog);
95 exit;
96 }
97 try {
98 foreach (["signature", "license", "version"] as $opt) {
99 if ($this->args[$opt]) {
100 printf("%s\n", $this->metadata($opt));
101 exit;
102 }
103 }
104 } catch (\Exception $e) {
105 $this->error("%s\n", $e->getMessage());
106 exit(self::EARGS);
107 }
108
109 try {
110 /* source needs to be evaluated before CliArgs validation,
111 * so e.g. name and version can be overriden and CliArgs
112 * does not complain about missing arguments
113 */
114 $this->loadSource();
115 } catch (\Exception $e) {
116 $errs[] = $e->getMessage();
117 }
118
119 foreach ($this->args->validate() as $error) {
120 $errs[] = $error;
121 }
122
123 if ($errs) {
124 if (!$this->args["quiet"]) {
125 $this->header();
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(self::EARGS);
135 }
136
137 $this->createPackage();
138 }
139
140 /**
141 * Download remote source
142 * @param string $source
143 * @return string local source
144 */
145 private function download($source) {
146 if ($this->args->git) {
147 $task = new Task\GitClone($source, $this->args->branch);
148 } else {
149 /* print newline only once */
150 $done = false;
151 $task = new Task\StreamFetch($source, function($bytes_pct) use(&$done) {
152 if (!$done) {
153 $this->info(" %3d%% [%s>%s] \r",
154 floor($bytes_pct*100),
155 str_repeat("=", round(50*$bytes_pct)),
156 str_repeat(" ", round(50*(1-$bytes_pct)))
157 );
158 if ($bytes_pct == 1) {
159 $done = true;
160 $this->info("\n");
161 }
162 }
163 });
164 }
165 $local = $task->run($this->verbosity());
166
167 $this->cleanup[] = new Task\Cleanup($local);
168 return $local;
169 }
170
171 /**
172 * Extract local archive
173 * @param stirng $source
174 * @return string extracted directory
175 */
176 private function extract($source) {
177 try {
178 $task = new Task\Extract($source);
179 $dest = $task->run($this->verbosity());
180 } catch (\Exception $e) {
181 if (false === strpos($e->getMessage(), "checksum mismatch")) {
182 throw $e;
183 }
184 $dest = (new Task\PaxFixup($source))->run($this->verbosity());
185 }
186
187 $this->cleanup[] = new Task\Cleanup($dest);
188 return $dest;
189 }
190
191 /**
192 * Localize a possibly remote source
193 * @param string $source
194 * @return string local source directory
195 */
196 private function localize($source) {
197 if (!stream_is_local($source) || ($this->args->git && isset($this->args->branch))) {
198 $source = $this->download($source);
199 $this->cleanup[] = new Task\Cleanup($source);
200 }
201 $source = realpath($source);
202 if (!is_dir($source)) {
203 $source = $this->extract($source);
204 $this->cleanup[] = new Task\Cleanup($source);
205
206 if (!$this->args->git) {
207 $source = (new Task\PeclFixup($source))->run($this->verbosity());
208 }
209 }
210 return $source;
211 }
212
213 /**
214 * Load the source dir
215 * @throws \pharext\Exception
216 */
217 private function loadSource(){
218 if ($this->args["source"]) {
219 $source = $this->localize($this->args["source"]);
220
221 if ($this->args["pecl"]) {
222 $this->source = new SourceDir\Pecl($source);
223 } elseif ($this->args["git"]) {
224 $this->source = new SourceDir\Git($source);
225 } elseif (is_file("$source/pharext_package.php")) {
226 $this->source = include "$source/pharext_package.php";
227 } else {
228 $this->source = new SourceDir\Basic($source);
229 }
230
231 if (!$this->source instanceof SourceDir) {
232 throw new Exception("Unknown source dir $source");
233 }
234
235 foreach ($this->source->getPackageInfo() as $key => $val) {
236 $this->args->$key = $val;
237 }
238 }
239 }
240
241 /**
242 * Creates the extension phar
243 */
244 private function createPackage() {
245 try {
246 $meta = array_merge(Metadata::all(), [
247 "name" => $this->args->name,
248 "release" => $this->args->release,
249 "license" => $this->source->getLicense(),
250 "type" => $this->args->zend ? "zend_extension" : "extension",
251 ]);
252 $file = (new Task\PharBuild($this->source, __DIR__."/../pharext_installer.php", $meta))->run($this->verbosity());
253 } catch (\Exception $e) {
254 $this->error("%s\n", $e->getMessage());
255 exit(self::EBUILD);
256 }
257
258 try {
259 if ($this->args->sign) {
260 $this->info("Using private key to sign phar ...\n");
261 $pass = (new Task\Askpass)->run($this->verbosity());
262 $sign = new Task\PharSign($file, $this->args->sign, $pass);
263 $pkey = $sign->run($this->verbosity());
264 }
265
266 } catch (\Exception $e) {
267 $this->error("%s\n", $e->getMessage());
268 exit(self::ESIGN);
269 }
270
271 if ($this->args->gzip) {
272 try {
273 $gzip = (new Task\PharCompress($file, Phar::GZ))->run();
274 $move = new Task\PharRename($gzip, $this->args->dest, $this->args->name ."-". $this->args->release);
275 $name = $move->run($this->verbosity());
276
277 $this->info("Created gzipped phar %s\n", $name);
278
279 if ($this->args->sign) {
280 $sign = new Task\PharSign($name, $this->args->sign, $pass);
281 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
282 }
283
284 } catch (\Exception $e) {
285 $this->warn("%s\n", $e->getMessage());
286 }
287 }
288
289 if ($this->args->bzip) {
290 try {
291 $bzip = (new Task\PharCompress($file, Phar::BZ2))->run();
292 $move = new Task\PharRename($bzip, $this->args->dest, $this->args->name ."-". $this->args->release);
293 $name = $move->run($this->verbosity());
294
295 $this->info("Created bzipped phar %s\n", $name);
296
297 if ($this->args->sign) {
298 $sign = new Task\PharSign($name, $this->args->sign, $pass);
299 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
300 }
301
302 } catch (\Exception $e) {
303 $this->warn("%s\n", $e->getMessage());
304 }
305 }
306
307 try {
308 $move = new Task\PharRename($file, $this->args->dest, $this->args->name ."-". $this->args->release);
309 $name = $move->run($this->verbosity());
310
311 $this->info("Created executable phar %s\n", $name);
312
313 if (isset($pkey)) {
314 $pkey->exportPublicKey($name.".pubkey");
315 }
316
317 } catch (\Exception $e) {
318 $this->error("%s\n", $e->getMessage());
319 exit(self::EBUILD);
320 }
321 }
322 }