Skip to content

Commit 1319186

Browse files
authored
Build phar with Box (#59)
* Add ads to `--version` * Build phar with Box * Readme: Remove composer install instructions
1 parent 405bef8 commit 1319186

File tree

10 files changed

+186
-47
lines changed

10 files changed

+186
-47
lines changed

.github/workflows/lint.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ jobs:
2828
- uses: ramsey/composer-install@v3
2929

3030
- run: vendor/bin/phpstan analyse --error-format=github
31+
32+
box:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v5
36+
with:
37+
fetch-depth: 0
38+
39+
- uses: shivammathur/setup-php@v2
40+
with:
41+
php-version: '8.4'
42+
coverage: none
43+
tools: box:4
44+
45+
- run: box validate

.github/workflows/test.yml

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,29 @@ jobs:
5454
files: coverage-unit.xml
5555
flags: unit
5656

57-
bin:
58-
runs-on: ubuntu-latest
59-
env:
60-
GOFLAGS: '-mod=mod'
57+
phar:
58+
runs-on: ubuntu-24.04
59+
outputs:
60+
php-version: ${{ steps.setup-php.outputs.php-version }}
6161
steps:
6262
- uses: actions/checkout@v5
63+
with:
64+
fetch-depth: 0
6365

6466
- uses: shivammathur/setup-php@v2
67+
id: setup-php
6568
with:
6669
php-version: '8.4'
6770
coverage: none
71+
tools: box:4
6872
- uses: ramsey/composer-install@v3
6973
with:
70-
composer-options: '--no-dev --prefer-dist'
74+
composer-options: --no-dev --prefer-dist
7175

72-
- name: Add bin into PATH
73-
run: echo "${WORKSPACE}/bin" >> "$GITHUB_PATH"
74-
env:
75-
WORKSPACE: ${{ github.workspace }}
76+
- run: box compile
7677

77-
- uses: actions/setup-go@v6
78+
- uses: actions/upload-artifact@v4
7879
with:
79-
go-version-file: 'go.mod'
80-
81-
- name: Print php-matrix binary path
82-
run: go test -count=1 -v ./internal
83-
84-
- run: go test -count=1 ./...
80+
name: php-matrix.phar
81+
path: phar/php-matrix
82+
if-no-files-found: error

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
# Composer
12
/vendor/
3+
4+
# Pest
25
/coverage*.xml
6+
7+
# Box
8+
/phar/

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ vendor:
1010

1111
bin: vendor
1212

13+
phar: vendor
14+
box compile
15+
box verify phar/php-matrix
16+
box info phar/php-matrix
17+
php phar/php-matrix --version
18+
1319
test-%: %
1420
PATH="$(shell pwd)/$*:$(shell echo $$PATH)" \
1521
go test -count=1 ./...

README.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,7 @@ Available sources:
9595

9696
## Installation
9797

98-
### Composer Global
99-
100-
```bash
101-
composer global require typisttech/php-matrix
102-
php-matrix --help
103-
```
104-
105-
### Composer Project
106-
107-
```bash
108-
composer create-project typisttech/php-matrix
109-
cd php-matrix
110-
./bin/php-matrix --help
111-
```
98+
TODO!
11299

113100
## Credits
114101

bin/php-matrix

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33

44
declare(strict_types=1);
55

6-
use TypistTech\PhpMatrix\Console\Application;
6+
namespace TypistTech\PhpMatrix;
77

8-
include $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php';
8+
use TypistTech\PhpMatrix\Console\Runner;
9+
use function in_array;
10+
use const PHP_EOL;
11+
use const PHP_SAPI;
912

10-
Application::make()
11-
->run();
13+
// Taken from https://box-project.github.io/box/faq/#what-is-the-canonical-way-to-write-a-cli-entry-file
14+
if (!in_array(PHP_SAPI, ['micro', 'cli', 'phpdbg', 'embed'], true)) {
15+
echo PHP_EOL . 'This app may only be invoked from a command line, got "' . PHP_SAPI . '"' . PHP_EOL;
16+
exit(1);
17+
}
18+
19+
include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
20+
21+
Runner::run();

box.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"output": "phar/php-matrix",
3+
"check-requirements": false,
4+
"files-bin": [
5+
"data/all-versions.json"
6+
],
7+
"datetime": "datetime",
8+
"datetime-format": "Y-m-d",
9+
"git-tag": "git-tag"
10+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
},
6565
"scripts": {
6666
"lint": [
67+
"box validate",
6768
"pint --test",
6869
"phpstan analyse"
6970
],

src/Console/Application.php

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,107 @@
44

55
namespace TypistTech\PhpMatrix\Console;
66

7-
use Composer\InstalledVersions;
8-
use Symfony\Component\Console\Application as ConsoleApplication;
7+
use Symfony\Component\Console\Application as SymfonyConsoleApplication;
8+
use Symfony\Component\Console\Helper\FormatterHelper;
99

10-
class Application
10+
class Application extends SymfonyConsoleApplication
1111
{
12-
private const string NAME = 'php-matrix';
12+
private const BANNER = <<<BANNER
13+
____ _ _ ____ __ __ _ _
14+
| _ \| | | | _ \ | \/ | __ _| |_ _ __(_)_ __
15+
| |_) | |_| | |_) | | |\/| |/ _` | __| '__| \ \/ /
16+
| __/| _ | __/ | | | | (_| | |_| | | |> <
17+
|_| |_| |_|_| |_| |_|\__,_|\__|_| |_/_/\_\
18+
BANNER;
1319

14-
public static function make(): ConsoleApplication
20+
public const BUILD_TIMESTAMP = '@datetime@';
21+
22+
public function getLongVersion(): string
1523
{
16-
$app = new ConsoleApplication(
17-
self::NAME,
18-
InstalledVersions::getPrettyVersion('typisttech/php-matrix') ?? 'unknown',
24+
$longVersion = self::BANNER;
25+
$longVersion .= PHP_EOL.PHP_EOL;
26+
27+
$app = sprintf(
28+
'%-15s <info>%s</info> %s',
29+
$this->getName(),
30+
$this->getVersion(),
31+
self::BUILD_TIMESTAMP,
32+
);
33+
$longVersion .= $app;
34+
35+
$githubUrl = sprintf(
36+
'<href=https://github.com/typisttech/php-matrix/releases/tag/%1$s>https://github.com/typisttech/php-matrix/releases/tag/%1$s</>',
37+
$this->getVersion(),
38+
);
39+
// https://github.com/box-project/box/blob/b0123f358f2a32488c92e09bf56f16d185e4e3cb/src/Configuration/Configuration.php#L2116
40+
if ((bool) preg_match('/^(?<tag>.+)-\d+-g(?<hash>[a-f0-9]{7})$/', $this->getVersion(), $matches)) {
41+
// Not on a tag.
42+
$githubUrl = sprintf(
43+
'<href=https://github.com/typisttech/php-matrix/compare/%1$s...%2$s>https://github.com/typisttech/php-matrix/compare/%1$s...%2$s</>',
44+
$matches['tag'],
45+
$matches['hash'],
46+
);
47+
}
48+
$longVersion .= PHP_EOL.$githubUrl;
49+
50+
$longVersion .= PHP_EOL.PHP_EOL.'<comment>PHP:</>';
51+
52+
$phpVersion = sprintf(
53+
'%-15s %s',
54+
'Version',
55+
PHP_VERSION,
56+
);
57+
$longVersion .= PHP_EOL.$phpVersion;
58+
59+
$phpSapi = sprintf(
60+
'%-15s %s',
61+
'SAPI',
62+
PHP_SAPI,
63+
);
64+
$longVersion .= PHP_EOL.$phpSapi;
65+
66+
$longVersion .= PHP_EOL.PHP_EOL.'<comment>Support Composer SemVer:</>';
67+
68+
$supportBlock = (new FormatterHelper)
69+
->formatBlock(
70+
[
71+
'If you find this tool useful, please consider supporting its development.',
72+
'Every contribution counts, regardless how big or small.',
73+
'I am eternally grateful to all sponsors who fund my open source journey.',
74+
],
75+
'question',
76+
true,
77+
);
78+
$longVersion .= PHP_EOL.$supportBlock;
79+
80+
$sponsorUrl = sprintf(
81+
'%1$-15s <href=%2$s>%2$s</>',
82+
'GitHub Sponsor',
83+
'https://github.com/sponsors/tangrufus',
1984
);
85+
$longVersion .= PHP_EOL.PHP_EOL.$sponsorUrl;
86+
87+
$longVersion .= PHP_EOL.PHP_EOL.'<comment>Hire Tang Rufus:</>';
2088

21-
$app->addCommands([
22-
new ComposerCommand,
23-
new ConstraintCommand,
24-
]);
89+
$hireBlock = (new FormatterHelper)
90+
->formatBlock(
91+
[
92+
'I am looking for my next role, freelance or full-time.',
93+
'If you find this tool useful, I can build you more weird stuffs like this.',
94+
"Let's talk if you are hiring PHP / Ruby / Go developers.",
95+
],
96+
'error',
97+
true,
98+
);
99+
$longVersion .= PHP_EOL.$hireBlock;
100+
101+
$sponsorUrl = sprintf(
102+
'%1$-15s <href=%2$s>%2$s</>',
103+
'Contact',
104+
'https://typist.tech/contact/',
105+
);
106+
$longVersion .= PHP_EOL.PHP_EOL.$sponsorUrl;
25107

26-
return $app;
108+
return $longVersion;
27109
}
28110
}

src/Console/Runner.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\PhpMatrix\Console;
6+
7+
class Runner
8+
{
9+
private const string NAME = 'PHP Matrix';
10+
11+
private const string GIT_TAG = '@git-tag@';
12+
13+
public static function run(): int
14+
{
15+
$app = new Application(self::NAME, self::GIT_TAG);
16+
17+
$app->addCommands([
18+
new ComposerCommand,
19+
new ConstraintCommand,
20+
]);
21+
22+
return $app->run();
23+
}
24+
}

0 commit comments

Comments
 (0)