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
git describe --dirty=+dirty returns:
<tag>if currentHEADpoints to the annotated (or signed) tag<tag>-<num>-g<hash>where<tag>is the newest annotated tag reachable fromHEAD,<num>is amount of commits since that tag, and<hash>is hash of currentHEAD<version>+dirtywhere<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.configfile (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
prethen 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
devpre tag - If all of above fails - return version
0.0.0(probably I should add tagdevthere as well)
1
Popular in Discussions
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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
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
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
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...
New
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
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
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
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
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
Chat & Discussions>Discussions
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









