{-# LANGUAGE BangPatterns #-}
module Distribution.Cab.Sandbox (
getSandbox
, getSandboxOpts
, getSandboxOpts2
) where
import Control.Exception as E (catch, SomeException, throwIO)
import Data.Char (isSpace)
import Data.List (isPrefixOf, tails)
import System.Directory (getCurrentDirectory, doesFileExist)
import System.FilePath ((</>), takeDirectory, takeFileName)
configFile :: String
configFile :: String
configFile = String
"cabal.sandbox.config"
pkgDbKey :: String
pkgDbKey :: String
pkgDbKey = String
"package-db:"
pkgDbKeyLen :: Int
pkgDbKeyLen :: Int
pkgDbKeyLen = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
pkgDbKey
getSandbox :: IO (Maybe FilePath)
getSandbox :: IO (Maybe String)
getSandbox = (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> IO String -> IO (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO String
getPkgDb) IO (Maybe String)
-> (SomeException -> IO (Maybe String)) -> IO (Maybe String)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` SomeException -> IO (Maybe String)
handler
where
getPkgDb :: IO String
getPkgDb = IO String
getCurrentDirectory IO String -> (String -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO String
getSandboxConfigFile IO String -> (String -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO String
getPackageDbDir
handler :: SomeException -> IO (Maybe String)
handler :: SomeException -> IO (Maybe String)
handler SomeException
_ = Maybe String -> IO (Maybe String)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing
getSandboxConfigFile :: FilePath -> IO FilePath
getSandboxConfigFile :: String -> IO String
getSandboxConfigFile String
dir = do
let cfile :: String
cfile = String
dir String -> String -> String
</> String
configFile
Bool
exist <- String -> IO Bool
doesFileExist String
cfile
if Bool
exist then
String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
cfile
else do
let dir' :: String
dir' = String -> String
takeDirectory String
dir
if String
dir String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
dir' then
IOError -> IO String
forall e a. Exception e => e -> IO a
throwIO (IOError -> IO String) -> IOError -> IO String
forall a b. (a -> b) -> a -> b
$ String -> IOError
userError String
"sandbox config file not found"
else
String -> IO String
getSandboxConfigFile String
dir'
getPackageDbDir :: FilePath -> IO FilePath
getPackageDbDir :: String -> IO String
getPackageDbDir String
sconf = do
!String
path <- String -> String
extractValue (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
parse (String -> String) -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO String
readFile String
sconf
String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
path
where
parse :: String -> String
parse = [String] -> String
forall a. [a] -> a
head ([String] -> String) -> (String -> [String]) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (String
"package-db:" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf`) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines
extractValue :: String -> String
extractValue = (String, String) -> String
forall a b. (a, b) -> a
fst ((String, String) -> String)
-> (String -> (String, String)) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Char -> Bool
isSpace (String -> (String, String))
-> (String -> String) -> String -> (String, String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
forall a. Int -> [a] -> [a]
drop Int
pkgDbKeyLen
getSandboxOpts :: Maybe FilePath -> String
getSandboxOpts :: Maybe String -> String
getSandboxOpts Maybe String
Nothing = String
""
getSandboxOpts (Just String
path) = String
pkgOpt String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
path
where
ghcver :: Int
ghcver = String -> Int
extractGhcVer String
path
pkgOpt :: String
pkgOpt | Int
ghcver Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
706 = String
"-package-db "
| Bool
otherwise = String
"-package-conf "
getSandboxOpts2 :: Maybe FilePath -> String
getSandboxOpts2 :: Maybe String -> String
getSandboxOpts2 Maybe String
Nothing = String
""
getSandboxOpts2 (Just String
path) = String
pkgOpt String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"=" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
path
where
ghcver :: Int
ghcver = String -> Int
extractGhcVer String
path
pkgOpt :: String
pkgOpt | Int
ghcver Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
706 = String
"--package-db"
| Bool
otherwise = String
"--package-conf"
extractGhcVer :: String -> Int
String
dir = Int
ver
where
file :: String
file = String -> String
takeFileName String
dir
findVer :: String -> String
findVer = Int -> String -> String
forall a. Int -> [a] -> [a]
drop Int
4 (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> String
forall a. [a] -> a
head ([String] -> String) -> (String -> [String]) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (String
"ghc-" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf`) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
forall a. [a] -> [[a]]
tails
(String
verStr1,Char
_:String
left) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') (String -> (String, String)) -> String -> (String, String)
forall a b. (a -> b) -> a -> b
$ String -> String
findVer String
file
(String
verStr2,String
_) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') String
left
ver :: Int
ver = String -> Int
forall a. Read a => String -> a
read String
verStr1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
100 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ String -> Int
forall a. Read a => String -> a
read String
verStr2