8b7ad226ed48de9b15a6458c3a8c7053ea3169b1
[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 ["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 ["E", "zend", "Mark as Zend Extension",
60 CliArgs::OPTIONAL|CliARgs::SINGLE|CliArgs::NOARG],
61 [null, "signature", "Show pharext signature",
62 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
63 [null, "license", "Show pharext license",
64 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
65 [null, "version", "Show pharext version",
66 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::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 CliArgs validation,
109 * so e.g. name and version can be overriden and CliArgs
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);
146 } else {
147 $task = new Task\StreamFetch($source, function($bytes_pct) {
148 $this->info(" %3d%% [%s>%s] \r%s",
149 floor($bytes_pct*100),
150 str_repeat("=", round(50*$bytes_pct)),
151 str_repeat(" ", round(50*(1-$bytes_pct))),
152 $bytes_pct == 1 ? "\n":""
153 );
154 });
155 }
156 $local = $task->run($this->verbosity());
157
158 $this->cleanup[] = new Task\Cleanup($local);
159 return $local;
160 }
161
162 /**
163 * Extract local archive
164 * @param stirng $source
165 * @return string extracted directory
166 */
167 private function extract($source) {
168 $task = new Task\Extract($source);
169 $dest = $task->run($this->verbosity());
170
171 $this->cleanup[] = new Task\Cleanup($dest);
172 return $dest;
173 }
174
175 /**
176 * Localize a possibly remote source
177 * @param string $source
178 * @return string local source directory
179 */
180 private function localize($source) {
181 if (!stream_is_local($source)) {
182 $source = $this->download($source);
183 $this->cleanup[] = new Task\Cleanup($source);
184 }
185 $source = realpath($source);
186 if (!is_dir($source)) {
187 $source = $this->extract($source);
188 $this->cleanup[] = new Task\Cleanup($source);
189
190 if (!$this->args->git) {
191 $source = (new Task\PeclFixup($source))->run($this->verbosity());
192 }
193 }
194 return $source;
195 }
196
197 /**
198 * Load the source dir
199 * @throws \pharext\Exception
200 */
201 private function loadSource(){
202 if ($this->args["source"]) {
203 $source = $this->localize($this->args["source"]);
204
205 if ($this->args["pecl"]) {
206 $this->source = new SourceDir\Pecl($source);
207 } elseif ($this->args["git"]) {
208 $this->source = new SourceDir\Git($source);
209 } elseif (is_file("$source/parext_package.php")) {
210 $this->source = include "$source/pharext_package.php";
211 } else {
212 $this->source = new SourceDir\Basic($source);
213 }
214
215 if (!$this->source instanceof SourceDir) {
216 throw new Exception("Unknown source dir $source");
217 }
218
219 foreach ($this->source->getPackageInfo() as $key => $val) {
220 $this->args->$key = $val;
221 }
222 }
223 }
224
225 /**
226 * Creates the extension phar
227 */
228 private function createPackage() {
229 try {
230 $meta = array_merge($this->metadata(), [
231 "date" => date("Y-m-d"),
232 "name" => $this->args->name,
233 "release" => $this->args->release,
234 "license" => @file_get_contents(current(glob($this->source->getBaseDir()."/LICENSE*"))),
235 "stub" => "pharext_installer.php",
236 "type" => $this->args->zend ? "zend_extension" : "extension",
237 ]);
238 $file = (new Task\PharBuild($this->source, $meta))->run($this->verbosity());
239 } catch (\Exception $e) {
240 $this->error("%s\n", $e->getMessage());
241 exit(self::EBUILD);
242 }
243
244 try {
245 if ($this->args->sign) {
246 $this->info("Using private key to sign phar ...\n");
247 $pass = (new Task\Askpass)->run($this->verbosity());
248 $sign = new Task\PharSign($file, $this->args->sign, $pass);
249 $pkey = $sign->run($this->verbosity());
250 }
251
252 } catch (\Exception $e) {
253 $this->error("%s\n", $e->getMessage());
254 exit(self::ESIGN);
255 }
256
257 if ($this->args->gzip) {
258 try {
259 $gzip = (new Task\PharCompress($file, Phar::GZ))->run();
260 $move = new Task\PharRename($gzip, $this->args->dest, $this->args->name ."-". $this->args->release);
261 $name = $move->run($this->verbosity());
262
263 $this->info("Created gzipped phar %s\n", $name);
264
265 if ($this->args->sign) {
266 $sign = new Task\PharSign($name, $this->args->sign, $pass);
267 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
268 }
269
270 } catch (\Exception $e) {
271 $this->warn("%s\n", $e->getMessage());
272 }
273 }
274
275 if ($this->args->bzip) {
276 try {
277 $bzip = (new Task\PharCompress($file, Phar::BZ2))->run();
278 $move = new Task\PharRename($bzip, $this->args->dest, $this->args->name ."-". $this->args->release);
279 $name = $move->run($this->verbosity());
280
281 $this->info("Created bzipped phar %s\n", $name);
282
283 if ($this->args->sign) {
284 $sign = new Task\PharSign($name, $this->args->sign, $pass);
285 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
286 }
287
288 } catch (\Exception $e) {
289 $this->warn("%s\n", $e->getMessage());
290 }
291 }
292
293 try {
294 $move = new Task\PharRename($file, $this->args->dest, $this->args->name ."-". $this->args->release);
295 $name = $move->run($this->verbosity());
296
297 $this->info("Created executable phar %s\n", $name);
298
299 if (isset($pkey)) {
300 $pkey->exportPublicKey($name.".pubkey");
301 }
302
303 } catch (\Exception $e) {
304 $this->error("%s\n", $e->getMessage());
305 exit(self::EBUILD);
306 }
307 }
308 }