Skip to content
This repository has been archived by the owner on Sep 15, 2019. It is now read-only.

Commit

Permalink
[#61] Add Util.Version module (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrom911 authored Apr 17, 2018
1 parent c669722 commit cf2ad8f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.9.0
=====

* [#61](https://github.com/serokell/serokell-util/issues/61):
Add utility feature to extract git revision in `Serokell.Util.Version`.
* [#45](https://github.com/serokell/serokell-util/issues/45):
Move `Aeson.Options` to separate package [`aeson-options`][1].
* [#34](https://github.com/serokell/serokell-util/issues/34):
Expand Down
3 changes: 3 additions & 0 deletions serokell-util.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ library
Serokell.Util.Text
Serokell.Util.Trace
Serokell.Util.Verify
Serokell.Util.Version

other-modules: Serokell.Data.Variant.Class
Serokell.Data.Variant.Helpers
Expand All @@ -64,12 +65,14 @@ library
, mtl
, o-clock ^>= 0.1.1
, parsec
, process
, QuickCheck >= 2.8.1
, quickcheck-instances
, scientific
, template-haskell
, text
, text-format
, th-lift-instances
, transformers
, universum ^>= 1.1.0
, unordered-containers >= 0.2.7.0
Expand Down
1 change: 1 addition & 0 deletions src/Serokell/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ import Serokell.Util.StaticAssert as Exports
import Serokell.Util.Text as Exports
import Serokell.Util.Trace as Exports
import Serokell.Util.Verify as Exports
import Serokell.Util.Version as Exports
45 changes: 45 additions & 0 deletions src/Serokell/Util/Version.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Serokell.Util.Version
( -- * Git revision
retrieveGitRev
) where

import Universum

import Instances.TH.Lift ()
import System.Environment (lookupEnv)
import System.Exit (ExitCode (..))
import System.Process (readProcessWithExitCode)

import qualified Data.Text as Text
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH

{- | Gets the git revision.
For example, you can use it configuring CLI options:
@
versionOption = infoOption
("Git revision: " <> toString $(retrieveGitRev))
(long "version" <> help "Show version")
@
You'll need @-XTemplateHaskell@.
Also note, that in order to see the latest git revision for the latest commit
you should force recompilation of the file which contains @$(retrieveGitRev)@.
-}
retrieveGitRev :: TH.Q TH.Exp
retrieveGitRev = do
cti <- TH.runIO $ Text.strip . fromString <$> retrieveGit
TH.lift cti
where
retrieveGit :: IO String
retrieveGit = whenNothingM (lookupEnv "GITREV") retrieveFromGitExecutable

retrieveFromGitExecutable :: IO String
retrieveFromGitExecutable = do
(exitCode, output, _) <-
readProcessWithExitCode "git" ["rev-parse", "--verify", "HEAD"] ""
pure $ case exitCode of
ExitSuccess -> output
_ -> fail "Couldn't retrieve 'git' revision"

0 comments on commit cf2ad8f

Please sign in to comment.