From 386d679e364180be4cf45f7c53f3c67a0b62e0bc Mon Sep 17 00:00:00 2001 From: ngyuki Date: Sun, 29 Jul 2018 09:47:43 +0900 Subject: [PATCH] Add box setting for Phar --- .gitignore | 1 + bin/compile.php | 101 ------------------------------------ bin/compile.stub | 12 ----- bin/db-migrate | 15 +++++- box.json | 38 ++++++++++++++ composer.json | 5 ++ src/Console/Application.php | 21 ++++++-- 7 files changed, 75 insertions(+), 118 deletions(-) delete mode 100644 bin/compile.php delete mode 100644 bin/compile.stub create mode 100644 box.json diff --git a/.gitignore b/.gitignore index 1c621d6..4068121 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /sql/config.php /.bin/ /.envrc +/*.phar diff --git a/bin/compile.php b/bin/compile.php deleted file mode 100644 index 5e051b8..0000000 --- a/bin/compile.php +++ /dev/null @@ -1,101 +0,0 @@ -files()->in('src'); - -$finders[] = $finder = new Finder(); -$finder->files()->in('build/vendor') - ->notName('*.md') - ->notName('composer.json') - ->notName('phpunit.xml.dist') - ->exclude('Tests') -; - -if (file_exists($phar)) { - unlink($phar); -} - -$stripWhitespace = function ($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; -}; - -$pharObj = new \Phar($phar, 0); -$pharObj->setSignatureAlgorithm(\Phar::SHA1); -$pharObj->addFromString('version', $version); -$pharObj->setMetadata(array('version' => $version)); -$pharObj->startBuffering(); - -foreach ($finders as $finder) { - /** @var $file \SplFileInfo */ - foreach ($finder as $file) { - if (pathinfo($file->getPathname(), PATHINFO_EXTENSION) === 'php') { - $source = file_get_contents($file->getPathname()); - $source = $stripWhitespace($source); - $pharObj->addFromString($file->getPathname(), $source); - echo "* " . $file->getPathname() . "\n"; - } else { - $pharObj->addFile($file->getPathname()); - echo " " . $file->getPathname() . "\n"; - } - } -} - -$pharObj->setStub(file_get_contents($stub)); -$pharObj->stopBuffering(); - -unset($pharObj); -chmod($phar, 0777); - -$size = filesize($phar); -echo "\nCompiled $phar ... $size Byte\n"; diff --git a/bin/compile.stub b/bin/compile.stub deleted file mode 100644 index b8f9cb9..0000000 --- a/bin/compile.stub +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env php -setVersion($version); -$application->run(); - -__HALT_COMPILER(); diff --git a/bin/db-migrate b/bin/db-migrate index e868bd3..43ac665 100755 --- a/bin/db-migrate +++ b/bin/db-migrate @@ -17,5 +17,18 @@ if ($loader === null) { use ngyuki\DbMigrate\Console\Application; -$application = new Application(); +$version = '@git-version@'; +if (preg_match('/^@/', $version)) { + $version = 'UNKNOWN'; +} +if (preg_match('/-/', $version) && !preg_match('/^@/', '@git-commit@')) { + $version = "$version (@git-commit@)"; +} +$longVersion = sprintf('%s version %s', basename(__FILE__), $version); +if (!preg_match('/^@/', '@datetime@')) { + $longVersion = "$longVersion @datetime@"; +} + +$application = new Application(basename(__FILE__), $version); +$application->setLongVersion($longVersion); $application->run(); diff --git a/box.json b/box.json new file mode 100644 index 0000000..9732bb6 --- /dev/null +++ b/box.json @@ -0,0 +1,38 @@ +{ + "directories": [ + "src" + ], + "files": [ + "vendor/autoload.php" + ], + "finder": [ + { + "in": [ + "vendor/composer", + "vendor/symfony/console", + "vendor/symfony/filesystem", + "vendor/symfony/polyfill-mbstring" + ], + "exclude": [ + "Tests" + ], + "notName": [ + "*.md", + "composer.json", + "phpunit.xml", + "phpunit.xml.dist" + ] + } + ], + "main": "bin/db-migrate", + "output": "db-migrate.phar", + "chmod": "0755", + "stub": true, + "git-commit": "git-commit", + "git-version": "git-version", + "datetime": "datetime", + "compression": "GZ", + "compactors": [ + "Herrera\\Box\\Compactor\\Php" + ] +} diff --git a/composer.json b/composer.json index ccf95c0..ceb0d37 100644 --- a/composer.json +++ b/composer.json @@ -50,6 +50,11 @@ "@composer test", "@composer cs", "@composer phan" + ], + "build": [ + "[ -e box.phar ] || curl -LS https://box-project.github.io/box2/installer.php | php", + "@composer dump-autoload -o -a --no-dev", + "@php -d phar.readonly=0 box.phar build -v" ] } } diff --git a/src/Console/Application.php b/src/Console/Application.php index de01cf4..9c16c92 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -10,15 +10,17 @@ class Application extends BaseApplication { - const NAME = 'db-migrate'; - const VERSION = '@dev'; + /** + * @var string + */ + private $longVersion; /** * {@inheritdoc} */ - public function __construct() + public function __construct($name = null, $version = null) { - parent::__construct(self::NAME, self::VERSION); + parent::__construct($name, $version); $commands = array(); $commands[] = new Command\MigrateCommand(); @@ -34,6 +36,17 @@ public function __construct() $this->addCommands($commands); } + public function setLongVersion($longVersion) + { + $this->longVersion = $longVersion; + return $this; + } + + public function getLongVersion() + { + return $this->longVersion; + } + /** * {@inheritdoc} */