module Distribution.Simple.BuildToolDepends where
import Prelude ()
import Distribution.Compat.Prelude
import qualified Data.Map as Map
import Distribution.Package
import Distribution.PackageDescription
import Distribution.Types.ExeDependency
import Distribution.Types.LegacyExeDependency
import Distribution.Types.UnqualComponentName
desugarBuildTool :: PackageDescription
-> LegacyExeDependency
-> Maybe ExeDependency
desugarBuildTool :: PackageDescription -> LegacyExeDependency -> Maybe ExeDependency
desugarBuildTool PackageDescription
pkg LegacyExeDependency
led =
if Bool
foundLocal
then ExeDependency -> Maybe ExeDependency
forall a. a -> Maybe a
Just (ExeDependency -> Maybe ExeDependency)
-> ExeDependency -> Maybe ExeDependency
forall a b. (a -> b) -> a -> b
$ PackageName -> UnqualComponentName -> VersionRange -> ExeDependency
ExeDependency (PackageDescription -> PackageName
forall pkg. Package pkg => pkg -> PackageName
packageName PackageDescription
pkg) UnqualComponentName
toolName VersionRange
reqVer
else String -> Map String ExeDependency -> Maybe ExeDependency
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup String
name Map String ExeDependency
whiteMap
where
LegacyExeDependency String
name VersionRange
reqVer = LegacyExeDependency
led
toolName :: UnqualComponentName
toolName = String -> UnqualComponentName
mkUnqualComponentName String
name
foundLocal :: Bool
foundLocal = UnqualComponentName
toolName UnqualComponentName -> [UnqualComponentName] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (Executable -> UnqualComponentName)
-> [Executable] -> [UnqualComponentName]
forall a b. (a -> b) -> [a] -> [b]
map Executable -> UnqualComponentName
exeName (PackageDescription -> [Executable]
executables PackageDescription
pkg)
whitelist :: [String]
whitelist = [ String
"hscolour", String
"haddock", String
"happy", String
"alex", String
"hsc2hs", String
"c2hs"
, String
"cpphs", String
"greencard", String
"hspec-discover"
]
whiteMap :: Map String ExeDependency
whiteMap = [(String, ExeDependency)] -> Map String ExeDependency
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(String, ExeDependency)] -> Map String ExeDependency)
-> [(String, ExeDependency)] -> Map String ExeDependency
forall a b. (a -> b) -> a -> b
$ ((String -> (String, ExeDependency))
-> [String] -> [(String, ExeDependency)])
-> [String]
-> (String -> (String, ExeDependency))
-> [(String, ExeDependency)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (String -> (String, ExeDependency))
-> [String] -> [(String, ExeDependency)]
forall a b. (a -> b) -> [a] -> [b]
map [String]
whitelist ((String -> (String, ExeDependency)) -> [(String, ExeDependency)])
-> (String -> (String, ExeDependency)) -> [(String, ExeDependency)]
forall a b. (a -> b) -> a -> b
$ \String
n ->
(String
n, PackageName -> UnqualComponentName -> VersionRange -> ExeDependency
ExeDependency (String -> PackageName
mkPackageName String
n) (String -> UnqualComponentName
mkUnqualComponentName String
n) VersionRange
reqVer)
getAllToolDependencies :: PackageDescription
-> BuildInfo
-> [ExeDependency]
getAllToolDependencies :: PackageDescription -> BuildInfo -> [ExeDependency]
getAllToolDependencies PackageDescription
pkg BuildInfo
bi =
BuildInfo -> [ExeDependency]
buildToolDepends BuildInfo
bi [ExeDependency] -> [ExeDependency] -> [ExeDependency]
forall a. [a] -> [a] -> [a]
++ (LegacyExeDependency -> Maybe ExeDependency)
-> [LegacyExeDependency] -> [ExeDependency]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (PackageDescription -> LegacyExeDependency -> Maybe ExeDependency
desugarBuildTool PackageDescription
pkg) (BuildInfo -> [LegacyExeDependency]
buildTools BuildInfo
bi)
isInternal :: PackageDescription -> ExeDependency -> Bool
isInternal :: PackageDescription -> ExeDependency -> Bool
isInternal PackageDescription
pkg (ExeDependency PackageName
n UnqualComponentName
_ VersionRange
_) = PackageName
n PackageName -> PackageName -> Bool
forall a. Eq a => a -> a -> Bool
== PackageDescription -> PackageName
forall pkg. Package pkg => pkg -> PackageName
packageName PackageDescription
pkg
getAllInternalToolDependencies :: PackageDescription
-> BuildInfo
-> [UnqualComponentName]
getAllInternalToolDependencies :: PackageDescription -> BuildInfo -> [UnqualComponentName]
getAllInternalToolDependencies PackageDescription
pkg BuildInfo
bi =
[ UnqualComponentName
toolname
| dep :: ExeDependency
dep@(ExeDependency PackageName
_ UnqualComponentName
toolname VersionRange
_) <- PackageDescription -> BuildInfo -> [ExeDependency]
getAllToolDependencies PackageDescription
pkg BuildInfo
bi
, PackageDescription -> ExeDependency -> Bool
isInternal PackageDescription
pkg ExeDependency
dep
]