php Composer - 仅在开发环境中运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13087088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:45:38  来源:igfitidea点击:

Composer - run scripts only in dev environment

phpjenkinscomposer-php

提问by Richard Knop

This is my composer.json file:

这是我的 composer.json 文件:

"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

回答by hakre

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

要做到非开发环境更新,而不会触发任何脚本,使用--no-scripts在命令行开关update命令

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.

默认情况下,Composer 脚本仅在基础包中执行。因此,您可以拥有一个用于开发的包,并在实时环境中使其成为实时系统的依赖项。

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

除此之外,我没有看到任何自动区分脚本的方法。

回答by Webber

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-scriptsflag.

如果在某些环境中您不想触发任何脚本,您可以使用该--no-scripts标志来防止这种情况。

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

文档内容::--no-scripts跳过在 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)

在命令中定义条件( docs)

Documentation reads: During a composer install or update process, a variable named COMPOSER_DEV_MODEwill 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.

文档内容如下:在 Composer 安装或更新过程中,一个名为的变量COMPOSER_DEV_MODE将被添加到环境中。如果命令使用 --no-dev 标志运行,则此变量将设置为 0,否则将设置为 1。

An example could look like

一个例子可能看起来像

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

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

如果您使用容器,我个人会说这是推荐的方法。

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

注意:这不适用于 Windows,因为它需要%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-scriptssection in the extrasection in composer.json

还有一些包(如neronmoon 的 scriptsdev)可以帮助您实现相同的目标,而无需在所有命令中键入上述内容,使用composer.jsondev-scripts中的extra部分中的一个部分

Defining a condition in your PHP script(docs)

在 PHP 脚本中定义条件( 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".

调用 PHP 方法,该方法根据您的应用程序已执行此操作的方式检查您的环境。您甚至可以通过将其与上述方法相结合来重用此条件;“在命令中定义条件”。

"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).

在许多现代框架中,已经存在一种机制来确定应用程序的运行时环境(Symfony 方式Laravel 方式)。

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:

由于现在大多数 PHP 应用程序也会转换它们的 javascript 文件,因此将安装 NPM 或 Yarn。您可以使用脚本部分仅在开发机器/容器上运行这部分。例如:

yarn run dev-only-script

having a section in package.json

在 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, testand build.

这一点是为了你的 composer.json 干净。在 yarn 中,您可以拥有dev-server,test和 的脚本build

回答by Christian Koch

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

这不是可以选择默认安装和不同的脚本--dev选项,但你可以使用的方法isDevMode()作曲\脚本\事件仅在一个开发环境运行命令。http://getcomposer.org/apidoc/master/Composer/Script/Event.html

回答by Veda

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

您可以使用COMPOSER_DEV_MODE环境变量(版本 1.3.0-RC - 2016-12-11 中的新内容):

"scripts": {
    "post-install-cmd": [
        "[ $COMPOSER_DEV_MODE -eq 0 ] || echo devmode only"
    ]
},

回答by Ali Gangji

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

您可以通过为 dev 路径设置自定义脚本来实现相同的效果,而不是使用post-update-cmd钩子。

"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

然后简单地运行 php composer.phar dev-update

回答by Krasnoperov Viltaly

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

这是一个小包,你可以用来做这个 https://github.com/neronmoon/scriptsdev它增加了定义 only-dev 脚本的能力。用法

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

回答by Majbah Habib

Run the following command.

运行以下命令。

 composer update --no-scripts