6 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 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
,
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
],
74 function __destruct() {
75 foreach ($this->cleanup
as $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 Cli\Args validation,
110 * so e.g. name and version can be overriden and Cli\Args
111 * does not complain about missing arguments
114 } catch (\Exception
$e) {
115 $errs[] = $e->getMessage();
118 foreach ($this->args
->validate() as $error) {
123 if (!$this->args
["quiet"]) {
126 foreach ($errs as $err) {
127 $this->error("%s\n", $err);
130 if (!$this->args
["quiet"]) {
136 $this->createPackage();
140 * Download remote source
141 * @param string $source
142 * @return string local source
144 private function download($source) {
145 if ($this->args
->git
) {
146 $task = new Task\
GitClone($source, $this->args
->branch
);
148 /* print newline only once */
150 $task = new Task\
StreamFetch($source, function($bytes_pct) use(&$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)))
157 if ($bytes_pct == 1) {
164 $local = $task->run($this->verbosity());
166 $this->cleanup
[] = new Task\
Cleanup($local);
171 * Extract local archive
172 * @param stirng $source
173 * @return string extracted directory
175 private function extract($source) {
177 $task = new Task\
Extract($source);
178 $dest = $task->run($this->verbosity());
179 } catch (\Exception
$e) {
180 if (false === strpos($e->getMessage(), "checksum mismatch")) {
183 $dest = (new Task\
PaxFixup($source))->run($this->verbosity());
186 $this->cleanup
[] = new Task\
Cleanup($dest);
191 * Localize a possibly remote source
192 * @param string $source
193 * @return string local source directory
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);
200 $source = realpath($source);
201 if (!is_dir($source)) {
202 $source = $this->extract($source);
203 $this->cleanup
[] = new Task\
Cleanup($source);
205 if (!$this->args
->git
) {
206 $source = (new Task\
PeclFixup($source))->run($this->verbosity());
213 * Load the source dir
214 * @throws \pharext\Exception
216 private function loadSource(){
217 if ($this->args
["source"]) {
218 $source = $this->localize($this->args
["source"]);
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";
227 $this->source
= new SourceDir\
Basic($source);
230 if (!$this->source
instanceof SourceDir
) {
231 throw new Exception("Unknown source dir $source");
234 foreach ($this->source
->getPackageInfo() as $key => $val) {
235 $this->args
->$key = $val;
241 * Creates the extension phar
243 private function createPackage() {
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",
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());
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());
265 } catch (\Exception
$e) {
266 $this->error("%s\n", $e->getMessage());
270 if ($this->args
->gzip
) {
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());
276 $this->info("Created gzipped phar %s\n", $name);
278 if ($this->args
->sign
) {
279 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
280 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
283 } catch (\Exception
$e) {
284 $this->warn("%s\n", $e->getMessage());
288 if ($this->args
->bzip
) {
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());
294 $this->info("Created bzipped phar %s\n", $name);
296 if ($this->args
->sign
) {
297 $sign = new Task\
PharSign($name, $this->args
->sign
, $pass);
298 $sign->run($this->verbosity())->exportPublicKey($name.".pubkey");
301 } catch (\Exception
$e) {
302 $this->warn("%s\n", $e->getMessage());
307 $move = new Task\
PharRename($file, $this->args
->dest
, $this->args
->name
."-". $this->args
->release
);
308 $name = $move->run($this->verbosity());
310 $this->info("Created executable phar %s\n", $name);
313 $pkey->exportPublicKey($name.".pubkey");
316 } catch (\Exception
$e) {
317 $this->error("%s\n", $e->getMessage());