ghc-mod/Language/Haskell/GhcMod/Cradle.hs

124 lines
4.2 KiB
Haskell
Raw Normal View History

2013-05-17 01:00:01 +00:00
module Language.Haskell.GhcMod.Cradle (findCradle) where
import Control.Applicative ((<$>))
import Control.Exception (throwIO)
import Control.Monad (unless, filterM)
2013-03-04 04:55:03 +00:00
import Data.List (isSuffixOf)
import Distribution.System (buildPlatform)
import qualified Distribution.Text as Text (display)
2013-05-17 01:00:01 +00:00
import Language.Haskell.GhcMod.Types
import System.Directory (getCurrentDirectory, getDirectoryContents, doesFileExist, doesDirectoryExist)
import System.FilePath ((</>),takeDirectory)
----------------------------------------------------------------
2013-05-20 05:28:56 +00:00
-- | Finding 'Cradle'.
-- An error would be thrown.
2013-09-05 05:35:28 +00:00
findCradle :: Maybe FilePath -- ^ A 'FilePath' for a sandbox.
2013-05-20 05:28:56 +00:00
-> GHCVersion
-> IO Cradle
2013-03-04 09:11:09 +00:00
findCradle (Just sbox) strver = do
(pkgConf,exist) <- checkPackageConf sbox strver
unless exist $ throwIO $ userError $ pkgConf ++ " not found"
wdir <- getCurrentDirectory
cfiles <- cabalDir wdir
return $ case cfiles of
Nothing -> Cradle {
2013-04-10 05:46:58 +00:00
cradleCurrentDir = wdir
, cradleCabalDir = Nothing
, cradleCabalFile = Nothing
2013-03-04 09:11:09 +00:00
, cradlePackageConf = Just pkgConf
}
Just (cdir,cfile,_) -> Cradle {
2013-04-10 05:46:58 +00:00
cradleCurrentDir = wdir
, cradleCabalDir = Just cdir
, cradleCabalFile = Just cfile
2013-03-04 09:11:09 +00:00
, cradlePackageConf = Just pkgConf
}
2013-03-04 09:11:09 +00:00
findCradle Nothing strver = do
wdir <- getCurrentDirectory
cfiles <- cabalDir wdir
case cfiles of
2013-03-03 06:57:31 +00:00
Nothing -> return Cradle {
2013-03-04 09:11:09 +00:00
cradleCurrentDir = wdir
, cradleCabalDir = Nothing
, cradleCabalFile = Nothing
, cradlePackageConf = Nothing
}
Just (cdir,cfile,Nothing) -> do
return Cradle {
cradleCurrentDir = wdir
, cradleCabalDir = Just cdir
, cradleCabalFile = Just cfile
, cradlePackageConf = Nothing
}
Just (cdir,cfile,Just sbox) -> do
(pkgConf,exist) <- checkPackageConf sbox strver
2013-03-03 06:57:31 +00:00
return Cradle {
2013-03-04 09:11:09 +00:00
cradleCurrentDir = wdir
, cradleCabalDir = Just cdir
, cradleCabalFile = Just cfile
, cradlePackageConf = if exist then Just pkgConf else Nothing
}
----------------------------------------------------------------
cabalSuffix :: String
cabalSuffix = ".cabal"
cabalSuffixLength :: Int
cabalSuffixLength = length cabalSuffix
-- Finding a Cabal file up to the root directory
-- Input: a directly to investigate
-- Output: (the path to the directory containing a Cabal file
-- ,the path to the Cabal file
-- ,Just the path to the sandbox directory)
cabalDir :: FilePath -> IO (Maybe (FilePath,FilePath,Maybe FilePath))
cabalDir dir = do
cnts <- (filter isCabal <$> getDirectoryContents dir)
>>= filterM (\file -> doesFileExist (dir </> file))
let dir' = takeDirectory dir
case cnts of
[] | dir' == dir -> return Nothing
| otherwise -> cabalDir dir'
cfile:_ -> do
msbox <- checkSandbox dir
return $ Just (dir,dir </> cfile, msbox)
where
isCabal name = cabalSuffix `isSuffixOf` name
&& length name > cabalSuffixLength
----------------------------------------------------------------
sandboxConfig :: String
sandboxConfig = "cabal.sandbox.config"
sandboxDir :: String
sandboxDir = ".cabal-sandbox"
checkSandbox :: FilePath -> IO (Maybe FilePath)
checkSandbox dir = do
let conf = dir </> sandboxConfig
sbox = dir </> sandboxDir
sandboxConfigExists <- doesFileExist conf
sandboxExists <- doesDirectoryExist sbox
if sandboxConfigExists && sandboxExists then
return (Just sbox)
else
return Nothing
----------------------------------------------------------------
packageConfName :: GHCVersion -> FilePath
packageConfName strver = Text.display buildPlatform
++ "-ghc-"
++ strver
++ "-packages.conf.d"
checkPackageConf :: FilePath -> GHCVersion -> IO (FilePath, Bool)
checkPackageConf path strver = do
let dir = path </> packageConfName strver
exist <- doesDirectoryExist dir
return (dir,exist)