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