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

111 lines
3.2 KiB
Haskell
Raw Normal View History

2013-09-21 09:37:33 +00:00
module Language.Haskell.GhcMod.Cradle (
findCradle
, findCradle'
, findSpecCradle
, cleanupCradle
2013-09-21 09:37:33 +00:00
) where
2014-11-01 21:02:47 +00:00
import Language.Haskell.GhcMod.PathsAndFiles
import Language.Haskell.GhcMod.Monad.Types
2014-11-01 21:02:47 +00:00
import Language.Haskell.GhcMod.Types
2014-11-02 18:00:25 +00:00
import Language.Haskell.GhcMod.Utils
import Control.Applicative
import Control.Monad
import Control.Monad.Trans.Maybe
import Data.Maybe
import System.Directory
import System.FilePath
2015-08-03 01:09:56 +00:00
import Prelude
----------------------------------------------------------------
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 = run $ do
2015-08-17 05:41:46 +00:00
(stackCradle dir `mplus` cabalCradle dir `mplus` sandboxCradle dir `mplus` plainCradle dir)
where run a = fillTempDir =<< (fromJust <$> runMaybeT a)
findSpecCradle :: FilePath -> IO Cradle
findSpecCradle dir = do
let cfs = [cabalCradle, sandboxCradle]
cs <- catMaybes <$> mapM (runMaybeT . ($ dir)) cfs
gcs <- filterM isNotGmCradle cs
fillTempDir =<< case gcs of
[] -> fromJust <$> runMaybeT (plainCradle dir)
c:_ -> return c
where
isNotGmCradle :: Cradle -> IO Bool
isNotGmCradle crdl = do
not <$> doesFileExist (cradleRootDir crdl </> "ghc-mod.cabal")
cleanupCradle :: Cradle -> IO ()
cleanupCradle crdl = removeDirectoryRecursive $ cradleTempDir crdl
fillTempDir :: MonadIO m => Cradle -> m Cradle
fillTempDir crdl = do
tmpDir <- liftIO $ newTempDir (cradleRootDir crdl)
return crdl { cradleTempDir = tmpDir }
cabalCradle :: FilePath -> MaybeT IO Cradle
cabalCradle wdir = do
cabalFile <- MaybeT $ findCabalFile wdir
2014-11-01 21:02:47 +00:00
let cabalDir = takeDirectory cabalFile
return Cradle {
2015-08-12 07:25:13 +00:00
cradleProjectType = CabalProject
, cradleCurrentDir = wdir
2014-11-01 21:02:47 +00:00
, cradleRootDir = cabalDir
, cradleTempDir = error "tmpDir"
2014-11-01 21:02:47 +00:00
, cradleCabalFile = Just cabalFile
2015-08-18 09:41:14 +00:00
, cradleDistDir = "dist"
}
2015-08-17 05:41:46 +00:00
stackCradle :: FilePath -> MaybeT IO Cradle
stackCradle wdir = do
cabalFile <- MaybeT $ findCabalFile wdir
let cabalDir = takeDirectory cabalFile
2015-08-17 09:20:43 +00:00
_stackConfigFile <- MaybeT $ findStackConfigFile cabalDir
2015-08-19 04:48:27 +00:00
distDir <- MaybeT $ getStackDistDir cabalDir
2015-08-17 05:41:46 +00:00
return Cradle {
cradleProjectType = StackProject
, cradleCurrentDir = wdir
, cradleRootDir = cabalDir
, cradleTempDir = error "tmpDir"
, cradleCabalFile = Just cabalFile
2015-08-18 09:41:14 +00:00
, cradleDistDir = distDir
2015-08-17 05:41:46 +00:00
}
sandboxCradle :: FilePath -> MaybeT IO Cradle
sandboxCradle wdir = do
sbDir <- MaybeT $ findCabalSandboxDir wdir
return Cradle {
2015-08-12 07:25:13 +00:00
cradleProjectType = SandboxProject
, cradleCurrentDir = wdir
2014-11-01 21:02:47 +00:00
, cradleRootDir = sbDir
, cradleTempDir = error "tmpDir"
, cradleCabalFile = Nothing
2015-08-18 09:41:14 +00:00
, cradleDistDir = "dist"
}
plainCradle :: FilePath -> MaybeT IO Cradle
plainCradle wdir = do
return $ Cradle {
2015-08-12 07:25:13 +00:00
cradleProjectType = PlainProject
, cradleCurrentDir = wdir
, cradleRootDir = wdir
, cradleTempDir = error "tmpDir"
, cradleCabalFile = Nothing
2015-08-18 09:41:14 +00:00
, cradleDistDir = "dist"
}