CabalDev.hs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. module CabalDev (modifyOptions) where
  2. {-
  3. If the directory 'cabal-dev/packages-X.X.X.conf' exists, add it to the
  4. options ghc-mod uses to check the source. Otherwise just pass it on.
  5. -}
  6. import Control.Applicative ((<$>))
  7. import Control.Exception (throwIO)
  8. import Control.Exception.IOChoice
  9. import Data.List (find)
  10. import System.Directory
  11. import System.FilePath (splitPath,joinPath,(</>))
  12. import Text.Regex.Posix ((=~))
  13. import Types
  14. modifyOptions :: Options -> IO Options
  15. modifyOptions opts = found ||> notFound
  16. where
  17. found = addPath opts <$> findCabalDev (sandbox opts)
  18. notFound = return opts
  19. findCabalDev :: Maybe String -> IO FilePath
  20. findCabalDev (Just path) = do
  21. exist <- doesDirectoryExist path
  22. if exist then
  23. findConf path
  24. else
  25. findCabalDev Nothing
  26. findCabalDev Nothing = getCurrentDirectory >>= searchIt . splitPath
  27. addPath :: Options -> String -> Options
  28. addPath orig_opts path = do
  29. let orig_ghcopt = ghcOpts orig_opts
  30. orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path, "-no-user-package-conf"] }
  31. searchIt :: [FilePath] -> IO FilePath
  32. searchIt [] = throwIO $ userError "Not found"
  33. searchIt path = do
  34. let cabalDir = mpath path
  35. exist <- doesDirectoryExist cabalDir
  36. if exist then
  37. findConf cabalDir
  38. else
  39. searchIt $ init path
  40. where
  41. mpath a = joinPath a </> "cabal-dev/"
  42. findConf :: FilePath -> IO FilePath
  43. findConf path = do
  44. Just f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
  45. return $ path </> f