6 use pharext\Cli\Args
as CliArgs
;
7 use pharext\Cli\Command
as CliCommand
;
10 * The extension packaging command executed by bin/pharext
12 class Packager
implements Command
17 * Extension source directory
18 * @var pharext\SourceDir
26 private $cleanup = [];
31 public function __construct() {
32 $this->args
= new CliArgs([
33 ["h", "help", "Display this help",
34 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
35 ["v", "verbose", "More output",
36 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
37 ["q", "quiet", "Less output",
38 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
39 ["n", "name", "Extension name",
40 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
41 ["r", "release", "Extension release version",
42 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
43 ["s", "source", "Extension source directory",
44 CliArgs
::REQUIRED|CliArgs
::SINGLE|CliArgs
::REQARG
],
45 ["g", "git", "Use `git ls-tree` to determine file list",
46 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
47 ["p", "pecl", "Use PECL package.xml to determine file list, name and release",
48 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
49 ["d", "dest", "Destination directory",
50 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
,
52 ["z", "gzip", "Create additional PHAR compressed with gzip",
53 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
54 ["Z", "bzip", "Create additional PHAR compressed with bzip",
55 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG
],
56 ["S", "sign", "Sign the PHAR with a private key",
57 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::REQARG
],
58 [null, "signature", "Dump signature",
59 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
60 [null, "license", "Show license",
61 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
62 [null, "version", "Show version",
63 CliArgs
::OPTIONAL|CliArgs
::SINGLE|CliArgs
::NOARG|CliArgs
::HALT
],
70 function __destruct() {
71 foreach ($this->cleanup
as $cleanup) {
72 if (is_dir($cleanup)) {
74 } elseif (file_exists($cleanup)) {
82 * @see \pharext\Command::run()
84 public function run($argc, array $argv) {
86 $prog = array_shift($argv);
87 foreach ($this->args
->parse(--$argc, $argv) as $error) {
91 if ($this->args
["help"]) {
97 foreach (["signature", "license", "version"] as $opt) {
98 if ($this->args
[$opt]) {
99 printf("%s\n", $this->metadata($opt));
103 } catch (\Exception
$e) {
104 $this->error("%s\n", $e->getMessage());
109 /* source needs to be evaluated before CliArgs validation,
110 * so e.g. name and version can be overriden and CliArgs
111 * does not complain about missing arguments
113 if ($this->args
["source"]) {
114 $source = $this->localize($this->args
["source"]);
115 if ($this->args
["pecl"]) {
116 $this->source
= new SourceDir\
Pecl($this, $source);
117 } elseif ($this->args
["git"]) {
118 $this->source
= new SourceDir\
Git($this, $source);
120 $this->source
= new SourceDir\
Pharext($this, $source);
123 } catch (\Exception
$e) {
124 $errs[] = $e->getMessage();
127 foreach ($this->args
->validate() as $error) {
132 if (!$this->args
["quiet"]) {
135 foreach ($errs as $err) {
136 $this->error("%s\n", $err);
139 if (!$this->args
["quiet"]) {
145 $this->createPackage();
149 * Download remote source
150 * @param string $source
151 * @return string local source
153 private function download($source) {
154 $this->info("Fetching remote source %s ...\n", $source);
156 if ($this->args
->git
) {
157 $task = new Task\
GitClone($source);
159 $task = new Task\
StreamFetch($source, function($bytes_pct) {
160 $this->debug(" %3d%% [%s>%s] \r",
161 floor($bytes_pct*100),
162 str_repeat("=", round(50*$bytes_pct)),
163 str_repeat(" ", round(50*(1-$bytes_pct)))
167 $local = $task->run($this->args
->verbose
);
170 $this->cleanup
[] = $local;
175 * Extract local archive
176 * @param stirng $source
177 * @return string extracted directory
179 private function extract($source) {
180 $this->debug("Extracting %s ...\n", $source);
182 $task = new Task\
Extract($source);
183 $dest = $task->run($this->args
->verbose
);
185 $this->cleanup
[] = $dest;
190 * Localize a possibly remote source
191 * @param string $source
192 * @return string local source directory
194 private function localize($source) {
195 if (!stream_is_local($source)) {
196 $source = $this->download($source);
197 $this->cleanup
[] = $source;
199 $source = realpath($source);
200 if (!is_dir($source)) {
201 $source = $this->extract($source);
202 $this->cleanup
[] = $source;
204 if ($this->args
->pecl
) {
205 $this->debug("Sanitizing PECL dir ...\n");
206 $source = (new Task\
PeclFixup($source))->run($this->args
->verbose
);
213 * Creates the extension phar
215 private function createPackage() {
217 $meta = array_merge($this->metadata(), [
218 "date" => date("Y-m-d"),
219 "name" => $this->args
->name
,
220 "release" => $this->args
->release
,
221 "license" => @file_get_contents
(current(glob($this->source
->getBaseDir()."/LICENSE*"))),
222 "stub" => "pharext_installer.php",
224 $file = (new Task\
PharBuild($this->source
, $meta))->run();
226 if ($this->args
->sign
) {
227 $this->info("Using private key to sign phar ...\n");
228 $pass = (new Task\Askpass
)->run($this->args
->verbose
);
229 $sign = new Task\
PharSign($file, $this->args
->sign
, $pass);
230 $pkey = $sign->run($this->args
->verbose
);
233 } catch (\Exception
$e) {
234 $this->error("%s\n", $e->getMessage());
238 if ($this->args
->gzip
) {
240 $gzip = (new Task\
PharCompress($file, Phar
::GZ
))->run();
241 $move = new Task\
PharRename($gzip, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
242 $name = $move->run($this->args
->verbose
);
244 $this->info("Created gzipped phar %s\n", $name);
246 if ($this->args
->sign
) {
247 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
248 $sign->run($this->args
->verbose
)->exportPublicKey($name.".pubkey");
251 } catch (\Exception
$e) {
252 $this->warn("%s\n", $e->getMessage());
256 if ($this->args
->bzip
) {
258 $bzip = (new Task\
PharCompress($file, Phar
::BZ2
))->run();
259 $move = new Task\
PharRename($bzip, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
260 $name = $move->run($this->args
->verbose
);
262 $this->info("Created bzipped phar %s\n", $name);
264 if ($this->args
->sign
) {
265 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
266 $sign->run($this->args
->verbose
)->exportPublicKey($name.".pubkey");
269 } catch (\Exception
$e) {
270 $this->warn("%s\n", $e->getMessage());
275 $move = new Task\
PharRename($file, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
276 $name = $move->run($this->args
->verbose
);
278 $this->info("Created executable phar %s\n", $name);
281 $pkey->exportPublicKey($name.".pubkey");
284 } catch (\Exception
$e) {
285 $this->error("%s\n", $e->getMessage());