Add haddocks
This commit is contained in:
@ -11,6 +11,7 @@ import Tempered.Options
|
|||||||
import Tempered.Parser
|
import Tempered.Parser
|
||||||
import Tempered.Template
|
import Tempered.Template
|
||||||
|
|
||||||
|
-- | Run tempered on cmdline args.
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
envVars <- getEnvVars
|
envVars <- getEnvVars
|
||||||
@ -20,6 +21,7 @@ main = do
|
|||||||
where
|
where
|
||||||
renderOutput = traverse_ (interpTemplate >=> liftIO . putStr)
|
renderOutput = traverse_ (interpTemplate >=> liftIO . putStr)
|
||||||
|
|
||||||
|
-- | Combine local and global environment variables
|
||||||
getEnvVars :: IO EnvVars
|
getEnvVars :: IO EnvVars
|
||||||
getEnvVars = do
|
getEnvVars = do
|
||||||
cwd <- getCurrentDirectory
|
cwd <- getCurrentDirectory
|
||||||
|
@ -18,25 +18,29 @@ import System.Exit
|
|||||||
type EnvVars = [(String, String)]
|
type EnvVars = [(String, String)]
|
||||||
type EnvMap = M.Map String String
|
type EnvMap = M.Map String String
|
||||||
|
|
||||||
|
-- | Given a directory tries to find env.yaml recursively upwards;
|
||||||
|
-- parses `EnvMap` from the file if found.
|
||||||
getProjectOptions :: FilePath -> IO EnvMap
|
getProjectOptions :: FilePath -> IO EnvMap
|
||||||
getProjectOptions path = do
|
getProjectOptions path = do
|
||||||
mProjSettingsFile <- findProjSettings path
|
mProjSettingsFile <- findProjSettings path
|
||||||
mOptions <- traverse optionsFromFilename mProjSettingsFile
|
mOptions <- traverse optionsFromFilename mProjSettingsFile
|
||||||
return $ fromMaybe mempty mOptions
|
return $ fromMaybe mempty mOptions
|
||||||
|
|
||||||
-- Retrieve an options object from a yaml file
|
-- Retrieve an EnvMap from a yaml file
|
||||||
optionsFromFilename :: FilePath -> IO EnvMap
|
optionsFromFilename :: FilePath -> IO EnvMap
|
||||||
optionsFromFilename = Y.decodeFileEither >=>
|
optionsFromFilename = Y.decodeFileEither >=>
|
||||||
\case
|
\case
|
||||||
Left err -> die . prettyPrintParseException $ err
|
Left err -> die . prettyPrintParseException $ err
|
||||||
Right options -> return options
|
Right options -> return options
|
||||||
|
|
||||||
|
-- Try to find an 'env.yaml' file.
|
||||||
findProjSettings :: FilePath -> IO (Maybe FilePath)
|
findProjSettings :: FilePath -> IO (Maybe FilePath)
|
||||||
findProjSettings fpath = do
|
findProjSettings fpath = do
|
||||||
absPath <- makeAbsolute fpath
|
absPath <- makeAbsolute fpath
|
||||||
let searchPaths = (</> "env.yaml") <$> recurseUp absPath
|
let searchPaths = (</> "env.yaml") <$> recurseUp absPath
|
||||||
listToMaybe <$> filterM doesFileExist searchPaths
|
listToMaybe <$> filterM doesFileExist searchPaths
|
||||||
|
|
||||||
|
-- Get all parent directories of a directory path.
|
||||||
recurseUp :: FilePath -> [FilePath]
|
recurseUp :: FilePath -> [FilePath]
|
||||||
recurseUp = unfoldr go
|
recurseUp = unfoldr go
|
||||||
where
|
where
|
||||||
|
@ -17,18 +17,22 @@ infix 0 ?>
|
|||||||
(?>) :: String -> ParsecT s u m a -> ParsecT s u m a
|
(?>) :: String -> ParsecT s u m a -> ParsecT s u m a
|
||||||
(?>) = flip (<?>)
|
(?>) = flip (<?>)
|
||||||
|
|
||||||
|
-- | Parse a template from a file.
|
||||||
templateFromFile :: FilePath -> IO (Either ParseError (Template Command))
|
templateFromFile :: FilePath -> IO (Either ParseError (Template Command))
|
||||||
templateFromFile fname = do
|
templateFromFile fname = do
|
||||||
file <- readFile fname
|
file <- readFile fname
|
||||||
return $ parseTemplate fname file
|
return $ parseTemplate fname file
|
||||||
|
|
||||||
|
-- | Parse a template from a string with a given filename for errors.
|
||||||
parseTemplate :: FilePath -> String -> Either ParseError (Template Command)
|
parseTemplate :: FilePath -> String -> Either ParseError (Template Command)
|
||||||
parseTemplate = runP templateP ()
|
parseTemplate = runP templateP ()
|
||||||
|
|
||||||
|
-- | Fail if parsing errors occurred, otherwise return the template.
|
||||||
handleTemplateError :: Either ParseError (Template a) -> IO (Template a)
|
handleTemplateError :: Either ParseError (Template a) -> IO (Template a)
|
||||||
handleTemplateError (Left err) = print err >> exitFailure
|
handleTemplateError (Left err) = print err >> exitFailure
|
||||||
handleTemplateError (Right temp) = return temp
|
handleTemplateError (Right temp) = return temp
|
||||||
|
|
||||||
|
-- | Template Parser
|
||||||
templateP :: Parser (Template Command)
|
templateP :: Parser (Template Command)
|
||||||
templateP = "template" ?> do
|
templateP = "template" ?> do
|
||||||
optional (try shebangP)
|
optional (try shebangP)
|
||||||
@ -39,10 +43,12 @@ templateP = "template" ?> do
|
|||||||
cmd = Right <$> commandP
|
cmd = Right <$> commandP
|
||||||
txt = Left <$> many1 (notFollowedBy (string "{{") *> anyChar)
|
txt = Left <$> many1 (notFollowedBy (string "{{") *> anyChar)
|
||||||
|
|
||||||
|
-- | Shebang Parser
|
||||||
shebangP :: Parser String
|
shebangP :: Parser String
|
||||||
shebangP = "shebang" ?>
|
shebangP = "shebang" ?>
|
||||||
liftA2 (++) (lookAhead (string "#!") *> string "#!") (manyTill anyChar (char '\n'))
|
liftA2 (++) (lookAhead (string "#!") *> string "#!") (manyTill anyChar (char '\n'))
|
||||||
|
|
||||||
|
-- | Command Parser
|
||||||
commandP :: Parser Command
|
commandP :: Parser Command
|
||||||
commandP = "command" ?> do
|
commandP = "command" ?> do
|
||||||
_ <- string "{{"
|
_ <- string "{{"
|
||||||
|
@ -13,19 +13,23 @@ import Data.Foldable
|
|||||||
|
|
||||||
import Tempered.Options
|
import Tempered.Options
|
||||||
|
|
||||||
|
-- | Represents values interspersed with text.
|
||||||
data Template a =
|
data Template a =
|
||||||
Template [Either String a]
|
Template [Either String a]
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
|
-- | Given an execution environment render a template into a string.
|
||||||
interpTemplate :: (MonadReader EnvVars m, MonadIO m) => Template Command -> m String
|
interpTemplate :: (MonadReader EnvVars m, MonadIO m) => Template Command -> m String
|
||||||
interpTemplate (Template elems) = fold <$> mapM toText elems
|
interpTemplate (Template elems) = fold <$> mapM toText elems
|
||||||
where
|
where
|
||||||
toText = either return interpCommand
|
toText = either return interpCommand
|
||||||
|
|
||||||
|
-- | Represents a command to be run by the system.
|
||||||
data Command =
|
data Command =
|
||||||
Command String
|
Command String
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
|
-- | Run a command in an environment returning the result.
|
||||||
interpCommand :: (MonadReader EnvVars m, MonadIO m) => Command -> m String
|
interpCommand :: (MonadReader EnvVars m, MonadIO m) => Command -> m String
|
||||||
interpCommand (Command cmd) = do
|
interpCommand (Command cmd) = do
|
||||||
envVars <- ask
|
envVars <- ask
|
||||||
|
Reference in New Issue
Block a user