Cabal.hs 1.88 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.
-----------------------------------------------------------------------------
12
13
14
module Hadrian.Haskell.Cabal (
    pkgNameVersion, pkgIdentifier, pkgDependencies
    ) where
15
16
17

import Development.Shake

18
import Hadrian.Haskell.Cabal.Parse
Andrey Mokhov's avatar
Andrey Mokhov committed
19
import Hadrian.Haskell.Package
20
import Hadrian.Oracles.TextFile
21

Andrey Mokhov's avatar
Andrey Mokhov committed
22
23
24
25
-- | 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
26
27
    cabal <- readCabalFile (pkgCabalFile pkg)
    return (name cabal, version cabal)
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- | Read the @.cabal@ file of a given package and return the package identifier.
-- If the @.cabal@ file does not exist return the package name. If the @.cabal@
-- file exists it is tracked.
pkgIdentifier :: Package -> Action String
pkgIdentifier pkg = do
    cabalExists <- doesFileExist (pkgCabalFile pkg)
    if cabalExists
    then do
        cabal <- readCabalFile (pkgCabalFile pkg)
        return $ if (null $ version cabal)
            then name cabal
            else name cabal ++ "-" ++ version cabal
    else return (pkgName pkg)

43
-- | Read the @.cabal@ file of a given package and return the sorted list of its
Andrey Mokhov's avatar
Andrey Mokhov committed
44
-- dependencies. The current version does not take care of Cabal conditionals
45
46
-- and therefore returns a crude overapproximation of actual dependencies. The
-- @.cabal@ file is tracked.
Andrey Mokhov's avatar
Andrey Mokhov committed
47
48
pkgDependencies :: Package -> Action [PackageName]
pkgDependencies pkg = do
49
    cabal <- readCabalFile (pkgCabalFile pkg)
Andrey Mokhov's avatar
Andrey Mokhov committed
50
    return (dependencies cabal)