{-# LANGUAGE TemplateHaskell #-}

module Data.Version.Extras
  ( gitAwareVersionInfo
  ) where


import Data.Version qualified as Data.Version
import Development.GitRev.Extras qualified as GitRev
import Language.Haskell.TH qualified as TH


gitAwareVersionInfo
  :: Data.Version.Version -- | The version, usually coming from Paths_<pkg>.version
  -> TH.ExpQ -- | A string that includes, if available, the git hash and the git commit date
gitAwareVersionInfo :: Version -> ExpQ
gitAwareVersionInfo Version
version = [| version' <> gitHash <> gitCommitDate |]
  where
    version' :: String
version' = Version -> String
Data.Version.showVersion Version
version

    gitCommitDate :: String
gitCommitDate = do
      let commitDate :: String
commitDate = $(GitRev.gitCommitDate)
      if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
commitDate then String
"" else String
" - " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
commitDate

    gitHash :: String
gitHash = do
      let hash :: String
hash = $(GitRev.gitHash)
      if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
hash then String
"" else String
" - git rev " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
hash