42

This is my composer.json file:

"require": {
    "php": ">=5.4",
    "zendframework/zendframework": "2.*",
    "doctrine/doctrine-module": "dev-master",
    "doctrine/doctrine-orm-module": "0.*",
    "gedmo/doctrine-extensions": "dev-master"
},
"require-dev": {
    "phpunit/phpunit": "3.7.*"
},
"scripts": {
    "post-update-cmd": [
        "rm -rf vendor/Behat",
        "git clone git://github.com/Behat/Behat.git",
        "cp composer.phar Behat/composer.phar",
        "cd Behat && git submodule update --init",
        "cd Behat && php composer.phar install",
        "cd Behat && php composer.phar require guzzle/guzzle:3.0.*",
        "mv Behat vendor/Behat",
        "ln -sf ../Behat/bin/behat vendor/bin/"
    ]
}

How can I make it so the scripts are only run in the dev environment?

Basically I want the scripts to run only when I call:

php composer.phar update --dev
0

8 Answers 8

68

To do the non-development environment update without triggering any scripts, use the --no-scripts command line switch for the update command:

php composer.phar update --no-scripts
                         ^^^^^^^^^^^^

By default, Composer scripts are only executed1 in the base package2. So you could have one package for development and in the live environment make it a dependency of the live system.

Apart from that, I do not see any way to differentiate scripts automatically.


This answer is back from 2012, the additional options in order of appearance can now safely be listed. As the list is composer-only and the original question smells to fall for x/y, the general answer is to use a build manager.

But turns out this could be done already at the day of asking, as in this composer-json example:

{
  "scripts": {
    "post-update-cmd": "composer::dev-post-update-cmd",
    "dev-post-update-cmd": [
      "rm -rf vendor/Behat",
      ": ... ",
      "ln -sf ../Behat/bin/behat vendor/bin/"
    ]
  }
}
  • 1.0.0-alpha6 (2012-10-23): dispatch dev-only scripts with PHP script class and __callStatic, e.g. composer::dev-post-update-cmd. if in_array('--dev', $_SERVER['argv']) then $eventDispatcher->dispatchCommandEvent($name).
  • 1.0.0-alpha7 (2013-05-04): --dev is now default and henceforth optional, --no-dev required for not --dev script dispatching. this effectively changes the command-line in question from: composer update --dev running only to: not running on composer update --no-dev. $event->isDevMode() now available, also required for new second parameter on dispatching the script on the event dispatcher. Compare with answer by Christian Koch.
  • 1.0.0-alpha9 (2014-12-07): autoload-dev now allows to not have the script class in --no-dev automatically by removing it from autoload. this works b/c non-existing classes fall-through w/o error.
  • 1.3.0-RC (2016-12-11): --dev is deprecated. COMPOSER_DEV_MODE environment parameter now available in update/install/dumpautoload scripts. no inherit need any longer for the PHP script class, unless enviroment parameters do not work in your setup, then use a PHP class script for a workaround. Compare with answer by Veda.
  • 3 (future): --dev will needlessly make composer fatal, at least this is announced. this closes the circle to pre-1.0.0-alpha7 which also failed on the opposite, --no-dev. the command-line library in use then and yet in the future is not able to cope with -[-not]-args symmetrically, and if the pair is divided and one must go, then it can only throw.

References

  1. See "Note:" in What is a script? - Scripts - Composer Docs
  2. Base package is commonly referred to as root package in the Composer documentation.
2
  • 1
    "By default Composer scripts are only executed in the base package." - Is that right? Adding --no-script broke my deployment, for it did not executed scripts in underlying dependencies... Aug 29, 2018 at 7:25
  • 4
    @TobiasGaertner: Quote: "Note: Only scripts defined in the root package's composer.json are executed. If a dependency of the root package specifies its own scripts, Composer does not execute those additional scripts." from: getcomposer.org/doc/articles/scripts.md#what-is-a-script-
    – hakre
    Aug 30, 2018 at 11:34
41

Introduction

Some of the answers are a bit brief and don't go into detail about the context in which this is done. I'd like to share some knowledge with whoever is still puzzled after reading the previous answers.

Determine the right option for you

First, take a moment to realise that you're effectively creating a different flow, specific for a certain environment (i.e. your development server/container). This is against any best practices, as it is generally prone to errors. Having said that, you can achieve what you want in several ways;

Not triggering any scripts (docs)

If on some environment you do not want to trigger any scripts, you can prevent this using the --no-scripts flag.

Documentation reads: --no-scripts: Skips execution of scripts defined in composer.json.

composer upgrade --no-scripts

This is especially useful when upgrading packages while your code is currently not working. It would also work if your only scripts are development and test-related.

Running one script separately (docs)

Simply run the specific command as needed:

composer run-script [--dev] [--no-dev] script

This is useful when you want to run a script only on specific occasions.

For example, on build systems that have to perform a certain script before running any tests; build systems offer configuration options to call custom scripts like the above.

Defining a condition in the command (docs)

Documentation reads: During a composer install or update process, a variable named COMPOSER_DEV_MODE will be added to the environment. If the command was run with the --no-dev flag, this variable will be set to 0, otherwise it will be set to 1.

An example could look like

"scripts": {
    "post-install-cmd": [
         "[ $COMPOSER_DEV_MODE -eq 0 ] || <your command>"
    ]
}

Personally i would say this is the recommended way if you are using containers.

Note: this does not work on windows, since it would need %COMPOSER_DEV_MODE%.

There are also packages (like scriptsdev by neronmoon) that help you achieve the same goal without having to type the above in all commands, using a dev-scripts section in the extra section in composer.json

Defining a condition in your PHP script (docs)

Call a PHP method, that checks your environment based on how your application already does this. You can even reuse this condition by combining it with the method above; "Defining a condition in the command".

"scripts": {
    "post-update-cmd": [
        "AppNameSpaceName\\YourClassName::methodName"
    ]
}

You can then go ahead and create the class, like so:

<?php

namespace AppNameSpaceName;

class YourClassName 
{
    methodName() {
         // do stuff
    }
}

In many modern frameworks there is already a mechanism present to determine the runtime environment of the application (Symfony way, Laravel way).

Yarn run (docs)

Since most PHP applications nowadays also transpile their javascript files, either NPM or Yarn would be installed. You can use the scripts section to run this part only on development machines/containers. For example:

yarn run dev-only-script

having a section in package.json

"scripts": {
    "dev-only-script": "rm some/folder && ln -s path/to/your/folder some/"
}

The point of this is would be to your composer.json clean. In yarn you could have scripts for dev-server, test and build.

0
14

It's not possible to choose different script for default install and the --dev option but you can use the method isDevMode() in Composer\Script\Event to run command only in a development enviroment. http://getcomposer.org/apidoc/master/Composer/Script/Event.html

14

You can use the COMPOSER_DEV_MODE environment variable (new in version 1.3.0-RC - 2016-12-11):

"scripts": {
    "post-install-cmd": [
        "[ $COMPOSER_DEV_MODE -eq 0 ] || echo devmode only"
    ]
},
6
  • I used to run this, but for some reason it started to show: sh: 1: [: -eq: unexpected operator. Anything in mind?
    – Fabiano
    Feb 4, 2019 at 16:51
  • @Fabiano I think the error means $COMPOSER_DEV_MODE is not set. Could it be that you are using a composer older than version 1.3.0-RC?
    – Veda
    Feb 6, 2019 at 14:30
  • Using composer version Composer 1.8.3 2019-01-30 08:31:33. I replaced sh commands to a PHP class static method call. It did add another directory and required autoloading with PSR-0, but in the end it was cleaner to add conditioned behaviour.
    – Fabiano
    Feb 7, 2019 at 10:35
  • @Veda same issue for me, I added "post-install-cmd": [ "export"] to see all variables and no COMPOSER_DEV_MODE variable exists. I have composer version 1.8.4
    – midlan
    Apr 12, 2020 at 22:46
  • @midlan, I don't know. How about using set? Maybe the variable hasn't been exporterd?
    – Veda
    Apr 13, 2020 at 13:28
7

You can achieve the same effect by setting up a custom script for the dev pathway, instead of using the post-update-cmd hook.

"scripts": {
    "update-behat": [
        "rm -rf vendor/Behat",
        "git clone git://github.com/Behat/Behat.git",
        "cp composer.phar Behat/composer.phar",
        "cd Behat && git submodule update --init",
        "cd Behat && php composer.phar install",
        "cd Behat && php composer.phar require guzzle/guzzle:3.0.*",
        "mv Behat vendor/Behat",
        "ln -sf ../Behat/bin/behat vendor/bin/"
    ],
    "dev-update": [
        "@composer update --dev",
        "@update-behat"
    ]
}

Then simply run php composer.phar dev-update

4

Run the following command.

 composer update --no-scripts
3

Here is the small package, that you may use to do that https://github.com/neronmoon/scriptsdev It adds ability to define only-dev scripts. usage

...
"extra": {
    "scripts-dev": {
    "post-install-cmd": [
        "npm install --dev"
    ],
    "post-update-cmd": "php ./someCoolCommand.php"
    },
}
...
-1

You can run either one and check,

composer update --no-scripts

or

 composer dump-autoload
1
  • 2
    Please share more details. How does this resolve the given problem? The --no-scripts part is part of a lot of other answers - please do not duplicate them, unless you want to add some new insights. If this is the case, share them in your answer
    – Nico Haase
    Mar 23, 2022 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.