Cabal.hs 2.33 KB
Newer Older
Andrey Mokhov's avatar
Andrey Mokhov committed
1
2
3
4
5
6
7
8
9
10
11
-----------------------------------------------------------------------------
-- |
-- Module     : Hadrian.Haskell.Cabal
-- Copyright  : (c) Andrey Mokhov 2014-2017
-- License    : MIT (see the file LICENSE)
-- Maintainer : andrey.mokhov@gmail.com
-- Stability  : experimental
--
-- Basic functionality for extracting Haskell package metadata stored in
-- @.cabal@ files.
-----------------------------------------------------------------------------
Andrey Mokhov's avatar
Andrey Mokhov committed
12
module Hadrian.Haskell.Cabal (readCabal, pkgNameVersion, pkgDependencies) where
13
14

import Development.Shake
Andrey Mokhov's avatar
Andrey Mokhov committed
15
16
17
18
19
20
import qualified Distribution.Package                  as C
import qualified Distribution.PackageDescription       as C
import qualified Distribution.PackageDescription.Parse as C
import qualified Distribution.Text                     as C
import qualified Distribution.Types.CondTree           as C
import qualified Distribution.Verbosity                as C
21

Andrey Mokhov's avatar
Andrey Mokhov committed
22
import Hadrian.Haskell.Package
23

Andrey Mokhov's avatar
Andrey Mokhov committed
24
25
26
27
28
29
-- | Read the @.cabal@ file of a given package and return the
-- 'GenericPackageDescription'. The @.cabal@ file is tracked.
readCabal :: Package -> Action C.GenericPackageDescription
readCabal pkg = do
    need [pkgCabalFile pkg]
    liftIO $ C.readGenericPackageDescription C.silent (pkgCabalFile pkg)
30

Andrey Mokhov's avatar
Andrey Mokhov committed
31
32
33
34
35
36
-- | Read the @.cabal@ file of a given package and return the package name and
-- version. The @.cabal@ file is tracked.
pkgNameVersion :: Package -> Action (PackageName, String)
pkgNameVersion pkg = do
    pkgId <- C.package . C.packageDescription <$> readCabal pkg
    return (C.unPackageName $ C.pkgName pkgId, C.display $ C.pkgVersion pkgId)
37

Andrey Mokhov's avatar
Andrey Mokhov committed
38
39
40
41
42
43
44
45
46
47
48
-- | Read the @.cabal@ file of a given package and return the list of its
-- dependencies. The current version does not take care of Cabal conditionals
-- and therefore returns a crude overapproximation of The @.cabal@ file is tracked.
pkgDependencies :: Package -> Action [PackageName]
pkgDependencies pkg = do
    gpd <- readCabal pkg
    let libDeps = collectDeps (C.condLibrary gpd)
        exeDeps = map (collectDeps . Just . snd) (C.condExecutables gpd)
    return [ C.unPackageName p | C.Dependency p _ <- concat (libDeps : exeDeps) ]

collectDeps :: Maybe (C.CondTree v [C.Dependency] a) -> [C.Dependency]
49
collectDeps Nothing = []
Andrey Mokhov's avatar
Andrey Mokhov committed
50
collectDeps (Just (C.CondNode _ deps ifs)) = deps ++ concatMap f ifs
51
  where
Andrey Mokhov's avatar
Andrey Mokhov committed
52
    f (C.CondBranch _ t mt) = collectDeps (Just t) ++ collectDeps mt