Added withMappedFile utility function

Works pretty much as `withTempFile`, except looks
mapping up and uses temp. file only when necessary.
This commit is contained in:
Nikolay Yakimov 2015-07-02 21:42:48 +03:00
parent d3b1bf125b
commit 5b0cca0353
3 changed files with 24 additions and 2 deletions

View File

@ -0,0 +1,22 @@
module Language.Haskell.GhcMod.FileMappingUtils where
import Language.Haskell.GhcMod.Types
import Language.Haskell.GhcMod.Monad.Types
import System.IO
import System.FilePath
import System.Directory
withMappedFile :: (IOish m, GmState m, GmEnv m) => forall a. FilePath -> (FilePath -> m a) -> m a
withMappedFile file action = lookupMMappedFile file >>= runWithFile
where
runWithFile (Just (RedirectedMapping to)) = action to
runWithFile (Just (MemoryMapping (Just src))) = do
crdl <- cradle
(fp,hndl) <- liftIO $ openTempFile (cradleTempDir crdl) (takeBaseName file)
liftIO $ hPutStr hndl src
liftIO $ hClose hndl
result <- action fp
liftIO $ removeFile fp
return result
runWithFile _ = action file

View File

@ -61,8 +61,7 @@ import Language.Haskell.GhcMod.Logger
import Language.Haskell.GhcMod.Monad.Types
import Language.Haskell.GhcMod.Types
import Language.Haskell.GhcMod.Gap (parseModuleHeader)
import System.IO
import Language.Haskell.GhcMod.FileMappingUtils
-- | Turn module graph into a graphviz dot file
--

View File

@ -106,6 +106,7 @@ Library
Language.Haskell.GhcMod.DynFlags
Language.Haskell.GhcMod.Error
Language.Haskell.GhcMod.FileMapping
Language.Haskell.GhcMod.FileMappingUtils
Language.Haskell.GhcMod.FillSig
Language.Haskell.GhcMod.Find
Language.Haskell.GhcMod.Flag