6 use pharext\Cli\Args
as CliArgs
;
7 use pharext\Cli\Command
as CliCommand
;
11 * The extension packaging command executed by bin/pharext
13 class Packager
implements Command
18 * Extension source directory
19 * @var pharext\SourceDir
27 private $cleanup = [];
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
,
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
],
75 function __destruct() {
76 foreach ($this->cleanup
as $cleanup) {
83 * @see \pharext\Command::run()
85 public function run($argc, array $argv) {
87 $prog = array_shift($argv);
88 foreach ($this->args
->parse(--$argc, $argv) as $error) {
92 if ($this->args
["help"]) {
98 foreach (["signature", "license", "version"] as $opt) {
99 if ($this->args
[$opt]) {
100 printf("%s\n", $this->metadata($opt));
104 } catch (\Exception
$e) {
105 $this->error("%s\n", $e->getMessage());
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
115 } catch (\Exception
$e) {
116 $errs[] = $e->getMessage();
119 foreach ($this->args
->validate() as $error) {
124 if (!$this->args
["quiet"]) {
127 foreach ($errs as $err) {
128 $this->error("%s\n", $err);
131 if (!$this->args
["quiet"]) {
137 $this->createPackage();
141 * Download remote source
142 * @param string $source
143 * @return string local source
145 private function download($source) {
146 if ($this->args
->git
) {
147 $task = new Task\
GitClone($source, $this->args
->branch
);
149 /* print newline only once */
151 $task = new Task\
StreamFetch($source, function($bytes_pct) use(&$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)))
158 if ($bytes_pct == 1) {
165 $local = $task->run($this->verbosity());
167 $this->cleanup
[] = new Task\
Cleanup($local);
172 * Extract local archive
173 * @param stirng $source
174 * @return string extracted directory
176 private function extract($source) {
178 $task = new Task\
Extract($source);
179 $dest = $task->run($this->verbosity());
180 } catch (\Exception
$e) {
181 if (false === strpos($e->getMessage(), "checksum mismatch")) {
184 $dest = (new Task\
PaxFixup($source))->run($this->verbosity());
187 $this->cleanup
[] = new Task\
Cleanup($dest);
192 * Localize a possibly remote source
193 * @param string $source
194 * @return string local source directory
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);
201 $source = realpath($source);
202 if (!is_dir($source)) {
203 $source = $this->extract($source);
204 $this->cleanup
[] = new Task\
Cleanup($source);
206 if (!$this->args
->git
) {
207 $source = (new Task\
PeclFixup($source))->run($this->verbosity());
214 * Load the source dir
215 * @throws \pharext\Exception
217 private function loadSource(){
218 if ($this->args
["source"]) {
219 $source = $this->localize($this->args
["source"]);
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";
228 $this->source
= new SourceDir\
Basic($source);
231 if (!$this->source
instanceof SourceDir
) {
232 throw new Exception("Unknown source dir $source");
235 foreach ($this->source
->getPackageInfo() as $key => $val) {
236 $this->args
->$key = $val;
242 * Creates the extension phar
244 private function createPackage() {
246 $meta = array_merge(Metadata
::all(), [
247 "name" => $this->args
->name
,
248 "release" => $this->args
->release
,
249 "license" => $this->source
->getLicense(),
250 "stub" => "pharext_installer.php",
251 "type" => $this->args
->zend ?
"zend_extension" : "extension",
253 $file = (new Task\
PharBuild($this->source
, $meta))->run($this->verbosity());
254 } catch (\Exception
$e) {
255 $this->error("%s\n", $e->getMessage());
260 if ($this->args
->sign
) {
261 $this->info("Using private key to sign phar ...\n");
262 $pass = (new Task\Askpass
)->run($this->verbosity());
263 $sign = new Task\
PharSign($file, $this->args
->sign
, $pass);
264 $pkey = $sign->run($this->verbosity());
267 } catch (\Exception
$e) {
268 $this->error("%s\n", $e->getMessage());
272 if ($this->args
->gzip
) {
274 $gzip = (new Task\
PharCompress($file, Phar
::GZ
))->run();
275 $move = new Task\
PharRename($gzip, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
276 $name = $move->run($this->verbosity());
278 $this->info("Created gzipped phar %s\n", $name);
280 if ($this->args
->sign
) {
281 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
282 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
285 } catch (\Exception
$e) {
286 $this->warn("%s\n", $e->getMessage());
290 if ($this->args
->bzip
) {
292 $bzip = (new Task\
PharCompress($file, Phar
::BZ2
))->run();
293 $move = new Task\
PharRename($bzip, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
294 $name = $move->run($this->verbosity());
296 $this->info("Created bzipped phar %s\n", $name);
298 if ($this->args
->sign
) {
299 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
300 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
303 } catch (\Exception
$e) {
304 $this->warn("%s\n", $e->getMessage());
309 $move = new Task\
PharRename($file, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
310 $name = $move->run($this->verbosity());
312 $this->info("Created executable phar %s\n", $name);
315 $pkey->exportPublicKey($name.".pubkey");
318 } catch (\Exception
$e) {
319 $this->error("%s\n", $e->getMessage());