hauleth

hauleth

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.

Showing Posts 1 to 2

axelson

axelson

Scenic Core Team

Do you have an example of what a git version looks like?

hauleth

hauleth OP

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)
— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews