2017-03-06 23:19:57 +00:00
|
|
|
-- ghc-mod: Happy Haskell Hacking
|
2015-11-26 14:03:14 +00:00
|
|
|
-- Copyright (C) 2015 Daniel Gröber <dxld ÄT darkboxed DOT org>
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-11-26 13:48:26 +00:00
|
|
|
{-# LANGUAGE CPP, 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
|
2015-11-26 13:48:26 +00:00
|
|
|
#if !MIN_VERSION_binary(0,7,0)
|
|
|
|
import Control.Exception
|
|
|
|
#endif
|
2015-03-28 01:30:51 +00:00
|
|
|
import Data.Maybe
|
2015-11-26 13:48:26 +00:00
|
|
|
import Data.Binary hiding (get)
|
2015-08-10 03:00:58 +00:00
|
|
|
import Data.Version
|
2015-08-11 04:35:14 +00:00
|
|
|
import Data.Label
|
2015-11-18 20:41:19 +00:00
|
|
|
import qualified Data.ByteString.Lazy as LBS
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.ByteString.Char8 as BS8
|
2015-03-28 01:30:51 +00:00
|
|
|
import System.FilePath
|
2015-11-26 13:48:26 +00:00
|
|
|
import System.Directory.ModTime
|
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-11-26 13:48:26 +00:00
|
|
|
import Prelude
|
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-11-18 19:51:37 +00:00
|
|
|
cached :: forall m a d. (Gm m, MonadIO m, Binary a, Eq d, Binary 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
|
|
|
|
case mcc of
|
2015-11-18 20:41:19 +00:00
|
|
|
Nothing -> do
|
2015-11-26 13:48:26 +00:00
|
|
|
t <- liftIO $ getCurrentModTime
|
2015-11-18 20:41:19 +00:00
|
|
|
writeCache (TimedCacheFiles t []) Nothing "cache missing or unreadable"
|
|
|
|
Just (t, ifs, d', a) | d /= d' -> do
|
|
|
|
tcfs <- timeCacheInput dir ifs
|
|
|
|
writeCache (TimedCacheFiles t tcfs) (Just a) $ "input data changed" -- ++ " was: " ++ show d ++ " is: " ++ show d'
|
|
|
|
Just (t, ifs, _, a) -> do
|
|
|
|
tcfs <- timeCacheInput dir ifs
|
|
|
|
case invalidatingInputFiles $ TimedCacheFiles t tcfs of
|
|
|
|
[] -> return a
|
|
|
|
_ -> writeCache (TimedCacheFiles t tcfs) (Just a) "input files changed"
|
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-11-26 13:48:26 +00:00
|
|
|
lbsToStrict = BS.concat . LBS.toChunks
|
|
|
|
lbsFromStrict bs = LBS.fromChunks [bs]
|
|
|
|
|
2015-11-18 20:41:19 +00:00
|
|
|
writeCache tcfs ma cause = do
|
|
|
|
(ifs', a) <- (cachedAction cd) tcfs d ma
|
2015-11-26 13:48:26 +00:00
|
|
|
t <- liftIO $ getCurrentModTime
|
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)
|
2015-11-18 19:58:29 +00:00
|
|
|
setLabel label $ Just (t, ifs', d, a)
|
2015-08-11 04:35:14 +00:00
|
|
|
|
2015-11-26 13:48:26 +00:00
|
|
|
let c = BS.append cacheHeader $ lbsToStrict $ encode (t, ifs', d, a)
|
2015-11-18 20:41:19 +00:00
|
|
|
|
|
|
|
liftIO $ BS.writeFile (dir </> cacheFile cd) c
|
|
|
|
|
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-11-26 13:48:26 +00:00
|
|
|
readCache :: m (Maybe (ModTime, [FilePath], d, a))
|
2015-03-28 01:30:51 +00:00
|
|
|
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
|
|
|
|
|
2015-11-26 13:48:26 +00:00
|
|
|
readCacheFromFile :: MaybeT m (ModTime, [FilePath], d, a)
|
2015-08-11 04:35:14 +00:00
|
|
|
readCacheFromFile = do
|
|
|
|
f <- MaybeT $ liftIO $ mightExist $ cacheFile cd
|
|
|
|
readCacheFromFile' f
|
|
|
|
|
2015-11-26 13:48:26 +00:00
|
|
|
readCacheFromFile' :: FilePath -> MaybeT m (ModTime, [FilePath], d, a)
|
2015-08-11 04:35:14 +00:00
|
|
|
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 ->
|
2015-11-26 13:48:26 +00:00
|
|
|
either (const Nothing) Just
|
|
|
|
`liftM` decodeE (lbsFromStrict $ BS.drop 1 rest)
|
2015-08-11 04:35:14 +00:00
|
|
|
_ -> return Nothing
|
2015-03-28 01:30:51 +00:00
|
|
|
|
2015-11-18 19:51:37 +00:00
|
|
|
decodeE b = do
|
2015-11-26 13:48:26 +00:00
|
|
|
#if MIN_VERSION_binary(0,7,0)
|
|
|
|
return $ case decodeOrFail b of
|
2015-11-18 19:51:37 +00:00
|
|
|
Left (_rest, _offset, errmsg) -> Left errmsg
|
|
|
|
Right (_reset, _offset, a) -> Right a
|
2015-11-26 13:48:26 +00:00
|
|
|
#else
|
|
|
|
ea <- liftIO $ try $ evaluate $ decode b
|
|
|
|
return $ case ea of
|
|
|
|
Left (ErrorCall errmsg) -> Left errmsg
|
|
|
|
Right a -> Right a
|
|
|
|
|
|
|
|
#endif
|
2015-11-18 19:58:29 +00:00
|
|
|
|
2015-11-18 20:41:19 +00:00
|
|
|
timeCacheInput :: MonadIO m => FilePath -> [FilePath] -> m [TimedFile]
|
|
|
|
timeCacheInput dir ifs = liftIO $ do
|
2015-03-28 01:30:51 +00:00
|
|
|
ins <- (timeMaybe . (dir </>)) `mapM` ifs
|
2015-11-18 20:41:19 +00:00
|
|
|
return $ catMaybes ins
|
2015-03-28 01:30:51 +00:00
|
|
|
|
2015-11-18 20:41:19 +00:00
|
|
|
invalidatingInputFiles :: TimedCacheFiles -> [FilePath]
|
|
|
|
invalidatingInputFiles (TimedCacheFiles tcreated tcfs) =
|
|
|
|
map tfPath $
|
|
|
|
-- get input files older than tcfile
|
|
|
|
filter ((TimedFile "" tcreated)<) tcfs
|