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
|
2014-10-14 17:52:58 +00:00
|
|
|
, cleanupCradle
|
2013-09-21 09:37:33 +00:00
|
|
|
) where
|
2013-03-02 03:18:55 +00:00
|
|
|
|
2014-04-15 03:13:10 +00:00
|
|
|
import Language.Haskell.GhcMod.GhcPkg
|
2014-11-01 21:02:47 +00:00
|
|
|
import Language.Haskell.GhcMod.PathsAndFiles
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-04-15 03:13:10 +00:00
|
|
|
|
2014-03-30 08:28:57 +00:00
|
|
|
import Control.Exception.IOChoice ((||>))
|
2014-11-01 21:02:47 +00:00
|
|
|
import System.Directory (getCurrentDirectory, removeDirectoryRecursive,
|
|
|
|
getTemporaryDirectory)
|
|
|
|
import System.FilePath (takeDirectory,pathSeparators,splitDrive)
|
2014-10-14 17:52:58 +00:00
|
|
|
import System.IO.Temp
|
|
|
|
|
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
|
|
|
|
2014-10-14 17:52:58 +00:00
|
|
|
newTempDir :: FilePath -> IO FilePath
|
|
|
|
newTempDir dir =
|
|
|
|
flip createTempDirectory uniqPathName =<< getTemporaryDirectory
|
|
|
|
where
|
2014-10-31 09:34:07 +00:00
|
|
|
uniqPathName = "ghc-mod" ++ map escapeSlash (snd $ splitDrive dir)
|
|
|
|
|
|
|
|
escapeSlash c | c `elem` pathSeparators = '-'
|
2014-10-14 17:52:58 +00:00
|
|
|
escapeSlash c = c
|
|
|
|
|
|
|
|
cleanupCradle :: Cradle -> IO ()
|
|
|
|
cleanupCradle crdl = removeDirectoryRecursive $ cradleTempDir crdl
|
|
|
|
|
2014-03-30 08:28:57 +00:00
|
|
|
cabalCradle :: FilePath -> IO Cradle
|
|
|
|
cabalCradle wdir = do
|
2014-11-01 21:02:47 +00:00
|
|
|
Just cabalFile <- findCabalFiles wdir
|
|
|
|
let cabalDir = takeDirectory cabalFile
|
|
|
|
pkgDbStack <- getPackageDbStack cabalDir
|
|
|
|
tmpDir <- newTempDir cabalDir
|
2014-03-30 08:28:57 +00:00
|
|
|
return Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = wdir
|
2014-11-01 21:02:47 +00:00
|
|
|
, cradleRootDir = cabalDir
|
2014-10-14 17:52:58 +00:00
|
|
|
, cradleTempDir = tmpDir
|
2014-11-01 21:02:47 +00:00
|
|
|
, cradleCabalFile = Just cabalFile
|
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
|
2014-11-01 21:02:47 +00:00
|
|
|
Just sbDir <- getSandboxDb wdir
|
|
|
|
pkgDbStack <- getPackageDbStack sbDir
|
|
|
|
tmpDir <- newTempDir sbDir
|
2013-09-20 06:48:50 +00:00
|
|
|
return Cradle {
|
2014-03-28 03:05:11 +00:00
|
|
|
cradleCurrentDir = wdir
|
2014-11-01 21:02:47 +00:00
|
|
|
, cradleRootDir = sbDir
|
2014-10-14 17:52:58 +00:00
|
|
|
, cradleTempDir = tmpDir
|
2014-03-30 08:28:57 +00:00
|
|
|
, 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
|
2014-10-14 17:52:58 +00:00
|
|
|
plainCradle wdir = do
|
|
|
|
tmpDir <- newTempDir wdir
|
|
|
|
return Cradle {
|
2014-03-30 08:28:57 +00:00
|
|
|
cradleCurrentDir = wdir
|
|
|
|
, cradleRootDir = wdir
|
2014-10-14 17:52:58 +00:00
|
|
|
, cradleTempDir = tmpDir
|
2014-03-30 08:28:57 +00:00
|
|
|
, 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
|