Skip to content

Commit

Permalink
fix #270 Composer Automation to build PHAR distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Nov 19, 2020
1 parent 5783e69 commit f813518
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 185 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles.

## [Unreleased]

### Changed

- [GH-270](https://github.com/llaville/php-compat-info/issues/270) - Composer Automation to build PHAR distribution

### Fixed

- [GH-284](https://github.com/llaville/php-compat-info/issues/284) - Errors while running on empty files
Expand Down
49 changes: 5 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,13 @@ composer require bartlett/php-compatinfo

## Build PHAR distribution

To build PHAR distribution, you'll need to get a copy of this project https://github.com/humbug/box
Since release **5.4.2** building of phar version of application is part of composer automated process.

**WARNING**: Don't forget to run following command (before compiling archive), if you want to have a PHAR manifest up-to-date !
```bash
php phar-manifest.php > manifest.txt
```
Either when you install or update project with Composer, the `bin/phpcompatinfo.phar` file will be built.

Run following command
```bash
box.phar compile
```

You should get output that look like
```
Box version 3.8.4@120b0a3 2019-12-13 17:22:43 UTC
// Loading the configuration file "/shared/backups/bartlett/php-compat-info/box.json.dist".
🔨 Building the PHAR "/shared/backups/bartlett/php-compat-info/bin/phpcompatinfo.phar"
? Removing the existing PHAR "/shared/backups/bartlett/php-compat-info/bin/phpcompatinfo.phar"
? No compactor to register
? Adding main file: /shared/backups/bartlett/php-compat-info/bin/phpcompatinfo
? Adding requirements checker
? Adding binary files
> No file found
? Auto-discover files? No
? Exclude dev files? No
? Adding files
> 1129 file(s)
? Using stub file: /shared/backups/bartlett/php-compat-info/phar-stub.php
? Skipping dumping the Composer autoloader
? Removing the Composer dump artefacts
? Compressing with the algorithm "GZ"
> Warning: the extension "zlib" will now be required to execute the PHAR
? Setting file permissions to 0755
* Done.
No recommendation found.
No warning found.
// PHAR: 1155 files (1.65MB)
// You can inspect the generated PHAR with the "info" command.
// Memory usage: 25.54MB (peak: 26.54MB), time: 1sec
```
**NOTE**
- You may avoid this by invoking composer command with `--no-scripts` option.
- You may also rebuild the phar file by invoking `composer compile-box` command (shortcut of `run`, `run-script`).

## Documentation

Expand Down
32 changes: 26 additions & 6 deletions bin/phpcompatinfo
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
#!/usr/bin/env php
<?php
// @link https://www.tomasvotruba.cz/blog/2018/08/02/5-gotchas-of-the-bin-file-in-php-cli-applications/

if (PHP_SAPI !== 'cli') {
return;
}

gc_disable(); // performance boost

if (\Phar::running()) {
$vendorDir = 'phar://phpcompatinfo.phar/vendor';
$possibleAutoloadPaths = [
'phar://phpcompatinfo.phar/vendor/autoload.php'
];
} else {
$baseDir = dirname(__DIR__);
$vendorDir = $baseDir . '/vendor';
$possibleAutoloadPaths = [
// local dev repository
__DIR__ . '/../vendor/autoload.php',
// dependency
__DIR__ . '/../../../../vendor/autoload.php',
];
}

if (!file_exists($vendorDir . '/autoload.php')) {
$vendorDir = dirname(dirname($baseDir));
$isAutoloadFound = false;
foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (file_exists($possibleAutoloadPath)) {
require_once $possibleAutoloadPath;
$isAutoloadFound = true;
break;
}
}
require_once $vendorDir . '/autoload.php';

if ($isAutoloadFound === false) {
throw new RuntimeException(sprintf(
'Unable to find "vendor/autoload.php" in "%s" paths.',
implode('", "', $possibleAutoloadPaths)
));
}

use Bartlett\CompatInfo\Console\ApplicationInterface;

Expand Down
121 changes: 1 addition & 120 deletions box.json.dist
Original file line number Diff line number Diff line change
@@ -1,124 +1,5 @@
{
"directories": ["src", "config"],
"directories": ["bin", "config", "src", "vendor"],
"compression": "GZ",
"dump-autoload": false,
"files": [
"manifest.txt",
"LICENSE",
"vendor/bartlett/php-compatinfo-db/LICENSE",
"vendor/bartlett/php-compatinfo-db/data/compatinfo.sqlite",
"vendor/bartlett/php-reflect/LICENSE",
"vendor/bartlett/php-reflect/bin/phpreflect.json-schema",
"vendor/composer/package-versions-deprecated/LICENSE",
"vendor/composer/semver/LICENSE",
"vendor/doctrine/collections/LICENSE",
"vendor/justinrainbow/json-schema/LICENSE",
"vendor/laminas/laminas-diagnostics/LICENSE.md",
"vendor/laminas/laminas-zendframework-bridge/LICENSE.md",
"vendor/nikic/php-parser/LICENSE",
"vendor/phpdocumentor/reflection-common/LICENSE",
"vendor/phpdocumentor/reflection-docblock/LICENSE",
"vendor/phpdocumentor/type-resolver/LICENSE",
"vendor/psr/container/LICENSE",
"vendor/psr/log/LICENSE",
"vendor/ramsey/uuid/LICENSE",
"vendor/sebastian/version/LICENSE",
"vendor/seld/jsonlint/LICENSE",
"vendor/symfony/config/LICENSE",
"vendor/symfony/console/LICENSE",
"vendor/symfony/dependency-injection/LICENSE",
"vendor/symfony/event-dispatcher/LICENSE",
"vendor/symfony/event-dispatcher-contracts/LICENSE",
"vendor/symfony/filesystem/LICENSE",
"vendor/symfony/finder/LICENSE",
"vendor/symfony/polyfill-ctype/LICENSE",
"vendor/symfony/polyfill-mbstring/LICENSE",
"vendor/symfony/polyfill-php73/LICENSE",
"vendor/symfony/polyfill-php80/LICENSE",
"vendor/symfony/serializer/LICENSE",
"vendor/symfony/service-contracts/LICENSE",
"vendor/symfony/stopwatch/LICENSE",
"vendor/webmozart/assert/LICENSE",
"vendor/autoload.php"
],
"finder": [
{
"name": "*.php",
"in": "vendor/composer"
},
{
"name": "*.php",
"in": "vendor/laminas/laminas-diagnostics/src"
},
{
"name": "*.php",
"in": "vendor/laminas/laminas-zendframework-bridge/src"
},
{
"name": "*.php",
"in": "vendor/league/tactician/src"
},
{
"name": "*.php",
"in": "vendor/nikic/php-parser/lib"
},
{
"name": "*.php",
"in": "vendor/ramsey/uuid/src"
},
{
"name": "Version.php",
"in": "vendor/sebastian/version/src"
},
{
"name": "*.php",
"notPath": "/Tests/",
"in": "vendor/symfony"
},
{
"name": "*.php",
"in": "vendor/seld/jsonlint/src"
},
{
"name": "*.php",
"in": "vendor/justinrainbow/json-schema/src"
},
{
"name": "*.php",
"in": "vendor/phpdocumentor/reflection-common/src"
},
{
"name": "*.php",
"in": "vendor/phpdocumentor/type-resolver/src"
},
{
"name": "*.php",
"in": "vendor/phpdocumentor/reflection-docblock/src"
},
{
"name": "*.php",
"in": "vendor/webmozart/assert/src"
},
{
"name": "*.php",
"in": "vendor/doctrine/collections/lib"
},
{
"name": "*.php",
"in": "vendor/psr/container/src"
},
{
"name": "*.php",
"in": "vendor/psr/log/Psr"
},
{
"name": "*.php",
"in": "vendor/bartlett/php-reflect/src"
},
{
"name": "*.php",
"in": "vendor/bartlett/php-compatinfo-db/src"
}
],
"stub": "phar-stub.php"
}
17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"symfony/serializer": "^4.4|^5.0",
"symfony/stopwatch": "^4.4|^5.0"
},
"require-dev": {
"humbug/box": "^3.7"
},
"authors": [
{
"name": "Laurent Laville",
Expand All @@ -57,6 +60,20 @@
"Bartlett\\Tests\\CompatInfo\\": "tests/"
}
},
"scripts": {
"compile-box": "box compile",
"post-install-cmd": [
"@compile-box"
],
"post-update-cmd": [
"@compile-box"
]
},
"scripts-descriptions": {
"compile-box": "Compiles application with box-project/box to build PHAR version"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"optimize-autoloader": true
}
Expand Down
23 changes: 8 additions & 15 deletions src/Bartlett/CompatInfo/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

use PackageVersions\Versions;

use Phar;
use function substr_count;

/**
Expand All @@ -46,7 +47,7 @@
class Application extends BaseApplication implements ApplicationInterface
{
public const NAME = 'phpCompatInfo';
public const VERSION = '5.4.1';
public const VERSION = '5.4.2';
public const API_NAMESPACE = 'Bartlett\CompatInfo\Api\\';

/**
Expand Down Expand Up @@ -197,7 +198,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
*/
public function doRun(InputInterface $input, OutputInterface $output): int
{
if (\Phar::running()
if (Phar::running()
&& true === $input->hasParameterOption('--manifest')
) {
$manifest = 'phar://' . strtolower($this->getName()) . '.phar/manifest.txt';
Expand Down Expand Up @@ -225,14 +226,6 @@ public function doRun(InputInterface $input, OutputInterface $output): int
protected function getDefaultInputDefinition(): InputDefinition
{
$definition = parent::getDefaultInputDefinition();
$definition->addOption(
new InputOption(
'config',
'c',
InputOption::VALUE_REQUIRED,
'Read configuration from PHP file.'
)
);
$definition->addOption(
new InputOption(
'profile',
Expand Down Expand Up @@ -265,13 +258,13 @@ protected function getDefaultInputDefinition(): InputDefinition
'Display debugging information.'
)
);
if (\Phar::running()) {
if (!Phar::running()) {
$definition->addOption(
new InputOption(
'manifest',
null,
InputOption::VALUE_NONE,
'Show which versions of dependencies are bundled.'
'config',
'c',
InputOption::VALUE_REQUIRED,
'Read configuration from PHP file.'
)
);
}
Expand Down

0 comments on commit f813518

Please sign in to comment.