From b0a4f9911b2e111b525f7098de99b210130c575c Mon Sep 17 00:00:00 2001 From: ivanwilliammd Date: Sun, 24 Mar 2024 17:51:12 +0700 Subject: [PATCH] updated upstream form upstream spatie skeleton package --- .github/workflows/dependabot-auto-merge.yml | 7 +- .../fix-php-code-style-issues-cs-fixer.yml | 30 ++ ...yml => fix-php-code-style-issues-pint.yml} | 1 + .github/workflows/run-tests-pest.yml | 48 +++ .../{run-tests.yml => run-tests-phpunit.yml} | 10 +- .github/workflows/update-changelog.yml | 1 + configure.php | 288 ++++++++++++++++++ tests/ExampleTestPest.php | 5 + ...ExampleTest.php => ExampleTestPhpunit.php} | 0 tests/Pest.php | 1 + 10 files changed, 383 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/fix-php-code-style-issues-cs-fixer.yml rename .github/workflows/{fix-php-code-style-issues.yml => fix-php-code-style-issues-pint.yml} (95%) create mode 100644 .github/workflows/run-tests-pest.yml rename .github/workflows/{run-tests.yml => run-tests-phpunit.yml} (89%) create mode 100644 configure.php create mode 100644 tests/ExampleTestPest.php rename tests/{ExampleTest.php => ExampleTestPhpunit.php} (100%) create mode 100644 tests/Pest.php diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml index ca2197d..70d8e7b 100644 --- a/.github/workflows/dependabot-auto-merge.yml +++ b/.github/workflows/dependabot-auto-merge.yml @@ -8,22 +8,23 @@ permissions: jobs: dependabot: runs-on: ubuntu-latest + timeout-minutes: 5 if: ${{ github.actor == 'dependabot[bot]' }} steps: - + - name: Dependabot metadata id: metadata uses: dependabot/fetch-metadata@v1.6.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" - + - name: Auto-merge Dependabot PRs for semver-minor updates if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} run: gh pr merge --auto --merge "$PR_URL" env: PR_URL: ${{github.event.pull_request.html_url}} GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - + - name: Auto-merge Dependabot PRs for semver-patch updates if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} run: gh pr merge --auto --merge "$PR_URL" diff --git a/.github/workflows/fix-php-code-style-issues-cs-fixer.yml b/.github/workflows/fix-php-code-style-issues-cs-fixer.yml new file mode 100644 index 0000000..8e61c64 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues-cs-fixer.yml @@ -0,0 +1,30 @@ +name: Check & fix styling + +on: + push: + paths: + - '**.php' + +permissions: + contents: write + +jobs: + php-cs-fixer: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Run PHP CS Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php-cs-fixer.dist.php --allow-risky=yes + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Fix styling diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues-pint.yml similarity index 95% rename from .github/workflows/fix-php-code-style-issues.yml rename to .github/workflows/fix-php-code-style-issues-pint.yml index ab1a6d2..cd4239c 100644 --- a/.github/workflows/fix-php-code-style-issues.yml +++ b/.github/workflows/fix-php-code-style-issues-pint.yml @@ -11,6 +11,7 @@ permissions: jobs: php-code-styling: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - name: Checkout code diff --git a/.github/workflows/run-tests-pest.yml b/.github/workflows/run-tests-pest.yml new file mode 100644 index 0000000..c15c842 --- /dev/null +++ b/.github/workflows/run-tests-pest.yml @@ -0,0 +1,48 @@ +name: Tests + +on: + push: + paths: + - '**.php' + - '.github/workflows/run-tests.yml' + - 'phpunit.xml.dist' + - 'composer.json' + - 'composer.lock' + +jobs: + test: + runs-on: ${{ matrix.os }} + timeout-minutes: 5 + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest, windows-latest] + php: [8.3, 8.2, 8.1] + stability: [prefer-lowest, prefer-stable] + + name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: List Installed Dependencies + run: composer show -D + + - name: Execute tests + run: vendor/bin/pest --ci diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests-phpunit.yml similarity index 89% rename from .github/workflows/run-tests.yml rename to .github/workflows/run-tests-phpunit.yml index 95bdf2c..e50d143 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests-phpunit.yml @@ -3,11 +3,11 @@ name: Tests on: push: paths: - - "**.php" - - ".github/workflows/run-tests.yml" - - "phpunit.xml.dist" - - "composer.json" - - "composer.lock" + - '**.php' + - '.github/workflows/run-tests.yml' + - 'phpunit.xml.dist' + - 'composer.json' + - 'composer.lock' jobs: test: diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml index ec40921..39de30d 100644 --- a/.github/workflows/update-changelog.yml +++ b/.github/workflows/update-changelog.yml @@ -10,6 +10,7 @@ permissions: jobs: update: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - name: Checkout code diff --git a/configure.php b/configure.php new file mode 100644 index 0000000..2db2e43 --- /dev/null +++ b/configure.php @@ -0,0 +1,288 @@ +#!/usr/bin/env php + $option === $default ? strtoupper($option) : $option, + $options, + )); + + $answer = ask("{$question} ({$suggestions})"); + + $validOptions = implode(', ', $options); + + while (! in_array($answer, $options)) { + if ($default && $answer === '') { + $answer = $default; + + break; + } + + writeln(PHP_EOL."Please pick one of the following options: {$validOptions}"); + + $answer = ask("{$question} ({$suggestions})"); + } + + if (! $answer) { + $answer = $default; + } + + return $answer; +} + +function confirm(string $question, bool $default = false): bool +{ + $answer = ask($question.' ('.($default ? 'Y/n' : 'y/N').')'); + + if (! $answer) { + return $default; + } + + return strtolower($answer) === 'y'; +} + +function writeln(string $line): void +{ + echo $line.PHP_EOL; +} + +function run(string $command): string +{ + return trim(shell_exec($command)); +} + +function str_after(string $subject, string $search): string +{ + $pos = strrpos($subject, $search); + + if ($pos === false) { + return $subject; + } + + return substr($subject, $pos + strlen($search)); +} + +function slugify(string $subject): string +{ + return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-')); +} + +function title_case(string $subject): string +{ + return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject))); +} + +function replace_in_file(string $file, array $replacements): void +{ + $contents = file_get_contents($file); + + file_put_contents( + $file, + str_replace( + array_keys($replacements), + array_values($replacements), + $contents + ) + ); +} + +function removeReadmeParagraphs(string $file): void +{ + $contents = file_get_contents($file); + + file_put_contents( + $file, + preg_replace('/.*/s', '', $contents) ?: $contents + ); +} + +function determineSeparator(string $path): string +{ + return str_replace('/', DIRECTORY_SEPARATOR, $path); +} + +function replaceForWindows(): array +{ + return preg_split('/\\r\\n|\\r|\\n/', run('dir /S /B * | findstr /v /i .git\ | findstr /v /i vendor | findstr /v /i '.basename(__FILE__).' | findstr /r /i /M /F:/ ":author :vendor :package VendorName skeleton vendor_name vendor_slug author@domain.com"')); +} + +function replaceForAllOtherOSes(): array +{ + return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|vendor_name|vendor_slug|author@domain.com" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__))); +} + +function setupTestingLibrary(string $testingLibrary): void +{ + if ($testingLibrary === 'pest') { + unlink(__DIR__.'/tests/ExampleTestPhpunit.php'); + unlink(__DIR__.'/.github/workflows/run-tests-phpunit.yml'); + + rename( + from: __DIR__.'/tests/ExampleTestPest.php', + to: __DIR__.'/tests/ExampleTest.php' + ); + + rename( + from: __DIR__.'/.github/workflows/run-tests-pest.yml', + to: __DIR__.'/.github/workflows/run-tests.yml' + ); + + replace_in_file(__DIR__.'/composer.json', [ + ':require_dev_testing' => '"pestphp/pest": "^2.20"', + ':scripts_testing' => '"test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage"', + ':plugins_testing' => '"pestphp/pest-plugin": true', + ]); + } elseif ($testingLibrary === 'phpunit') { + unlink(__DIR__.'/tests/ExampleTestPest.php'); + unlink(__DIR__.'/tests/ArchTest.php'); + unlink(__DIR__.'/tests/Pest.php'); + unlink(__DIR__.'/.github/workflows/run-tests-pest.yml'); + + rename( + from: __DIR__.'/tests/ExampleTestPhpunit.php', + to: __DIR__.'/tests/ExampleTest.php' + ); + + rename( + from: __DIR__.'/.github/workflows/run-tests-phpunit.yml', + to: __DIR__.'/.github/workflows/run-tests.yml' + ); + + replace_in_file(__DIR__.'/composer.json', [ + ':require_dev_testing' => '"phpunit/phpunit": "^10.3.2"', + ':scripts_testing' => '"test": "vendor/bin/phpunit", + "test-coverage": "vendor/bin/phpunit --coverage"', + ':plugins_testing,' => '', // We need to remove the comma here as well, since there's nothing to add + ]); + } +} + +function setupCodeStyleLibrary(string $codeStyleLibrary): void +{ + if ($codeStyleLibrary === 'pint') { + unlink(__DIR__.'/.github/workflows/fix-php-code-style-issues-cs-fixer.yml'); + + rename( + from: __DIR__.'/.github/workflows/fix-php-code-style-issues-pint.yml', + to: __DIR__.'/.github/workflows/fix-php-code-style-issues.yml' + ); + + replace_in_file(__DIR__.'/composer.json', [ + ':require_dev_codestyle' => '"laravel/pint": "^1.0"', + ':scripts_codestyle' => '"format": "vendor/bin/pint"', + ':plugins_testing' => '', + ]); + + unlink(__DIR__.'/.php-cs-fixer.dist.php'); + } elseif ($codeStyleLibrary === 'cs fixer') { + unlink(__DIR__.'/.github/workflows/fix-php-code-style-issues-pint.yml'); + + rename( + from: __DIR__.'/.github/workflows/fix-php-code-style-issues-cs-fixer.yml', + to: __DIR__.'/.github/workflows/fix-php-code-style-issues.yml' + ); + + replace_in_file(__DIR__.'/composer.json', [ + ':require_dev_codestyle' => '"friendsofphp/php-cs-fixer": "^3.21.1"', + ':scripts_codestyle' => '"format": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --allow-risky=yes"', + ':plugins_testing' => '', + ]); + } +} + +$gitName = run('git config user.name'); +$authorName = ask('Author name', $gitName); + +$gitEmail = run('git config user.email'); +$authorEmail = ask('Author email', $gitEmail); + +$usernameGuess = explode(':', run('git config remote.origin.url'))[1]; +$usernameGuess = dirname($usernameGuess); +$usernameGuess = basename($usernameGuess); +$authorUsername = ask('Author username', $usernameGuess); + +$vendorName = ask('Vendor name', $authorUsername); +$vendorSlug = slugify($vendorName); +$vendorNamespace = ucwords($vendorName); +$vendorNamespace = ask('Vendor namespace', $vendorNamespace); + +$currentDirectory = getcwd(); +$folderName = basename($currentDirectory); + +$packageName = ask('Package name', $folderName); +$packageSlug = slugify($packageName); + +$className = title_case($packageName); +$className = ask('Class name', $className); +$description = ask('Package description', "This is my package {$packageSlug}"); + +$testingLibrary = askWithOptions( + 'Which testing library do you want to use?', + ['pest', 'phpunit'], + 'phpunit', +); + +$codeStyleLibrary = askWithOptions( + 'Which code style library do you want to use?', + ['pint', 'cs fixer'], + 'pint', +); + +writeln('------'); +writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); +writeln("Vendor : {$vendorName} ({$vendorSlug})"); +writeln("Package : {$packageSlug} <{$description}>"); +writeln("Namespace : {$vendorNamespace}\\{$className}"); +writeln("Class name : {$className}"); +writeln("Testing library : {$testingLibrary}"); +writeln("Code style library : {$codeStyleLibrary}"); +writeln('------'); + +writeln('This script will replace the above values in all relevant files in the project directory.'); + +if (! confirm('Modify files?', true)) { + exit(1); +} + +$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); + +foreach ($files as $file) { + replace_in_file($file, [ + ':author_name' => $authorName, + ':author_username' => $authorUsername, + 'author@domain.com' => $authorEmail, + ':vendor_name' => $vendorName, + ':vendor_slug' => $vendorSlug, + 'VendorName' => $vendorNamespace, + ':package_name' => $packageName, + ':package_slug' => $packageSlug, + 'Skeleton' => $className, + ':package_description' => $description, + ]); + + match (true) { + str_contains($file, determineSeparator('src/SkeletonClass.php')) => rename($file, determineSeparator('./src/'.$className.'Class.php')), + str_contains($file, 'README.md') => removeReadmeParagraphs($file), + default => [], + }; +} + +setupTestingLibrary($testingLibrary); +setupCodeStyleLibrary($codeStyleLibrary); + +confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); + +confirm('Let this script delete itself?', true) && unlink(__FILE__); diff --git a/tests/ExampleTestPest.php b/tests/ExampleTestPest.php new file mode 100644 index 0000000..5d36321 --- /dev/null +++ b/tests/ExampleTestPest.php @@ -0,0 +1,5 @@ +toBeTrue(); +}); diff --git a/tests/ExampleTest.php b/tests/ExampleTestPhpunit.php similarity index 100% rename from tests/ExampleTest.php rename to tests/ExampleTestPhpunit.php diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1 @@ +