Cabal.hs 2.8 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
module Hadrian.Haskell.Cabal (
13
    pkgVersion, pkgIdentifier, pkgDependencies, pkgSynopsis
14
    ) where
15

16
import Control.Monad
17 18
import Development.Shake

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

24 25 26 27
-- | Read the @.cabal@ file of a given package and return the package version.
-- The @.cabal@ file is tracked.
pkgVersion :: Package -> Action String
pkgVersion pkg = do
28
    cabal <- readCabalFile (pkgCabalFile pkg)
29
    return (version cabal)
30

31 32 33 34
-- | Read the @.cabal@ file of a given package and return the package identifier,
-- e.g. @base-4.10.0.0@. If the @.cabal@ file does not exist return just the
-- package name, e.g. @rts@. If the @.cabal@ file exists then it is tracked, and
-- furthermore we check that the recorded package name matches the name of the
35
-- package passed as the parameter, and raise an error otherwise.
36 37 38
pkgIdentifier :: Package -> Action String
pkgIdentifier pkg = do
    cabalExists <- doesFileExist (pkgCabalFile pkg)
39 40 41
    if not cabalExists
    then return (pkgName pkg)
    else do
42
        cabal <- readCabalFile (pkgCabalFile pkg)
43 44 45 46
        when (pkgName pkg /= name cabal) $
            error $ "[Hadrian.Haskell.Cabal] Inconsistent package name: expected "
                 ++ quote (pkgName pkg) ++ ", but " ++ quote (pkgCabalFile pkg)
                 ++ " specifies " ++ quote (name cabal) ++ "."
47
        return $ if (null $ version cabal)
48 49
            then pkgName pkg
            else pkgName pkg ++ "-" ++ version cabal
50

51
-- | Read the @.cabal@ file of a given package and return the sorted list of its
Andrey Mokhov's avatar
Andrey Mokhov committed
52
-- dependencies. The current version does not take care of Cabal conditionals
53 54
-- and therefore returns a crude overapproximation of actual dependencies. The
-- @.cabal@ file is tracked.
Andrey Mokhov's avatar
Andrey Mokhov committed
55 56
pkgDependencies :: Package -> Action [PackageName]
pkgDependencies pkg = do
57
    cabal <- readCabalFile (pkgCabalFile pkg)
Andrey Mokhov's avatar
Andrey Mokhov committed
58
    return (dependencies cabal)
59 60 61 62 63 64 65 66 67 68 69 70

-- | Read the @.cabal@ file of a given package and return the package synopsis
-- or @Nothing@ if the @.cabal@ file does not exist. The existence and contents
-- of the @.cabal@ file are tracked.
pkgSynopsis :: Package -> Action (Maybe String)
pkgSynopsis pkg = do
    cabalExists <- doesFileExist (pkgCabalFile pkg)
    if not cabalExists
    then return Nothing
    else do
        cabal <- readCabalFile (pkgCabalFile pkg)
        return $ Just (synopsis cabal)