-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Castor tasks and update Infection workflow This commit introduces a new Castor PHP script with different tasks for test execution, mutation testing, coding standards check, PHPStan, and others. The Infection GitHub workflow was also updated to use the new Castor 'infect' task instead of 'make ci-mu'. The 'Infection' workflow name was also corrected. * Refactor castor validation and integrate tasks in workflows The code refactors the validation of the composer configuration and autoload dumping in the 'castor.php' file. It also modifies the '.github/workflows/integrate.yml' to replace previous tasks with equivalent 'castor' commands for validation, testing, static analysis, code style checking, Deptrac analysis, and make rector. Additionally, '.castor.stub.php' is added to the '.gitignore' file to prevent it from being tracked. * Add license check functionality to castor A new function has been introduced in `castor.php` to check licenses and ensure compliance with defined acceptable licenses. This function has also been integrated into the CI process via the `integrate.yml` GitHub workflow file. It checks license compliance for each dependency before a pull request is merged.
- Loading branch information
Showing
7 changed files
with
227 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ yarn-error.log | |
/composer.lock | ||
/vendor | ||
infection.txt | ||
/.castor.stub.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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(bool $fix = false): void | ||
{ | ||
io()->title('Running coding standards check'); | ||
$command = ['php', 'vendor/bin/ecs', 'check']; | ||
$environment = [ | ||
'XDEBUG_MODE' => 'off', | ||
]; | ||
if ($fix) { | ||
$command[] = '--fix'; | ||
} | ||
run($command, environment: $environment); | ||
} | ||
|
||
#[AsTask(description: 'Running PHPStan')] | ||
function stan(): void | ||
{ | ||
io()->title('Running PHPStan'); | ||
$command = ['php', 'vendor/bin/phpstan', 'analyse']; | ||
$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($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(bool $fix = false): void | ||
{ | ||
io()->title('Running Rector'); | ||
$command = ['php', 'vendor/bin/rector', 'process', '--ansi']; | ||
if (! $fix) { | ||
$command[] = '--dry-run'; | ||
} | ||
$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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.