{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, TypeFamilies #-}
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
readConfigFile :: FilePath -> IO (Map.HashMap String String)
readConfigFile :: String -> IO (HashMap String String)
readConfigFile = [(String, String)] -> String -> IO (HashMap String String)
readConfigFileWithEnv []
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]
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 ()
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 ()
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
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 ()