简体   繁体   English

以编程方式确定 Elixir 混合任务中的依赖版本

[英]Programmatically determine a dependency version in Elixir Mix Task

Is there a way to programmatically the current version of a installed dependency in a custom Mix task?有没有办法以编程方式在自定义 Mix 任务中安装依赖项的当前版本?

I have a package that includes some custom Mix tasks that run some generator commands.我有一个 package,其中包含一些运行某些生成器命令的自定义 Mix 任务。 I want to determine the version of Phoenix the user of my hex package is running so that I can conditionally perform some logic in my generator tasks.我想确定我的 hex package 用户正在运行的 Phoenix 版本,以便我可以有条件地在我的生成器任务中执行一些逻辑。

My hex package depends on Phoenix, but also supports Phoenix 1.3, 1.4 and the newly released 1.5.我的十六进制 package 依赖于 Phoenix,但也支持 Phoenix 1.3、1.4 和新发布的 1.5。

I realize I could release a new version of the hex package that has a fixed dependency requirement of phoenix 1.5, while still back-porting changes to the older major versions of my package;我意识到我可以发布一个新版本的十六进制 package,它具有 phoenix 1.5 的固定依赖要求,同时仍然向后移植更改到我的 package 的旧主要版本; but I'd prefer to keep a single mainline branch that supports as many versions as possible for the time being.但我宁愿保留一个单一的主线分支,暂时支持尽可能多的版本。

Is there a "decent" way to do this, or should I parse the mix.lock file myself inside the mix task?有没有一种“体面”的方式来做到这一点,或者我应该在混合任务中自己解析mix.lock文件? Doing so seems pretty brittle, and I don't think my package code should be searching around and parsing the lockfile of a larger project it is included in.这样做似乎很脆弱,我认为我的 package 代码不应该四处搜索并解析它包含在的一个更大项目的锁文件。

I haven't tested this for your exact requirements, but you might be able to do something like this:我没有根据您的确切要求对此进行测试,但您可以执行以下操作:

app_version =
  Application.spec(:phoenix)[:vsn]
  |> List.to_string()
# OR
:application.get_key(:phoenix, :vsn)
|> List.to_string()

The following snippet might also be useful: if you have a module but you don't know the atom app name it belongs to:以下代码片段也可能有用:如果您有一个模块但您不知道它所属的原子应用程序名称:

app_name = Application.get_application(SomeApp)

See the Application docs for more details.有关更多详细信息,请参阅应用程序文档

Hope that helps.希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM