Oracles.hs 1.88 KB
Newer Older
1
module Oracles (
2
3
4
5
6
    module Oracles.Base,
    module Oracles.Flag,
    module Oracles.Option,
    module Oracles.Builder,
    module Oracles.PackageData,
7
8
9
    oracleRules
    ) where

10
import Development.Shake.Config
11
12
import qualified Data.HashMap.Strict as M
import Base
13
import Config
14
15
16
17
18
import Oracles.Base
import Oracles.Flag
import Oracles.Option
import Oracles.Builder
import Oracles.PackageData
19

Andrey Mokhov's avatar
Andrey Mokhov committed
20
21
22
23
24
25
26
defaultConfig, userConfig :: FilePath
defaultConfig = cfgPath </> "default.config"
userConfig    = cfgPath </> "user.config"

-- Oracle for configuration files.
configOracle :: Rules ()
configOracle = do
27
    cfg <- newCache $ \() -> do
Andrey Mokhov's avatar
Andrey Mokhov committed
28
        unless (doesFileExist $ defaultConfig <.> "in") $ do
29
            error $ "\nDefault configuration file '"
Andrey Mokhov's avatar
Andrey Mokhov committed
30
                ++ (defaultConfig <.> "in")
31
32
                ++ "' is missing; unwilling to proceed."
            return ()
Andrey Mokhov's avatar
Andrey Mokhov committed
33
34
35
        need [defaultConfig]
        cfgDefault <- liftIO $ readConfigFile defaultConfig
        existsUser <- doesFileExist userConfig
36
        cfgUser    <- if existsUser
Andrey Mokhov's avatar
Andrey Mokhov committed
37
                      then liftIO $ readConfigFile userConfig
38
39
                      else do
                          putLoud $ "\nUser defined configuration file '"
Andrey Mokhov's avatar
Andrey Mokhov committed
40
41
                              ++ userConfig ++ "' is missing; "
                              ++ "proceeding with default configuration.\n"
42
43
                          return M.empty
        return $ cfgUser `M.union` cfgDefault
44
    addOracle $ \(ConfigKey key) -> M.lookup key <$> cfg ()
Andrey Mokhov's avatar
Andrey Mokhov committed
45
    return ()
46

Andrey Mokhov's avatar
Andrey Mokhov committed
47
48
49
-- Oracle for 'package-data.mk' files.
packageDataOracle :: Rules ()
packageDataOracle = do
50
51
    pkgData <- newCache $ \file -> do
        need [file]
Andrey Mokhov's avatar
Andrey Mokhov committed
52
        putNormal $ "Parsing " ++ toStandard file ++ "..."
53
        liftIO $ readConfigFile file
Andrey Mokhov's avatar
Andrey Mokhov committed
54
    addOracle $ \(PackageDataKey (file, key)) -> M.lookup key <$> pkgData file
55
    return ()
Andrey Mokhov's avatar
Andrey Mokhov committed
56
57
58

oracleRules :: Rules ()
oracleRules = configOracle <> packageDataOracle