From 504d642ae2ba56c9243bc231cc4bee973b7caf5e Mon Sep 17 00:00:00 2001 From: Mistral Contrastin Date: Thu, 26 Sep 2019 01:41:46 +0100 Subject: [PATCH 1/4] Expose the determined cradle config This restructures the code so that the selected configuration (whether a yaml-based cradle or an implicit one) is exposed in the API. As a result of the rearrangement there is no longer any need to separately expose the implicit, default, and yaml cradle finding functions. --- exe/Main.hs | 6 ++--- src/HIE/Bios.hs | 4 +--- src/HIE/Bios/Cradle.hs | 51 +++++++++++++++++------------------------- 3 files changed, 24 insertions(+), 37 deletions(-) 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..ea0fe71a1 100644 --- a/src/HIE/Bios.hs +++ b/src/HIE/Bios.hs @@ -3,10 +3,8 @@ module HIE.Bios ( -- * Find and load a Cradle Cradle(..) - , findCradle + , cradleConfig , loadCradle - , loadImplicitCradle - , defaultCradle -- * Compiler Options , CompilerOptions(..) , getCompilerOptions diff --git a/src/HIE/Bios/Cradle.hs b/src/HIE/Bios/Cradle.hs index 7f7791c98..1dad6b9e4 100644 --- a/src/HIE/Bios/Cradle.hs +++ b/src/HIE/Bios/Cradle.hs @@ -1,10 +1,8 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TupleSections #-} module HIE.Bios.Cradle ( - findCradle - , loadCradle - , loadImplicitCradle - , defaultCradle + loadCradle + , cradleConfig ) where import System.Process @@ -26,33 +24,26 @@ import System.PosixCompat.Files ---------------------------------------------------------------- --- | Given root/foo/bar.hs, return root/hie.yaml, or wherever the yaml file was found -findCradle :: FilePath -> IO (Maybe FilePath) -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 (takeDirectory wfile) + return $ maybe defCradle getCradle mConfig + where + defCradle = defaultCradle (takeDirectory wfile) [] + +-- | Given root/hie-bios.yaml return the Cradle configuration and yaml directory +cradleConfig :: FilePath -> IO (Maybe (CradleConfig, FilePath)) +cradleConfig wdir = do + mConfigPath <- runMaybeT $ yamlConfig wdir + + rConf <- sequence $ coupleConfAndConfPath <$> mConfigPath + iConf <- runMaybeT $ implicitConfig wdir + + pure $ rConf <|> iConf + where + coupleConfAndConfPath confPath = (,takeDirectory confPath) + <$> readCradleConfig confPath getCradle :: (CradleConfig, FilePath) -> Cradle getCradle (cc, wdir) = case cradleType cc of From 186844e8cd8af8deb1809dbadc5336e34e9d1384 Mon Sep 17 00:00:00 2001 From: Mistral Contrastin Date: Thu, 26 Sep 2019 10:35:25 +0100 Subject: [PATCH 2/4] Restore `findCradle` API --- src/HIE/Bios.hs | 1 + src/HIE/Bios/Cradle.hs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/HIE/Bios.hs b/src/HIE/Bios.hs index ea0fe71a1..e5a5ca63d 100644 --- a/src/HIE/Bios.hs +++ b/src/HIE/Bios.hs @@ -5,6 +5,7 @@ module HIE.Bios ( Cradle(..) , cradleConfig , loadCradle + , findCradle -- * Compiler Options , CompilerOptions(..) , getCompilerOptions diff --git a/src/HIE/Bios/Cradle.hs b/src/HIE/Bios/Cradle.hs index 1dad6b9e4..6df2e321e 100644 --- a/src/HIE/Bios/Cradle.hs +++ b/src/HIE/Bios/Cradle.hs @@ -2,6 +2,7 @@ {-# LANGUAGE TupleSections #-} module HIE.Bios.Cradle ( loadCradle + , findCradle , cradleConfig ) where @@ -24,6 +25,12 @@ import System.PosixCompat.Files ---------------------------------------------------------------- +-- | Given root/foo/bar.hs, return root/hie.yaml, or wherever the yaml file was found +findCradle :: FilePath -> IO (Maybe FilePath) +findCradle wfile = do + let wdir = takeDirectory wfile + runMaybeT (yamlConfig wdir) + -- | Given root/foo/bar.hs return Cradle loadCradle :: FilePath -> IO Cradle loadCradle wfile = do From f465410437f217cb2c033eeb63cc6c7bd84cc1ec Mon Sep 17 00:00:00 2001 From: Mistral Contrastin Date: Thu, 26 Sep 2019 10:37:26 +0100 Subject: [PATCH 3/4] Tidy exported API order and documentation --- src/HIE/Bios.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HIE/Bios.hs b/src/HIE/Bios.hs index e5a5ca63d..a056a77a3 100644 --- a/src/HIE/Bios.hs +++ b/src/HIE/Bios.hs @@ -1,11 +1,11 @@ -- | The HIE Bios module HIE.Bios ( - -- * Find and load a Cradle + -- * Find and load a Cradle and its configuration Cradle(..) - , cradleConfig , loadCradle , findCradle + , cradleConfig -- * Compiler Options , CompilerOptions(..) , getCompilerOptions From dcaac035acd5d682fbe5d1d95ee8ff8092ce80e0 Mon Sep 17 00:00:00 2001 From: Mistral Contrastin Date: Thu, 26 Sep 2019 12:22:06 +0100 Subject: [PATCH 4/4] `cradleConfig` should take a file not a directory `cradleConfig` now uses `findCradle` to get the configuration path. --- src/HIE/Bios/Cradle.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/HIE/Bios/Cradle.hs b/src/HIE/Bios/Cradle.hs index 6df2e321e..b87c67699 100644 --- a/src/HIE/Bios/Cradle.hs +++ b/src/HIE/Bios/Cradle.hs @@ -34,21 +34,22 @@ findCradle wfile = do -- | Given root/foo/bar.hs return Cradle loadCradle :: FilePath -> IO Cradle loadCradle wfile = do - mConfig <- cradleConfig (takeDirectory wfile) + mConfig <- cradleConfig wfile return $ maybe defCradle getCradle mConfig where defCradle = defaultCradle (takeDirectory wfile) [] --- | Given root/hie-bios.yaml return the Cradle configuration and yaml directory +-- | Given root/foo/bar.hs return the Cradle configuration and yaml directory cradleConfig :: FilePath -> IO (Maybe (CradleConfig, FilePath)) -cradleConfig wdir = do - mConfigPath <- runMaybeT $ yamlConfig wdir +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