module CabalHelper.Shared.Sandbox where
import Control.Applicative
import Data.Char
import Data.Maybe
import Data.List
import Data.Version
import System.FilePath
import System.Directory
import Prelude
import qualified Data.Traversable as T
getSandboxPkgDb :: FilePath
-> String
-> Version
-> IO (Maybe FilePath)
getSandboxPkgDb d platform ghcVer = do
mConf <- T.traverse readFile =<< mightExist (d </> "cabal.sandbox.config")
return $ fixPkgDbVer <$> (extractSandboxDbDir =<< mConf)
where
fixPkgDbVer dir =
case takeFileName dir == ghcSandboxPkgDbDir platform ghcVer of
True -> dir
False -> takeDirectory dir </> ghcSandboxPkgDbDir platform ghcVer
ghcSandboxPkgDbDir :: String -> Version -> String
ghcSandboxPkgDbDir platform ghcVer =
platform ++ "-ghc-" ++ showVersion ghcVer ++ "-packages.conf.d"
extractSandboxDbDir :: String -> Maybe FilePath
extractSandboxDbDir conf = extractValue <$> parse conf
where
key = "package-db:"
keyLen = length key
parse = listToMaybe . filter (key `isPrefixOf`) . lines
extractValue = CabalHelper.Shared.Sandbox.dropWhileEnd isSpace . dropWhile isSpace . drop keyLen
mightExist :: FilePath -> IO (Maybe FilePath)
mightExist f = do
exists <- doesFileExist f
return $ if exists then (Just f) else (Nothing)
dropWhileEnd :: (a -> Bool) -> [a] -> [a]
dropWhileEnd p = foldr (\x xs -> if p x && null xs then [] else x : xs) []