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:
parent
d3b1bf125b
commit
5b0cca0353
22
Language/Haskell/GhcMod/FileMappingUtils.hs
Normal file
22
Language/Haskell/GhcMod/FileMappingUtils.hs
Normal 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
|
@ -61,8 +61,7 @@ import Language.Haskell.GhcMod.Logger
|
|||||||
import Language.Haskell.GhcMod.Monad.Types
|
import Language.Haskell.GhcMod.Monad.Types
|
||||||
import Language.Haskell.GhcMod.Types
|
import Language.Haskell.GhcMod.Types
|
||||||
import Language.Haskell.GhcMod.Gap (parseModuleHeader)
|
import Language.Haskell.GhcMod.Gap (parseModuleHeader)
|
||||||
|
import Language.Haskell.GhcMod.FileMappingUtils
|
||||||
import System.IO
|
|
||||||
|
|
||||||
-- | Turn module graph into a graphviz dot file
|
-- | Turn module graph into a graphviz dot file
|
||||||
--
|
--
|
||||||
|
@ -106,6 +106,7 @@ Library
|
|||||||
Language.Haskell.GhcMod.DynFlags
|
Language.Haskell.GhcMod.DynFlags
|
||||||
Language.Haskell.GhcMod.Error
|
Language.Haskell.GhcMod.Error
|
||||||
Language.Haskell.GhcMod.FileMapping
|
Language.Haskell.GhcMod.FileMapping
|
||||||
|
Language.Haskell.GhcMod.FileMappingUtils
|
||||||
Language.Haskell.GhcMod.FillSig
|
Language.Haskell.GhcMod.FillSig
|
||||||
Language.Haskell.GhcMod.Find
|
Language.Haskell.GhcMod.Find
|
||||||
Language.Haskell.GhcMod.Flag
|
Language.Haskell.GhcMod.Flag
|
||||||
|
Loading…
Reference in New Issue
Block a user