问题
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": "contact@lionel-peramo.com",
"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
回答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. That won't allow you to execute scripts, but will allow you to set-up a working project for your users.
来源:https://stackoverflow.com/questions/59919005/how-to-launch-scripts-after-installing-a-package-via-composer