How to know the project version at runtime

416 views
Skip to first unread message

niahoo osef

unread,
Sep 9, 2015, 11:04:24 AM9/9/15
to elixir-lang-talk
Hi everybody, 

Just a tiny question today : I would like to know if in production I will be able to call MyApp.Mixfile.project[:version] to know the version of the compiled project.

And if not, is there another way to get the version.

Thank you 


José Valim

unread,
Sep 9, 2015, 11:19:15 AM9/9/15
to elixir-l...@googlegroups.com
Please do not rely on mix functions during runtime. Use ":application.which_applications" to get this information.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/0e95ed04-4a9a-4165-b36a-55bb38c83a55%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eksperimental

unread,
Sep 9, 2015, 11:24:11 AM9/9/15
to elixir-l...@googlegroups.com
A few weeks ago, I added a function to ExDoc to print at run time it's
version number
https://github.com/elixir-lang/ex_doc/blob/e6c4a931111bbcb1db146715ed36ac7ee611c436/lib/ex_doc.ex#L21-L27

defmodule ExDoc do
@ex_doc_version Mix.Project.config[:version]

@doc """
Returns the ExDoc version (used in templates).
"""
@spec version :: String.t
def version, do: @ex_doc_version
end

So basically, you read store in an attribute at compile time, and you
obtain the value via Mix.Project.config/1
and bellow define a function that reads this attribute, and this is
what you call at run time.

niahoo osef

unread,
Sep 9, 2015, 11:43:25 AM9/9/15
to elixir-lang-talk, eksper...@autistici.org
Ok, thank you both !

It seems that your trick with mix works per project, so event dependencies can use it and still point to their own version, which is exactly what i need :)

eksperimental

unread,
Sep 9, 2015, 12:21:58 PM9/9/15
to elixir-l...@googlegroups.com
Cool. I didn't know that trick José,

I had a similar situation like this, trying to access the app version
number from a Task.
just keep in mind you need to start the app first, even if its the very
same project you are running

so, using José's example:

:application.start(:ex_doc)
{_, _, version} =
List.keyfind(:application.which_applications, :ex_doc, 0)

another way to obtain the version number,

:application.start(:ex_doc)
{:ok, version} = :application.get_key(:ex_doc, :vsn)


On Wed, 9 Sep 2015 17:18:53 +0200
José Valim <jose....@plataformatec.com.br> wrote:

> Please do not rely on mix functions during runtime. Use
> ":application.which_applications" to get this information.
>
>
>
> *José Valim*
> www.plataformatec.com.br
> Skype: jv.ptec
> Founder and Director of R&D
>
> On Wed, Sep 9, 2015 at 5:04 PM, niahoo osef
> <ludovic....@gmail.com> wrote:
>
> > Hi everybody,
> >
> > Just a tiny question today : I would like to know if in production
> > I will be able to call MyApp.Mixfile.project[:version] to know the
> > version of the compiled project.
> >
> > And if not, is there another way to get the version.
> >
> > Thank you
> >
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "elixir-lang-talk" group.
> > To unsubscribe from this group and stop receiving emails from it,
> > send an email to elixir-lang-ta...@googlegroups.com.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/elixir-lang-talk/0e95ed04-4a9a-4165-b36a-55bb38c83a55%40googlegroups.com
> > <https://groups.google.com/d/msgid/elixir-lang-talk/0e95ed04-4a9a-4165-b36a-55bb38c83a55%40googlegroups.com?utm_medium=email&utm_source=footer>
> > .
Reply all
Reply to author
Forward
0 new messages