hauleth

hauleth

PSA using Git tags as library version in Hex packages

For some time I had big problem with Mix that it didn’t supported fetching project version from Git, like Rebar3 does with {version, git} in .app.src. So finally I have crafted the snippet that works and does exactly that:

defp version do
  case :file.consult('hex_metadata.config') do
    {:ok, data} ->
      {"version", version} = List.keyfind(data, "version", 0)
      version
    _ ->
      version =
        case System.cmd("git", ~w[describe --dirty=+dirty]) do
          {version, 0} ->
            String.trim(version)

          {_, code} ->
            Mix.shell().error("Git exited with code #{code}, falling back to 0.0.0")

            "0.0.0"
        end

      case Version.parse(version) do
        {:ok, %Version{pre: ["pre" <> _ | _]} = version} ->
          to_string(version)

        {:ok, %Version{pre: []} = version} ->
          to_string(version)

        {:ok, %Version{patch: patch, pre: pre} = version} ->
          to_string(%{version | patch: patch + 1, pre: ["dev" | pre]})

        :error ->
          Mix.shell().error("Failed to parse #{version}, falling back to 0.0.0")

          "0.0.0"
      end
  end
end

And now use [version: version()] and it will automatically use version from Hex when available, Git version when project is fetched via Git, if everything fails then it will fall back to 0.0.0.

Unfortunately as Mix do not have a way to have per-project extensions preloaded it need to be distributed as such snippet and I cannot make it into library that would make it clearer.


I have used above in Watermelon and I have tested that it works as expected.

Most Liked

hauleth

hauleth

git describe --dirty=+dirty returns:

  • <tag> if current HEAD points to the annotated (or signed) tag
  • <tag>-<num>-g<hash> where <tag> is the newest annotated tag reachable from HEAD, <num> is amount of commits since that tag, and <hash> is hash of current HEAD
  • <version>+dirty where <version> is one of the above, this happens when there are uncommitted changes in current work tree

Rest of the code does that:

  • If there is hex_metadata.config file (which is always there for libs fetched from the Hex) it will use version from there
  • If project is fetched from Git then it will try to use git describe --dirty=+dirty
  • If version returned by Git contains pre version tag that starts with pre then it will leave it as is
  • If version returned by Git do not contain pre version tag (so we are directly on signed tag) then we leave version as is
  • If above didn’t match, then we bump patch version by one and add dev pre tag
  • If all of above fails - return version 0.0.0 (probably I should add tag dev there as well)

Where Next?

Popular in Discussions Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14350 124
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

Other popular topics Top

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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement