1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
{-# LANGUAGE CPP, TemplateHaskell #-}
{-
Version number-related utilities. See also the Makefile.
-}
module Hledger.Cli.Version (
progname,
version,
prognameandversion,
binaryfilename
)
where
import System.Info (os, arch)
import Text.Printf
import Hledger.Utils
-- package name and version from the cabal file
progname, version, prognameandversion :: String
progname = "hledger"
#ifdef VERSION
version = VERSION
#else
version = ""
#endif
prognameandversion = progname ++ " " ++ version
-- developer build version strings include PATCHLEVEL (number of
-- patches since the last tag). If defined, it must be a number.
patchlevel :: String
#ifdef PATCHLEVEL
patchlevel = "." ++ show (PATCHLEVEL :: Int)
#else
patchlevel = ""
#endif
-- the package version plus patchlevel if specified
buildversion :: String
buildversion = version ++ patchlevel
-- | Given a program name, return a precise platform-specific executable
-- name suitable for naming downloadable binaries. Can raise an error if
-- the version and patch level was not defined correctly at build time.
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"{-,"98","99"-}] = ""
| 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"
|