2015-08-19 06:46:56 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
module Language.Haskell.GhcMod.Cradle
|
|
|
|
#ifndef SPEC
|
|
|
|
(
|
2013-09-21 09:37:33 +00:00
|
|
|
findCradle
|
2014-05-18 01:32:09 +00:00
|
|
|
, findCradle'
|
2015-11-25 15:06:24 +00:00
|
|
|
, findCradleNoLog
|
2015-03-03 20:12:43 +00:00
|
|
|
, findSpecCradle
|
2014-10-14 17:52:58 +00:00
|
|
|
, cleanupCradle
|
2015-08-19 06:46:56 +00:00
|
|
|
)
|
|
|
|
#endif
|
|
|
|
where
|
2013-03-02 03:18:55 +00:00
|
|
|
|
2014-11-01 21:02:47 +00:00
|
|
|
import Language.Haskell.GhcMod.PathsAndFiles
|
2015-03-03 20:12:43 +00:00
|
|
|
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
|
2015-09-15 03:25:00 +00:00
|
|
|
import Language.Haskell.GhcMod.Stack
|
2015-10-30 18:05:41 +00:00
|
|
|
import Language.Haskell.GhcMod.Logging
|
2016-01-08 16:14:10 +00:00
|
|
|
import Language.Haskell.GhcMod.Error
|
2015-10-30 18:05:41 +00:00
|
|
|
|
2016-01-13 03:49:38 +00:00
|
|
|
import Safe
|
2015-03-03 20:12:43 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Control.Monad.Trans.Maybe
|
|
|
|
import Data.Maybe
|
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
2015-11-25 15:06:24 +00:00
|
|
|
import Control.Monad.Trans.Journal (runJournalT)
|
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.
|
2015-10-30 18:05:41 +00:00
|
|
|
findCradle :: (GmLog m, IOish m, GmOut m) => m Cradle
|
2015-09-01 08:27:12 +00:00
|
|
|
findCradle = findCradle' =<< liftIO getCurrentDirectory
|
|
|
|
|
2015-11-25 15:06:24 +00:00
|
|
|
findCradleNoLog :: forall m. (IOish m, GmOut m) => m Cradle
|
|
|
|
findCradleNoLog = fst <$> (runJournalT findCradle :: m (Cradle, GhcModLog))
|
2016-01-08 16:17:21 +00:00
|
|
|
|
2015-10-30 18:05:41 +00:00
|
|
|
findCradle' :: (GmLog m, IOish m, GmOut m) => FilePath -> m Cradle
|
2015-09-01 08:27:12 +00:00
|
|
|
findCradle' dir = run $
|
|
|
|
msum [ stackCradle dir
|
|
|
|
, cabalCradle dir
|
|
|
|
, sandboxCradle dir
|
|
|
|
, plainCradle dir
|
|
|
|
]
|
2016-01-13 03:49:38 +00:00
|
|
|
where run a = fillTempDir =<< (fromJustNote "findCradle'" <$> runMaybeT a)
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-10-30 18:05:41 +00:00
|
|
|
findSpecCradle :: (GmLog m, IOish m, GmOut m) => FilePath -> m Cradle
|
2015-03-03 20:12:43 +00:00
|
|
|
findSpecCradle dir = do
|
2015-09-14 05:11:45 +00:00
|
|
|
let cfs = [stackCradleSpec, cabalCradle, sandboxCradle]
|
2015-03-03 20:12:43 +00:00
|
|
|
cs <- catMaybes <$> mapM (runMaybeT . ($ dir)) cfs
|
|
|
|
gcs <- filterM isNotGmCradle cs
|
|
|
|
fillTempDir =<< case gcs of
|
|
|
|
[] -> fromJust <$> runMaybeT (plainCradle dir)
|
|
|
|
c:_ -> return c
|
|
|
|
where
|
2015-09-14 03:59:01 +00:00
|
|
|
isNotGmCradle crdl =
|
|
|
|
liftIO $ not <$> doesFileExist (cradleRootDir crdl </> "ghc-mod.cabal")
|
2014-03-30 08:28:57 +00:00
|
|
|
|
2014-10-14 17:52:58 +00:00
|
|
|
cleanupCradle :: Cradle -> IO ()
|
|
|
|
cleanupCradle crdl = removeDirectoryRecursive $ cradleTempDir crdl
|
|
|
|
|
2015-09-01 08:27:12 +00:00
|
|
|
fillTempDir :: IOish m => Cradle -> m Cradle
|
2015-03-03 20:12:43 +00:00
|
|
|
fillTempDir crdl = do
|
|
|
|
tmpDir <- liftIO $ newTempDir (cradleRootDir crdl)
|
|
|
|
return crdl { cradleTempDir = tmpDir }
|
|
|
|
|
2016-05-11 13:10:00 +00:00
|
|
|
cabalCradle :: (IOish m, GmLog m, GmOut m) => FilePath -> MaybeT m Cradle
|
2014-03-30 08:28:57 +00:00
|
|
|
cabalCradle wdir = do
|
2015-09-01 08:27:12 +00:00
|
|
|
cabalFile <- MaybeT $ liftIO $ findCabalFile wdir
|
2014-11-01 21:02:47 +00:00
|
|
|
let cabalDir = takeDirectory cabalFile
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2016-05-11 13:10:00 +00:00
|
|
|
-- If cabal doesn't exist the user probably wants to use something else
|
2016-05-14 18:17:11 +00:00
|
|
|
whenM ((==Nothing) <$> liftIO (findExecutable "cabal")) $ do
|
|
|
|
gmLog GmInfo "" $ text "'dist/setup-config' exists but 'cabal' executable wasn't found"
|
2016-05-11 13:10:00 +00:00
|
|
|
mzero
|
|
|
|
|
2014-03-30 08:28:57 +00:00
|
|
|
return Cradle {
|
2015-09-11 01:48:52 +00:00
|
|
|
cradleProject = CabalProject
|
2015-08-12 07:25:13 +00:00
|
|
|
, cradleCurrentDir = wdir
|
2014-11-01 21:02:47 +00:00
|
|
|
, cradleRootDir = cabalDir
|
2015-03-03 20:12:43 +00:00
|
|
|
, cradleTempDir = error "tmpDir"
|
2014-11-01 21:02:47 +00:00
|
|
|
, cradleCabalFile = Just cabalFile
|
2015-08-18 09:41:14 +00:00
|
|
|
, cradleDistDir = "dist"
|
2013-09-20 06:48:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 18:05:41 +00:00
|
|
|
stackCradle :: (GmLog m, IOish m, GmOut m) => FilePath -> MaybeT m Cradle
|
2015-09-01 08:27:12 +00:00
|
|
|
stackCradle wdir = do
|
2015-11-26 18:21:15 +00:00
|
|
|
#if !MIN_VERSION_ghc(7,8,0)
|
|
|
|
-- GHC < 7.8 is not supported by stack
|
|
|
|
mzero
|
|
|
|
#endif
|
2015-09-01 08:27:12 +00:00
|
|
|
cabalFile <- MaybeT $ liftIO $ findCabalFile wdir
|
2015-08-17 05:41:46 +00:00
|
|
|
|
|
|
|
let cabalDir = takeDirectory cabalFile
|
|
|
|
|
2015-09-01 08:27:12 +00:00
|
|
|
_stackConfigFile <- MaybeT $ liftIO $ findStackConfigFile cabalDir
|
2015-08-20 06:43:36 +00:00
|
|
|
|
|
|
|
-- If dist/setup-config already exists the user probably wants to use cabal
|
|
|
|
-- rather than stack, or maybe that's just me ;)
|
2016-01-08 16:17:21 +00:00
|
|
|
whenM (liftIO $ doesFileExist $ cabalDir </> setupConfigPath "dist") $ do
|
2016-01-09 22:21:59 +00:00
|
|
|
gmLog GmWarning "" $ text "'dist/setup-config' exists, ignoring Stack and using cabal-install instead."
|
|
|
|
mzero
|
|
|
|
|
|
|
|
senv <- MaybeT $ getStackEnv cabalDir
|
2015-08-17 05:41:46 +00:00
|
|
|
|
|
|
|
return Cradle {
|
2015-09-11 01:48:52 +00:00
|
|
|
cradleProject = StackProject senv
|
2015-08-17 05:41:46 +00:00
|
|
|
, cradleCurrentDir = wdir
|
|
|
|
, cradleRootDir = cabalDir
|
|
|
|
, cradleTempDir = error "tmpDir"
|
|
|
|
, cradleCabalFile = Just cabalFile
|
2015-09-11 01:48:52 +00:00
|
|
|
, cradleDistDir = seDistDir senv
|
2015-08-17 05:41:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 18:05:41 +00:00
|
|
|
stackCradleSpec :: (GmLog m, IOish m, GmOut m) => FilePath -> MaybeT m Cradle
|
2015-09-14 05:11:45 +00:00
|
|
|
stackCradleSpec wdir = do
|
|
|
|
crdl <- stackCradle wdir
|
|
|
|
case crdl of
|
|
|
|
Cradle { cradleProject = StackProject StackEnv { seDistDir } } -> do
|
|
|
|
b <- isGmDistDir seDistDir
|
|
|
|
when b mzero
|
|
|
|
return crdl
|
|
|
|
_ -> error "stackCradleSpec"
|
|
|
|
where
|
|
|
|
isGmDistDir dir =
|
|
|
|
liftIO $ not <$> doesFileExist (dir </> ".." </> "ghc-mod.cabal")
|
|
|
|
|
2015-09-01 08:27:12 +00:00
|
|
|
sandboxCradle :: IOish m => FilePath -> MaybeT m Cradle
|
2014-03-30 08:28:57 +00:00
|
|
|
sandboxCradle wdir = do
|
2015-09-01 08:27:12 +00:00
|
|
|
sbDir <- MaybeT $ liftIO $ findCabalSandboxDir wdir
|
2013-09-20 06:48:50 +00:00
|
|
|
return Cradle {
|
2015-09-11 01:48:52 +00:00
|
|
|
cradleProject = SandboxProject
|
2015-08-12 07:25:13 +00:00
|
|
|
, cradleCurrentDir = wdir
|
2014-11-01 21:02:47 +00:00
|
|
|
, cradleRootDir = sbDir
|
2015-03-03 20:12:43 +00:00
|
|
|
, cradleTempDir = error "tmpDir"
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleCabalFile = Nothing
|
2015-08-18 09:41:14 +00:00
|
|
|
, cradleDistDir = "dist"
|
2013-09-20 06:48:50 +00:00
|
|
|
}
|
2013-03-02 03:18:55 +00:00
|
|
|
|
2015-09-01 08:27:12 +00:00
|
|
|
plainCradle :: IOish m => FilePath -> MaybeT m Cradle
|
2014-10-14 17:52:58 +00:00
|
|
|
plainCradle wdir = do
|
2015-03-03 20:12:43 +00:00
|
|
|
return $ Cradle {
|
2015-09-11 01:48:52 +00:00
|
|
|
cradleProject = PlainProject
|
2015-08-12 07:25:13 +00:00
|
|
|
, cradleCurrentDir = wdir
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleRootDir = wdir
|
2015-03-03 20:12:43 +00:00
|
|
|
, cradleTempDir = error "tmpDir"
|
2014-03-30 08:28:57 +00:00
|
|
|
, cradleCabalFile = Nothing
|
2015-08-18 09:41:14 +00:00
|
|
|
, cradleDistDir = "dist"
|
2014-03-30 08:28:57 +00:00
|
|
|
}
|