{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, TypeFamilies #-}

-- | A module for parsing and using config files in a Shake build system. Config files
--   consist of variable bindings, for example:
--
-- > # This is my Config file
-- > HEADERS_DIR = /path/to/dir
-- > CFLAGS = -g -I${HEADERS_DIR}
-- > CFLAGS = $CFLAGS -O2
-- > include extra/file.cfg
--
--   This defines the variable @HEADERS_DIR@ (equal to @\/path\/to\/dir@), and
--   @CFLAGS@ (equal to @-g -I\/path\/to\/dir -O2@), and also includes the configuration
--   statements in the file @extra/file.cfg@. The full lexical syntax for configuration
--   files is defined here: <https://ninja-build.org/manual.html#_lexical_syntax>.
--   The use of Ninja file syntax is due to convenience and the desire to reuse an
--    externally-defined specification (but the choice of configuration language is mostly arbitrary).
--
--   To use the configuration file either use 'readConfigFile' to parse the configuration file
--   and use the values directly, or 'usingConfigFile' and 'getConfig' to track the configuration
--   values, so they become build dependencies.
module Development.Shake.Config(
    readConfigFile, readConfigFileWithEnv,
    usingConfigFile, usingConfig,
    getConfig, getConfigKeys
    ) where

import Development.Shake
import Development.Shake.Classes
import qualified Development.Ninja.Parse as Ninja
import qualified Development.Ninja.Env as Ninja
import qualified Data.HashMap.Strict as Map
import qualified Data.ByteString.UTF8 as UTF8
import Data.Tuple.Extra
import Data.List


-- | Read a config file, returning a list of the variables and their bindings.
--   Config files use the Ninja lexical syntax:
--   <https://ninja-build.org/manual.html#_lexical_syntax>
readConfigFile :: FilePath -> IO (Map.HashMap String String)
readConfigFile :: String -> IO (HashMap String String)
readConfigFile = [(String, String)] -> String -> IO (HashMap String String)
readConfigFileWithEnv []


-- | Read a config file with an initial environment, returning a list of the variables and their bindings.
--   Config files use the Ninja lexical syntax:
--   <https://ninja-build.org/manual.html#_lexical_syntax>
readConfigFileWithEnv :: [(String, String)] -> FilePath -> IO (Map.HashMap String String)
readConfigFileWithEnv :: [(String, String)] -> String -> IO (HashMap String String)
readConfigFileWithEnv [(String, String)]
vars String
file = do
    Env ByteString ByteString
env <- forall k v. IO (Env k v)
Ninja.newEnv
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall k v. (Eq k, Hashable k) => Env k v -> k -> v -> IO ()
Ninja.addEnv Env ByteString ByteString
env) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> ByteString
UTF8.fromString forall a a' b b'. (a -> a') -> (b -> b') -> (a, b) -> (a', b')
*** String -> ByteString
UTF8.fromString)) [(String, String)]
vars
    String -> Env ByteString ByteString -> IO Ninja
Ninja.parse String
file Env ByteString ByteString
env
    HashMap ByteString ByteString
mp <- forall k v. Env k v -> IO (HashMap k v)
Ninja.fromEnv Env ByteString ByteString
env
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
Map.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (ByteString -> String
UTF8.toString forall a a' b b'. (a -> a') -> (b -> b') -> (a, b) -> (a', b')
*** ByteString -> String
UTF8.toString) forall a b. (a -> b) -> a -> b
$ forall k v. HashMap k v -> [(k, v)]
Map.toList HashMap ByteString ByteString
mp


newtype Config = Config String deriving (Int -> Config -> ShowS
[Config] -> ShowS
Config -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Config] -> ShowS
$cshowList :: [Config] -> ShowS
show :: Config -> String
$cshow :: Config -> String
showsPrec :: Int -> Config -> ShowS
$cshowsPrec :: Int -> Config -> ShowS
Show,Typeable,Config -> Config -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Config -> Config -> Bool
$c/= :: Config -> Config -> Bool
== :: Config -> Config -> Bool
$c== :: Config -> Config -> Bool
Eq,Eq Config
Int -> Config -> Int
Config -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Config -> Int
$chash :: Config -> Int
hashWithSalt :: Int -> Config -> Int
$chashWithSalt :: Int -> Config -> Int
Hashable,Get Config
[Config] -> Put
Config -> Put
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [Config] -> Put
$cputList :: [Config] -> Put
get :: Get Config
$cget :: Get Config
put :: Config -> Put
$cput :: Config -> Put
Binary,Config -> ()
forall a. (a -> ()) -> NFData a
rnf :: Config -> ()
$crnf :: Config -> ()
NFData)

newtype ConfigKeys = ConfigKeys () deriving (Int -> ConfigKeys -> ShowS
[ConfigKeys] -> ShowS
ConfigKeys -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConfigKeys] -> ShowS
$cshowList :: [ConfigKeys] -> ShowS
show :: ConfigKeys -> String
$cshow :: ConfigKeys -> String
showsPrec :: Int -> ConfigKeys -> ShowS
$cshowsPrec :: Int -> ConfigKeys -> ShowS
Show,Typeable,ConfigKeys -> ConfigKeys -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConfigKeys -> ConfigKeys -> Bool
$c/= :: ConfigKeys -> ConfigKeys -> Bool
== :: ConfigKeys -> ConfigKeys -> Bool
$c== :: ConfigKeys -> ConfigKeys -> Bool
Eq,Eq ConfigKeys
Int -> ConfigKeys -> Int
ConfigKeys -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: ConfigKeys -> Int
$chash :: ConfigKeys -> Int
hashWithSalt :: Int -> ConfigKeys -> Int
$chashWithSalt :: Int -> ConfigKeys -> Int
Hashable,Get ConfigKeys
[ConfigKeys] -> Put
ConfigKeys -> Put
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [ConfigKeys] -> Put
$cputList :: [ConfigKeys] -> Put
get :: Get ConfigKeys
$cget :: Get ConfigKeys
put :: ConfigKeys -> Put
$cput :: ConfigKeys -> Put
Binary,ConfigKeys -> ()
forall a. (a -> ()) -> NFData a
rnf :: ConfigKeys -> ()
$crnf :: ConfigKeys -> ()
NFData)

type instance RuleResult Config = Maybe String
type instance RuleResult ConfigKeys = [String]


-- | Specify the file to use with 'getConfig'.
usingConfigFile :: FilePath -> Rules ()
usingConfigFile :: String -> Rules ()
usingConfigFile String
file = do
    () -> Action (HashMap String String)
mp <- forall k v.
(Eq k, Hashable k) =>
(k -> Action v) -> Rules (k -> Action v)
newCache forall a b. (a -> b) -> a -> b
$ \() -> do
        Partial => [String] -> Action ()
need [String
file]
        forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> IO (HashMap String String)
readConfigFile String
file
    forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a, Partial) =>
(q -> Action a) -> Rules (q -> Action a)
addOracle forall a b. (a -> b) -> a -> b
$ \(Config String
x) -> forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup String
x forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> () -> Action (HashMap String String)
mp ()
    forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a, Partial) =>
(q -> Action a) -> Rules (q -> Action a)
addOracle forall a b. (a -> b) -> a -> b
$ \(ConfigKeys ()) -> forall a. Ord a => [a] -> [a]
sort forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. HashMap k v -> [k]
Map.keys forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> () -> Action (HashMap String String)
mp ()
    forall (f :: * -> *) a. Applicative f => a -> f a
pure ()


-- | Specify the values to use with 'getConfig', generally prefer
--   'usingConfigFile' unless you also need access to the values
--   of variables outside 'Action'.
usingConfig :: Map.HashMap String String -> Rules ()
usingConfig :: HashMap String String -> Rules ()
usingConfig HashMap String String
mp = do
    forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a, Partial) =>
(q -> Action a) -> Rules (q -> Action a)
addOracle forall a b. (a -> b) -> a -> b
$ \(Config String
x) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup String
x HashMap String String
mp
    forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a, Partial) =>
(q -> Action a) -> Rules (q -> Action a)
addOracle forall a b. (a -> b) -> a -> b
$ \(ConfigKeys ()) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> [a]
sort forall a b. (a -> b) -> a -> b
$ forall k v. HashMap k v -> [k]
Map.keys HashMap String String
mp
    forall (f :: * -> *) a. Applicative f => a -> f a
pure ()


-- | Obtain the value of a configuration variable, returns 'Nothing' to indicate the variable
--   has no binding. Any build system using 'getConfig' /must/ call either 'usingConfigFile'
--   or 'usingConfig'. The 'getConfig' function will introduce a dependency on the configuration
--   variable (but not the whole configuration file), and if the configuration variable changes, the rule will be rerun.
--   As an example:
--
-- @
-- 'usingConfigFile' \"myconfiguration.cfg\"
-- \"*.o\" '%>' \\out -> do
--     cflags <- 'getConfig' \"CFLAGS\"
--     'cmd' \"gcc\" [out '-<.>' \"c\"] (fromMaybe \"\" cflags)
-- @
getConfig :: String -> Action (Maybe String)
getConfig :: String -> Action (Maybe String)
getConfig = forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a) =>
q -> Action a
askOracle forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Config
Config


-- | Obtain the configuration keys.
--   Any build system using 'getConfigKeys' /must/ call either 'usingConfigFile' or 'usingConfig'.
--   The 'getConfigKeys' function will introduce a dependency on the configuration keys
--   (but not the whole configuration file), and if the configuration keys change, the rule will be rerun.
--   Usually use as part of an action.
--   As an example:
--
-- @
-- 'usingConfigFile' \"myconfiguration.cfg\"
-- 'action' $ need =<< getConfigKeys
-- @
getConfigKeys :: Action [String]
getConfigKeys :: Action [String]
getConfigKeys = forall q a.
(RuleResult q ~ a, ShakeValue q, ShakeValue a) =>
q -> Action a
askOracle forall a b. (a -> b) -> a -> b
$ () -> ConfigKeys
ConfigKeys ()