{-# LANGUAGE CPP, TemplateHaskell #-}
module Hledger.Cli.Version (
progname,
version,
prognameandversion,
prognameanddetailedversion,
binaryfilename
)
where
import System.Info (os, arch)
import Text.Printf
import Hledger.Utils
progname, version, prognameandversion, prognameanddetailedversion :: String
progname = "hledger"
#ifdef VERSION
version = VERSION
#else
version = "dev build"
#endif
prognameandversion = progname ++ " " ++ version
prognameanddetailedversion = printf "%s %s" progname version
patchlevel :: String
#ifdef PATCHLEVEL
patchlevel = "." ++ show (PATCHLEVEL :: Int)
#else
patchlevel = ""
#endif
buildversion :: String
buildversion = version ++ patchlevel
binaryfilename :: String -> String
binaryfilename progname = prettify $ splitAtElement '.' buildversion
where
prettify (major:minor:bugfix:patches:[]) =
printf "%s-%s.%s%s%s-%s-%s%s" progname major minor bugfix' patches' os' arch suffix
where
bugfix'
| bugfix `elem` ["0"] = ""
| otherwise = '.' : bugfix
patches'
| patches/="0" = '+' : patches
| otherwise = ""
(os',suffix)
| os == "darwin" = ("mac","" :: String)
| os == "mingw32" = ("windows",".exe")
| otherwise = (os,"")
prettify (major:minor:bugfix:[]) = prettify [major,minor,bugfix,"0"]
prettify (major:minor:[]) = prettify [major,minor,"0","0"]
prettify (major:[]) = prettify [major,"0","0","0"]
prettify [] = error' "VERSION is empty, please fix"
prettify _ = error' "VERSION has too many components, please fix"