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 ["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
,
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
],
73 function __destruct() {
74 foreach ($this->cleanup
as $cleanup) {
81 * @see \pharext\Command::run()
83 public function run($argc, array $argv) {
85 $prog = array_shift($argv);
86 foreach ($this->args
->parse(--$argc, $argv) as $error) {
90 if ($this->args
["help"]) {
96 foreach (["signature", "license", "version"] as $opt) {
97 if ($this->args
[$opt]) {
98 printf("%s\n", $this->metadata($opt));
102 } catch (\Exception
$e) {
103 $this->error("%s\n", $e->getMessage());
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
113 } catch (\Exception
$e) {
114 $errs[] = $e->getMessage();
117 foreach ($this->args
->validate() as $error) {
122 if (!$this->args
["quiet"]) {
125 foreach ($errs as $err) {
126 $this->error("%s\n", $err);
129 if (!$this->args
["quiet"]) {
135 $this->createPackage();
139 * Download remote source
140 * @param string $source
141 * @return string local source
143 private function download($source) {
144 if ($this->args
->git
) {
145 $task = new Task\
GitClone($source);
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":""
156 $local = $task->run($this->verbosity());
158 $this->cleanup
[] = new Task\
Cleanup($local);
163 * Extract local archive
164 * @param stirng $source
165 * @return string extracted directory
167 private function extract($source) {
169 $task = new Task\
Extract($source);
170 $dest = $task->run($this->verbosity());
171 } catch (\Exception
$e) {
172 if (false === strpos($e->getMessage(), "checksum mismatch")) {
175 $dest = (new Task\
PaxFixup($source))->run($this->verbosity());
178 $this->cleanup
[] = new Task\
Cleanup($dest);
183 * Localize a possibly remote source
184 * @param string $source
185 * @return string local source directory
187 private function localize($source) {
188 if (!stream_is_local($source)) {
189 $source = $this->download($source);
190 $this->cleanup
[] = new Task\
Cleanup($source);
192 $source = realpath($source);
193 if (!is_dir($source)) {
194 $source = $this->extract($source);
195 $this->cleanup
[] = new Task\
Cleanup($source);
197 if (!$this->args
->git
) {
198 $source = (new Task\
PeclFixup($source))->run($this->verbosity());
205 * Load the source dir
206 * @throws \pharext\Exception
208 private function loadSource(){
209 if ($this->args
["source"]) {
210 $source = $this->localize($this->args
["source"]);
212 if ($this->args
["pecl"]) {
213 $this->source
= new SourceDir\
Pecl($source);
214 } elseif ($this->args
["git"]) {
215 $this->source
= new SourceDir\
Git($source);
216 } elseif (is_file("$source/pharext_package.php")) {
217 $this->source
= include "$source/pharext_package.php";
219 $this->source
= new SourceDir\
Basic($source);
222 if (!$this->source
instanceof SourceDir
) {
223 throw new Exception("Unknown source dir $source");
226 foreach ($this->source
->getPackageInfo() as $key => $val) {
227 $this->args
->$key = $val;
233 * Creates the extension phar
235 private function createPackage() {
237 $meta = array_merge($this->metadata(), [
238 "date" => date("Y-m-d"),
239 "name" => $this->args
->name
,
240 "release" => $this->args
->release
,
241 "license" => @file_get_contents
(current(glob($this->source
->getBaseDir()."/LICENSE*"))),
242 "stub" => "pharext_installer.php",
243 "type" => $this->args
->zend ?
"zend_extension" : "extension",
245 $file = (new Task\
PharBuild($this->source
, $meta))->run($this->verbosity());
246 } catch (\Exception
$e) {
247 $this->error("%s\n", $e->getMessage());
252 if ($this->args
->sign
) {
253 $this->info("Using private key to sign phar ...\n");
254 $pass = (new Task\Askpass
)->run($this->verbosity());
255 $sign = new Task\
PharSign($file, $this->args
->sign
, $pass);
256 $pkey = $sign->run($this->verbosity());
259 } catch (\Exception
$e) {
260 $this->error("%s\n", $e->getMessage());
264 if ($this->args
->gzip
) {
266 $gzip = (new Task\
PharCompress($file, Phar
::GZ
))->run();
267 $move = new Task\
PharRename($gzip, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
268 $name = $move->run($this->verbosity());
270 $this->info("Created gzipped phar %s\n", $name);
272 if ($this->args
->sign
) {
273 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
274 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
277 } catch (\Exception
$e) {
278 $this->warn("%s\n", $e->getMessage());
282 if ($this->args
->bzip
) {
284 $bzip = (new Task\
PharCompress($file, Phar
::BZ2
))->run();
285 $move = new Task\
PharRename($bzip, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
286 $name = $move->run($this->verbosity());
288 $this->info("Created bzipped phar %s\n", $name);
290 if ($this->args
->sign
) {
291 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
292 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
295 } catch (\Exception
$e) {
296 $this->warn("%s\n", $e->getMessage());
301 $move = new Task\
PharRename($file, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
302 $name = $move->run($this->verbosity());
304 $this->info("Created executable phar %s\n", $name);
307 $pkey->exportPublicKey($name.".pubkey");
310 } catch (\Exception
$e) {
311 $this->error("%s\n", $e->getMessage());