Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the the cradle config output #62

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/HIE/Bios.hs
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this deleted?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Their only use was from Main.hs and I thought it would be better to encapsulate Cradle selection to a single function. Do you want me to revert it?

, findCradle
, cradleConfig
-- * Compiler Options
, CompilerOptions(..)
, getCompilerOptions
Expand Down
47 changes: 23 additions & 24 deletions src/HIE/Bios/Cradle.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
module HIE.Bios.Cradle (
findCradle
, loadCradle
, loadImplicitCradle
, defaultCradle
loadCradle
, findCradle
, cradleConfig
) where

import System.Process
Expand Down Expand Up @@ -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
Expand Down