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

76 lines
2.2 KiB
Haskell
Raw Normal View History

2013-09-21 09:37:33 +00:00
module Language.Haskell.GhcMod.Cradle (
findCradle
, findCradle'
2013-09-21 09:37:33 +00:00
, findCradleWithoutSandbox
, cleanupCradle
2013-09-21 09:37:33 +00:00
) where
import Language.Haskell.GhcMod.GhcPkg
2014-11-01 21:02:47 +00:00
import Language.Haskell.GhcMod.PathsAndFiles
import Language.Haskell.GhcMod.Types
2014-11-02 18:00:25 +00:00
import Language.Haskell.GhcMod.Utils
import Control.Exception.IOChoice ((||>))
2014-11-02 18:00:25 +00:00
import System.Directory (getCurrentDirectory, removeDirectoryRecursive)
import System.FilePath (takeDirectory)
----------------------------------------------------------------
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.
findCradle :: IO Cradle
2014-07-17 08:16:44 +00:00
findCradle = findCradle' =<< getCurrentDirectory
findCradle' :: FilePath -> IO Cradle
findCradle' dir = cabalCradle dir ||> sandboxCradle dir ||> plainCradle dir
cleanupCradle :: Cradle -> IO ()
cleanupCradle crdl = removeDirectoryRecursive $ cradleTempDir crdl
cabalCradle :: FilePath -> IO Cradle
cabalCradle wdir = do
2014-11-02 23:45:27 +00:00
Just cabalFile <- findCabalFile wdir
2014-11-01 21:02:47 +00:00
let cabalDir = takeDirectory cabalFile
pkgDbStack <- getPackageDbStack cabalDir
tmpDir <- newTempDir cabalDir
return Cradle {
cradleCurrentDir = wdir
2014-11-01 21:02:47 +00:00
, cradleRootDir = cabalDir
, cradleTempDir = tmpDir
2014-11-01 21:02:47 +00:00
, cradleCabalFile = Just cabalFile
, cradlePkgDbStack = pkgDbStack
}
sandboxCradle :: FilePath -> IO Cradle
sandboxCradle wdir = do
2014-11-01 21:02:47 +00:00
Just sbDir <- getSandboxDb wdir
pkgDbStack <- getPackageDbStack sbDir
tmpDir <- newTempDir sbDir
return Cradle {
cradleCurrentDir = wdir
2014-11-01 21:02:47 +00:00
, cradleRootDir = sbDir
, cradleTempDir = tmpDir
, cradleCabalFile = Nothing
, cradlePkgDbStack = pkgDbStack
}
plainCradle :: FilePath -> IO Cradle
plainCradle wdir = do
tmpDir <- newTempDir wdir
return Cradle {
cradleCurrentDir = wdir
, cradleRootDir = wdir
, cradleTempDir = tmpDir
, cradleCabalFile = Nothing
, cradlePkgDbStack = [GlobalDb, UserDb]
}
2013-09-21 09:37:33 +00:00
-- Just for testing
findCradleWithoutSandbox :: IO Cradle
findCradleWithoutSandbox = do
cradle <- findCradle
return cradle { cradlePkgDbStack = [GlobalDb]} -- FIXME