{-# OPTIONS_HADDOCK hide #-}
module Trace.Hpc.Codecov.Options
(
Options(..)
, defaultOptions
, emptyOptions
, parseOptions
, opt2rpt
, printHelp
, printVersion
, versionString
) where
import Control.Exception (throw, throwIO)
import Data.Version (showVersion)
import System.Console.GetOpt (ArgDescr (..), ArgOrder (..),
OptDescr (..), getOpt, usageInfo)
import System.Environment (getProgName)
import System.Directory (doesFileExist)
import Paths_hpc_codecov (version)
import Trace.Hpc.Codecov.Discover
import Trace.Hpc.Codecov.Exception
import Trace.Hpc.Codecov.Report
data Options = Options
{ Options -> FilePath
optTix :: FilePath
, Options -> [FilePath]
optMixDirs :: [FilePath]
, Options -> [FilePath]
optSrcDirs :: [FilePath]
, Options -> [FilePath]
optExcludes :: [String]
, Options -> Maybe FilePath
optOutFile :: Maybe FilePath
, Options -> Bool
optVerbose :: Bool
, Options -> FilePath
optRootDir :: FilePath
, Options -> Maybe FilePath
optBuildDir :: Maybe FilePath
, Options -> [FilePath]
optSkipDirs :: [String]
, Options -> Bool
optShowVersion :: Bool
, Options -> Bool
optShowNumeric :: Bool
, Options -> Bool
optShowHelp :: Bool
}
emptyOptions :: Options
emptyOptions :: Options
emptyOptions = Options :: FilePath
-> [FilePath]
-> [FilePath]
-> [FilePath]
-> Maybe FilePath
-> Bool
-> FilePath
-> Maybe FilePath
-> [FilePath]
-> Bool
-> Bool
-> Bool
-> Options
Options
{ optTix :: FilePath
optTix = HpcCodecovError -> FilePath
forall a e. Exception e => e -> a
throw HpcCodecovError
NoTarget
, optMixDirs :: [FilePath]
optMixDirs = []
, optSrcDirs :: [FilePath]
optSrcDirs = []
, optExcludes :: [FilePath]
optExcludes = []
, optOutFile :: Maybe FilePath
optOutFile = Maybe FilePath
forall a. Maybe a
Nothing
, optVerbose :: Bool
optVerbose = Bool
False
, optRootDir :: FilePath
optRootDir = FilePath
""
, optBuildDir :: Maybe FilePath
optBuildDir = Maybe FilePath
forall a. Maybe a
Nothing
, optSkipDirs :: [FilePath]
optSkipDirs = []
, optShowVersion :: Bool
optShowVersion = Bool
False
, optShowNumeric :: Bool
optShowNumeric = Bool
False
, optShowHelp :: Bool
optShowHelp = Bool
False
}
defaultOptions :: Options
defaultOptions :: Options
defaultOptions = Options
emptyOptions
{ optMixDirs :: [FilePath]
optMixDirs = [FilePath
".hpc"]
, optSrcDirs :: [FilePath]
optSrcDirs = [FilePath
""]
}
options :: [OptDescr (Options -> Options)]
options :: [OptDescr (Options -> Options)]
options =
[ FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'm'] [FilePath
"mix"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
d Options
o -> Options
o {optMixDirs :: [FilePath]
optMixDirs = FilePath -> [FilePath]
uncommas FilePath
d [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ Options -> [FilePath]
optMixDirs Options
o})
FilePath
"DIR")
FilePath
".mix file directory, can repeat\n\
\(default: .hpc)"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
's'] [FilePath
"src"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
d Options
o -> Options
o {optSrcDirs :: [FilePath]
optSrcDirs = FilePath -> [FilePath]
uncommas FilePath
d [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ Options -> [FilePath]
optSrcDirs Options
o})
FilePath
"DIR")
FilePath
"Source directory, can repeat\n\
\(default: current directory)"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'x'] [FilePath
"exclude"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
m Options
o -> Options
o {optExcludes :: [FilePath]
optExcludes = FilePath -> [FilePath]
uncommas FilePath
m [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ Options -> [FilePath]
optExcludes Options
o})
FilePath
"MODULE")
FilePath
"Module name to exclude, can repeat"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'o'] [FilePath
"out"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
p Options
o -> Options
o {optOutFile :: Maybe FilePath
optOutFile = FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
p}) FilePath
"FILE")
FilePath
"Output file\n\
\(default: stdout)"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'r'] [FilePath
"root"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
d Options
o -> Options
o {optRootDir :: FilePath
optRootDir = FilePath
d})
FilePath
"DIR")
FilePath
"Project root directory for TOOL\n\
\Usually the directory containing\n\
\'stack.yaml' or 'cabal.project'\n\
\(default: current directory)"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'b'] [FilePath
"build"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
d Options
o -> Options
o {optBuildDir :: Maybe FilePath
optBuildDir = FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
d})
FilePath
"DIR")
FilePath
"Build directory made by the TOOL\n\
\(default:\n\
\ - '.stack-work' for stack\n\
\ - 'dist-newstyle' for cabal)"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'X'] [FilePath
"skip"]
((FilePath -> Options -> Options)
-> FilePath -> ArgDescr (Options -> Options)
forall a. (FilePath -> a) -> FilePath -> ArgDescr a
ReqArg (\FilePath
d Options
o -> Options
o {optSkipDirs :: [FilePath]
optSkipDirs = FilePath -> [FilePath]
uncommas FilePath
d [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ Options -> [FilePath]
optSkipDirs Options
o})
FilePath
"DIR")
FilePath
"Basename of directory to skip while\n\
\searching data for TOOL, can repeat"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'v'] [FilePath
"verbose"]
((Options -> Options) -> ArgDescr (Options -> Options)
forall a. a -> ArgDescr a
NoArg (\Options
o -> Options
o {optVerbose :: Bool
optVerbose = Bool
True}))
FilePath
"Show verbose output"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [] [FilePath
"version"]
((Options -> Options) -> ArgDescr (Options -> Options)
forall a. a -> ArgDescr a
NoArg (\Options
o -> Options
o {optShowVersion :: Bool
optShowVersion = Bool
True}))
FilePath
"Show versoin and exit"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [] [FilePath
"numeric-version"]
((Options -> Options) -> ArgDescr (Options -> Options)
forall a. a -> ArgDescr a
NoArg (\Options
o -> Options
o {optShowNumeric :: Bool
optShowNumeric = Bool
True}))
FilePath
"Show numeric version and exit"
, FilePath
-> [FilePath]
-> ArgDescr (Options -> Options)
-> FilePath
-> OptDescr (Options -> Options)
forall a.
FilePath -> [FilePath] -> ArgDescr a -> FilePath -> OptDescr a
Option [Char
'h'] [FilePath
"help"]
((Options -> Options) -> ArgDescr (Options -> Options)
forall a. a -> ArgDescr a
NoArg (\Options
o -> Options
o {optShowHelp :: Bool
optShowHelp = Bool
True}))
FilePath
"Show this help and exit"
]
parseOptions :: [String]
-> Either [String] Options
parseOptions :: [FilePath] -> Either [FilePath] Options
parseOptions [FilePath]
args =
case ArgOrder (Options -> Options)
-> [OptDescr (Options -> Options)]
-> [FilePath]
-> ([Options -> Options], [FilePath], [FilePath])
forall a.
ArgOrder a
-> [OptDescr a] -> [FilePath] -> ([a], [FilePath], [FilePath])
getOpt ArgOrder (Options -> Options)
forall a. ArgOrder a
Permute [OptDescr (Options -> Options)]
options [FilePath]
args of
([Options -> Options]
flags, [FilePath]
rest, []) ->
let opts0 :: Options
opts0 = ((Options -> Options) -> Options -> Options)
-> Options -> [Options -> Options] -> Options
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Options -> Options) -> Options -> Options
forall a b. (a -> b) -> a -> b
($) Options
emptyOptions [Options -> Options]
flags
opts1 :: Options
opts1 = Options -> Options
fillDefaultIfNotGiven Options
opts0
in case [FilePath]
rest of
[] -> Options -> Either [FilePath] Options
forall a b. b -> Either a b
Right Options
opts1
(FilePath
tix:[FilePath]
_) -> Options -> Either [FilePath] Options
forall a b. b -> Either a b
Right (Options
opts1 {optTix :: FilePath
optTix = FilePath
tix})
([Options -> Options]
_, [FilePath]
_, [FilePath]
errs) -> [FilePath] -> Either [FilePath] Options
forall a b. a -> Either a b
Left [FilePath]
errs
fillDefaultIfNotGiven :: Options -> Options
fillDefaultIfNotGiven :: Options -> Options
fillDefaultIfNotGiven Options
opts = Options
opts
{ optMixDirs :: [FilePath]
optMixDirs = ([FilePath] -> Bool) -> (Options -> [FilePath]) -> [FilePath]
forall p. (p -> Bool) -> (Options -> p) -> p
fillIf [FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Options -> [FilePath]
optMixDirs
, optSrcDirs :: [FilePath]
optSrcDirs = ([FilePath] -> Bool) -> (Options -> [FilePath]) -> [FilePath]
forall p. (p -> Bool) -> (Options -> p) -> p
fillIf [FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Options -> [FilePath]
optSrcDirs
}
where
fillIf :: (p -> Bool) -> (Options -> p) -> p
fillIf p -> Bool
test Options -> p
fld =
let orig :: p
orig = Options -> p
fld Options
opts
in if p -> Bool
test p
orig
then Options -> p
fld Options
defaultOptions
else p
orig
data Target
= TixFile FilePath
| TestSuite BuildTool String
parseTarget :: String -> IO Target
parseTarget :: FilePath -> IO Target
parseTarget FilePath
str = do
Bool
file_found <- FilePath -> IO Bool
doesFileExist FilePath
str
if Bool
file_found
then Target -> IO Target
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Target -> IO Target) -> Target -> IO Target
forall a b. (a -> b) -> a -> b
$ FilePath -> Target
TixFile FilePath
str
else case (Char -> Bool) -> FilePath -> (FilePath, FilePath)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':') FilePath
str of
(FilePath
"cabal", Char
':':FilePath
name) -> Target -> IO Target
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Target -> IO Target) -> Target -> IO Target
forall a b. (a -> b) -> a -> b
$ BuildTool -> FilePath -> Target
TestSuite BuildTool
Cabal FilePath
name
(FilePath
"stack", Char
':':FilePath
name) -> Target -> IO Target
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Target -> IO Target) -> Target -> IO Target
forall a b. (a -> b) -> a -> b
$ BuildTool -> FilePath -> Target
TestSuite BuildTool
Stack FilePath
name
(FilePath
tool, Char
':':FilePath
_) -> HpcCodecovError -> IO Target
forall e a. Exception e => e -> IO a
throwIO (HpcCodecovError -> IO Target) -> HpcCodecovError -> IO Target
forall a b. (a -> b) -> a -> b
$ FilePath -> HpcCodecovError
InvalidBuildTool FilePath
tool
(FilePath, FilePath)
_ -> Target -> IO Target
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Target -> IO Target) -> Target -> IO Target
forall a b. (a -> b) -> a -> b
$ FilePath -> Target
TixFile FilePath
str
uncommas :: String -> [String]
uncommas :: FilePath -> [FilePath]
uncommas = FilePath -> [FilePath]
go
where
go :: FilePath -> [FilePath]
go FilePath
str = case (Char -> Bool) -> FilePath -> (FilePath, FilePath)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
',') FilePath
str of
(FilePath
cs, Char
',':FilePath
rest) -> FilePath
cs FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: FilePath -> [FilePath]
go FilePath
rest
(FilePath
cs, FilePath
_) -> [FilePath
cs]
opt2rpt :: Options -> IO Report
opt2rpt :: Options -> IO Report
opt2rpt Options
opt = do
let rpt1 :: Report
rpt1 = Report
forall a. Monoid a => a
mempty
{ reportMixDirs :: [FilePath]
reportMixDirs = Options -> [FilePath]
optMixDirs Options
opt
, reportSrcDirs :: [FilePath]
reportSrcDirs = Options -> [FilePath]
optSrcDirs Options
opt
, reportExcludes :: [FilePath]
reportExcludes = Options -> [FilePath]
optExcludes Options
opt
, reportOutFile :: Maybe FilePath
reportOutFile = Options -> Maybe FilePath
optOutFile Options
opt
, reportVerbose :: Bool
reportVerbose = Bool
verbose
}
tix :: FilePath
tix = Options -> FilePath
optTix Options
opt
verbose :: Bool
verbose = Options -> Bool
optVerbose Options
opt
Target
target <- FilePath -> IO Target
parseTarget FilePath
tix
case Target
target of
TixFile FilePath
path -> Report -> IO Report
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Report
rpt1 {reportTix :: FilePath
reportTix = FilePath
path})
TestSuite BuildTool
tool FilePath
name -> do
Report
rpt2 <- DiscoverArgs -> IO Report
discover DiscoverArgs :: BuildTool
-> FilePath
-> FilePath
-> Maybe FilePath
-> [FilePath]
-> Bool
-> DiscoverArgs
DiscoverArgs
{ da_tool :: BuildTool
da_tool = BuildTool
tool
, da_testsuite :: FilePath
da_testsuite = FilePath
name
, da_rootdir :: FilePath
da_rootdir = Options -> FilePath
optRootDir Options
opt
, da_builddir :: Maybe FilePath
da_builddir = Options -> Maybe FilePath
optBuildDir Options
opt
, da_skipdirs :: [FilePath]
da_skipdirs = Options -> [FilePath]
optSkipDirs Options
opt
, da_verbose :: Bool
da_verbose = Bool
verbose
}
Report -> IO Report
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Report -> IO Report) -> Report -> IO Report
forall a b. (a -> b) -> a -> b
$ Report
rpt1 Report -> Report -> Report
forall a. Monoid a => a -> a -> a
`mappend` Report
rpt2
printHelp :: IO ()
printHelp :: IO ()
printHelp = IO FilePath
getProgName IO FilePath -> (FilePath -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> (FilePath -> FilePath) -> FilePath -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
helpMessage
printVersion :: IO ()
printVersion :: IO ()
printVersion =
do FilePath
me <- IO FilePath
getProgName
FilePath -> IO ()
putStrLn (FilePath
me FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" version " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
versionString)
helpMessage :: String
-> String
helpMessage :: FilePath -> FilePath
helpMessage FilePath
name = FilePath -> [OptDescr (Options -> Options)] -> FilePath
forall a. FilePath -> [OptDescr a] -> FilePath
usageInfo FilePath
header [OptDescr (Options -> Options)]
options FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
footer
where
header :: FilePath
header = FilePath
"USAGE: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
name FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" [OPTIONS] TARGET\n\
\\n\
\Generate Codecov JSON coverage report for Haskell source codes\n\
\from .tix and .mix files made with hpc.\n\
\\n\
\TARGET is either a path to .tix file or 'TOOL:TEST_SUITE'.\n\
\Supported TOOL values are 'stack' and 'cabal'. When the TOOL is\n\
\'stack' and building project with multiple packages, use 'all' as\n\
\TEST_SUITE value to refer the combined report.\n\
\\n\
\OPTIONS:\n"
footer :: FilePath
footer = FilePath
"\
\\n\
\For more info, see:\n\
\\n\
\ https://github.com/8c6794b6/hpc-codecov#readme\n\
\\n"
versionString :: String
versionString :: FilePath
versionString = Version -> FilePath
showVersion Version
version