vrod

vrod

Get app versions at compile time?

I encountered this interesting thing while trying to add more info to Logger messages.

I can get the version of an app by running something like this:

:myapp
        |> Application.spec()
        |> Keyword.get(:vsn)
        |> List.to_string()

However, I thought this calculation maybe is expensive, so I tried to save this at compile time by putting it into a module attribute:

@app_ver :myapp
        |> Application.spec()
        |> Keyword.get(:vsn)
        |> List.to_string()

However I have noticed that Application.spec/1 returns nil at compile time. Is there a way to read and store an application version at compile time? Or is the version only known AFTER compilation?

Most Liked

wojtekmach

wojtekmach

Hex Core Team

You cannot access the application spec because your application is being compiled. Fortunately, you can access information about your Mix project like this:

defmodule Foo do
  IO.inspect(Mix.Project.config()[:version])
end
wojtekmach

wojtekmach

Hex Core Team

You can grab information about your deps in that project config too.

I believe the dep apps are automatically loaded (if not, you can Application.load/1 them) so you can definitely call Application.spec/1 and it will work :slight_smile:

vrod

vrod

I found solution to this: just add mix.lock to .formatter.exs:

[
  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}", "mix.lock"]
]

and then the above works!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement