'How to launch scripts after installing a package via Composer?
I use the command composer require otra/otra:dev-develop --no-update --no-cache && composer update --no-autoloader
to install my own framework.
I have put this section in my composer.json
from my framework :
"scripts": {
"pre-install-cmd": "@composer config bin-dir bin/"
}
But Composer does not run it. Is this normal, does Composer consider this as a dependency and not the root package so it does not allow my script to run?
If this is the case, how can I have the same behaviour?
I want to :
- have my binary in the
bin
folder, notvendor/bin
without having to ask the user to do a symlink manually (or another solution) - copy a
web
folder from my framework to the root of the project.
Edit : with create-project
command
If I type composer create-project otra/otra:dev-develop crashtest --remove-vcs
, I get this composer.json
:
{
"name": "otra/otra",
"type": "library",
"description": "The OTRA PHP framework",
"keywords": ["framework"],
"homepage": "https://github.com/lperamo/otra",
"license": "X11",
"authors": [
{
"name": "Lionel Péramo",
"email": "[email protected]",
"homepage": "https://wexample.com"
}
],
"bin" : ["otra.php"],
"config": {
"bin-dir" : "bin/",
"sort-packages": true
},
"require": {
"ext-mbstring": "*",
"php": ">=7.4.0",
"symfony/yaml": "^4.0"
},
"require-dev": {
"ext-pdo": "*",
"ext-pdo_mysql": "*"
},
"scripts": {
"pre-install-cmd": "@composer config bin-dir bin/"
}
}
which is exactly the same as my framework so I cannot update it via Composer. I could with git if I do not use --remove-vcs
but it is not the goal.
The output of the composer
command is :
Installing otra/otra (dev-develop ab37237565155dab11812a7b2982d30ee240f051)
- Installing otra/otra (dev-develop ab37237): Cloning ab37237565 from cache
Created project in crashtest
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Solution 1:[1]
Only scripts defined in the project's proper composer.json
file are executed.
Scripts from required and installed packages are never executed because it would be terrible safety risk.
This is summarily explained in the docs:
Only scripts defined in the root package's composer.json are executed. If a dependency of the root package specifies its own scripts, Composer does not execute those additional scripts.
If your package users need to perform additional steps to use your package or library, explain those steps in your package documentation, and or provide scripts that they can execute manually and will perform those steps for them.
If your package is a "framework", as opposed to a library, what you could do is take advantage of composers create-project
command.
That would require you to setup are repository with the default structure for a project, which would in turn depend on your package.
That's the way it's done with Symfony's Skeleton, for example.
With this kind of setup, you can create custom installation scripts and activate them with the post-create-project-cmd
, and do some some extra setup steps, even interactive one, with something like this. (docs)
Be mindful that this script would only run when the package is installed using create-project
, and never when using require
.
Solution 2:[2]
Nobody mentioned, this can be achieved by creating a composer plugin and defining event handler for event post-package-install
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | JohnyProkie |