ghc-mod/CabalApi.hs

95 lines
3.4 KiB
Haskell
Raw Normal View History

module CabalApi (
cabalParseFile
, cabalBuildInfo
2013-03-01 06:25:43 +00:00
, cabalAllDependPackages
, cabalAllExtentions
) where
import Control.Applicative
2013-03-01 06:25:43 +00:00
import Data.Maybe (fromJust, maybeToList, catMaybes)
import Data.Set (fromList, toList)
import Distribution.Package (Dependency(Dependency), PackageName(PackageName))
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse (readPackageDescription)
2013-03-01 06:25:43 +00:00
import Distribution.Text (display)
import Distribution.Verbosity (silent)
2013-03-01 06:25:43 +00:00
import Language.Haskell.Extension (Extension(..))
----------------------------------------------------------------
cabalParseFile :: FilePath -> IO GenericPackageDescription
cabalParseFile = readPackageDescription silent
----------------------------------------------------------------
-- Causes error, catched in the upper function.
2013-03-01 04:14:46 +00:00
cabalBuildInfo :: GenericPackageDescription -> BuildInfo
cabalBuildInfo pd = fromJust $ fromLibrary pd <|> fromExecutable pd
where
fromLibrary c = libBuildInfo . condTreeData <$> condLibrary c
fromExecutable c = buildInfo . condTreeData . snd <$> toMaybe (condExecutables c)
toMaybe [] = Nothing
toMaybe (x:_) = Just x
----------------------------------------------------------------
2013-03-01 06:25:43 +00:00
cabalAllDependPackages :: GenericPackageDescription -> [String]
cabalAllDependPackages pd = uniqueAndSort pkgs
where
2013-03-01 06:25:43 +00:00
pkgs = map getDependencyPackageName $ cabalAllDependency pd
2013-03-01 06:25:43 +00:00
cabalAllDependency :: GenericPackageDescription -> [Dependency]
cabalAllDependency = fromPackageDescription getDeps getDeps getDeps getDeps
where
getDeps :: [Tree a] -> [Dependency]
getDeps = concatMap condTreeConstraints
getDependencyPackageName :: Dependency -> String
2013-03-01 06:25:43 +00:00
getDependencyPackageName (Dependency (PackageName nm) _) = nm
----------------------------------------------------------------
cabalAllExtentions :: GenericPackageDescription -> [String]
cabalAllExtentions pd = uniqueAndSort exts
where
buildInfos = cabalAllBuildInfos pd
eexts = concatMap oldExtensions buildInfos
++ concatMap defaultExtensions buildInfos
exts = catMaybes $ map getExtensionName eexts
getExtensionName :: Extension -> Maybe String
getExtensionName (EnableExtension nm) = Just (display nm)
getExtensionName _ = Nothing
----------------------------------------------------------------
2013-03-01 06:25:43 +00:00
cabalAllBuildInfos :: GenericPackageDescription -> [BuildInfo]
cabalAllBuildInfos = fromPackageDescription f1 f2 f3 f4
where
f1 = map (libBuildInfo . condTreeData)
f2 = map (buildInfo . condTreeData)
f3 = map (testBuildInfo . condTreeData)
f4 = map (benchmarkBuildInfo . condTreeData)
----------------------------------------------------------------
type Tree = CondTree ConfVar [Dependency]
fromPackageDescription :: ([Tree Library] -> [a])
-> ([Tree Executable] -> [a])
-> ([Tree TestSuite] -> [a])
-> ([Tree Benchmark] -> [a])
-> GenericPackageDescription
-> [a]
fromPackageDescription f1 f2 f3 f4 pd = lib ++ exe ++ tests ++ bench
where
lib = f1 . maybeToList . condLibrary $ pd
exe = f2 . map snd . condExecutables $ pd
tests = f3 . map snd . condTestSuites $ pd
bench = f4 . map snd . condBenchmarks $ pd
2013-03-01 06:25:43 +00:00
----------------------------------------------------------------
uniqueAndSort :: [String] -> [String]
uniqueAndSort = toList . fromList