operations/project-bin-scripts

在顶级Composer项目中包含bin脚本。

2.1.0 2024-01-25 15:15 UTC

README

您的脚本在Composer的bin目录中。

此插件允许您将自定义脚本作为composer bins添加到项目中。

它的工作方式与依赖包的"bin"脚本一样:从您的供应商bin目录到脚本的链接被创建。

优势

  1. 您项目中的所有命令都可以从同一个目录,供应商bin目录中运行。
  2. Composer的bin目录和自动加载路径可通过bash或PHP变量访问。
  3. 脚本可以使用此路径确保它们调用确切的脚本版本,例如drushnpm

有关更多信息,请参阅Composer "Vendor Binaries" 文档

用法

  1. 安装插件。

    $ composer require operations/project-bin-scripts
    
  2. 创建脚本文件。

    #!/usr/bin/env bash
    # File: scripts/hello-world
    
    # If your script wants to rely on the other scripts in bin-dir, check for $COMPOSER_RUNTIME_BIN_DIR
    if [[ -z $COMPOSER_RUNTIME_BIN_DIR ]]; then
      echo "ERROR: The COMPOSER_RUNTIME_BIN_DIR environment variable is missing. Run this script from the composer vendor bin directory."
      exit 1
    fi
    
    # Set the PATH variable to include COMPOSER_RUNTIME_BIN_DIR to allow calling
    # other bin scripts directly.
    PATH=$PATH:$COMPOSER_RUNTIME_BIN_DIR
    
    echo "Hello World!"
    echo "You are here:"
    pwd
    
    # To confirm this works, try using "which"
    echo "You are using this drush: "
    which drush
    drush --version
    
  3. 添加到composer.json

    {
      "bin": [
        "scripts/hello-world"
      ]
    }
  4. 运行composer install

    $ composer install
    
  5. 从composer bin路径运行您的脚本

    ./vendor/bin/hello-world

    或者,如果您设置了PATH,只需使用命令。

    PATH=$PATH:./vendor/bin
    hello-world

关于composer "bin"模式的说明

这是与composer.json模式中依赖项相同的配置。在此处使用此相同配置可能会引起一些混淆。

使用此配置的主要原因是为了使插件能够使用与安装依赖项二进制文件相同的代码来安装项目二进制文件。

如果您认为这很令人困惑并应该更改,请提交一个pull request。

关于