2011-11-01 22:06:53 +00:00
|
|
|
module CabalDev (modifyOptions) where
|
|
|
|
|
|
|
|
{-
|
2012-02-06 06:13:41 +00:00
|
|
|
If the directory 'cabal-dev/packages-X.X.X.conf' exists, add it to the
|
|
|
|
options ghc-mod uses to check the source. Otherwise just pass it on.
|
2011-11-01 22:06:53 +00:00
|
|
|
-}
|
|
|
|
|
2012-02-16 05:44:20 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2012-02-15 05:52:48 +00:00
|
|
|
import Control.Exception (throwIO)
|
2012-02-16 05:44:20 +00:00
|
|
|
import Control.Exception.IOChoice
|
2012-02-15 05:52:48 +00:00
|
|
|
import Data.List (find)
|
2011-11-01 22:06:53 +00:00
|
|
|
import System.Directory
|
2012-02-15 05:52:48 +00:00
|
|
|
import System.FilePath (splitPath,joinPath,(</>))
|
|
|
|
import Text.Regex.Posix ((=~))
|
2011-11-01 22:06:53 +00:00
|
|
|
import Types
|
|
|
|
|
|
|
|
modifyOptions :: Options -> IO Options
|
2012-02-16 05:44:20 +00:00
|
|
|
modifyOptions opts = found ||> notFound
|
2012-02-06 06:13:41 +00:00
|
|
|
where
|
2012-04-10 00:43:02 +00:00
|
|
|
found = addPath opts <$> findCabalDev (sandbox opts)
|
2012-02-15 05:52:48 +00:00
|
|
|
notFound = return opts
|
2011-11-01 22:06:53 +00:00
|
|
|
|
2012-04-09 19:39:58 +00:00
|
|
|
findCabalDev :: Maybe String -> IO FilePath
|
|
|
|
findCabalDev (Just path) = do
|
2012-04-10 00:43:02 +00:00
|
|
|
exist <- doesDirectoryExist path
|
|
|
|
if exist then
|
2012-04-09 19:39:58 +00:00
|
|
|
findConf path
|
|
|
|
else
|
|
|
|
findCabalDev Nothing
|
|
|
|
findCabalDev Nothing = getCurrentDirectory >>= searchIt . splitPath
|
2011-11-01 22:06:53 +00:00
|
|
|
|
|
|
|
addPath :: Options -> String -> Options
|
|
|
|
addPath orig_opts path = do
|
2012-02-06 06:13:41 +00:00
|
|
|
let orig_ghcopt = ghcOpts orig_opts
|
2012-04-09 19:43:08 +00:00
|
|
|
orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path, "-no-user-package-conf"] }
|
2011-11-01 22:06:53 +00:00
|
|
|
|
2012-02-14 09:37:45 +00:00
|
|
|
searchIt :: [FilePath] -> IO FilePath
|
|
|
|
searchIt [] = throwIO $ userError "Not found"
|
2011-11-01 22:06:53 +00:00
|
|
|
searchIt path = do
|
2012-07-06 07:55:14 +00:00
|
|
|
let cabalDir = mpath path
|
|
|
|
exist <- doesDirectoryExist cabalDir
|
|
|
|
if exist then
|
|
|
|
findConf cabalDir
|
2012-02-06 06:13:41 +00:00
|
|
|
else
|
|
|
|
searchIt $ init path
|
2011-11-02 01:43:34 +00:00
|
|
|
where
|
2011-11-14 10:20:02 +00:00
|
|
|
mpath a = joinPath a </> "cabal-dev/"
|
2011-11-26 13:38:46 +00:00
|
|
|
|
2012-02-14 09:37:45 +00:00
|
|
|
findConf :: FilePath -> IO FilePath
|
2011-11-26 13:38:46 +00:00
|
|
|
findConf path = do
|
2012-02-14 09:37:45 +00:00
|
|
|
Just f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
|
|
|
|
return $ path </> f
|