From 31020c41125cc66be4da06cd6efd1b4a5be28eb1 Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Fri, 3 Jul 2015 06:43:32 +0300 Subject: [PATCH] Move withMappedFile to Language.Haskell.GhcMod.Utils --- Language/Haskell/GhcMod/FileMappingUtils.hs | 22 --------------------- Language/Haskell/GhcMod/HomeModuleGraph.hs | 2 +- Language/Haskell/GhcMod/Lint.hs | 2 +- Language/Haskell/GhcMod/Utils.hs | 21 ++++++++++++++++++-- ghc-mod.cabal | 1 - 5 files changed, 21 insertions(+), 27 deletions(-) delete mode 100644 Language/Haskell/GhcMod/FileMappingUtils.hs diff --git a/Language/Haskell/GhcMod/FileMappingUtils.hs b/Language/Haskell/GhcMod/FileMappingUtils.hs deleted file mode 100644 index ee26417..0000000 --- a/Language/Haskell/GhcMod/FileMappingUtils.hs +++ /dev/null @@ -1,22 +0,0 @@ -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 diff --git a/Language/Haskell/GhcMod/HomeModuleGraph.hs b/Language/Haskell/GhcMod/HomeModuleGraph.hs index c3fa6dc..a5aae65 100644 --- a/Language/Haskell/GhcMod/HomeModuleGraph.hs +++ b/Language/Haskell/GhcMod/HomeModuleGraph.hs @@ -61,7 +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 Language.Haskell.GhcMod.FileMappingUtils +import Language.Haskell.GhcMod.Utils (withMappedFile) -- | Turn module graph into a graphviz dot file -- diff --git a/Language/Haskell/GhcMod/Lint.hs b/Language/Haskell/GhcMod/Lint.hs index 8fc710a..735411a 100644 --- a/Language/Haskell/GhcMod/Lint.hs +++ b/Language/Haskell/GhcMod/Lint.hs @@ -8,7 +8,7 @@ import Language.Haskell.GhcMod.Monad import Language.Haskell.GhcMod.Types import Language.Haskell.HLint (hlint) -import Language.Haskell.GhcMod.FileMappingUtils +import Language.Haskell.GhcMod.Utils (withMappedFile) import Data.List (stripPrefix) diff --git a/Language/Haskell/GhcMod/Utils.hs b/Language/Haskell/GhcMod/Utils.hs index c9da5a2..87c9a53 100644 --- a/Language/Haskell/GhcMod/Utils.hs +++ b/Language/Haskell/GhcMod/Utils.hs @@ -27,13 +27,15 @@ import Control.Applicative import Data.Char import Exception import Language.Haskell.GhcMod.Error +import Language.Haskell.GhcMod.Types import Language.Haskell.GhcMod.Monad.Types import System.Directory (getCurrentDirectory, setCurrentDirectory, doesFileExist, - getTemporaryDirectory, canonicalizePath) + getTemporaryDirectory, canonicalizePath, removeFile) import System.Environment import System.FilePath (splitDrive, takeDirectory, takeFileName, pathSeparators, ()) -import System.IO.Temp (createTempDirectory) +import System.IO.Temp (createTempDirectory, openTempFile) +import System.IO (hPutStr, hClose) import System.Process (readProcess) import Text.Printf @@ -157,3 +159,18 @@ canonFilePath f = do e <- doesFileExist p when (not e) $ error $ "canonFilePath: not a file: " ++ p return p + +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) (takeFileName file) + liftIO $ hPutStr hndl src + liftIO $ hClose hndl + result <- action fp + liftIO $ removeFile fp + return result + runWithFile _ = action file diff --git a/ghc-mod.cabal b/ghc-mod.cabal index 5b5b547..8336665 100644 --- a/ghc-mod.cabal +++ b/ghc-mod.cabal @@ -106,7 +106,6 @@ 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