From 8f276954a6edc4b77d9f209e2b4907d6b2841cac Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 12 Sep 2023 10:42:40 +0200 Subject: [PATCH 1/6] Initial code to get Box for phar building --- .github/workflows/phar-build.yml | 64 ++++++++++++++++++++++++++++++++ box.json | 10 +++++ composer.json | 4 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/phar-build.yml create mode 100644 box.json diff --git a/.github/workflows/phar-build.yml b/.github/workflows/phar-build.yml new file mode 100644 index 000000000..ff2de60b1 --- /dev/null +++ b/.github/workflows/phar-build.yml @@ -0,0 +1,64 @@ +name: Build PHAR + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build-phar: + name: "Build PHAR" + + runs-on: ${{ matrix.operating-system }} + + strategy: + fail-fast: false + matrix: + dependencies: + - "locked" + php-version: + - "8.1" + operating-system: + - "ubuntu-latest" + + steps: + - name: "Checkout" + uses: "actions/checkout@v4" + + - name: "Install PHP" + uses: "shivammathur/setup-php@v2" + with: + coverage: none + tools: composer:v2 + php-version: "${{ matrix.php-version }}" + ini-values: memory_limit=-1 + + - name: "Get Composer Cache Directory" + id: composer-cache + run: | + echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: "Cache Composer dependencies" + uses: "actions/cache@v3" + with: + path: "${{ steps.composer-cache.outputs.dir }}" + key: "php-${{ matrix.php-version }}-locked-composer-${{ hashFiles('**/composer.lock') }}" + restore-keys: | + php-${{ matrix.php-version }}-locked-composer- + + - name: "Install locked dependencies" + run: "composer install --no-interaction --no-progress" + + - name: "Build PHAR file" + run: "composer build:phar" + + - name: "Test PHAR file" + run: "./spc.phar dev:extensions" + + - uses: actions/upload-artifact@v3 + with: + path: spc.phar diff --git a/box.json b/box.json new file mode 100644 index 000000000..e688155e0 --- /dev/null +++ b/box.json @@ -0,0 +1,10 @@ +{ + "alias": "spc-php.phar", + "banner": false, + "blacklist": [ + ".github", + "tests" + ], + "git-commit-short": "git_commit_short", + "output": "spc.phar" +} diff --git a/composer.json b/composer.json index 14f6189be..706f8d646 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "captainhook/captainhook": "^5.10", "captainhook/plugin-composer": "^5.3", "friendsofphp/php-cs-fixer": "^3.25", + "humbug/box": "^4.3", "nunomaduro/collision": "^7.8", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^10.3" @@ -44,7 +45,8 @@ "scripts": { "analyse": "phpstan analyse --memory-limit 300M", "cs-fix": "php-cs-fixer fix", - "test": "vendor/bin/phpunit tests/ --no-coverage" + "test": "vendor/bin/phpunit tests/ --no-coverage", + "build:phar": "vendor/bin/box compile" }, "config": { "allow-plugins": { From 4917ad3173bb6fa0456e4f506c8a130c8ae22c43 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 12 Sep 2023 16:11:32 +0200 Subject: [PATCH 2/6] Adjust box configuration for smaller phar size --- box.json | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/box.json b/box.json index e688155e0..ad1d3c01d 100644 --- a/box.json +++ b/box.json @@ -2,8 +2,15 @@ "alias": "spc-php.phar", "banner": false, "blacklist": [ - ".github", - "tests" + ".github" + ], + "directories": [ + "config", + "src", + "vendor/psr", + "vendor/laravel/prompts", + "vendor/symfony", + "vendor/zhamao" ], "git-commit-short": "git_commit_short", "output": "spc.phar" From 75406fe391a8e6180d564299fee4585f8623302e Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 12 Sep 2023 17:58:46 +0200 Subject: [PATCH 3/6] Add version into phar --- box.json | 1 + 1 file changed, 1 insertion(+) diff --git a/box.json b/box.json index ad1d3c01d..e5f82806e 100644 --- a/box.json +++ b/box.json @@ -13,5 +13,6 @@ "vendor/zhamao" ], "git-commit-short": "git_commit_short", + "metadata": "ConsoleApplication::VERSION", "output": "spc.phar" } From 96dff7ed28d3b08513c0e81ff06d9953e1b7eccf Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 12 Sep 2023 18:06:39 +0200 Subject: [PATCH 4/6] Temporary disable phar build in GH actions --- .github/workflows/phar-build.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/phar-build.yml b/.github/workflows/phar-build.yml index ff2de60b1..a1029e9e9 100644 --- a/.github/workflows/phar-build.yml +++ b/.github/workflows/phar-build.yml @@ -1,10 +1,11 @@ name: Build PHAR -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] +# Temporary disabled, please follow: https://github.com/crazywhalecc/static-php-cli/issues/179 +#on: +# push: +# branches: [ "main" ] +# pull_request: +# branches: [ "main" ] permissions: contents: read From bc6a1735fe4add340e53fc130b7e2226c3264b06 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Wed, 13 Sep 2023 15:06:08 +0200 Subject: [PATCH 5/6] Remove the phar workflow temporary --- .github/workflows/phar-build.yml | 65 -------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 .github/workflows/phar-build.yml diff --git a/.github/workflows/phar-build.yml b/.github/workflows/phar-build.yml deleted file mode 100644 index a1029e9e9..000000000 --- a/.github/workflows/phar-build.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: Build PHAR - -# Temporary disabled, please follow: https://github.com/crazywhalecc/static-php-cli/issues/179 -#on: -# push: -# branches: [ "main" ] -# pull_request: -# branches: [ "main" ] - -permissions: - contents: read - -jobs: - build-phar: - name: "Build PHAR" - - runs-on: ${{ matrix.operating-system }} - - strategy: - fail-fast: false - matrix: - dependencies: - - "locked" - php-version: - - "8.1" - operating-system: - - "ubuntu-latest" - - steps: - - name: "Checkout" - uses: "actions/checkout@v4" - - - name: "Install PHP" - uses: "shivammathur/setup-php@v2" - with: - coverage: none - tools: composer:v2 - php-version: "${{ matrix.php-version }}" - ini-values: memory_limit=-1 - - - name: "Get Composer Cache Directory" - id: composer-cache - run: | - echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - - name: "Cache Composer dependencies" - uses: "actions/cache@v3" - with: - path: "${{ steps.composer-cache.outputs.dir }}" - key: "php-${{ matrix.php-version }}-locked-composer-${{ hashFiles('**/composer.lock') }}" - restore-keys: | - php-${{ matrix.php-version }}-locked-composer- - - - name: "Install locked dependencies" - run: "composer install --no-interaction --no-progress" - - - name: "Build PHAR file" - run: "composer build:phar" - - - name: "Test PHAR file" - run: "./spc.phar dev:extensions" - - - uses: actions/upload-artifact@v3 - with: - path: spc.phar From 08d2e16d7b92bca8b2ce7ebea7ebc5004dabf6ec Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Wed, 13 Sep 2023 21:33:12 +0800 Subject: [PATCH 6/6] remove unused old deploy command --- src/SPC/ConsoleApplication.php | 2 - src/SPC/command/DeployCommand.php | 166 ------------------------------ 2 files changed, 168 deletions(-) delete mode 100644 src/SPC/command/DeployCommand.php diff --git a/src/SPC/ConsoleApplication.php b/src/SPC/ConsoleApplication.php index 09c572385..b3e1de8bc 100644 --- a/src/SPC/ConsoleApplication.php +++ b/src/SPC/ConsoleApplication.php @@ -6,7 +6,6 @@ use SPC\command\BuildCliCommand; use SPC\command\BuildLibsCommand; -use SPC\command\DeployCommand; use SPC\command\dev\AllExtCommand; use SPC\command\dev\PhpVerCommand; use SPC\command\dev\SortConfigCommand; @@ -39,7 +38,6 @@ public function __construct() [ new BuildCliCommand(), new BuildLibsCommand(), - new DeployCommand(), new DoctorCommand(), new DownloadCommand(), new DumpLicenseCommand(), diff --git a/src/SPC/command/DeployCommand.php b/src/SPC/command/DeployCommand.php deleted file mode 100644 index 770fc39ce..000000000 --- a/src/SPC/command/DeployCommand.php +++ /dev/null @@ -1,166 +0,0 @@ -addArgument('target', InputArgument::OPTIONAL, 'The file or directory to pack.'); - $this->addOption('auto-phar-fix', null, InputOption::VALUE_NONE, 'Automatically fix ini option.'); - $this->addOption('overwrite', 'W', InputOption::VALUE_NONE, 'Overwrite existing files.'); - $this->addOption('with-no-dev', 'D', InputOption::VALUE_NONE, 'Automatically use non-dev composer dependencies to reduce size'); - $this->addOption('with-dev', 'd', InputOption::VALUE_NONE, 'Automatically use dev composer dependencies'); - } - - /** - * @throws \PharException - */ - public function handle(): int - { - $composer = require ROOT_DIR . '/vendor/composer/installed.php'; - if (($composer['root']['dev'] ?? false) === true) { - if (!$this->getOption('with-no-dev')) { - $this->output->writeln('Current static-php-cli dependencies have installed dev-dependencies'); - $this->output->writeln('If you want to remove, you can choose "Yes" to run command "composer update --no-dev" to remove.'); - $this->output->writeln('Or choose "No", just pack, deploy.'); - $ask = confirm('Do you want to remove dev-dependencies to reduce size of phar file?'); - } elseif (!$this->getOption('with-dev')) { - $ask = true; - } else { - $ask = false; - } - if ($ask) { - [$code] = shell()->execWithResult('composer update --no-dev'); - if ($code !== 0) { - $this->output->writeln('"composer update --no-dev" failed with exit code [' . $code . ']'); - $this->output->writeln('You may need to run this command by your own.'); - return static::FAILURE; - } - $this->output->writeln('Update successfully, you need to re-run deploy command to pack.'); - return static::SUCCESS; - } - } - // 首先得确认是不是关闭了readonly模式 - if (ini_get('phar.readonly') == 1) { - if ($this->getOption('auto-phar-fix')) { - $ask = true; - } else { - $this->output->writeln('pack command needs "phar.readonly" = "Off" !'); - $ask = confirm('Do you want to automatically set it and continue ?'); - // $ask = $prompt->requireBool('pack command needs "phar.readonly" = "Off" !' . PHP_EOL . 'If you want to automatically set it and continue, just Enter', true); - } - if ($ask) { - global $argv; - $args = array_merge(['-d', 'phar.readonly=0'], $_SERVER['argv'], ['--no-motd']); - if (function_exists('pcntl_exec')) { - $this->output->writeln('Changing to phar.readonly=0 mode ...'); - if (pcntl_exec(PHP_BINARY, $args) === false) { - throw new \PharException('Switching to read write mode failed, please check the environment.'); - } - } else { - $this->output->writeln('Now running command in child process.'); - passthru(PHP_BINARY . ' -d phar.readonly=0 ' . implode(' ', $argv), $retcode); - exit($retcode); - } - } - } - // 获取路径 - $path = WORKING_DIR; - // 如果是目录,则将目录下的所有文件打包 - $phar_path = text('Please input the phar target filename', default: '/tmp/static-php-cli.phar'); - // $phar_path = $prompt->requireArgument('target', 'Please input the phar target filename', 'static-php-cli.phar'); - - if (FileSystem::isRelativePath($phar_path)) { - $phar_path = WORKING_DIR . '/' . $phar_path; - } - if (file_exists($phar_path)) { - if (!$this->getOption('overwrite')) { - $this->output->writeln('The file "' . $phar_path . '" already exists.'); - $ask = confirm('Do you want to overwrite it?'); - } else { - $ask = true; - } - if (!$ask) { - $this->output->writeln('User canceled.'); - return static::FAILURE; - } - @unlink($phar_path); - } - $phar = new \Phar($phar_path); - $phar->startBuffering(); - - $all = FileSystem::scanDirFiles($path, true, true); - - $all = array_filter($all, function ($x) { - $dirs = preg_match('/(^(config|src|vendor)\\/|^(composer\\.json|README\\.md|source\\.json|LICENSE|README-en\\.md)$)/', $x); - return !($dirs !== 1); - }); - sort($all); - $map = []; - foreach ($all as $v) { - $map[$v] = $path . '/' . $v; - } - - $this->output->writeln('Start packing files...'); - try { - foreach ($this->progress()->iterate($map) as $file => $origin_file) { - $phar->addFromString($file, php_strip_whitespace($origin_file)); - } - // $phar->buildFromIterator(new SeekableArrayIterator($map, new ProgressBar($output))); - $phar->addFromString( - '.phar-entry.php', - str_replace( - '/../vendor/autoload.php', - '/vendor/autoload.php', - file_get_contents(ROOT_DIR . '/bin/spc') - ) - ); - $stub = '.phar-entry.php'; - $phar->setStub($phar->createDefaultStub($stub)); - } catch (\Throwable $e) { - $this->output->writeln($e); - return static::FAILURE; - } - $phar->addFromString('.prod', 'true'); - $phar->stopBuffering(); - $this->output->writeln(PHP_EOL . 'Done! Phar file is generated at "' . $phar_path . '".'); - if (file_exists(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx')) { - $this->output->writeln('Detected you have already compiled micro binary, I will make executable now for you!'); - file_put_contents( - pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc', - file_get_contents(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx') . - file_get_contents($phar_path) - ); - chmod(pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc', 0755); - $this->output->writeln('Binary Executable: ' . pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc'); - } - chmod($phar_path, 0755); - $this->output->writeln('Phar Executable: ' . $phar_path . ''); - return static::SUCCESS; - } - - private function progress(): ProgressBar - { - $progress = new ProgressBar($this->output, 0); - $progress->setBarCharacter('⚬'); - $progress->setEmptyBarCharacter('⚬'); - $progress->setProgressCharacter('➤'); - $progress->setFormat( - "%current%/%max% [%bar%] %percent:3s%%\n🪅 %estimated:-20s% %memory:20s%" . PHP_EOL - ); - return $progress; - } -}