git-annex/Build/Version.hs

75 lines
1.8 KiB
Haskell
Raw Normal View History

{- Package version determination. -}
2013-10-06 21:48:38 +00:00
{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
2013-10-06 21:48:38 +00:00
module Build.Version where
import Data.List
import System.Environment
import Data.Char
import System.Process
2016-01-26 23:12:33 +00:00
import Control.Applicative
import Prelude
2013-10-06 21:48:38 +00:00
import Utility.Monad
import Utility.Exception
2019-01-22 23:37:49 +00:00
import Utility.Misc
2013-10-06 21:48:38 +00:00
type Version = String
2013-10-06 21:48:38 +00:00
{- Set when making an official release. (Distribution vendors should set
- this too.) -}
isReleaseBuild :: IO Bool
isReleaseBuild = (== Just "1") <$> catchMaybeIO (getEnv "RELEASE_BUILD")
2013-10-06 21:48:38 +00:00
{- Version comes from the CHANGELOG, plus the git rev of the last commit.
2013-10-06 21:48:38 +00:00
- This works for autobuilds, ad-hoc builds, etc.
-
- If git or a git repo is not available, or something goes wrong,
- or this is a release build, just use the version from the CHANGELOG. -}
getVersion :: IO Version
2013-10-06 21:48:38 +00:00
getVersion = do
changelogversion <- getChangelogVersion
ifM (isReleaseBuild)
2013-10-06 21:48:38 +00:00
( return changelogversion
, catchDefaultIO changelogversion $ do
gitversion <- takeWhile (\c -> isAlphaNum c) <$> readProcess "sh"
2013-10-06 21:48:38 +00:00
[ "-c"
, "git log -n 1 --format=format:'%h'"
2013-10-06 21:48:38 +00:00
] ""
return $ if null gitversion
then changelogversion
else concat
[ changelogversion
, "-g"
, gitversion
]
2013-10-06 21:48:38 +00:00
)
getChangelogVersion :: IO Version
2013-10-06 21:48:38 +00:00
getChangelogVersion = do
changelog <- readFile "CHANGELOG"
2013-10-06 21:48:38 +00:00
let verline = takeWhile (/= '\n') changelog
return $ middle (words verline !! 1)
where
middle = drop 1 . init
writeVersion :: Version -> IO ()
2019-01-22 23:37:49 +00:00
writeVersion ver = catchMaybeIO (readFileStrict f) >>= \case
Just s | s == body -> return ()
_ -> writeFile f body
where
body = unlines $ concat
[ header
, ["packageversion :: String"]
, ["packageversion = \"" ++ ver ++ "\""]
, footer
]
header = [
"{- Automatically generated. -}"
, ""
]
footer = []
f = "Build/Version"