commit 00ebb6ff9533e836c4d21dd19d752f26164e0ac2 Author: Christopher Vagnetoft Date: Mon Jan 9 02:24:59 2017 +0100 Initial commit of new codebase diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e2fbf1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +vendor +composer.lock +makephar.phar diff --git a/bin/makephar b/bin/makephar new file mode 100755 index 0000000..8c4129c --- /dev/null +++ b/bin/makephar @@ -0,0 +1,4 @@ +#!/usr/bin/env php + false, + 'manifest' => getcwd()."/makephar.sdl", + 'output' => null, + 'compress' => false, + ]; + + foreach ($parsed as $k=>$v) switch ($k) { + case 'h': + case 'help': + $config['help'] = true; + break; + } + + $this->config = (object)$config; + } + + private function printHelp() + { + printf("Usage: makephar [opts]\n"); + printf("Options: -h,--help Show this help\n"); + } + + public function run() + { + $this->parseCommandLine(); + + if ($this->config->help) { + $this->printHelp(); + return; + } + + $this->buildManifest(); + } + + protected function buildManifest() + { + $manifests = Manifest::createFromFile($this->config->manifest); + + foreach ($manifests as $manifest) { + log_info("Building %s", $manifest->getOutput()); + $builder = new PharBuilder($manifest); + $builder->prepare(); + $builder->build(); + $builder->finalize(); + } + + } + +} diff --git a/src/MakePhar/Manifest.php b/src/MakePhar/Manifest.php new file mode 100644 index 0000000..8eddbe6 --- /dev/null +++ b/src/MakePhar/Manifest.php @@ -0,0 +1,154 @@ +getChildren() as $child) { + $mf = self::createFromSdlTag($child); + $ret[] = $mf; + } + return $ret; + } + + /** + * Build a manifest from a SdlTag + * + * @return Manifest The manifest + */ + public static function createFromSdlTag(SdlTag $tag) + { + $mf = new Manifest(); + + // Output + $mf->setOutput($tag->getValue()); + + foreach ($tag->getChildren() as $child) switch ($child->getTagName()) { + case 'library': + $mf->setIsLibrary($child->getValue()); + break; + case 'compress': + $mf->setCompression($child->getValue()); + break; + case 'stub': + $mf->setStubFile($child->getValue()); + break; + case 'include': + foreach ($child->getChildren() as $inc) switch ($inc->getTagName()) { + case 'dir': + case 'file': + $mf->addSource($inc->getTagName(),$inc->getValue(),$inc->getAttributeStrings()); + break; + } + break; + } + + return $mf; + } + + + public function setOutput($output) + { + $this->output = $output; + return $this; + } + + public function getOutput() + { + return $this->output; + } + + public function setIsLibrary($value) + { + $this->library = (bool)$value; + return $this; + } + + public function getIsLibrary() + { + return $this->library; + } + + public function setStubFile($file) + { + $this->stub = $file; + return $this; + } + + public function getStubFile() + { + return $this->stub; + } + + public function setCompression($value) + { + $this->compress = $value; + return $this; + } + + public function getCompression() + { + return $this->compress; + } + + public function addSource($type, $path, array $opts) + { + $this->sources[] = (object)[ + 'type' => $type, + 'path' => $path, + 'opts' => (object)$opts, + ]; + return $this; + } + + public function getSources() + { + return $this->sources; + } + + public function findSourceFiles() + { + $items = []; + foreach ($this->sources as $source) { + if ($source->type == 'dir') { + $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source->path)); + foreach ($it as $item) { + if ($item->isDir()) continue; + $items[] = (object)['src'=>$item->getPathname(),'dest'=>null]; + } + } elseif ($source->type == 'file') { + $items[] = (object)['src'=>$source->path,'dest'=>null]; + } else { + log_warn("Unsupported source type: %s", $source->type); + } + } + return $items; + } + +} diff --git a/src/MakePhar/PharBuilder.php b/src/MakePhar/PharBuilder.php new file mode 100644 index 0000000..7ed3989 --- /dev/null +++ b/src/MakePhar/PharBuilder.php @@ -0,0 +1,77 @@ +manifest = $manifest; + } + + public function prepare() + { + log_debug("Finding files to add..."); + $this->files = $this->manifest->findSourceFiles(); + log_debug("Found %d files", count($this->files)); + } + + public function build() + { + // Create the phar + $phar = new \Phar("/tmp/output.phar"); + + // Add files + log_debug("Adding files to phar archive..."); + foreach ($this->files as $file) { + $phar->addFile($file->dest?:$file->src, $file->src); + } + + // Create stub + if ($this->manifest->getIsLibrary()) { + log_debug("Creating library stub..."); + // Create library stub + $stub = 'addFromString("index.php", $stub); + } else { + log_debug("Creating application stub..."); + // Write application stub + $stubfile = $this->manifest->getStubFile(); + $stub = 'addFromString("index.php", $stub); + // Make the phar stub executable + $mainstub = $phar->createDefaultStub("index.php"); + $mainstub = "#!/usr/bin/env php\n{$mainstub}"; + $phar->setStub($mainstub); + } + + // Close the phar + $phar = null; + } + + public function finalize() + { + $output = $this->manifest->getOutput(); + + if (file_exists($output)) { + unlink($output); + } + log_debug("Writing %s", $output); + rename("/tmp/output.phar", $output); + + if (!$this->manifest->getIsLibrary()) { + chmod($output, 0777); + } + + } + +} diff --git a/src/bootstrap.php b/src/bootstrap.php new file mode 100644 index 0000000..2e66dcb --- /dev/null +++ b/src/bootstrap.php @@ -0,0 +1,6 @@ +run(); diff --git a/src/global.php b/src/global.php new file mode 100644 index 0000000..514e1db --- /dev/null +++ b/src/global.php @@ -0,0 +1,11 @@ +