Added minification and manifest generation (-n)

This commit is contained in:
Chris 2017-01-10 01:34:08 +01:00
parent 00ebb6ff95
commit dd2bb032dd
2 changed files with 66 additions and 4 deletions

View File

@ -9,11 +9,12 @@ class Application
private function parseCommandLine() private function parseCommandLine()
{ {
$opts = "h"; $opts = "hn";
$long = [ 'help' ]; $long = [ 'help', 'new' ];
$parsed = getopt($opts, $long); $parsed = getopt($opts, $long);
$config = [ $config = [
'init' => false,
'help' => false, 'help' => false,
'manifest' => getcwd()."/makephar.sdl", 'manifest' => getcwd()."/makephar.sdl",
'output' => null, 'output' => null,
@ -25,6 +26,10 @@ class Application
case 'help': case 'help':
$config['help'] = true; $config['help'] = true;
break; break;
case 'n':
case 'new':
$config['init'] = true;
break;
} }
$this->config = (object)$config; $this->config = (object)$config;
@ -34,6 +39,7 @@ class Application
{ {
printf("Usage: makephar [opts]\n"); printf("Usage: makephar [opts]\n");
printf("Options: -h,--help Show this help\n"); printf("Options: -h,--help Show this help\n");
printf(" -n,--new Create a new configuration file\n");
} }
public function run() public function run()
@ -44,6 +50,42 @@ class Application
$this->printHelp(); $this->printHelp();
return; return;
} }
if ($this->config->init) {
if (!file_exists("composer.json")) {
log_error("No composer.json in current directory");
return 1;
}
$proj = json_decode(file_get_contents("composer.json"));
$proj_name = basename(getcwd()).".phar";
$proj_files = [];
$proj_dirs = [];
if (!empty($proj->autoload)) {
foreach ((array)$proj->autoload as $type=>$loaders) {
if ($type == 'files') {
$proj_files = array_merge($proj_files, $loaders);
} else {
foreach ($loaders as $ns=>$path) {
$dir = rtrim($path," /");
if (!in_array($dir,$proj_dirs)) {
$proj_dirs[] = $dir;
}
}
}
}
printf("phar \"%s\" {\n", $proj_name);
printf(" include {\n");
foreach ($proj_files as $file)
printf(" file \"%s\";\n", $file);
foreach ($proj_dirs as $dir)
printf(" dir \"%s\";\n", $dir);
printf(" dir \"vendor\";\n");
printf(" }\n");
printf(" // stub \"src/path-to-stub.php\";\n");
printf("}\n");
}
return;
}
$this->buildManifest(); $this->buildManifest();
} }

View File

@ -27,20 +27,37 @@ class PharBuilder
{ {
// Create the phar // Create the phar
$phar = new \Phar("/tmp/output.phar"); $phar = new \Phar("/tmp/output.phar");
$vo = posix_isatty(STDOUT);
// Add files // Add files
log_debug("Adding files to phar archive..."); log_debug("Adding files to phar archive...");
$t = count($this->files); $i = 0; $lp = null;
$phar->startBuffering();
foreach ($this->files as $file) { foreach ($this->files as $file) {
$phar->addFile($file->dest?:$file->src, $file->src); $i++;
$tp = dirname($file->src);
if ($tp!=$lp) {
$lp=$tp;
printf("\r\e[K%d/%d %s", $i, $t, $tp);
}
if (fnmatch("*.php",$file->src)) {
$phar->addFromString($file->dest?:$file->src, php_strip_whitespace($file->src));
} else {
$phar->addFile($file->dest?:$file->src, $file->src);
}
} }
($vo) && printf("\r\e[K");
$phar->stopBuffering();
// Create stub // Create stub
if ($this->manifest->getIsLibrary()) { if ($this->manifest->getIsLibrary()) {
log_debug("Creating library stub..."); log_debug("Creating library stub...");
// Create library stub // Create library stub
$stub = '<?php '; $stub = '<?php ';
$stub.= 'if (basename(__FILE__)==basename($_SERVER["SCRIPT_FILENAME"])) { echo "Error: This is a library and can not be executed\n"; exit(1); }';
$stub.= 'require_once __DIR__."/vendor/autoload.php";'; $stub.= 'require_once __DIR__."/vendor/autoload.php";';
if (($stubfile = $this->manifest->getStubFile())) {
$stub.= 'require_once __DIR__."/'.ltrim($stubfile,'/').'";';
}
$phar->addFromString("index.php", $stub); $phar->addFromString("index.php", $stub);
} else { } else {
log_debug("Creating application stub..."); log_debug("Creating application stub...");
@ -50,6 +67,9 @@ class PharBuilder
$phar->addFromString("index.php", $stub); $phar->addFromString("index.php", $stub);
// Make the phar stub executable // Make the phar stub executable
$mainstub = $phar->createDefaultStub("index.php"); $mainstub = $phar->createDefaultStub("index.php");
$tmp = "/tmp/".uniqid("stub"); file_put_contents($tmp, $mainstub);
$mainstub = php_strip_whitespace($tmp);
unlink($tmp);
$mainstub = "#!/usr/bin/env php\n{$mainstub}"; $mainstub = "#!/usr/bin/env php\n{$mainstub}";
$phar->setStub($mainstub); $phar->setStub($mainstub);
} }