diff --git a/exe/Main.hs b/exe/Main.hs index db5ec4d3a..76505b5b3 100644 --- a/exe/Main.hs +++ b/exe/Main.hs @@ -56,10 +56,8 @@ main = flip E.catches handlers $ do hSetEncoding stdout utf8 args <- getArgs cradle <- getCurrentDirectory >>= \cwd -> - -- find cradle does a takeDirectory on the argument, so make it into a file - findCradle (cwd "File.hs") >>= \case - Just yaml -> loadCradle yaml - Nothing -> loadImplicitCradle (cwd "File.hs") + -- loadCradle does a takeDirectory on the argument, so make it into a file + loadCradle (cwd "File.hs") let cmdArg0 = args !. 0 remainingArgs = tail args opt = defaultOptions diff --git a/src/HIE/Bios.hs b/src/HIE/Bios.hs index e03c64b63..a056a77a3 100644 --- a/src/HIE/Bios.hs +++ b/src/HIE/Bios.hs @@ -1,12 +1,11 @@ -- | The HIE Bios module HIE.Bios ( - -- * Find and load a Cradle + -- * Find and load a Cradle and its configuration Cradle(..) - , findCradle , loadCradle - , loadImplicitCradle - , defaultCradle + , findCradle + , cradleConfig -- * Compiler Options , CompilerOptions(..) , getCompilerOptions diff --git a/src/HIE/Bios/Cradle.hs b/src/HIE/Bios/Cradle.hs index 7f7791c98..b87c67699 100644 --- a/src/HIE/Bios/Cradle.hs +++ b/src/HIE/Bios/Cradle.hs @@ -1,10 +1,9 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TupleSections #-} module HIE.Bios.Cradle ( - findCradle - , loadCradle - , loadImplicitCradle - , defaultCradle + loadCradle + , findCradle + , cradleConfig ) where import System.Process @@ -32,27 +31,27 @@ findCradle wfile = do let wdir = takeDirectory wfile runMaybeT (yamlConfig wdir) --- | Given root/hie.yaml load the Cradle +-- | Given root/foo/bar.hs return Cradle loadCradle :: FilePath -> IO Cradle -loadCradle = loadCradleWithOpts defaultCradleOpts - --- | Given root/foo/bar.hs, load an implicit cradle -loadImplicitCradle :: FilePath -> IO Cradle -loadImplicitCradle wfile = do - let wdir = takeDirectory wfile - cfg <- runMaybeT (implicitConfig wdir) - return $ case cfg of - Just bc -> getCradle bc - Nothing -> defaultCradle wdir [] - --- | Finding 'Cradle'. --- Find a cabal file by tracing ancestor directories. --- Find a sandbox according to a cabal sandbox config --- in a cabal directory. -loadCradleWithOpts :: CradleOpts -> FilePath -> IO Cradle -loadCradleWithOpts _copts wfile = do - cradleConfig <- readCradleConfig wfile - return $ getCradle (cradleConfig, takeDirectory wfile) +loadCradle wfile = do + mConfig <- cradleConfig wfile + return $ maybe defCradle getCradle mConfig + where + defCradle = defaultCradle (takeDirectory wfile) [] + +-- | Given root/foo/bar.hs return the Cradle configuration and yaml directory +cradleConfig :: FilePath -> IO (Maybe (CradleConfig, FilePath)) +cradleConfig wfile = do + mConfigPath <- findCradle wfile + + rConf <- sequence $ coupleConfAndConfPath <$> mConfigPath + iConf <- runMaybeT $ implicitConfig wdir + + pure $ rConf <|> iConf + where + wdir = takeDirectory wfile + coupleConfAndConfPath confPath = (,takeDirectory confPath) + <$> readCradleConfig confPath getCradle :: (CradleConfig, FilePath) -> Cradle getCradle (cc, wdir) = case cradleType cc of