operations/composer-project-bins

此包已被废弃,不再维护。作者建议使用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 schema的"bin"说明

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

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

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

关于