ghc-mod/CabalDev.hs

52 lines
1.5 KiB
Haskell
Raw Normal View History

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.
-}
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)
import System.Directory
2012-02-15 05:52:48 +00:00
import System.FilePath (splitPath,joinPath,(</>))
import Text.Regex.Posix ((=~))
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
findCabalDev :: Maybe String -> IO FilePath
findCabalDev (Just path) = do
2012-04-10 00:43:02 +00:00
exist <- doesDirectoryExist path
if exist then
findConf path
else
findCabalDev Nothing
findCabalDev Nothing = getCurrentDirectory >>= searchIt . splitPath
addPath :: Options -> String -> Options
addPath orig_opts path = do
2012-02-06 06:13:41 +00:00
let orig_ghcopt = ghcOpts orig_opts
orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path, "-no-user-package-conf"] }
searchIt :: [FilePath] -> IO FilePath
searchIt [] = throwIO $ userError "Not found"
searchIt path = do
2012-02-06 06:13:41 +00:00
a <- doesDirectoryExist (mpath path)
2012-02-08 07:11:36 +00:00
if a then
2012-02-06 06:13:41 +00:00
findConf (mpath path)
else
searchIt $ init path
2011-11-02 01:43:34 +00:00
where
mpath a = joinPath a </> "cabal-dev/"
2011-11-26 13:38:46 +00:00
findConf :: FilePath -> IO FilePath
2011-11-26 13:38:46 +00:00
findConf path = do
Just f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
return $ path </> f