module HsDev.Cache (
escapePath,
cabalCache,
projectCache,
standaloneCache,
dump,
load,
Database
) where
import Control.DeepSeq (force)
import Control.Lens (view)
import Data.Aeson (eitherDecode)
import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Char (isAlphaNum)
import Data.List (intercalate)
import System.FilePath
import HsDev.Symbols (Cabal(..))
import HsDev.Project
import HsDev.Database (Database)
escapePath :: FilePath -> FilePath
escapePath = intercalate "." . map (filter isAlphaNum) . splitDirectories
cabalCache :: Cabal -> FilePath
cabalCache Cabal = "cabal" <.> "json"
cabalCache (Sandbox p) = escapePath p <.> "json"
projectCache :: Project -> FilePath
projectCache p = (escapePath . view projectPath $ p) <.> "json"
standaloneCache :: FilePath
standaloneCache = "standalone" <.> "json"
dump :: FilePath -> Database -> IO ()
dump file = BS.writeFile file . encodePretty
load :: FilePath -> IO (Either String Database)
load file = do
cts <- BS.readFile file
return $ force $ eitherDecode cts