Skip to content

Commit

Permalink
Use implicit-hie to generate implicit stack cradles
Browse files Browse the repository at this point in the history
Now all the logic for generating implicit cradles lives in hie-bios.
  • Loading branch information
wz1000 committed Nov 14, 2023
1 parent 6812a71 commit 6d117ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions hie-bios.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Library
exceptions ^>= 0.10,
cryptohash-sha1 >= 0.11.100 && < 0.12,
directory >= 1.3.0 && < 1.4,
implicit-hie >= 0.1.4.0 && < 0.1.5,
filepath >= 1.4.1 && < 1.5,
time >= 1.8.0 && < 1.13,
extra >= 1.6.14 && < 1.8,
Expand Down
33 changes: 24 additions & 9 deletions src/HIE/Bios/Cradle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import qualified Data.Conduit.Combinators as C
import qualified Data.Conduit as C
import qualified Data.Conduit.Text as C
import qualified Data.HashMap.Strict as Map
import Data.Maybe (fromMaybe, maybeToList)
import Data.Maybe (fromMaybe, maybeToList, catMaybes)
import Data.List
import Data.List.Extra (trimEnd)
import Data.Ord (Down(..))
Expand All @@ -68,6 +68,10 @@ import HIE.Bios.Wrappers
import qualified HIE.Bios.Types as Types
import qualified HIE.Bios.Ghc.Gap as Gap

import Hie.Locate
import Hie.Cabal.Parser
import qualified Hie.Yaml as Implicit

import GHC.Fingerprint (fingerprintString)
import GHC.ResponseFile (escapeArgs)

Expand Down Expand Up @@ -318,16 +322,27 @@ inferCradleTree fp =
maybeItsBios
-- If we have both a config file (cabal.project/stack.yaml) and a work dir
-- (dist-newstyle/.stack-work), prefer that
<|> (cabalExecutable >> cabalConfigDir fp >>= \dir -> cabalWorkDir dir >>= pure $ cabalCradle dir)
<|> (stackExecutable >> stackConfigDir fp >>= \dir -> stackWorkDir dir >>= pure $ stackCradle dir)
<|> (cabalExecutable >> cabalConfigDir fp >>= \dir -> cabalWorkDir dir >> pure (cabalCradle dir))
<|> (stackExecutable >> stackConfigDir fp >>= \dir -> stackWorkDir dir >> stackCradle dir)
-- Redo the checks, but don't check for the work-dir, maybe the user hasn't run a build yet
<|> (cabalExecutable >> cabalConfigDir fp >>= pure . cabalCradle dir)
<|> (stackExecutable >> stackConfigDir fp >>= pure . stackCradle dir)
<|> (cabalExecutable >> cabalConfigDir fp >>= pure . cabalCradle)
<|> (stackExecutable >> stackConfigDir fp >>= stackCradle)

where
maybeItsBios = (\wdir -> (Bios (Program $ wdir </> ".hie-bios") Nothing Nothing, wdir)) <$> biosWorkDir fp

stackCradle fp = (Stack $ StackType Nothing Nothing, fp)
stackCradle :: FilePath -> MaybeT IO (CradleTree a, FilePath)
stackCradle fp = do
pkgs <- stackYamlPkgs fp
pkgsWithComps <- liftIO $ catMaybes <$> mapM (nestedPkg fp) pkgs
let yaml = fp </> "stack.yaml"
pure $ (,fp) $ case pkgsWithComps of
[] -> Stack (StackType Nothing (Just yaml))
ps -> StackMulti mempty $ do
Package n cs <- ps
c <- cs
let (prefix, comp) = Implicit.stackComponent n c
pure (prefix, StackType (Just comp) (Just yaml))
cabalCradle fp = (Cabal $ CabalType Nothing Nothing, fp)

-- | Wraps up the cradle inferred by @inferCradleTree@ as a @CradleConfig@ with no dependencies
Expand Down Expand Up @@ -896,7 +911,7 @@ cabalConfigDir wdir = findFileUpwards (== "cabal.project") wdir

cabalWorkDir :: FilePath -> MaybeT IO ()
cabalWorkDir wdir = do
check <- doesDirectoryExist (wdir </> "dist-newstyle")
check <- liftIO $ doesDirectoryExist (wdir </> "dist-newstyle")
unless check $ fail "No dist-newstyle"

cabalExecutable :: MaybeT IO FilePath
Expand Down Expand Up @@ -1020,13 +1035,13 @@ stackExecutable :: MaybeT IO FilePath
stackExecutable = MaybeT $ findExecutable "stack"

stackConfigDir :: FilePath -> MaybeT IO FilePath
stackConfigDir = findFileUpwards isStack odir
stackConfigDir = findFileUpwards isStack
where
isStack name = name == "stack.yaml"

stackWorkDir :: FilePath -> MaybeT IO ()
stackWorkDir wdir = do
check <- doesDirectoryExist (wdir </> ".stack-work")
check <- liftIO $ doesDirectoryExist (wdir </> ".stack-work")
unless check $ fail "No .stack-work"

{-
Expand Down

0 comments on commit 6d117ca

Please sign in to comment.