PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 2, 2022

[FIXED] Composer script - run only sometimes

 March 02, 2022     composer-php, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing