lib: factor out logic for obtaining a list of files to include

This commit is contained in:
Joseph Weston 2018-07-23 13:48:45 +02:00 committed by Simon Michael
parent 2d420a33a4
commit 2c4d0c0acd

View File

@ -190,23 +190,25 @@ includedirectivep :: MonadIO m => JournalParser m ()
includedirectivep = do includedirectivep = do
string "include" string "include"
lift (skipSome spacenonewline) lift (skipSome spacenonewline)
fileglob <- T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet filename <- T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet
parentpos <- getPosition parentpos <- getPosition
curdir <- lift $ expandPath (takeDirectory $ sourceName parentpos) "" curdir <- lift $ expandPath (takeDirectory $ sourceName parentpos) ""
`orRethrowIOError` (show parentpos ++ " locating " ++ fileglob) `orRethrowIOError` (show parentpos ++ " locating " ++ filename)
filepaths <- if isLiteral (compile fileglob) filepaths <- getFilePaths curdir filename
-- </> and globDir1 correctly handle case when 'fileglob' is absolute
then pure [curdir </> fileglob]
else liftIO $ globDir1 (compile fileglob) curdir
forM_ filepaths $ parseChild parentpos forM_ filepaths $ parseChild parentpos
void newline void newline
where where
getFilePaths curdir fileglob =
if isLiteral (compile fileglob)
-- </> and globDir1 correctly handle case when 'fileglob' is absolute
then pure [curdir </> fileglob]
else liftIO $ globDir1 (compile fileglob) curdir
parseChild parentpos filepath = do parseChild parentpos filepath = do
childInput <- lift $ readFilePortably filepath childInput <- lift $ readFilePortably filepath
`orRethrowIOError` (show parentpos ++ " reading " ++ filepath) `orRethrowIOError` (show parentpos ++ " reading " ++ filepath)