{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
module Distribution.Simple.Hpc
( Way (..)
, guessWay
, htmlDir
, mixDir
, tixDir
, tixFilePath
, HPCMarkupInfo (..)
, markupPackage
) where
import Distribution.Compat.Prelude
import Prelude ()
import Distribution.ModuleName (ModuleName, main)
import Distribution.PackageDescription
( TestSuite (..)
, testModules
)
import qualified Distribution.PackageDescription as PD
import Distribution.Pretty
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo (..)
, interpretSymbolicPathLBI
, mbWorkDirLBI
)
import Distribution.Simple.Program
( hpcProgram
, requireProgramVersion
)
import Distribution.Simple.Program.Hpc (markup, union)
import Distribution.Simple.Utils (notice)
import Distribution.Types.UnqualComponentName
import Distribution.Utils.Path
import Distribution.Verbosity (Verbosity ())
import Distribution.Version (anyVersion)
import System.Directory (createDirectoryIfMissing, doesFileExist)
data Way = Vanilla | Prof | Dyn | ProfDyn
deriving (Way
Way -> Way -> Bounded Way
forall a. a -> a -> Bounded a
$cminBound :: Way
minBound :: Way
$cmaxBound :: Way
maxBound :: Way
Bounded, Int -> Way
Way -> Int
Way -> [Way]
Way -> Way
Way -> Way -> [Way]
Way -> Way -> Way -> [Way]
(Way -> Way)
-> (Way -> Way)
-> (Int -> Way)
-> (Way -> Int)
-> (Way -> [Way])
-> (Way -> Way -> [Way])
-> (Way -> Way -> [Way])
-> (Way -> Way -> Way -> [Way])
-> Enum Way
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Way -> Way
succ :: Way -> Way
$cpred :: Way -> Way
pred :: Way -> Way
$ctoEnum :: Int -> Way
toEnum :: Int -> Way
$cfromEnum :: Way -> Int
fromEnum :: Way -> Int
$cenumFrom :: Way -> [Way]
enumFrom :: Way -> [Way]
$cenumFromThen :: Way -> Way -> [Way]
enumFromThen :: Way -> Way -> [Way]
$cenumFromTo :: Way -> Way -> [Way]
enumFromTo :: Way -> Way -> [Way]
$cenumFromThenTo :: Way -> Way -> Way -> [Way]
enumFromThenTo :: Way -> Way -> Way -> [Way]
Enum, Way -> Way -> Bool
(Way -> Way -> Bool) -> (Way -> Way -> Bool) -> Eq Way
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Way -> Way -> Bool
== :: Way -> Way -> Bool
$c/= :: Way -> Way -> Bool
/= :: Way -> Way -> Bool
Eq, ReadPrec [Way]
ReadPrec Way
Int -> ReadS Way
ReadS [Way]
(Int -> ReadS Way)
-> ReadS [Way] -> ReadPrec Way -> ReadPrec [Way] -> Read Way
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Way
readsPrec :: Int -> ReadS Way
$creadList :: ReadS [Way]
readList :: ReadS [Way]
$creadPrec :: ReadPrec Way
readPrec :: ReadPrec Way
$creadListPrec :: ReadPrec [Way]
readListPrec :: ReadPrec [Way]
Read, Int -> Way -> ShowS
[Way] -> ShowS
Way -> String
(Int -> Way -> ShowS)
-> (Way -> String) -> ([Way] -> ShowS) -> Show Way
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Way -> ShowS
showsPrec :: Int -> Way -> ShowS
$cshow :: Way -> String
show :: Way -> String
$cshowList :: [Way] -> ShowS
showList :: [Way] -> ShowS
Show)
hpcDir
:: SymbolicPath Pkg (Dir Dist)
-> Way
-> SymbolicPath Pkg (Dir Artifacts)
hpcDir :: SymbolicPath Pkg ('Dir Dist)
-> Way -> SymbolicPath Pkg ('Dir Artifacts)
hpcDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way = SymbolicPath Pkg ('Dir Dist)
distPref SymbolicPath Pkg ('Dir Dist)
-> RelativePath Dist ('Dir Artifacts)
-> SymbolicPath Pkg ('Dir Artifacts)
forall p q r. PathLike p q r => p -> q -> r
</> String -> RelativePath Dist ('Dir Artifacts)
forall from (to :: FileOrDir).
HasCallStack =>
String -> RelativePath from to
makeRelativePathEx (String
"hpc" String -> ShowS
forall p q r. PathLike p q r => p -> q -> r
</> String
wayDir)
where
wayDir :: String
wayDir = case Way
way of
Way
Vanilla -> String
"vanilla"
Way
Prof -> String
"prof"
Way
Dyn -> String
"dyn"
Way
ProfDyn -> String
"prof_dyn"
mixDir
:: SymbolicPath Pkg (Dir Dist)
-> Way
-> SymbolicPath Pkg (Dir Mix)
mixDir :: SymbolicPath Pkg ('Dir Dist) -> Way -> SymbolicPath Pkg ('Dir Mix)
mixDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way = SymbolicPath Pkg ('Dir Dist)
-> Way -> SymbolicPath Pkg ('Dir Artifacts)
hpcDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way SymbolicPath Pkg ('Dir Artifacts)
-> RelativePath Artifacts ('Dir Mix) -> SymbolicPath Pkg ('Dir Mix)
forall p q r. PathLike p q r => p -> q -> r
</> String -> RelativePath Artifacts ('Dir Mix)
forall from (to :: FileOrDir).
HasCallStack =>
String -> RelativePath from to
makeRelativePathEx String
"mix"
tixDir
:: SymbolicPath Pkg (Dir Dist)
-> Way
-> SymbolicPath Pkg (Dir Tix)
tixDir :: SymbolicPath Pkg ('Dir Dist) -> Way -> SymbolicPath Pkg ('Dir Tix)
tixDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way = SymbolicPath Pkg ('Dir Dist)
-> Way -> SymbolicPath Pkg ('Dir Artifacts)
hpcDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way SymbolicPath Pkg ('Dir Artifacts)
-> RelativePath Artifacts ('Dir Tix) -> SymbolicPath Pkg ('Dir Tix)
forall p q r. PathLike p q r => p -> q -> r
</> String -> RelativePath Artifacts ('Dir Tix)
forall from (to :: FileOrDir).
HasCallStack =>
String -> RelativePath from to
makeRelativePathEx String
"tix"
tixFilePath
:: SymbolicPath Pkg (Dir Dist)
-> Way
-> FilePath
-> SymbolicPath Pkg File
tixFilePath :: SymbolicPath Pkg ('Dir Dist)
-> Way -> String -> SymbolicPath Pkg 'File
tixFilePath SymbolicPath Pkg ('Dir Dist)
distPref Way
way String
name = SymbolicPath Pkg ('Dir Dist) -> Way -> SymbolicPath Pkg ('Dir Tix)
tixDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way SymbolicPath Pkg ('Dir Tix)
-> RelativePath Tix 'File -> SymbolicPath Pkg 'File
forall p q r. PathLike p q r => p -> q -> r
</> String -> RelativePath Tix 'File
forall from (to :: FileOrDir).
HasCallStack =>
String -> RelativePath from to
makeRelativePathEx (String
name String -> ShowS
forall p. FileLike p => p -> String -> p
<.> String
"tix")
htmlDir
:: SymbolicPath Pkg (Dir Dist)
-> Way
-> SymbolicPath Pkg (Dir Artifacts)
htmlDir :: SymbolicPath Pkg ('Dir Dist)
-> Way -> SymbolicPath Pkg ('Dir Artifacts)
htmlDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way = SymbolicPath Pkg ('Dir Dist)
-> Way -> SymbolicPath Pkg ('Dir Artifacts)
hpcDir SymbolicPath Pkg ('Dir Dist)
distPref Way
way SymbolicPath Pkg ('Dir Artifacts)
-> RelativePath Artifacts ('Dir Artifacts)
-> SymbolicPath Pkg ('Dir Artifacts)
forall p q r. PathLike p q r => p -> q -> r
</> String -> RelativePath Artifacts ('Dir Artifacts)
forall from (to :: FileOrDir).
HasCallStack =>
String -> RelativePath from to
makeRelativePathEx String
"html"
guessWay :: LocalBuildInfo -> Way
guessWay :: LocalBuildInfo -> Way
guessWay LocalBuildInfo
lbi
| LocalBuildInfo -> Bool
withProfExe LocalBuildInfo
lbi = Way
Prof
| LocalBuildInfo -> Bool
withDynExe LocalBuildInfo
lbi = Way
Dyn
| Bool
otherwise = Way
Vanilla
data HPCMarkupInfo = HPCMarkupInfo
{ HPCMarkupInfo -> [SymbolicPath Pkg ('Dir Artifacts)]
pathsToLibsArtifacts :: [SymbolicPath Pkg (Dir Artifacts)]
, HPCMarkupInfo -> [ModuleName]
libsModulesToInclude :: [ModuleName]
}
markupPackage
:: Verbosity
-> HPCMarkupInfo
-> LocalBuildInfo
-> SymbolicPath Pkg (Dir Dist)
-> PD.PackageDescription
-> [TestSuite]
-> IO ()
markupPackage :: Verbosity
-> HPCMarkupInfo
-> LocalBuildInfo
-> SymbolicPath Pkg ('Dir Dist)
-> PackageDescription
-> [TestSuite]
-> IO ()
markupPackage Verbosity
verbosity HPCMarkupInfo{[SymbolicPath Pkg ('Dir Artifacts)]
pathsToLibsArtifacts :: HPCMarkupInfo -> [SymbolicPath Pkg ('Dir Artifacts)]
pathsToLibsArtifacts :: [SymbolicPath Pkg ('Dir Artifacts)]
pathsToLibsArtifacts, [ModuleName]
libsModulesToInclude :: HPCMarkupInfo -> [ModuleName]
libsModulesToInclude :: [ModuleName]
libsModulesToInclude} LocalBuildInfo
lbi SymbolicPath Pkg ('Dir Dist)
testDistPref PackageDescription
pkg_descr [TestSuite]
suites = do
let tixFiles :: [SymbolicPath Pkg 'File]
tixFiles = (String -> SymbolicPath Pkg 'File)
-> [String] -> [SymbolicPath Pkg 'File]
forall a b. (a -> b) -> [a] -> [b]
map (SymbolicPath Pkg ('Dir Dist)
-> Way -> String -> SymbolicPath Pkg 'File
tixFilePath SymbolicPath Pkg ('Dir Dist)
testDistPref Way
way) [String]
testNames
mbWorkDir :: Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir = LocalBuildInfo -> Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDirLBI LocalBuildInfo
lbi
i :: SymbolicPathX allowAbsolute Pkg to -> String
i = LocalBuildInfo -> SymbolicPathX allowAbsolute Pkg to -> String
forall (allowAbsolute :: AllowAbsolute) (to :: FileOrDir).
LocalBuildInfo -> SymbolicPathX allowAbsolute Pkg to -> String
interpretSymbolicPathLBI LocalBuildInfo
lbi
[Bool]
tixFilesExist <- (SymbolicPath Pkg 'File -> IO Bool)
-> [SymbolicPath Pkg 'File] -> IO [Bool]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (String -> IO Bool
doesFileExist (String -> IO Bool)
-> (SymbolicPath Pkg 'File -> String)
-> SymbolicPath Pkg 'File
-> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymbolicPath Pkg 'File -> String
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> String
i) [SymbolicPath Pkg 'File]
tixFiles
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and [Bool]
tixFilesExist) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
(ConfiguredProgram
hpc, Version
hpcVer, ProgramDb
_) <-
Verbosity
-> Program
-> VersionRange
-> ProgramDb
-> IO (ConfiguredProgram, Version, ProgramDb)
requireProgramVersion
Verbosity
verbosity
Program
hpcProgram
VersionRange
anyVersion
(LocalBuildInfo -> ProgramDb
withPrograms LocalBuildInfo
lbi)
let htmlDir' :: SymbolicPath Pkg ('Dir Artifacts)
htmlDir' = SymbolicPath Pkg ('Dir Dist)
-> Way -> SymbolicPath Pkg ('Dir Artifacts)
htmlDir SymbolicPath Pkg ('Dir Dist)
testDistPref Way
way
SymbolicPath Pkg 'File
tixFile <- case [TestSuite]
suites of
[TestSuite
oneTest] -> do
let testName' :: String
testName' = UnqualComponentName -> String
unUnqualComponentName (UnqualComponentName -> String) -> UnqualComponentName -> String
forall a b. (a -> b) -> a -> b
$ TestSuite -> UnqualComponentName
testName TestSuite
oneTest
SymbolicPath Pkg 'File -> IO (SymbolicPath Pkg 'File)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (SymbolicPath Pkg 'File -> IO (SymbolicPath Pkg 'File))
-> SymbolicPath Pkg 'File -> IO (SymbolicPath Pkg 'File)
forall a b. (a -> b) -> a -> b
$
SymbolicPath Pkg ('Dir Dist)
-> Way -> String -> SymbolicPath Pkg 'File
tixFilePath SymbolicPath Pkg ('Dir Dist)
testDistPref Way
way String
testName'
[TestSuite]
_ -> do
let excluded :: [ModuleName]
excluded = (TestSuite -> [ModuleName]) -> [TestSuite] -> [ModuleName]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TestSuite -> [ModuleName]
testModules [TestSuite]
suites [ModuleName] -> [ModuleName] -> [ModuleName]
forall a. [a] -> [a] -> [a]
++ [ModuleName
main]
pkgName :: String
pkgName = PackageIdentifier -> String
forall a. Pretty a => a -> String
prettyShow (PackageIdentifier -> String) -> PackageIdentifier -> String
forall a b. (a -> b) -> a -> b
$ PackageDescription -> PackageIdentifier
PD.package PackageDescription
pkg_descr
summedTixFile :: SymbolicPath Pkg 'File
summedTixFile = SymbolicPath Pkg ('Dir Dist)
-> Way -> String -> SymbolicPath Pkg 'File
tixFilePath SymbolicPath Pkg ('Dir Dist)
testDistPref Way
way String
pkgName
Bool -> String -> IO ()
createDirectoryIfMissing Bool
True (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ SymbolicPathX 'AllowAbsolute Pkg ('Dir Any) -> String
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> String
i (SymbolicPathX 'AllowAbsolute Pkg ('Dir Any) -> String)
-> SymbolicPathX 'AllowAbsolute Pkg ('Dir Any) -> String
forall a b. (a -> b) -> a -> b
$ SymbolicPath Pkg 'File
-> SymbolicPathX 'AllowAbsolute Pkg ('Dir Any)
forall (allowAbsolute :: AllowAbsolute) from to'.
SymbolicPathX allowAbsolute from 'File
-> SymbolicPathX allowAbsolute from ('Dir to')
takeDirectorySymbolicPath SymbolicPath Pkg 'File
summedTixFile
Maybe (SymbolicPath CWD ('Dir Pkg))
-> ConfiguredProgram
-> Verbosity
-> [SymbolicPath Pkg 'File]
-> SymbolicPath Pkg 'File
-> [ModuleName]
-> IO ()
union Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir ConfiguredProgram
hpc Verbosity
verbosity [SymbolicPath Pkg 'File]
tixFiles SymbolicPath Pkg 'File
summedTixFile [ModuleName]
excluded
SymbolicPath Pkg 'File -> IO (SymbolicPath Pkg 'File)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return SymbolicPath Pkg 'File
summedTixFile
Maybe (SymbolicPath CWD ('Dir Pkg))
-> ConfiguredProgram
-> Version
-> Verbosity
-> SymbolicPath Pkg 'File
-> [SymbolicPath Pkg ('Dir Mix)]
-> SymbolicPath Pkg ('Dir Artifacts)
-> [ModuleName]
-> IO ()
markup Maybe (SymbolicPath CWD ('Dir Pkg))
mbWorkDir ConfiguredProgram
hpc Version
hpcVer Verbosity
verbosity SymbolicPath Pkg 'File
tixFile [SymbolicPath Pkg ('Dir Mix)]
mixDirs SymbolicPath Pkg ('Dir Artifacts)
htmlDir' [ModuleName]
libsModulesToInclude
Verbosity -> String -> IO ()
notice Verbosity
verbosity (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$
String
"Package coverage report written to "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ SymbolicPath Pkg ('Dir Artifacts) -> String
forall {allowAbsolute :: AllowAbsolute} {to :: FileOrDir}.
SymbolicPathX allowAbsolute Pkg to -> String
i SymbolicPath Pkg ('Dir Artifacts)
htmlDir'
String -> ShowS
forall p q r. PathLike p q r => p -> q -> r
</> String
"hpc_index.html"
where
way :: Way
way = LocalBuildInfo -> Way
guessWay LocalBuildInfo
lbi
testNames :: [String]
testNames = (TestSuite -> String) -> [TestSuite] -> [String]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (UnqualComponentName -> String
unUnqualComponentName (UnqualComponentName -> String)
-> (TestSuite -> UnqualComponentName) -> TestSuite -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TestSuite -> UnqualComponentName
testName) [TestSuite]
suites
mixDirs :: [SymbolicPath Pkg ('Dir Mix)]
mixDirs = (SymbolicPath Pkg ('Dir Artifacts) -> SymbolicPath Pkg ('Dir Mix))
-> [SymbolicPath Pkg ('Dir Artifacts)]
-> [SymbolicPath Pkg ('Dir Mix)]
forall a b. (a -> b) -> [a] -> [b]
map ((SymbolicPath Pkg ('Dir Dist) -> Way -> SymbolicPath Pkg ('Dir Mix)
`mixDir` Way
way) (SymbolicPath Pkg ('Dir Dist) -> SymbolicPath Pkg ('Dir Mix))
-> (SymbolicPath Pkg ('Dir Artifacts)
-> SymbolicPath Pkg ('Dir Dist))
-> SymbolicPath Pkg ('Dir Artifacts)
-> SymbolicPath Pkg ('Dir Mix)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymbolicPath Pkg ('Dir Artifacts) -> SymbolicPath Pkg ('Dir Dist)
forall (allowAbsolute :: AllowAbsolute) from (to1 :: FileOrDir)
(to2 :: FileOrDir).
SymbolicPathX allowAbsolute from to1
-> SymbolicPathX allowAbsolute from to2
coerceSymbolicPath) [SymbolicPath Pkg ('Dir Artifacts)]
pathsToLibsArtifacts