Browse Source

Handle project versioning via git tag & hooks

theenglishway (time) 4 years ago
parent
commit
bf8c642699
4 changed files with 57 additions and 3 deletions
  1. 1 0
      .gitignore
  2. 10 1
      README.md
  3. 14 2
      mix.exs
  4. 32 0
      priv/build/setup-git-hooks.sh

+ 1 - 0
.gitignore

@@ -34,3 +34,4 @@ npm-debug.log
 /priv/static/
 
 .idea/
+VERSION

+ 10 - 1
README.md

@@ -8,7 +8,7 @@ of mine in the aspects of build, integration and deployment.
 * [x] Document build
 * [] Implement and document cross-build
 * [] Install the project as a systemd service
-* [] Handle the current version via a git tag
+* [x] Handle the current version via a git tag
 * [] Build as `dev` or `prod`
 
 ## Preconditions
@@ -21,6 +21,15 @@ completely outdated, e.g. using `Mix.Config` when the standard is now `Config`.
 It is advised above all to update the configuration, and try to match the
 [master branch](https://github.com/phoenixframework/phoenix/tree/master/installer/templates/phx_single/config) 
 
+### Setup version from a git tag
+
+The project version is read from a git tag in the form "vX.Y.Z", provided the
+git hooks in `priv/build/setup-git-hooks.sh` are installed AND `mix.exs` is
+updated with the `from_file` function.
+
+The version can then be read directly from the VERSION file or using the command :
+`mix run -e "Mix.Project.config[:version] |> IO.puts"`
+
 ### PostgreSQL role
 
 In `dev` as well as in `prod` modes, the role used in `config/#{mode}.exs` must 

+ 14 - 2
mix.exs

@@ -4,7 +4,7 @@ defmodule Toy.MixProject do
   def project do
     [
       app: :toy,
-      version: "0.1.0",
+      version: from_file(),
       elixir: "~> 1.7",
       elixirc_paths: elixirc_paths(Mix.env()),
       compilers: [:phoenix, :gettext] ++ Mix.compilers(),
@@ -61,7 +61,19 @@ defmodule Toy.MixProject do
       setup: ["deps.get", "ecto.setup", "cmd npm install --prefix assets"],
       "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
       "ecto.reset": ["ecto.drop", "ecto.setup"],
-      test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
+      test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
+      version: ["run  -e \"Mix.Project.config[:version] |> IO.puts\""]
     ]
   end
+
+  defp from_file(file \\ "VERSION") do
+    with {:ok, described} <- File.read(file),
+         {:ok, version} <- described |> String.trim() |> to_semver_string() do
+      version
+    end
+  end
+
+  defp to_semver_string(described) when is_binary(described) do
+    with {:ok, _} <- described |> Version.parse(), do: {:ok, described}
+  end
 end

+ 32 - 0
priv/build/setup-git-hooks.sh

@@ -0,0 +1,32 @@
+#!/bin/sh
+# Install git hooks
+
+CHECK_FORMAT="mix format --check-formatted"
+WRITE_VERSION="git describe --always --tags --match "v*" | sed 's/^v//' | sed 's/-/+/2' > VERSION"
+
+cat <<EOF > .git/hooks/pre-commit
+#!/bin/sh
+$CHECK_FORMAT
+EOF
+
+cat <<EOF > .git/hooks/post-commit
+#!/bin/sh
+$WRITE_VERSION
+EOF
+
+cat <<EOF > .git/hooks/post-checkout
+#!/bin/sh
+$WRITE_VERSION
+EOF
+
+cat <<EOF > .git/hooks/post-merge
+#!/bin/sh
+$WRITE_VERSION
+EOF
+
+cat <<EOF > .git/hooks/post-rewrite
+#!/bin/sh
+$WRITE_VERSION
+EOF
+
+chmod +x .git/hooks/pre-commit .git/hooks/post-commit .git/hooks/post-checkout .git/hooks/post-merge .git/hooks/post-rewrite