2012-10-24 01:06:24 +00:00
|
|
|
module CabalApi (
|
2012-10-24 01:48:13 +00:00
|
|
|
cabalParseFile,
|
2012-10-24 01:06:24 +00:00
|
|
|
cabalBuildInfo,
|
|
|
|
cabalDependPackages
|
|
|
|
) where
|
2012-10-24 00:11:09 +00:00
|
|
|
|
2012-10-24 01:06:24 +00:00
|
|
|
import Control.Applicative
|
|
|
|
|
|
|
|
import Data.Maybe (fromJust, maybeToList)
|
2012-10-24 00:11:09 +00:00
|
|
|
import Data.Set (fromList, toList)
|
|
|
|
|
|
|
|
import Distribution.Verbosity (silent)
|
|
|
|
import Distribution.Package (Dependency(Dependency), PackageName(PackageName))
|
|
|
|
import Distribution.PackageDescription
|
|
|
|
(GenericPackageDescription,
|
|
|
|
condLibrary, condExecutables, condTestSuites, condBenchmarks,
|
2012-10-24 01:10:13 +00:00
|
|
|
BuildInfo, libBuildInfo, buildInfo,
|
2012-10-24 01:06:24 +00:00
|
|
|
CondTree, condTreeConstraints, condTreeData)
|
2012-10-24 00:11:09 +00:00
|
|
|
import Distribution.PackageDescription.Parse (readPackageDescription)
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2012-10-24 01:48:13 +00:00
|
|
|
cabalParseFile :: FilePath -> IO GenericPackageDescription
|
|
|
|
cabalParseFile = readPackageDescription silent
|
|
|
|
|
2012-10-24 01:06:24 +00:00
|
|
|
-- Causes error, catched in the upper function.
|
2012-10-24 01:48:13 +00:00
|
|
|
cabalBuildInfo :: GenericPackageDescription -> IO BuildInfo
|
|
|
|
cabalBuildInfo pd = do
|
|
|
|
return . fromJust $ fromLibrary pd <|> fromExecutable pd
|
2012-10-24 01:06:24 +00:00
|
|
|
where
|
|
|
|
fromLibrary c = libBuildInfo . condTreeData <$> condLibrary c
|
|
|
|
fromExecutable c = buildInfo . condTreeData . snd <$> toMaybe (condExecutables c)
|
|
|
|
toMaybe [] = Nothing
|
|
|
|
toMaybe (x:_) = Just x
|
|
|
|
|
2012-10-24 00:11:09 +00:00
|
|
|
getDepsOfPairs :: [(a1, CondTree v [b] a)] -> [b]
|
|
|
|
getDepsOfPairs = concatMap (condTreeConstraints . snd)
|
|
|
|
|
|
|
|
allDependsOfDescription :: GenericPackageDescription -> [Dependency]
|
|
|
|
allDependsOfDescription pd =
|
|
|
|
concat [depLib, depExe, depTests, depBench]
|
|
|
|
where
|
|
|
|
depLib = concatMap condTreeConstraints (maybeToList . condLibrary $ pd)
|
|
|
|
depExe = getDepsOfPairs . condExecutables $ pd
|
|
|
|
depTests = getDepsOfPairs . condTestSuites $ pd
|
|
|
|
depBench = getDepsOfPairs . condBenchmarks $ pd
|
|
|
|
|
|
|
|
getDependencyPackageName :: Dependency -> String
|
|
|
|
getDependencyPackageName (Dependency (PackageName n) _) = n
|
|
|
|
|
2012-10-24 01:48:13 +00:00
|
|
|
cabalDependPackages :: GenericPackageDescription -> IO [String]
|
2012-10-24 01:06:24 +00:00
|
|
|
cabalDependPackages =
|
2012-10-24 01:48:13 +00:00
|
|
|
return . toList . fromList
|
|
|
|
. map getDependencyPackageName
|
|
|
|
. allDependsOfDescription
|