Cabal.hs 1.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Hadrian.Haskell.Cabal (readCabal, cabalNameVersion, cabalDependencies) where

import Development.Shake
import Distribution.Package
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
import Distribution.Text
import Distribution.Types.CondTree
import Distribution.Verbosity

-- TODO: Track the values?

-- | Read a given @.cabal@ file and return the 'GenericPackageDescription'.
readCabal :: FilePath -> Action GenericPackageDescription
readCabal cabal = do
    need [cabal]
    liftIO $ readGenericPackageDescription silent cabal

-- | Read a given @.cabal@ file and return the package name and version.
cabalNameVersion :: FilePath -> Action (String, String)
cabalNameVersion cabal = do
    identifier <- package . packageDescription <$> readCabal cabal
    return (unPackageName $ pkgName identifier, display $ pkgVersion identifier)

-- | Read a given @.cabal@ file and return the package dependencies.
cabalDependencies :: FilePath -> Action [String]
cabalDependencies cabal = do
    gpd <- readCabal cabal
    let depsLib  = collectDeps $ condLibrary gpd
        depsExes = map (collectDeps . Just . snd) $ condExecutables gpd
        deps     = concat $ depsLib : depsExes
    return $ [ unPackageName name | Dependency name _ <- deps ]

collectDeps :: Maybe (CondTree v [Dependency] a) -> [Dependency]
collectDeps Nothing = []
collectDeps (Just (CondNode _ deps ifs)) = deps ++ concatMap f ifs
  where
    f (CondBranch _ t mt) = collectDeps (Just t) ++ collectDeps mt