ghc-mod/CabalDev.hs

50 lines
1.3 KiB
Haskell
Raw Normal View History

2011-11-02 01:43:34 +00:00
{-# LANGUAGE DoAndIfThenElse #-}
module CabalDev (modifyOptions) where
{-
2011-11-02 01:43:34 +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 ((<$>))
2011-11-26 13:38:46 +00:00
import Data.List (find)
import System.FilePath (splitPath,joinPath,(</>))
import System.Directory
2011-11-26 13:38:46 +00:00
import Text.Regex.Posix ((=~))
import Types
modifyOptions :: Options -> IO Options
modifyOptions opts =
fmap (has_cdev opts) findCabalDev
where
2011-11-02 01:43:34 +00:00
has_cdev :: Options -> Maybe String -> Options
has_cdev op Nothing = op
has_cdev op (Just path) = addPath op path
findCabalDev :: IO (Maybe String)
findCabalDev =
2011-11-26 13:38:46 +00:00
getCurrentDirectory >>= searchIt . splitPath
addPath :: Options -> String -> Options
addPath orig_opts path = do
let orig_ghcopt = ghcOpts orig_opts
orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path] }
searchIt :: [FilePath] -> IO (Maybe FilePath)
searchIt [] = return Nothing
searchIt path = do
a <- doesDirectoryExist (mpath path)
2011-11-02 01:43:34 +00:00
if a then do
2011-11-26 13:38:46 +00:00
findConf (mpath path)
2011-11-02 01:43:34 +00:00
else
2011-11-24 11:43:15 +00:00
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 (Maybe FilePath)
findConf path = do
f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
return $ ((path </>) <$> f)