ghc-mod/CabalDev.hs

47 lines
1.4 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.
-}
2011-11-24 11:43:15 +00:00
import Control.Applicative ((<$>))
import Control.Exception (SomeException, throwIO)
2011-11-26 13:38:46 +00:00
import Data.List (find)
import GHC (gcatch)
import System.Directory
import System.FilePath (splitPath,joinPath,(</>))
2011-11-26 13:38:46 +00:00
import Text.Regex.Posix ((=~))
import Types
modifyOptions :: Options -> IO Options
modifyOptions opts = found `gcatch` notFound
2012-02-06 06:13:41 +00:00
where
found = addPath opts <$> findCabalDev
notFound :: SomeException -> IO Options
notFound _ = return opts
findCabalDev :: IO String
findCabalDev = 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] }
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