operations / project-bin-scripts
在顶级Composer项目中包含bin脚本。
2.1.0
2024-01-25 15:15 UTC
Requires
- composer-plugin-api: ^2.0
This package is auto-updated.
Last update: 2024-09-11 16:19:39 UTC
README
您的脚本在Composer的bin目录中。
此插件允许您将自定义脚本作为composer bins添加到项目中。
它的工作方式与依赖包的"bin"脚本一样:从您的供应商bin目录到脚本的链接被创建。
优势
- 您项目中的所有命令都可以从同一个目录,供应商bin目录中运行。
- Composer的bin目录和自动加载路径可通过bash或PHP变量访问。
- 脚本可以使用此路径确保它们调用确切的脚本版本,例如
drush
或npm
。
有关更多信息,请参阅Composer "Vendor Binaries" 文档。
用法
-
安装插件。
$ composer require operations/project-bin-scripts
-
创建脚本文件。
#!/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
-
添加到
composer.json
{ "bin": [ "scripts/hello-world" ] }
-
运行composer install
$ composer install
-
从composer bin路径运行您的脚本
./vendor/bin/hello-world
或者,如果您设置了PATH,只需使用命令。
PATH=$PATH:./vendor/bin hello-world
关于composer "bin"模式的说明
这是与composer.json
模式中依赖项相同的配置。在此处使用此相同配置可能会引起一些混淆。
使用此配置的主要原因是为了使插件能够使用与安装依赖项二进制文件相同的代码来安装项目二进制文件。
如果您认为这很令人困惑并应该更改,请提交一个pull request。
关于
- 由:Jon Pugh 创建