Programmatically determine a dependency version in Elixir Mix Task

647 views Asked by At

Is there a way to programmatically the current version of a installed dependency in a custom Mix task?

I have a package that includes some custom Mix tasks that run some generator commands. 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.

My hex package depends on Phoenix, but also supports Phoenix 1.3, 1.4 and the newly released 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; 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? 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.

1

There are 1 answers

2
Everett On BEST ANSWER

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.