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.

First 2 of 2 Posts Switch mode

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 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement