Cleanup project type handling

This commit is contained in:
Daniel Gröber 2015-08-12 09:25:13 +02:00
parent a94d8977a9
commit 28f06e035d
7 changed files with 33 additions and 36 deletions

View File

@ -3,6 +3,7 @@
module Language.Haskell.GhcMod ( module Language.Haskell.GhcMod (
-- * Cradle -- * Cradle
Cradle(..) Cradle(..)
, ProjectType(..)
, findCradle , findCradle
-- * Options -- * Options
, Options(..) , Options(..)

View File

@ -29,7 +29,7 @@ findCradle = findCradle' =<< getCurrentDirectory
findCradle' :: FilePath -> IO Cradle findCradle' :: FilePath -> IO Cradle
findCradle' dir = run $ do findCradle' dir = run $ do
(customCradle dir `mplus` cabalCradle dir `mplus` sandboxCradle dir `mplus` plainCradle dir) (cabalCradle dir `mplus` sandboxCradle dir `mplus` plainCradle dir)
where run a = fillTempDir =<< (fromJust <$> runMaybeT a) where run a = fillTempDir =<< (fromJust <$> runMaybeT a)
findSpecCradle :: FilePath -> IO Cradle findSpecCradle :: FilePath -> IO Cradle
@ -53,17 +53,6 @@ fillTempDir crdl = do
tmpDir <- liftIO $ newTempDir (cradleRootDir crdl) tmpDir <- liftIO $ newTempDir (cradleRootDir crdl)
return crdl { cradleTempDir = tmpDir } return crdl { cradleTempDir = tmpDir }
customCradle :: FilePath -> MaybeT IO Cradle
customCradle wdir = do
cabalFile <- MaybeT $ findCabalFile wdir
let cabalDir = takeDirectory cabalFile
return Cradle {
cradleCurrentDir = wdir
, cradleRootDir = cabalDir
, cradleTempDir = error "tmpDir"
, cradleCabalFile = Just cabalFile
}
cabalCradle :: FilePath -> MaybeT IO Cradle cabalCradle :: FilePath -> MaybeT IO Cradle
cabalCradle wdir = do cabalCradle wdir = do
cabalFile <- MaybeT $ findCabalFile wdir cabalFile <- MaybeT $ findCabalFile wdir
@ -71,7 +60,8 @@ cabalCradle wdir = do
let cabalDir = takeDirectory cabalFile let cabalDir = takeDirectory cabalFile
return Cradle { return Cradle {
cradleCurrentDir = wdir cradleProjectType = CabalProject
, cradleCurrentDir = wdir
, cradleRootDir = cabalDir , cradleRootDir = cabalDir
, cradleTempDir = error "tmpDir" , cradleTempDir = error "tmpDir"
, cradleCabalFile = Just cabalFile , cradleCabalFile = Just cabalFile
@ -81,7 +71,8 @@ sandboxCradle :: FilePath -> MaybeT IO Cradle
sandboxCradle wdir = do sandboxCradle wdir = do
sbDir <- MaybeT $ findCabalSandboxDir wdir sbDir <- MaybeT $ findCabalSandboxDir wdir
return Cradle { return Cradle {
cradleCurrentDir = wdir cradleProjectType = SandboxProject
, cradleCurrentDir = wdir
, cradleRootDir = sbDir , cradleRootDir = sbDir
, cradleTempDir = error "tmpDir" , cradleTempDir = error "tmpDir"
, cradleCabalFile = Nothing , cradleCabalFile = Nothing
@ -90,7 +81,8 @@ sandboxCradle wdir = do
plainCradle :: FilePath -> MaybeT IO Cradle plainCradle :: FilePath -> MaybeT IO Cradle
plainCradle wdir = do plainCradle wdir = do
return $ Cradle { return $ Cradle {
cradleCurrentDir = wdir cradleProjectType = PlainProject
, cradleCurrentDir = wdir
, cradleRootDir = wdir , cradleRootDir = wdir
, cradleTempDir = error "tmpDir" , cradleTempDir = error "tmpDir"
, cradleCabalFile = Nothing , cradleCabalFile = Nothing

View File

@ -26,9 +26,9 @@ debugInfo = do
Cradle {..} <- cradle Cradle {..} <- cradle
cabal <- cabal <-
case cradleCabalFile of case cradleProjectType of
Just _ -> cabalDebug CabalProject -> cabalDebug
Nothing -> return [] _ -> return []
pkgOpts <- packageGhcOptions pkgOpts <- packageGhcOptions

View File

@ -61,14 +61,14 @@ ghcDbOpt (PackageDb pkgDb)
getPackageCachePaths :: IOish m => FilePath -> GhcModT m [FilePath] getPackageCachePaths :: IOish m => FilePath -> GhcModT m [FilePath]
getPackageCachePaths sysPkgCfg = do getPackageCachePaths sysPkgCfg = do
crdl <- cradle crdl <- cradle
pkgDbStack <- if isJust $ cradleCabalFile crdl pkgDbStack <- case cradleProjectType crdl of
then do PlainProject ->
getPackageDbStack return [GlobalDb, UserDb]
else do SandboxProject -> do
mdb <- liftIO $ getSandboxDb $ cradleRootDir crdl Just db <- liftIO $ getSandboxDb $ cradleRootDir crdl
return $ case mdb of return $ [GlobalDb, db]
Just db -> [db] CabalProject ->
Nothing -> [GlobalDb, UserDb] getPackageDbStack
catMaybes <$> (liftIO . resolvePackageConfig sysPkgCfg) `mapM` pkgDbStack catMaybes <$> (liftIO . resolvePackageConfig sysPkgCfg) `mapM` pkgDbStack

View File

@ -176,9 +176,9 @@ targetGhcOptions :: forall m. IOish m
targetGhcOptions crdl sefnmn = do targetGhcOptions crdl sefnmn = do
when (Set.null sefnmn) $ error "targetGhcOptions: no targets given" when (Set.null sefnmn) $ error "targetGhcOptions: no targets given"
case cradleCabalFile crdl of case cradleProjectType crdl of
Just _ -> cabalOpts crdl CabalProject -> cabalOpts crdl
Nothing -> sandboxOpts crdl _ -> sandboxOpts crdl
where where
zipMap f l = l `zip` (f `map` l) zipMap f l = l `zip` (f `map` l)
@ -288,10 +288,11 @@ packageGhcOptions :: (Applicative m, IOish m, GmEnv m, GmState m, GmLog m)
=> m [GHCOption] => m [GHCOption]
packageGhcOptions = do packageGhcOptions = do
crdl <- cradle crdl <- cradle
case cradleCabalFile crdl of case cradleProjectType crdl of
Just _ -> getGhcMergedPkgOptions CabalProject -> getGhcMergedPkgOptions
Nothing -> sandboxOpts crdl _ -> sandboxOpts crdl
-- also works for plain projects!
sandboxOpts :: MonadIO m => Cradle -> m [String] sandboxOpts :: MonadIO m => Cradle -> m [String]
sandboxOpts crdl = do sandboxOpts crdl = do
pkgDbStack <- liftIO $ getSandboxPackageDbStack $ cradleRootDir crdl pkgDbStack <- liftIO $ getSandboxPackageDbStack $ cradleRootDir crdl

View File

@ -109,10 +109,14 @@ defaultOptions = Options {
---------------------------------------------------------------- ----------------------------------------------------------------
data ProjectType = CabalProject | SandboxProject | PlainProject
deriving (Eq, Show)
-- | The environment where this library is used. -- | The environment where this library is used.
data Cradle = Cradle { data Cradle = Cradle {
cradleProjectType:: ProjectType
-- | The directory where this library is executed. -- | The directory where this library is executed.
cradleCurrentDir :: FilePath , cradleCurrentDir :: FilePath
-- | The project root directory. -- | The project root directory.
, cradleRootDir :: FilePath , cradleRootDir :: FilePath
-- | Per-Project temporary directory -- | Per-Project temporary directory

View File

@ -589,10 +589,9 @@ nukeCaches = do
chdir <- liftIO $ (</> "cabal-helper") <$> getAppUserDataDirectory "ghc-mod" chdir <- liftIO $ (</> "cabal-helper") <$> getAppUserDataDirectory "ghc-mod"
c <- cradle c <- cradle
when (isJust $ cradleCabalFile c) $ do when (cradleProjectType c == CabalProject) $ do
let root = cradleRootDir c let root = cradleRootDir c
when (isJust $ cradleCabalFile c) $ liftIO $ (trySome . removeDirectoryRecursive) `mapM_` [chdir, root </> "dist"]
liftIO $ (trySome . removeDirectoryRecursive) `mapM_` [chdir, root </> "dist"]
trySome :: IO a -> IO (Either SomeException a) trySome :: IO a -> IO (Either SomeException a)
trySome = try trySome = try