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?
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'