2013-09-21 09:37:33 +00:00
|
|
|
module Language.Haskell.GhcMod.Cradle (
|
|
|
|
findCradle
|
2014-05-18 01:32:09 +00:00
|
|
|
, findCradle'
|
2013-09-21 09:37:33 +00:00
|
|
|
, findCradleWithoutSandbox
|
|
|
|
) where
|
2013-03-02 03:18:55 +00:00
|
|
|
|
2014-04-15 03:13:10 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
|
|
|
import Language.Haskell.GhcMod.GhcPkg
|
|
|
|
|
2013-03-02 03:18:55 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2014-03-27 06:21:18 +00:00
|
|
|
import qualified Control.Exception as E
|
2014-03-30 08:28:57 +00:00
|
|
|
import Control.Exception.IOChoice ((||>))
|
2013-09-20 06:48:50 +00:00
|
|
|
import Control.Monad (filterM)
|
2014-04-15 03:13:10 +00:00
|
|
|
import Data.List (isSuffixOf)
|
2013-09-20 06:48:50 +00:00
|
|
|
import System.Directory (getCurrentDirectory, getDirectoryContents, doesFileExist)
|
2014-04-15 03:13:10 +00:00
|
|
|
import System.FilePath ((</>), takeDirectory)
|
2013-03-02 03:18:55 +00:00
|
|
|
|
2013-09-05 07:38:17 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Finding 'Cradle'.
|
2013-09-20 06:53:51 +00:00
|
|
|
-- Find a cabal file by tracing ancestor directories.
|
|
|
|
-- Find a sandbox according to a cabal sandbox config
|
|
|
|
-- in a cabal directory.
|
2013-09-20 06:48:50 +00:00
|
|
|
findCradle :: IO Cradle
|
2014-07-17 08:16:44 +00:00
|
|
|
findCradle = findCradle' =<< getCurrentDirectory
|
2014-05-18 01:32:09 +00:00
|
|
|
|
|
|
|
findCradle' :: FilePath -> IO Cradle
|
|
|
|
findCradle' dir = cabalCradle dir ||> sandboxCradle dir ||> plainCradle dir
|
2014-03-30 08:28:57 +00:00
|
|
|
|
|
|
|
cabalCradle :: FilePath -> IO Cradle
|
|
|
|
cabalCradle wdir = do
|
|
|
|
(rdir,cfile) <- cabalDir wdir
|
2014-04-15 03:13:10 +00:00
|
|
|
pkgDbStack <- getPackageDbStack rdir
|
2014-03-30 08:28:57 +00:00
|
|
|
return Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = wdir
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleRootDir = rdir
|
|
|
|
, cradleCabalFile = Just cfile
|
2014-04-15 03:13:10 +00:00
|
|
|
, cradlePkgDbStack = pkgDbStack
|
2013-09-20 06:48:50 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 08:28:57 +00:00
|
|
|
sandboxCradle :: FilePath -> IO Cradle
|
|
|
|
sandboxCradle wdir = do
|
|
|
|
rdir <- getSandboxDir wdir
|
2014-04-15 03:13:10 +00:00
|
|
|
pkgDbStack <- getPackageDbStack rdir
|
2013-09-20 06:48:50 +00:00
|
|
|
return Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = wdir
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleRootDir = rdir
|
|
|
|
, cradleCabalFile = Nothing
|
2014-04-15 03:13:10 +00:00
|
|
|
, cradlePkgDbStack = pkgDbStack
|
2013-09-20 06:48:50 +00:00
|
|
|
}
|
2013-03-02 03:18:55 +00:00
|
|
|
|
2014-03-30 08:28:57 +00:00
|
|
|
plainCradle :: FilePath -> IO Cradle
|
|
|
|
plainCradle wdir = return Cradle {
|
|
|
|
cradleCurrentDir = wdir
|
|
|
|
, cradleRootDir = wdir
|
|
|
|
, cradleCabalFile = Nothing
|
2014-08-20 06:31:26 +00:00
|
|
|
, cradlePkgDbStack = [GlobalDb, UserDb]
|
2014-03-30 08:28:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-21 09:37:33 +00:00
|
|
|
-- Just for testing
|
|
|
|
findCradleWithoutSandbox :: IO Cradle
|
|
|
|
findCradleWithoutSandbox = do
|
|
|
|
cradle <- findCradle
|
2014-08-20 06:31:26 +00:00
|
|
|
return cradle { cradlePkgDbStack = [GlobalDb]} -- FIXME
|
2013-09-21 09:37:33 +00:00
|
|
|
|
2013-09-05 07:38:17 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
2013-09-20 06:48:50 +00:00
|
|
|
-- ,the path to the Cabal file)
|
|
|
|
cabalDir :: FilePath -> IO (FilePath,FilePath)
|
2013-03-02 03:18:55 +00:00
|
|
|
cabalDir dir = do
|
2013-09-20 06:48:50 +00:00
|
|
|
cnts <- getCabalFiles dir
|
2013-03-02 03:18:55 +00:00
|
|
|
case cnts of
|
2014-03-27 06:21:18 +00:00
|
|
|
[] | dir' == dir -> E.throwIO $ userError "cabal files not found"
|
2013-03-02 03:18:55 +00:00
|
|
|
| otherwise -> cabalDir dir'
|
2013-09-20 06:48:50 +00:00
|
|
|
cfile:_ -> return (dir,dir </> cfile)
|
|
|
|
where
|
|
|
|
dir' = takeDirectory dir
|
|
|
|
|
|
|
|
getCabalFiles :: FilePath -> IO [FilePath]
|
|
|
|
getCabalFiles dir = getFiles >>= filterM doesCabalFileExist
|
2013-03-02 03:18:55 +00:00
|
|
|
where
|
2013-09-05 07:38:17 +00:00
|
|
|
isCabal name = cabalSuffix `isSuffixOf` name
|
|
|
|
&& length name > cabalSuffixLength
|
2013-09-20 06:48:50 +00:00
|
|
|
getFiles = filter isCabal <$> getDirectoryContents dir
|
|
|
|
doesCabalFileExist file = doesFileExist $ dir </> file
|
2013-09-05 07:38:17 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-03-30 08:28:57 +00:00
|
|
|
getSandboxDir :: FilePath -> IO FilePath
|
|
|
|
getSandboxDir dir = do
|
|
|
|
exist <- doesFileExist sfile
|
|
|
|
if exist then
|
|
|
|
return dir
|
|
|
|
else if dir == dir' then
|
|
|
|
E.throwIO $ userError "sandbox not found"
|
|
|
|
else
|
|
|
|
getSandboxDir dir'
|
|
|
|
where
|
2014-04-15 03:13:10 +00:00
|
|
|
sfile = dir </> "cabal.sandbox.config"
|
2014-03-30 08:28:57 +00:00
|
|
|
dir' = takeDirectory dir
|