Skip to content

Commit

Permalink
Merge pull request #10 from Aeliot-Tm/phar
Browse files Browse the repository at this point in the history
Build PHAR file
  • Loading branch information
Aeliot-Tm authored Aug 30, 2024
2 parents 81f4975 + 7892fa1 commit 7940cdf
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 7 deletions.
10 changes: 10 additions & 0 deletions .docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ENV COMPOSER_NO_INTERACTION 1
ENV COMPOSER_VERSION 2.7.6
ENV DEBIAN_FRONTEND noninteractive
ENV DOCKER_PHP_DEPS \
gnupg \
libxml2-dev \
libzip-dev \
unzip
Expand All @@ -26,6 +27,15 @@ RUN set -xe; \
curl -sS https://getcomposer.org/installer | php -- --filename=composer --install-dir=/usr/local/bin --version=${COMPOSER_VERSION} > /dev/null 2>&1 && \
chmod +x /usr/local/bin/composer

# Install PHIVE
RUN set -xe; \
curl -sSL -o phive.phar https://phar.io/releases/phive.phar && \
curl -sSL -o phive.phar.asc https://phar.io/releases/phive.phar.asc && \
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 && \
gpg --verify phive.phar.asc phive.phar && \
chmod +x phive.phar && \
mv phive.phar /usr/local/bin/phive

WORKDIR /app

COPY composer.* ./
Expand Down
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ indent_size = 4
[composer.json]
indent_style = space
indent_size = 4

[.phive/trust-gpg-keys.txt]
insert_final_newline = false
68 changes: 68 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Release

on:
release:
types: [ created ]

# See https://stackoverflow.com/a/72408109
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build-phar:
runs-on: ubuntu-latest
name: Build PHAR
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
ini-values: phar.readonly=0
tools: composer
coverage: none

- name: Install Composer dependencies
uses: ramsey/composer-install@v3
with:
composer-options: '--no-dev'

- name: Build PHAR
run: composer build

# Smoke test.
# It is recommended ot have some sorts of tests for your PHAR.
#- name: Ensure the PHAR works
# run: pcsf-baseline.phar --version

# The following section is done only for releases
- name: Import GPG key
if: github.event_name == 'release'
uses: crazy-max/ghaction-import-gpg@v5
with:
gpg_private_key: ${{ secrets.GPG_KEY_83F9945BC33EC39E9710206C8B4927076BA50A83 }}
passphrase: "${{ secrets.GPG_KEY_83F9945BC33EC39E9710206C8B4927076BA50A83_PASSPHRASE }}"

- name: Sign the PHAR
if: github.event_name == 'release'
run: |
gpg --local-user 83F9945BC33EC39E9710206C8B4927076BA50A83 \
--batch \
--yes \
--passphrase="${{ secrets.GPG_KEY_83F9945BC33EC39E9710206C8B4927076BA50A83_PASSPHRASE }}" \
--detach-sign \
--output pcsf-baseline.phar.asc \
pcsf-baseline.phar
- name: Upload PHAR to the release
uses: softprops/action-gh-release@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
files: |
pcsf-baseline.phar
pcsf-baseline.phar.asc
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
/.phpunit.cache/
/tools/
/vendor/
/.php-cs-fixer.cache
pcsf-baseline.phar
4 changes: 4 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="humbug/box" version="^4.6.2" installed="4.6.2" location="./tools/box.phar" copy="true"/>
</phive>
1 change: 1 addition & 0 deletions .phive/trust-gpg-keys.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2DF45277AEF09A2F
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ So, it's some work around till baseline will be implemented in the PHP CS Fixer.
```shell
composer require --dev aeliot/php-cs-fixer-baseline
```
2. Extract `Finder` from the config of PHP CS Fixer to the separate file.
2. Extract `Finder` from the config of PHP CS Fixer to the separate file.
It expects `.php-cs-fixer-finder.php` at the root of the project.
3. Add filtering of files detected by Finder
```php
use Aeliot\PhpCsFixerBaseline\Service\FilterFactory;
$finder->filter((new FilterFactory())->createFilter(__DIR__ . '/.php-cs-fixer-baseline.json', $config));
```
4. Generate baseline. Just call script without options when all config files uses default names.
```shell
vendor/bin/pcsf-baseline
vendor/bin/pcsf-baseline
```
See options of it below.

You can see how it is configured in this project.

### Autoload classes from PHAR

If you use this project as PHAR file, you need to require autoloader of it to use provided filter.
Do it in the main config file of PHP CS Fixer (`.php-cs-fixer.dist.php`)
```php
Phar::loadPhar('/path/to/pcsf-baseline.phar', 'pcsf-baseline.phar');
require_once 'phar://pcsf-baseline.phar/vendor/autoload.php';
```

### Options of baseline generator

| Short name | Long name | Description | Default value |
Expand All @@ -39,5 +50,5 @@ You can see how it is configured in this project.
| c | config | Name of config file | .php-cs-fixer.dist.php |
| f | finder | Name of file with definition of Finder | .php-cs-fixer-finder.php |

Path to files can be absolute or related or omitted at all. It the last case it is expected that files
Path to files can be absolute or related or omitted at all. It the last case it is expected that files
in the root directory of project.
12 changes: 8 additions & 4 deletions bin/pcsf-baseline
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use Aeliot\PhpCsFixerBaseline\Service\Builder;
use Aeliot\PhpCsFixerBaseline\Service\Saver;

$autoloaderPath = (static function (): string {
if (Phar::running()) {
return __DIR__ . '/../vendor/autoload.php';
}

if (isset($GLOBALS['_composer_autoload_path'])) {
return $GLOBALS['_composer_autoload_path'];
}
Expand All @@ -27,14 +33,12 @@ $autoloaderPath = (static function (): string {

require_once $autoloaderPath;

$projectPath = dirname($autoloaderPath, 2);

$absolutePathMaker = static function (string $path) use ($projectPath): string {
$absolutePathMaker = static function (string $path): string {
if (preg_match('#^(?:[[:alpha:]]:[/\\\\]|/)#', $path)) {
return $path;
}

return $projectPath . '/' . $path;
return getcwd() . '/' . $path;
};

$options = (static function () use ($absolutePathMaker): array {
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
"baseline"
],
"scripts": {
"build": "tools/box.phar compile --config=scripts/box/config.json",
"cs-check": "@cs-fixer-check",
"cs-fix": "@cs-fixer-fix",
"cs-fixer-check": "vendor/bin/php-cs-fixer fix --dry-run",
"cs-fixer-fix": "vendor/bin/php-cs-fixer fix -vv",
"phive-install": "phive install --trust-gpg-keys $(cat .phive/trust-gpg-keys.txt)",
"phpstan": [
"@phpstan-clear",
"@phpstan-analyse"
Expand Down
30 changes: 30 additions & 0 deletions scripts/box/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"alias": "pcsf-baseline.phar",
"base-path": "/app/",
"directories": [
"src"
],
"files": [
"composer.json",
"composer.lock",
"LICENSE",
"README.md"
],
"finder": [
{
"name": "*.php",
"exclude": [
".github",
"doc",
"test",
"test_old",
"tests",
"Tests",
"vendor-bin"
],
"in": "vendor"
}
],
"main": "bin/pcsf-baseline",
"output": "pcsf-baseline.phar"
}

0 comments on commit 7940cdf

Please sign in to comment.