2015-08-10 03:00:58 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-11 04:35:14 +00:00
|
|
|
module Language.Haskell.GhcMod.Caching (
|
|
|
|
module Language.Haskell.GhcMod.Caching
|
|
|
|
, module Language.Haskell.GhcMod.Caching.Types
|
|
|
|
) where
|
2015-03-28 01:30:51 +00:00
|
|
|
|
2015-08-10 03:00:58 +00:00
|
|
|
import Control.Arrow (first)
|
2015-08-11 04:35:14 +00:00
|
|
|
import Control.Monad
|
2015-03-28 01:30:51 +00:00
|
|
|
import Control.Monad.Trans.Maybe
|
|
|
|
import Data.Maybe
|
2015-08-11 04:35:14 +00:00
|
|
|
import Data.Serialize (Serialize, encode, decode)
|
2015-08-10 03:00:58 +00:00
|
|
|
import Data.Version
|
2015-08-11 04:35:14 +00:00
|
|
|
import Data.Label
|
2015-03-28 01:30:51 +00:00
|
|
|
import qualified Data.ByteString as BS
|
2015-08-10 03:00:58 +00:00
|
|
|
import qualified Data.ByteString.Char8 as BS8
|
2015-03-28 01:30:51 +00:00
|
|
|
import System.FilePath
|
2015-04-12 00:39:18 +00:00
|
|
|
import Utils (TimedFile(..), timeMaybe, mightExist)
|
2015-08-10 03:00:58 +00:00
|
|
|
import Paths_ghc_mod (version)
|
2015-03-28 01:30:51 +00:00
|
|
|
|
|
|
|
import Language.Haskell.GhcMod.Monad.Types
|
2015-08-11 04:35:14 +00:00
|
|
|
import Language.Haskell.GhcMod.Caching.Types
|
2015-03-28 01:30:51 +00:00
|
|
|
import Language.Haskell.GhcMod.Logging
|
|
|
|
|
|
|
|
-- | Cache a MonadIO action with proper invalidation.
|
2015-08-11 04:35:14 +00:00
|
|
|
cached :: forall m a d. (MonadIO m, GmLog m, GmState m, Serialize a, Eq d, Serialize d, Show d)
|
2015-03-28 01:30:51 +00:00
|
|
|
=> FilePath -- ^ Directory to prepend to 'cacheFile'
|
2015-08-11 04:35:14 +00:00
|
|
|
-> Cached m GhcModState d a -- ^ Cache descriptor
|
2015-03-28 01:30:51 +00:00
|
|
|
-> d
|
|
|
|
-> m a
|
|
|
|
cached dir cd d = do
|
|
|
|
mcc <- readCache
|
|
|
|
tcfile <- liftIO $ timeMaybe (cacheFile cd)
|
|
|
|
case mcc of
|
2015-06-07 00:44:11 +00:00
|
|
|
Nothing ->
|
2015-08-10 03:00:58 +00:00
|
|
|
writeCache (TimedCacheFiles tcfile []) Nothing "cache missing or unreadable"
|
2015-04-12 00:39:18 +00:00
|
|
|
Just (ifs, d', a) | d /= d' -> do
|
2015-03-28 01:30:51 +00:00
|
|
|
tcf <- timeCacheInput dir (cacheFile cd) ifs
|
2015-08-03 03:39:52 +00:00
|
|
|
writeCache tcf (Just a) $ "input data changed" -- ++ " was: " ++ show d ++ " is: " ++ show d'
|
2015-03-28 01:30:51 +00:00
|
|
|
Just (ifs, _, a) -> do
|
2015-06-07 00:44:11 +00:00
|
|
|
tcf <- timeCacheInput dir (cacheFile cd) ifs
|
|
|
|
case invalidatingInputFiles tcf of
|
|
|
|
Just [] -> return a
|
|
|
|
Just _ -> writeCache tcf (Just a) "input files changed"
|
|
|
|
Nothing -> writeCache tcf (Just a) "cache missing, existed a sec ago WTF?"
|
2015-03-28 01:30:51 +00:00
|
|
|
|
|
|
|
where
|
2015-08-10 03:00:58 +00:00
|
|
|
cacheHeader = BS8.pack $ "Written by ghc-mod " ++ showVersion version ++ "\n"
|
|
|
|
|
2015-04-12 00:39:18 +00:00
|
|
|
writeCache tcf ma cause = do
|
|
|
|
(ifs', a) <- (cachedAction cd) tcf d ma
|
2015-03-28 01:30:51 +00:00
|
|
|
gmLog GmDebug "" $ (text "regenerating cache") <+>: text (cacheFile cd)
|
|
|
|
<+> parens (text cause)
|
2015-08-11 04:35:14 +00:00
|
|
|
case cacheLens cd of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just label -> do
|
|
|
|
gmLog GmDebug "" $ (text "writing memory cache") <+>: text (cacheFile cd)
|
|
|
|
setLabel label $ Just (ifs', d, a)
|
|
|
|
|
2015-08-10 03:00:58 +00:00
|
|
|
liftIO $ BS.writeFile (dir </> cacheFile cd) $
|
|
|
|
BS.append cacheHeader $ encode (ifs', d, a)
|
2015-03-28 01:30:51 +00:00
|
|
|
return a
|
|
|
|
|
2015-08-11 04:35:14 +00:00
|
|
|
setLabel l x = do
|
|
|
|
s <- gmsGet
|
|
|
|
gmsPut $ set l x s
|
|
|
|
|
2015-03-28 01:30:51 +00:00
|
|
|
readCache :: m (Maybe ([FilePath], d, a))
|
|
|
|
readCache = runMaybeT $ do
|
2015-08-11 04:35:14 +00:00
|
|
|
case cacheLens cd of
|
|
|
|
Just label -> do
|
|
|
|
c <- MaybeT (get label `liftM` gmsGet) `mplus` readCacheFromFile
|
|
|
|
setLabel label $ Just c
|
|
|
|
return c
|
|
|
|
Nothing ->
|
|
|
|
readCacheFromFile
|
|
|
|
|
|
|
|
readCacheFromFile = do
|
|
|
|
f <- MaybeT $ liftIO $ mightExist $ cacheFile cd
|
|
|
|
readCacheFromFile' f
|
|
|
|
|
|
|
|
readCacheFromFile' f = MaybeT $ do
|
|
|
|
gmLog GmDebug "" $ (text "reading cache") <+>: text (cacheFile cd)
|
|
|
|
cc <- liftIO $ BS.readFile f
|
|
|
|
case first BS8.words $ BS8.span (/='\n') cc of
|
|
|
|
(["Written", "by", "ghc-mod", ver], rest)
|
|
|
|
| BS8.unpack ver == showVersion version ->
|
|
|
|
return $ either (const Nothing) Just $ decode $ BS.drop 1 rest
|
|
|
|
_ -> return Nothing
|
2015-03-28 01:30:51 +00:00
|
|
|
|
|
|
|
timeCacheInput :: MonadIO m => FilePath -> FilePath -> [FilePath] -> m TimedCacheFiles
|
|
|
|
timeCacheInput dir cfile ifs = liftIO $ do
|
|
|
|
-- TODO: is checking the times this way around race free?
|
|
|
|
ins <- (timeMaybe . (dir </>)) `mapM` ifs
|
|
|
|
mtcfile <- timeMaybe cfile
|
|
|
|
return $ TimedCacheFiles mtcfile (catMaybes ins)
|
|
|
|
|
|
|
|
invalidatingInputFiles :: TimedCacheFiles -> Maybe [FilePath]
|
|
|
|
invalidatingInputFiles tcf =
|
|
|
|
case tcCacheFile tcf of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just tcfile -> Just $ map tfPath $
|
|
|
|
-- get input files older than tcfile
|
|
|
|
filter (tcfile<) $ tcFiles tcf
|