Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
You wouldnt believe how simple copy&adapting Composer\Compiler made g…
…enerating a PHAR.
- Loading branch information
Showing
6 changed files
with
265 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| /vendor/ | ||
| fiddler.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,6 @@ | |
| }, | ||
| "autoload": { | ||
| "psr-0": {"Fiddler\\": "src/main"} | ||
| } | ||
| }, | ||
| "bin": ["src/bin/fiddler"] | ||
| } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| require __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| use Fiddler\Compiler; | ||
|
|
||
| error_reporting(-1); | ||
| ini_set('display_errors', 1); | ||
|
|
||
| try { | ||
| $compiler = new Compiler(); | ||
| $compiler->compile(); | ||
| } catch (\Exception $e) { | ||
| echo 'Failed to compile phar: ['.get_class($e).'] '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine(); | ||
| exit(1); | ||
| } | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of Composer. | ||
| * | ||
| * (c) Nils Adermann <naderman@naderman.de> | ||
| * Jordi Boggiano <j.boggiano@seld.be> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Fiddler; | ||
|
|
||
| use Composer\Json\JsonFile; | ||
| use Symfony\Component\Finder\Finder; | ||
| use Symfony\Component\Process\Process; | ||
|
|
||
| /** | ||
| * The Compiler class compiles composer into a phar | ||
| * | ||
| * @author Fabien Potencier <fabien@symfony.com> | ||
| * @author Jordi Boggiano <j.boggiano@seld.be> | ||
| */ | ||
| class Compiler | ||
| { | ||
| private $version; | ||
| private $branchAliasVersion = ''; | ||
| private $versionDate; | ||
|
|
||
| /** | ||
| * Compiles composer into a single phar file | ||
| * | ||
| * @throws \RuntimeException | ||
| * @param string $pharFile The full path to the file to create | ||
| */ | ||
| public function compile($pharFile = 'fiddler.phar') | ||
| { | ||
| if (file_exists($pharFile)) { | ||
| unlink($pharFile); | ||
| } | ||
|
|
||
| $process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__); | ||
| if ($process->run() != 0) { | ||
| throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from fiddler git repository clone and that git binary is available.'); | ||
| } | ||
| $this->version = trim($process->getOutput()); | ||
|
|
||
| $process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__); | ||
| if ($process->run() != 0) { | ||
| throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from fiddler git repository clone and that git binary is available.'); | ||
| } | ||
|
|
||
| $date = new \DateTime(trim($process->getOutput())); | ||
| $date->setTimezone(new \DateTimeZone('UTC')); | ||
| $this->versionDate = $date->format('Y-m-d H:i:s'); | ||
|
|
||
| $process = new Process('git describe --tags --exact-match HEAD'); | ||
| if ($process->run() == 0) { | ||
| $this->version = trim($process->getOutput()); | ||
| } else { | ||
| // get branch-alias defined in composer.json for dev-master (if any) | ||
| $localConfig = __DIR__.'/../../../composer.json'; | ||
| $file = new JsonFile($localConfig); | ||
| $localConfig = $file->read(); | ||
| if (isset($localConfig['extra']['branch-alias']['dev-master'])) { | ||
| $this->branchAliasVersion = $localConfig['extra']['branch-alias']['dev-master']; | ||
| } | ||
| } | ||
|
|
||
| $phar = new \Phar($pharFile, 0, 'fiddler.phar'); | ||
| $phar->setSignatureAlgorithm(\Phar::SHA1); | ||
|
|
||
| $phar->startBuffering(); | ||
|
|
||
| $finder = new Finder(); | ||
| $finder->files() | ||
| ->ignoreVCS(true) | ||
| ->name('*.php') | ||
| ->notName('Compiler.php') | ||
| ->in(__DIR__.'/..') | ||
| ; | ||
|
|
||
| foreach ($finder as $file) { | ||
| $this->addFile($phar, $file); | ||
| } | ||
|
|
||
| foreach ($finder as $file) { | ||
| $this->addFile($phar, $file, false); | ||
| } | ||
|
|
||
| $finder = new Finder(); | ||
| $finder->files() | ||
| ->ignoreVCS(true) | ||
| ->name('*.php') | ||
| ->name('LICENSE') | ||
| ->exclude('Tests') | ||
| ->exclude('tests') | ||
| ->exclude('docs') | ||
| ->in(__DIR__.'/../../../vendor/composer/') | ||
| ->in(__DIR__.'/../../../vendor/symfony/') | ||
| ->in(__DIR__.'/../../../vendor/seld/jsonlint/') | ||
| ->in(__DIR__.'/../../../vendor/justinrainbow/json-schema/') | ||
| ; | ||
|
|
||
| foreach ($finder as $file) { | ||
| $this->addFile($phar, $file); | ||
| } | ||
|
|
||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/autoload.php')); | ||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/autoload_namespaces.php')); | ||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/autoload_psr4.php')); | ||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/autoload_classmap.php')); | ||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/autoload_real.php')); | ||
| if (file_exists(__DIR__.'/../../../vendor/composer/include_paths.php')) { | ||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/include_paths.php')); | ||
| } | ||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/ClassLoader.php')); | ||
| $this->addComposerBin($phar); | ||
|
|
||
| // Stubs | ||
| $phar->setStub($this->getStub()); | ||
|
|
||
| $phar->stopBuffering(); | ||
|
|
||
| // disabled for interoperability with systems without gzip ext | ||
| // $phar->compressFiles(\Phar::GZ); | ||
|
|
||
| $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../LICENSE'), false); | ||
|
|
||
| unset($phar); | ||
| } | ||
|
|
||
| private function addFile($phar, $file, $strip = true) | ||
| { | ||
| $path = strtr(str_replace(dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/'); | ||
|
|
||
| $content = file_get_contents($file); | ||
| if ($strip) { | ||
| $content = $this->stripWhitespace($content); | ||
| } elseif ('LICENSE' === basename($file)) { | ||
| $content = "\n".$content."\n"; | ||
| } | ||
|
|
||
| echo $path . "\n"; | ||
| $phar->addFromString($path, $content); | ||
| } | ||
|
|
||
| private function addComposerBin($phar) | ||
| { | ||
| $content = file_get_contents(__DIR__.'/../../bin/fiddler'); | ||
| $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content); | ||
| $phar->addFromString('src/bin/fiddler', $content); | ||
| } | ||
|
|
||
| /** | ||
| * Removes whitespace from a PHP source string while preserving line numbers. | ||
| * | ||
| * @param string $source A PHP string | ||
| * @return string The PHP string with the whitespace removed | ||
| */ | ||
| private function stripWhitespace($source) | ||
| { | ||
| if (!function_exists('token_get_all')) { | ||
| return $source; | ||
| } | ||
|
|
||
| $output = ''; | ||
| foreach (token_get_all($source) as $token) { | ||
| if (is_string($token)) { | ||
| $output .= $token; | ||
| } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { | ||
| $output .= str_repeat("\n", substr_count($token[1], "\n")); | ||
| } elseif (T_WHITESPACE === $token[0]) { | ||
| // reduce wide spaces | ||
| $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]); | ||
| // normalize newlines to \n | ||
| $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace); | ||
| // trim leading spaces | ||
| $whitespace = preg_replace('{\n +}', "\n", $whitespace); | ||
| $output .= $whitespace; | ||
| } else { | ||
| $output .= $token[1]; | ||
| } | ||
| } | ||
|
|
||
| return $output; | ||
| } | ||
|
|
||
| private function getStub() | ||
| { | ||
| $stub = <<<'EOF' | ||
| #!/usr/bin/env php | ||
| <?php | ||
| /* | ||
| * This file is part of Composer. | ||
| * | ||
| * (c) Nils Adermann <naderman@naderman.de> | ||
| * Jordi Boggiano <j.boggiano@seld.be> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the license that is located at the bottom of this file. | ||
| */ | ||
| // Avoid APC causing random fatal errors per https://github.com/composer/composer/issues/264 | ||
| if (extension_loaded('apc') && ini_get('apc.enable_cli') && ini_get('apc.cache_by_default')) { | ||
| if (version_compare(phpversion('apc'), '3.0.12', '>=')) { | ||
| ini_set('apc.cache_by_default', 0); | ||
| } else { | ||
| fwrite(STDERR, 'Warning: APC <= 3.0.12 may cause fatal errors when running composer commands.'.PHP_EOL); | ||
| fwrite(STDERR, 'Update APC, or set apc.enable_cli or apc.cache_by_default to 0 in your php.ini.'.PHP_EOL); | ||
| } | ||
| } | ||
| Phar::mapPhar('fiddler.phar'); | ||
| EOF; | ||
|
|
||
| return $stub . <<<'EOF' | ||
| require 'phar://fiddler.phar/src/bin/fiddler'; | ||
| __HALT_COMPILER(); | ||
| EOF; | ||
| } | ||
| } |