Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir - how to dynamically fetch library version used in project?

I have a problem with fetching dependency version I use in one of the projects I work on. I want to fetch version of this dependency in my code. Is there any easy way to do that?

I tried following methods:

{:ok, dependency_version} = Mix.Dep.cached 
                           |> Enum.filter(fn(d) -> d.app == :dependency_name end) 
                           |> hd 
                           |> Map.fetch!(:status)

But as I can read in in docs of Mix.dep.cached this may return an empty array if MIX_NO_DEPS is set. Is there any reliable way to do that apart from making a function in dependency that returns the version of itself?

like image 723
Bartosz Łęcki Avatar asked Sep 02 '25 02:09

Bartosz Łęcki


1 Answers

You can get the version of the dependency using Application.spec/2, passing the name of the dependency as the first argument and :vsn as the second argument.

From Ecto master's example application:

$ iex -S mix
iex(1)> Application.spec(:ecto, :vsn)
'3.0.0-dev'
iex(2)> Application.spec(:postgrex, :vsn)
'0.14.0-dev'
like image 123
Dogbert Avatar answered Sep 05 '25 12:09

Dogbert