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