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 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews