2017-05-28 02:22:56 +00:00
|
|
|
module GhcMod.FileMapping
|
2015-05-31 08:32:46 +00:00
|
|
|
( loadMappedFile
|
2015-08-16 20:20:00 +00:00
|
|
|
, loadMappedFileSource
|
2015-06-09 09:45:27 +00:00
|
|
|
, unloadMappedFile
|
2015-05-31 08:32:46 +00:00
|
|
|
, mapFile
|
2015-07-03 19:31:52 +00:00
|
|
|
, fileModSummaryWithMapping
|
2015-05-31 08:32:46 +00:00
|
|
|
) where
|
|
|
|
|
2017-05-28 02:22:56 +00:00
|
|
|
import GhcMod.Types
|
|
|
|
import GhcMod.Monad.Types
|
|
|
|
import GhcMod.Gap
|
|
|
|
import GhcMod.HomeModuleGraph
|
|
|
|
import GhcMod.Utils
|
2015-05-31 08:32:46 +00:00
|
|
|
|
2015-08-16 20:20:00 +00:00
|
|
|
import System.IO
|
|
|
|
import System.FilePath
|
|
|
|
import System.Directory
|
2015-05-31 08:32:46 +00:00
|
|
|
|
2015-06-16 08:28:14 +00:00
|
|
|
import Control.Monad.Trans.Maybe
|
2015-05-31 08:32:46 +00:00
|
|
|
import GHC
|
2015-08-16 20:20:00 +00:00
|
|
|
import Control.Monad
|
2015-05-31 08:32:46 +00:00
|
|
|
|
2015-12-20 10:45:51 +00:00
|
|
|
{- | maps 'FilePath', given as first argument to take source from
|
|
|
|
'FilePath' given as second argument. Works exactly the same as
|
|
|
|
first form of `--map-file` CLI option.
|
|
|
|
|
|
|
|
\'from\' can be either full path, or path relative to project root.
|
|
|
|
\'to\' has to be either relative to project root, or full path (preferred)
|
|
|
|
-}
|
|
|
|
loadMappedFile :: IOish m
|
|
|
|
=> FilePath -- ^ \'from\', file that will be mapped
|
|
|
|
-> FilePath -- ^ \'to\', file to take source from
|
|
|
|
-> GhcModT m ()
|
2015-08-16 20:38:32 +00:00
|
|
|
loadMappedFile from to = loadMappedFile' from to False
|
2015-05-31 08:32:46 +00:00
|
|
|
|
2015-12-20 10:45:51 +00:00
|
|
|
{- |
|
|
|
|
maps 'FilePath', given as first argument to have source as given
|
|
|
|
by second argument.
|
|
|
|
|
|
|
|
\'from\' may or may not exist, and should be either full path,
|
|
|
|
or relative to project root.
|
|
|
|
-}
|
|
|
|
loadMappedFileSource :: IOish m
|
|
|
|
=> FilePath -- ^ \'from\', file that will be mapped
|
|
|
|
-> String -- ^ \'src\', source
|
|
|
|
-> GhcModT m ()
|
2015-08-16 20:20:00 +00:00
|
|
|
loadMappedFileSource from src = do
|
|
|
|
tmpdir <- cradleTempDir `fmap` cradle
|
2016-02-09 13:25:30 +00:00
|
|
|
enc <- liftIO . mkTextEncoding . optEncoding =<< options
|
2015-08-16 20:20:00 +00:00
|
|
|
to <- liftIO $ do
|
|
|
|
(fn, h) <- openTempFile tmpdir (takeFileName from)
|
2016-02-09 13:25:30 +00:00
|
|
|
hSetEncoding h enc
|
2015-08-16 20:20:00 +00:00
|
|
|
hPutStr h src
|
|
|
|
hClose h
|
|
|
|
return fn
|
2015-08-16 20:38:32 +00:00
|
|
|
loadMappedFile' from to True
|
|
|
|
|
|
|
|
loadMappedFile' :: IOish m => FilePath -> FilePath -> Bool -> GhcModT m ()
|
|
|
|
loadMappedFile' from to isTemp = do
|
|
|
|
cfn <- getCanonicalFileNameSafe from
|
|
|
|
unloadMappedFile' cfn
|
2015-09-19 06:34:53 +00:00
|
|
|
crdl <- cradle
|
|
|
|
let to' = makeRelative (cradleRootDir crdl) to
|
|
|
|
addMMappedFile cfn (FileMapping to' isTemp)
|
2015-08-16 20:20:00 +00:00
|
|
|
|
2016-05-22 00:53:51 +00:00
|
|
|
mapFile :: (IOish m, GmState m) => HscEnv -> Target -> m Target
|
2015-05-31 08:32:46 +00:00
|
|
|
mapFile _ (Target tid@(TargetFile filePath _) taoc _) = do
|
|
|
|
mapping <- lookupMMappedFile filePath
|
2016-05-22 00:53:51 +00:00
|
|
|
return $ mkMappedTarget (Just filePath) tid taoc mapping
|
2015-05-31 08:32:46 +00:00
|
|
|
mapFile env (Target tid@(TargetModule moduleName) taoc _) = do
|
2015-08-16 20:20:00 +00:00
|
|
|
(fp, mapping) <- do
|
|
|
|
filePath <- fmap (fmap mpPath) (liftIO $ findModulePath env moduleName)
|
|
|
|
mmf <- runMaybeT $ MaybeT (return filePath) >>= MaybeT . lookupMMappedFile
|
|
|
|
return (filePath, mmf)
|
2016-05-22 00:53:51 +00:00
|
|
|
return $ mkMappedTarget fp tid taoc mapping
|
2015-08-16 20:20:00 +00:00
|
|
|
|
2016-05-22 00:53:51 +00:00
|
|
|
mkMappedTarget :: Maybe FilePath -> TargetId -> Bool -> Maybe FileMapping -> Target
|
2015-08-16 20:20:00 +00:00
|
|
|
mkMappedTarget _ _ taoc (Just to) =
|
2016-05-22 00:53:51 +00:00
|
|
|
mkTarget (TargetFile (fmPath to) Nothing) taoc Nothing
|
|
|
|
mkMappedTarget _ tid taoc _ =
|
|
|
|
mkTarget tid taoc Nothing
|
2015-06-09 09:45:27 +00:00
|
|
|
|
2015-12-20 10:45:51 +00:00
|
|
|
{-|
|
|
|
|
unloads previously mapped file \'file\', so that it's no longer mapped,
|
|
|
|
and removes any temporary files created when file was
|
|
|
|
mapped.
|
|
|
|
|
|
|
|
\'file\' should be either full path, or relative to project root.
|
|
|
|
-}
|
|
|
|
unloadMappedFile :: IOish m
|
|
|
|
=> FilePath -- ^ \'file\', file to unmap
|
|
|
|
-> GhcModT m ()
|
2015-08-16 20:38:32 +00:00
|
|
|
unloadMappedFile = getCanonicalFileNameSafe >=> unloadMappedFile'
|
|
|
|
|
|
|
|
unloadMappedFile' :: IOish m => FilePath -> GhcModT m ()
|
|
|
|
unloadMappedFile' cfn = void $ runMaybeT $ do
|
2015-08-16 20:20:00 +00:00
|
|
|
fm <- MaybeT $ lookupMMappedFile cfn
|
|
|
|
liftIO $ when (fmTemp fm) $ removeFile (fmPath fm)
|
|
|
|
delMMappedFile cfn
|
2015-07-03 19:31:52 +00:00
|
|
|
|
|
|
|
fileModSummaryWithMapping :: (IOish m, GmState m, GhcMonad m, GmEnv m) =>
|
|
|
|
FilePath -> m ModSummary
|
2015-08-16 20:20:00 +00:00
|
|
|
fileModSummaryWithMapping fn =
|
|
|
|
withMappedFile fn $ \fn' -> fileModSummary fn'
|