File: CabalDev.hs

package info (click to toggle)
ghc-mod 1.10.18-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 204 kB
  • sloc: lisp: 818; haskell: 721; sh: 34; makefile: 27
file content (51 lines) | stat: -rw-r--r-- 1,503 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module CabalDev (modifyOptions) where

{-
  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.
-}

import Control.Applicative ((<$>))
import Control.Exception (throwIO)
import Control.Exception.IOChoice
import Data.List (find)
import System.Directory
import System.FilePath (splitPath,joinPath,(</>))
import Text.Regex.Posix ((=~))
import Types

modifyOptions :: Options -> IO Options
modifyOptions opts = found ||> notFound
  where
    found = addPath opts <$> findCabalDev (sandbox opts)
    notFound = return opts

findCabalDev :: Maybe String -> IO FilePath
findCabalDev (Just path) = do
    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
    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
    a <- doesDirectoryExist (mpath path)
    if a then
        findConf (mpath path)
      else
        searchIt $ init path
  where
    mpath a = joinPath a </> "cabal-dev/"

findConf :: FilePath -> IO FilePath
findConf path = do
    Just f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path
    return $ path </> f