Skip to content

Commit

Permalink
feat(ci): add add_rules for cs fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Oct 2, 2024
1 parent 692c58e commit a09e537
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
25 changes: 20 additions & 5 deletions .github/workflows/code-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ on:
add-rules:
type: string
default: "{}"
exclude-patterns:
type: string
default: ""

permissions:
contents: read
Expand All @@ -42,23 +45,35 @@ jobs:
php_code_standards:
runs-on: ubuntu-latest
name: PHP Code Standards
env:
CONFIG: ${{ inputs.config }}
CONFIG_PATH: ${{ inputs.path }}
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- if: ${{ startsWith(inputs.config, 'GoogleCloudPlatform/php-tools/') }}
name: Install Google Cloud Tools
run: |
BRANCH=${CONFIG#GoogleCloudPlatform/php-tools/}
composer global require google/cloud-tools:dev-${BRANCH#*@} -q
echo "CONFIG=$HOME/.composer/vendor/google/cloud-tools/${BRANCH%@*}" >> $GITHUB_ENV
- name: 'Setup jq'
uses: dcarbone/install-jq-action@v2
- name: Run PHP CS Fixer
run: |
composer global require friendsofphp/php-cs-fixer:${{ inputs.version }} -q
CONFIG="${{ inputs.config }}"
RULES=$(echo $'${{ inputs.rules }} ${{ inputs.add-rules }}'|tr -d '\n\t\r '|jq -s '.[0] * .[1]' -crM)
composer global require friendsofphp/php-cs-fixer:${{ inputs.version }}
export RULES=$(echo $'${{ inputs.rules }} ${{ inputs.add-rules }}'|tr -d '\n\t\r '|jq -s '.[0] * .[1]' -crM)
export EXCLUDE_PATTERNS=$(echo $'${{ inputs.exclude-patterns }}'|tr -d '\n\t\r ')
CMD_PATH=$([ "$EXCLUDE_PATTERNS" == "" ] && echo "$CONFIG_PATH")
CONFIG_OR_RULES=$([ ! -z "$CONFIG" ] && echo "--config=$CONFIG" || echo --rules=$RULES)
echo "TEST"
set -x
~/.composer/vendor/bin/php-cs-fixer fix \
${{ inputs.path }} \
$(if [ ! -z "$CONFIG" ]; then echo "--config=$CONFIG"; else echo --rules=$RULES; fi) \
$CMD_PATH \
$CONFIG_OR_RULES \
--dry-run --diff
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ jobs:
with:
path: src

code-standards-with-config:
uses: ./.github/workflows/code-standards.yml
with:
path: .
config: .php-cs-fixer.default.php
exclude-patterns: '["vendor", "test", "examples", "scripts"]'

static-analysis:
uses: ./.github/workflows/static-analysis.yml

Expand Down
15 changes: 9 additions & 6 deletions .php-cs-fixer.default.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Yaml\Yaml;

$workflow = Yaml::parse(file_get_contents(__DIR__ . '/.github/workflows/code-standards.yml'));
$rules = json_decode($workflow['on']['workflow_call']['inputs']['rules']['default'], true);
if (!$rulesJson = getenv('RULES')) {
$workflow = Yaml::parse(file_get_contents(__DIR__ . '/.github/workflows/code-standards.yml'));
$rulesJson = $workflow['on']['workflow_call']['inputs']['rules']['default'];
}

$excludePatterns = json_decode(getenv('EXCLUDE_PATTERNS') ?: '[]', true);

return (new PhpCsFixer\Config())
->setRules($rules)
->setRules(json_decode($rulesJson, true))
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->in(getenv('CONFIG_PATH') ?: __DIR__)
->notPath($excludePatterns)
)
;

0 comments on commit a09e537

Please sign in to comment.