Composer - run scripts only in dev environment

composer run-script manually
composer scripts-dev only
composerscriptevent
composer script pass arguments
composer autoload
composer post-install-cmd not executed
composer dump-autoload
composer environment variables

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

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 executed in the base package. 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.

Scripts, Hello, Just an idea, as I was working with the autoload-dev for a project, how about having a scripts-dev section in the composer.json file to run  So basically, running this script on our --non-dev environment was messing with our linked directories. So the thing here, could be to put this script only on dev (non symlinked envs), which is run only on --dev scripts.

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

Running scripts only on dev environments · Issue #4358 · composer , 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  Note: 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. Event classes# When an event is fired, your PHP callback receives as first argument a Composer\EventDispatcher\Event object.

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.

Composer - run scripts only in dev environment, --no-scripts: Skips execution of scripts defined in composer.json . If you only want to update a few packages and not all, you can list them as such: --update-​no-dev: Run the dependency update with the --no-dev option. Make sure your global vendor binaries directory is in your $PATH environment variable, you can​  rm -rf vendor/ composer install --no-dev -vvv and it installed fine. My deploy script to staging server (which also has older PHP to verify compatibility with the live environment) currently has composer update --no-dev. The script is invoked through Bitbucket webhook when a commit comes in.

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

Command-line interface / Commands, Composer is a tool for dependency management in PHP. It allows you to Autoload classes from classmap only. Implicitely enable optimize_autoloader. Recommended especially for production, but can take a bit of time to run. aliases: no-dev Skips the execution of all scripts defined in composer.json (see --no-scripts). Controlling your dev environment Starting and stopping Hyperledger Fabric. You control your runtime using a set of scripts which you'll find in ~/fabric-dev-servers if you followed the suggested defaults. The first time you start up a new runtime, you'll need to run the start script, then generate a PeerAdmin card:

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"
    ]
},

composer – Dependency Manager for PHP, either explicitly: composer run-script phpcs # or via shortcut: composer phpcs I'​ve created c:\dev\php\x.cmd with this content ( %* passes through to write composer up (or only x up if you also applied the previous tip) Timeout can be configured in the ENV variable COMPOSER_PROCESS_TIMEOUT : Require this package in your composer.json and run composer update (or run composer require menthol/scripts-dev:dev-master directly): Publish the config. Configure your local dev environment by adding 'dev' => true, to your local package config file. You can configure your composer.json.

Have you tried Composer Scripts? You may not need Phing , In this tutorial, you'll install and get started with Composer on an Ubuntu Now execute the following PHP script to verify that the installation script is safe to run: Updating dependencies (including require-dev) Package operations: 1 install, control - you only need to include the composer.json and composer.lock files. General rule is that you want packages from require-dev section only in development (dev) environments, for example local environment. Packages in require-dev section are packages which help you debug app, run tests etc. At staging and production environment you probably want only packages from require section.

How To Install and Use Composer on Ubuntu 18.04, The composer.json file schema contains a script section that allows project to get started by simply running git clone and composer require. composer test will no longer work in environments where /dev/tty is not available. The autoloader generation skips the autoload-dev rules.--lock: Only updates the lock file hash to suppress warning about the lock file being out of date.--no-autoloader: Skips autoloader generation.--no-scripts: Skips execution of scripts defined in composer.json.

Writing Composer Scripts, To install this package on only development systems, add the --dev flag to your composer command: public function register() { if ($this->app->environment() !== If run composer update --no-dev or composer update get error Script php artisan ide-helper:generate handling the post-update-cmd event  For Composer-based projects that are pure PHP, and include all of their test and build tools in the require-dev section of their composer.json file, it makes a lot of sense to use Composer scripts as a lightweight replacement for Make.

Comments
  • "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...
  • @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-
  • I used to run this, but for some reason it started to show: sh: 1: [: -eq: unexpected operator. Anything in mind?
  • @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?
  • 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.