Issue
In my composer.json I have a script called tests that is just an array of several commands for testing tools.
One of those commands is pretty slow and somewhat unimportant. So I would like to run it only occasionally and not always (whole composer tests is run as pre-commit hook)
I had an idea of doing something like
"scripts": {
"slowScript": "rand(0,10) != 5 || actualSlowScript"
}
but I can't get it to work. The script needs to be platform independent as we use different OS.
Do you have an idea on how to get it working?
Solution
This is a solution:
Create a PHP class with static method as in https://getcomposer.org/doc/articles/scripts.md#defining-scripts
<?php
namespace ComposerScripts;
use Composer\Console\Application;
use Composer\Script\Event;
use Symfony\Component\Console\Input\ArrayInput;
final class RandomLint
{
public static function runLintAtRandom(Event $event): void
{
$random = random_int(1, 5);
if ($random === 1) {
$event->getIO()->write('Lint skipped');
return;
}
$input = new ArrayInput(array('command' => 'lint'));
$application = new Application();
$application->setAutoExit(false);
$application->run($input);
}
}
then define the script as
"scripts": {
"test": [
"ComposerScripts\\RandomLint::runLintAtRandom",
"secondScript",
"@thirdOne"
]
}
and you can control scripts from PHP Class. I can see quite some potential in this solution.
Answered By - Noximo
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.