通过Composer安装软件包后如何启动脚本? [英] How to launch scripts after installing a package via Composer?

查看:161
本文介绍了通过Composer安装软件包后如何启动脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用命令 composer require otra / otra:dev-develop --no-update --no-cache&& composer update --no-autoloader 来安装我自己的框架。


我已将此部分放在我的 composer.json 来自我的框架:

 脚本:{
pre-install-cmd: @作曲者配置bin-dir bin /
}

但是Composer不会运行它。这是正常现象吗,Composer是否将其视为依赖项而不是根包,因此它不允许我的脚本运行?


如果是这种情况,我该怎么做? ?


我要:



  • 将我的二进制文件放在 bin 文件夹,而不是 vendor / bin 文件夹,而无需要求用户手动进行符号链接(或其他解决方案)

  • 复制从我的框架到项目根目录的 web 文件夹。


编辑:使用 create-project 命令
如果我键入 composer create-project otra / otra:dev-develop crashtest --remove -vcs ,我得到这个 composer.json

  {
名称: otra / otra,
类型:库,
描述: OTRA PHP框架,
keywords:[ framework],
homepage: https://github.c om / lperamo / otra,
许可证: X11,
作者:[
{
名称: LionelPéramo ,
电子邮件: contact@lionel-peramo.com,
首页: https://wexample.com;
}
],
bin :: [ otra.php],
config:{
bin-dir : bin /,
sort-packages:真
},
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 /;
}
}

与我的框架完全相同,因此无法通过Composer更新。如果不使用-remove-vcs ,我可以使用git,但这不是目标。


composer 命令为:


安装otra / otra(dev-develop ab37237565155dab11812a7b2982d30ee240f051)



  • 安装otra / otra(dev-develop ab37237):从缓存中克隆ab37237565


在崩溃测试中创建了项目


使用包信息加载作曲家存储库


从锁定文件安装依赖项(包括require-dev)



解决方案

仅执行项目正确的 composer.json 文件中定义的脚本。



必需的和已安装的软件包中的脚本永远不会执行,因为这会带来极大的安全风险。



在文档中


仅执行在根包的composer.json中定义的脚本。如果根软件包的依赖项指定了自己的脚本,则Composer不会执行这些其他脚本。


如果您的软件包用户需要执行使用软件包或库的其他步骤,在软件包文档中说明这些步骤,或提供可以手动执行并为它们执行这些步骤的脚本。






如果您的软件包是框架而不是库,则您可以利用作曲家 create-project 命令



这将要求您使用项目的默认结构设置存储库,而该存储库又将依赖取决于您的软件包。



例如,这就是使用Symfony的Skeleton完成的方式。那将不允许您执行脚本,但是将允许您为用户设置一个有效的项目。


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, not vendor/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

解决方案

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.

这篇关于通过Composer安装软件包后如何启动脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆