Skip to content

Commit

Permalink
Replace Makefile with castor.php and update workflow
Browse files Browse the repository at this point in the history
A new castor.php file is introduced, replacing the existing Makefile which has been removed. This update modifies the GitHub workflows to call the appropriate Castor tasks. Minor changes are also made to existing source files and static analysis configurations to include the new castor.php file.
  • Loading branch information
Spomky committed Jul 20, 2024
1 parent cc698c6 commit e701a6d
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/infection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, curl, dom, json, libxml, mbstring, openssl, phar, simplexml, sodium, tokenizer, xml, xmlwriter, zlib"
tools: "castor"
coverage: "xdebug"

- name: "Checkout code"
Expand All @@ -30,4 +31,4 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute Infection"
run: "make ci-mu"
run: "castor infect"
23 changes: 12 additions & 11 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
dependency-versions: "highest"

- name: "Check source code for syntax errors"
run: "composer exec -- parallel-lint src/ tests/"
run: "castor lint"

php_tests:
name: "2️⃣ Unit and functional tests"
Expand All @@ -66,6 +66,7 @@ jobs:
with:
php-version: "${{ matrix.php-version }}"
extensions: "ctype, curl, dom, json, libxml, mbstring, openssl, phar, simplexml, sodium, tokenizer, xml, xmlwriter, zlib"
tools: "castor"
coverage: "xdebug"

- name: "Checkout code"
Expand All @@ -80,10 +81,7 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute tests (PHP)"
run: "make ci-cc"

- name: "Fix code coverage paths"
run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml
run: "castor test"

js_tests:
name: "2️⃣ JS tests"
Expand All @@ -103,6 +101,7 @@ jobs:
with:
php-version: "${{ matrix.php-version }}"
extensions: "ctype, curl, dom, json, libxml, mbstring, openssl, phar, simplexml, sodium, tokenizer, xml, xmlwriter, zlib"
tools: "castor"
coverage: "xdebug"

- name: "Checkout code"
Expand All @@ -117,7 +116,7 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute tests (JS)"
run: "make js"
run: "castor js"

static_analysis:
name: "3️⃣ Static Analysis"
Expand All @@ -131,6 +130,7 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, curl, dom, json, libxml, mbstring, openssl, phar, simplexml, sodium, tokenizer, xml, xmlwriter, zlib"
tools: "castor"
coverage: "none"

- name: "Checkout code"
Expand All @@ -149,7 +149,7 @@ jobs:
run: "composer dump-autoload --optimize --strict-psr"

- name: "Execute static analysis"
run: "make st"
run: "castor stan"

coding_standards:
name: "4️⃣ Coding Standards"
Expand All @@ -163,6 +163,7 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, curl, dom, json, libxml, mbstring, openssl, phar, simplexml, sodium, tokenizer, xml, xmlwriter, zlib"
tools: "castor"
coverage: "none"

- name: "Checkout code"
Expand All @@ -181,11 +182,10 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Check coding style"
run: "make ci-cs"
run: "castor cs"

- name: "Deptrac"
run: |
vendor/bin/deptrac analyse --fail-on-uncovered --no-cache
run: "castor deptrac"

rector_checkstyle:
name: "6️⃣ Rector Checkstyle"
Expand All @@ -199,6 +199,7 @@ jobs:
with:
php-version: "8.3"
extensions: "ctype, curl, dom, json, libxml, mbstring, openssl, phar, simplexml, sodium, tokenizer, xml, xmlwriter, zlib"
tools: "castor"
coverage: "xdebug"

- name: "Checkout code"
Expand All @@ -214,7 +215,7 @@ jobs:
composer-options: "--optimize-autoloader"

- name: "Execute Rector"
run: "make rector"
run: "castor rector"

exported_files:
name: "7️⃣ Exported files"
Expand Down
57 changes: 0 additions & 57 deletions Makefile

This file was deleted.

215 changes: 215 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

declare(strict_types=1);

use Castor\Attribute\AsOption;
use Castor\Attribute\AsTask;
use function Castor\io;
use function Castor\run;

#[AsTask(description: 'Run mutation testing')]
function infect(int $minMsi = 0, int $minCoveredMsi = 0, bool $ci = false): void
{
io()->title('Running infection');
$nproc = run('nproc', quiet: true);
if (! $nproc->isSuccessful()) {
io()->error('Cannot determine the number of processors');
return;
}
$threads = (int) $nproc->getOutput();
$command = [
'php',
'vendor/bin/infection',
sprintf('--min-msi=%s', $minMsi),
sprintf('--min-covered-msi=%s', $minCoveredMsi),
sprintf('--threads=%s', $threads),
];
if ($ci) {
$command[] = '--logger-github';
$command[] = '-s';
}
$environment = [
'XDEBUG_MODE' => 'coverage',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Run tests')]
function test(bool $coverageHtml = false, bool $coverageText = false, null|string $group = null): void
{
io()->title('Running tests');
$command = ['php', 'vendor/bin/phpunit', '--color'];
$environment = [
'XDEBUG_MODE' => 'off',
];
if ($coverageHtml) {
$command[] = '--coverage-html=build/coverage';
$environment['XDEBUG_MODE'] = 'coverage';
}
if ($coverageText) {
$command[] = '--coverage-text';
$environment['XDEBUG_MODE'] = 'coverage';
}
if ($group !== null) {
$command[] = sprintf('--group=%s', $group);
}
run($command, environment: $environment);
}

#[AsTask(description: 'Coding standards check')]
function cs(
#[AsOption(description: 'Fix issues if possible')]
bool $fix = false,
#[AsOption(description: 'Clear cache')]
bool $clearCache = false
): void {
io()->title('Running coding standards check');
$command = ['php', 'vendor/bin/ecs', 'check'];
$environment = [
'XDEBUG_MODE' => 'off',
];
if ($fix) {
$command[] = '--fix';
}
if ($clearCache) {
$command[] = '--clear-cache';
}
run($command, environment: $environment);
}

#[AsTask(description: 'Running PHPStan')]
function stan(bool $baseline = false): void
{
io()->title('Running PHPStan');
$command = ['php', 'vendor/bin/phpstan', 'analyse'];
if ($baseline) {
$command[] = '--generate-baseline';
}
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Validate Composer configuration')]
function validate(): void
{
io()->title('Validating Composer configuration');
$command = ['composer', 'validate', '--strict'];
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);

$command = ['composer', 'dump-autoload', '--optimize', '--strict-psr'];
run($command, environment: $environment);
}

/**
* @param array<string> $allowedLicenses
*/
#[AsTask(description: 'Check licenses')]
function checkLicenses(
array $allowedLicenses = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'MIT', 'MPL-2.0', 'OSL-3.0']
): void {
io()->title('Checking licenses');
$allowedExceptions = [];
$command = ['composer', 'licenses', '-f', 'json'];
$environment = [
'XDEBUG_MODE' => 'off',
];
$result = run($command, environment: $environment, quiet: true);
if (! $result->isSuccessful()) {
io()->error('Cannot determine licenses');
exit(1);
}
$licenses = json_decode((string) $result->getOutput(), true);
$disallowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => ! in_array($name, $allowedExceptions, true)
&& count(array_diff($info['license'], $allowedLicenses)) === 1,
\ARRAY_FILTER_USE_BOTH
);
$allowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => in_array($name, $allowedExceptions, true)
|| count(array_diff($info['license'], $allowedLicenses)) === 0,
\ARRAY_FILTER_USE_BOTH
);
if (count($disallowed) > 0) {
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($disallowed),
$disallowed
)
);
io()
->error('Disallowed licenses found');
exit(1);
}
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($allowed),
$allowed
)
);
io()
->success('All licenses are allowed');
}

#[AsTask(description: 'Run Rector')]
function rector(
#[AsOption(description: 'Fix issues if possible')]
bool $fix = false,
#[AsOption(description: 'Clear cache')]
bool $clearCache = false
): void {
io()->title('Running Rector');
$command = ['php', 'vendor/bin/rector', 'process', '--ansi'];
if (! $fix) {
$command[] = '--dry-run';
}
if ($clearCache) {
$command[] = '--clear-cache';
}
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Run Rector')]
function deptrac(): void
{
io()->title('Running Rector');
$command = ['php', 'vendor/bin/deptrac', 'analyse', '--fail-on-uncovered', '--no-cache'];
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Run Linter')]
function lint(): void
{
io()->title('Running Linter');
$command = ['composer', 'exec', '--', 'parallel-lint', __DIR__ . '/src/', __DIR__ . '/tests/'];
$environment = [
'XDEBUG_MODE' => 'off',
];
run($command, environment: $environment);
}

#[AsTask(description: 'Run JS tests')]
function js(): void
{
io()->title('Running JS tests');
run(['npm', 'install', '--force']);
run(['npm', 'test']);
}
4 changes: 3 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@
]);

$config->parallel();
$config->paths([__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php', __DIR__ . '/rector.php']);
$config->paths(
[__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php', __DIR__ . '/rector.php', __DIR__ . '/castor.php']
);
};
Loading

0 comments on commit e701a6d

Please sign in to comment.