2012-04-13 10:13:52 +00:00
|
|
|
<!--
|
|
|
|
tagline: Expose command-line scripts from packages
|
|
|
|
-->
|
2012-08-29 20:30:47 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
# Vendor binaries and the `vendor/bin` directory
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
## What is a vendor binary?
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2012-01-22 21:06:38 +00:00
|
|
|
Any command line script that a Composer package would like to pass along
|
2013-01-14 18:01:09 +00:00
|
|
|
to a user who installs the package should be listed as a vendor binary.
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2012-01-22 21:06:38 +00:00
|
|
|
If a package contains other scripts that are not needed by the package
|
|
|
|
users (like build or compile scripts) that code should not be listed
|
2013-01-14 18:01:09 +00:00
|
|
|
as a vendor binary.
|
2012-01-18 17:04:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
## How is it defined?
|
|
|
|
|
|
|
|
It is defined by adding the `bin` key to a project's `composer.json`.
|
2013-01-14 18:01:09 +00:00
|
|
|
It is specified as an array of files so multiple binaries can be added
|
2012-01-18 17:04:55 +00:00
|
|
|
for any given project.
|
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"bin": ["bin/my-script", "bin/my-other-script"]
|
|
|
|
}
|
2012-01-18 17:04:55 +00:00
|
|
|
|
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
## What does defining a vendor binary in composer.json do?
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
It instructs Composer to install the package's binaries to `vendor/bin`
|
2012-01-18 17:04:55 +00:00
|
|
|
for any project that **depends** on that project.
|
|
|
|
|
2012-01-22 21:06:38 +00:00
|
|
|
This is a convenient way to expose useful scripts that would
|
2012-01-18 17:04:55 +00:00
|
|
|
otherwise be hidden deep in the `vendor/` directory.
|
|
|
|
|
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
## What happens when Composer is run on a composer.json that defines vendor binaries?
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
For the binaries that a package defines directly, nothing happens.
|
2012-01-18 17:04:55 +00:00
|
|
|
|
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
## What happens when Composer is run on a composer.json that has dependencies with vendor binaries listed?
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
Composer looks for the binaries defined in all of the dependencies. A
|
|
|
|
symlink is created from each dependency's binaries to `vendor/bin`.
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
Say package `my-vendor/project-a` has binaries setup like this:
|
2012-01-18 17:04:55 +00:00
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"name": "my-vendor/project-a",
|
|
|
|
"bin": ["bin/project-a-bin"]
|
|
|
|
}
|
2012-01-18 17:04:55 +00:00
|
|
|
|
|
|
|
Running `composer install` for this `composer.json` will not do
|
|
|
|
anything with `bin/project-a-bin`.
|
|
|
|
|
|
|
|
Say project `my-vendor/project-b` has requirements setup like this:
|
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"name": "my-vendor/project-b",
|
|
|
|
"requires": {
|
|
|
|
"my-vendor/project-a": "*"
|
|
|
|
}
|
2012-01-18 17:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Running `composer install` for this `composer.json` will look at
|
|
|
|
all of project-b's dependencies and install them to `vendor/bin`.
|
|
|
|
|
2012-01-18 20:11:16 +00:00
|
|
|
In this case, Composer will make `vendor/my-vendor/project-a/bin/project-a-bin`
|
|
|
|
available as `vendor/bin/project-a-bin`. On a Unix-like platform
|
|
|
|
this is accomplished by creating a symlink.
|
2012-01-18 17:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
## What about Windows and .bat files?
|
|
|
|
|
2012-01-18 20:11:16 +00:00
|
|
|
Packages managed entirely by Composer do not *need* to contain any
|
|
|
|
`.bat` files for Windows compatibility. Composer handles installation
|
2013-01-14 18:01:09 +00:00
|
|
|
of binaries in a special way when run in a Windows environment:
|
2012-01-18 20:11:16 +00:00
|
|
|
|
2013-01-26 03:52:28 +00:00
|
|
|
* A `.bat` file is generated automatically to reference the binary
|
2013-01-14 18:01:09 +00:00
|
|
|
* A Unix-style proxy file with the same name as the binary is generated
|
2012-01-18 20:11:16 +00:00
|
|
|
automatically (useful for Cygwin or Git Bash)
|
|
|
|
|
|
|
|
Packages that need to support workflows that may not include Composer
|
|
|
|
are welcome to maintain custom `.bat` files. In this case, the package
|
2013-01-14 18:01:09 +00:00
|
|
|
should **not** list the `.bat` file as a binary as it is not needed.
|
2012-01-19 06:54:59 +00:00
|
|
|
|
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
## Can vendor binaries be installed somewhere other than vendor/bin?
|
2012-01-19 06:54:59 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
Yes, there are two ways an alternate vendor binary location can be specified:
|
2012-01-19 06:54:59 +00:00
|
|
|
|
2013-01-14 18:01:09 +00:00
|
|
|
1. Setting the `bin-dir` configuration setting in `composer.json`
|
|
|
|
1. Setting the environment variable `COMPOSER_BIN_DIR`
|
2012-01-19 06:54:59 +00:00
|
|
|
|
|
|
|
An example of the former looks like this:
|
|
|
|
|
2012-02-29 14:56:53 +00:00
|
|
|
{
|
|
|
|
"config": {
|
|
|
|
"bin-dir": "scripts"
|
|
|
|
}
|
2012-01-19 06:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Running `composer install` for this `composer.json` will result in
|
2013-01-14 18:01:09 +00:00
|
|
|
all of the vendor binaries being installed in `scripts/` instead of
|
2012-04-13 10:13:52 +00:00
|
|
|
`vendor/bin/`.
|