Add lifted directory operations in GF.System.Directory to eliminate the need for liftIO in various places

This commit is contained in:
hallgren
2014-08-20 17:47:08 +00:00
parent ff960a27b8
commit e1644ef319
5 changed files with 30 additions and 18 deletions

View File

@@ -51,7 +51,7 @@ batchCompile opts files = do
-- to compile a set of modules, e.g. an old GF or a .cf file -- to compile a set of modules, e.g. an old GF or a .cf file
compileSourceGrammar :: Options -> SourceGrammar -> IOE SourceGrammar compileSourceGrammar :: Options -> SourceGrammar -> IOE SourceGrammar
compileSourceGrammar opts gr = do compileSourceGrammar opts gr = do
cwd <- liftIO getCurrentDirectory cwd <- getCurrentDirectory
(_,gr',_) <- foldM (\env -> compileSourceModule opts cwd env Nothing) (_,gr',_) <- foldM (\env -> compileSourceModule opts cwd env Nothing)
emptyCompileEnv emptyCompileEnv
(modules gr) (modules gr)
@@ -81,13 +81,13 @@ compileModule opts1 env@(_,rfs) file =
foldM (compileOne' opts) env files foldM (compileOne' opts) env files
where where
getRealFile file = do getRealFile file = do
exists <- liftIO $ doesFileExist file exists <- doesFileExist file
if exists if exists
then return file then return file
else if isRelative file else if isRelative file
then do lib_dir <- getLibraryDirectory opts1 then do lib_dir <- getLibraryDirectory opts1
let file1 = lib_dir </> file let file1 = lib_dir </> file
exists <- liftIO $ doesFileExist file1 exists <- doesFileExist file1
if exists if exists
then return file1 then return file1
else raise (render ("None of these files exists:" $$ nest 2 (file $$ file1))) else raise (render ("None of these files exists:" $$ nest 2 (file $$ file1)))
@@ -108,7 +108,7 @@ extendCompileEnv (gr,menv) (mfile,mo) =
do menv2 <- case mfile of do menv2 <- case mfile of
Just file -> Just file ->
do let (mod,imps) = importsOfModule mo do let (mod,imps) = importsOfModule mo
t <- liftIO $ getModificationTime file t <- getModificationTime file
return $ Map.insert mod (t,imps) menv return $ Map.insert mod (t,imps) menv
_ -> return menv _ -> return menv
return (prependModule gr mo,menv2) return (prependModule gr mo,menv2)

View File

@@ -139,7 +139,7 @@ findFile gfoDir ps name =
noGFO = raise (render ("File" <+> gfFile name <+> "does not exist." $$ noGFO = raise (render ("File" <+> gfFile name <+> "does not exist." $$
"searched in:" <+> vcat ps)) "searched in:" <+> vcat ps))
modtime path = liftIO $ getModificationTime path modtime path = getModificationTime path
gfImports opts file = importsOfModule `fmap` parseModHeader opts file gfImports opts file = importsOfModule `fmap` parseModHeader opts file

View File

@@ -37,7 +37,7 @@ compileOne :: Options -> SourceGrammar -> FullPath -> IOE OneOutput
compileOne opts srcgr file = compileOne opts srcgr file =
if isGFO file if isGFO file
then reuseGFO opts srcgr file then reuseGFO opts srcgr file
else do b1 <- liftIO $ doesFileExist file else do b1 <- doesFileExist file
if b1 then useTheSource if b1 then useTheSource
else reuseGFO opts srcgr (gf2gfo opts file) else reuseGFO opts srcgr (gf2gfo opts file)
where where
@@ -47,7 +47,7 @@ compileOne opts srcgr file =
("- compiling" +++ file ++ "... ") ("- compiling" +++ file ++ "... ")
(getSourceModule opts file) (getSourceModule opts file)
idump opts Source sm idump opts Source sm
cwd <- liftIO getCurrentDirectory cwd <- getCurrentDirectory
compileSourceModule opts cwd (Just file) srcgr sm compileSourceModule opts cwd (Just file) srcgr sm
putpOpt v m act putpOpt v m act
@@ -65,7 +65,7 @@ reuseGFO opts srcgr file =
idump opts Source sm0 idump opts Source sm0
let sm1 = unsubexpModule sm0 let sm1 = unsubexpModule sm0
cwd <- liftIO getCurrentDirectory cwd <- getCurrentDirectory
(sm,warnings) <- -- putPointE Normal opts "creating indirections" $ (sm,warnings) <- -- putPointE Normal opts "creating indirections" $
runCheck $ extendModule cwd srcgr sm1 runCheck $ extendModule cwd srcgr sm1
warnOut opts warnings warnOut opts warnings

View File

@@ -1,7 +1,19 @@
-- | Isolate backwards incompatible library changes to 'getModificationTime' -- | Isolate backwards incompatible library changes to 'getModificationTime'
module GF.System.Directory(getModificationTime,module D) where -- and provide lifted versions of some directory operations
module GF.System.Directory(module GF.System.Directory,module D) where
import Control.Monad.Trans(MonadIO(..))
import qualified System.Directory as D import qualified System.Directory as D
import System.Directory as D hiding (getModificationTime) import System.Directory as D
hiding (doesDirectoryExist,doesFileExist,getModificationTime,
getCurrentDirectory,getDirectoryContents,removeFile)
import Data.Time.Compat import Data.Time.Compat
getModificationTime path = fmap toUTCTime (D.getModificationTime path) doesDirectoryExist path = liftIO $ D.doesDirectoryExist path
doesFileExist path = liftIO $ D.doesFileExist path
getModificationTime path = liftIO $ fmap toUTCTime (D.getModificationTime path)
getDirectoryContents path = liftIO $ D.getDirectoryContents path
getCurrentDirectory :: MonadIO io => io FilePath
getCurrentDirectory = liftIO D.getCurrentDirectory
removeFile path = liftIO $ D.removeFile path

View File

@@ -190,8 +190,8 @@ handle logLn documentroot state0 cache execute1 stateVar
inDir ok = cd =<< look "dir" inDir ok = cd =<< look "dir"
where where
cd ('/':dir@('t':'m':'p':_)) = cd ('/':dir@('t':'m':'p':_)) =
do cwd <- liftIO $ getCurrentDirectory do cwd <- getCurrentDirectory
b <- liftIO $ doesDirectoryExist dir b <- doesDirectoryExist dir
case b of case b of
False -> do b <- liftIO $ try $ readFile dir -- poor man's symbolic links False -> do b <- liftIO $ try $ readFile dir -- poor man's symbolic links
case b of case b of
@@ -247,7 +247,7 @@ handle logLn documentroot state0 cache execute1 stateVar
logPutStrLn cmd logPutStrLn cmd
out@(ecode,_,_) <- liftIO $ readProcessWithExitCode "gf" args "" out@(ecode,_,_) <- liftIO $ readProcessWithExitCode "gf" args ""
logPutStrLn $ show ecode logPutStrLn $ show ecode
cwd <- liftIO $ getCurrentDirectory cwd <- getCurrentDirectory
return $ json200 (jsonresult cwd ('/':dir++"/") cmd out files) return $ json200 (jsonresult cwd ('/':dir++"/") cmd out files)
upload skip files = upload skip files =
@@ -265,15 +265,15 @@ handle logLn documentroot state0 cache execute1 stateVar
jsonList' details ext = fmap (json200) (details =<< ls_ext "." ext) jsonList' details ext = fmap (json200) (details =<< ls_ext "." ext)
addTime path = addTime path =
do t <- liftIO $ getModificationTime path do t <- getModificationTime path
return $ makeObj ["path".=path,"time".=format t] return $ makeObj ["path".=path,"time".=format t]
where where
format = formatTime defaultTimeLocale rfc822DateFormat format = formatTime defaultTimeLocale rfc822DateFormat
rm path | takeExtension path `elem` ok_to_delete = rm path | takeExtension path `elem` ok_to_delete =
do b <- liftIO $ doesFileExist path do b <- doesFileExist path
if b if b
then do liftIO $ removeFile path then do removeFile path
return $ ok200 "" return $ ok200 ""
else err $ resp404 path else err $ resp404 path
rm path = err $ resp400 $ "unacceptable extension "++path rm path = err $ resp400 $ "unacceptable extension "++path
@@ -306,7 +306,7 @@ handle logLn documentroot state0 cache execute1 stateVar
return $ jsonp qs pgfs return $ jsonp qs pgfs
ls_ext dir ext = ls_ext dir ext =
do paths <- liftIO $ getDirectoryContents dir do paths <- getDirectoryContents dir
return [path | path<-paths, takeExtension path==ext] return [path | path<-paths, takeExtension path==ext]
-- * Dynamic content -- * Dynamic content