@@ -7,10 +7,9 @@ module Ide.Cradle where
77
88import Control.Exception
99import Control.Monad.IO.Class
10- import Data.Char (toLower )
1110import Data.Foldable (toList )
1211import Data.Function ((&) )
13- import Data.List (isPrefixOf , isInfixOf , sortOn , find )
12+ import Data.List (isPrefixOf , sortOn , find )
1413import Data.List.NonEmpty (NonEmpty )
1514import qualified Data.List.NonEmpty as NonEmpty
1615import qualified Data.Map as Map
@@ -25,14 +24,17 @@ import Distribution.Helper (Package, projectPackages, pUnits,
2524 Unit , unitInfo , uiComponents ,
2625 ChEntrypoint (.. ), UnitInfo (.. ))
2726import Distribution.Helper.Discover (findProjects , getDefaultDistDir )
28- import HIE.Bios as BIOS
29- import HIE.Bios.Types as BIOS
27+ import HIE.Bios as Bios
28+ import qualified HIE.Bios.Cradle as Bios
29+ import HIE.Bios.Types (CradleAction (.. ))
30+ import qualified HIE.Bios.Types as Bios
3031import System.Directory (getCurrentDirectory , canonicalizePath , findExecutable )
3132import System.Exit
3233import System.FilePath
3334import System.Log.Logger
3435import System.Process (readCreateProcessWithExitCode , shell )
3536
37+
3638-- ---------------------------------------------------------------------
3739
3840-- | Find the cradle that the given File belongs to.
@@ -45,44 +47,49 @@ import System.Process (readCreateProcessWithExitCode, shell)
4547-- If no "hie.yaml" can be found, the implicit config is used.
4648-- The implicit config uses different heuristics to determine the type
4749-- of the project that may or may not be accurate.
48- findLocalCradle :: FilePath -> IO Cradle
50+ findLocalCradle :: FilePath -> IO ( Cradle CabalHelper )
4951findLocalCradle fp = do
50- cradleConf <- BIOS . findCradle fp
51- crdl <- case cradleConf of
52+ cradleConf <- Bios . findCradle fp
53+ crdl <- case cradleConf of
5254 Just yaml -> do
5355 debugm $ " Found \" " ++ yaml ++ " \" for \" " ++ fp ++ " \" "
54- BIOS. loadCradle yaml
55- Nothing -> cabalHelperCradle fp
56+ crdl <- Bios. loadCradle yaml
57+ return $ fmap (const CabalNone ) crdl
58+ Nothing -> cabalHelperCradle fp
5659 logm $ " Module \" " ++ fp ++ " \" is loaded by Cradle: " ++ show crdl
5760 return crdl
5861
5962-- | Check if the given cradle is a stack cradle.
6063-- This might be used to determine the GHC version to use on the project.
6164-- If it is a stack-cradle, we have to use @"stack path --compiler-exe"@
6265-- otherwise we may ask `ghc` directly what version it is.
63- isStackCradle :: Cradle -> Bool
64- isStackCradle = (`elem` [" stack" , " Cabal-Helper-Stack" , " Cabal-Helper-Stack-None" ])
65- . BIOS. actionName
66- . BIOS. cradleOptsProg
66+ isStackCradle :: Cradle CabalHelper -> Bool
67+ isStackCradle crdl = Bios. isStackCradle crdl || cabalHelperStackCradle crdl
68+ where
69+ cabalHelperStackCradle =
70+ (`elem` [Bios. Other Stack , Bios. Other StackNone ])
71+ . Bios. actionName
72+ . Bios. cradleOptsProg
73+
6774
6875-- | Check if the given cradle is a cabal cradle.
6976-- This might be used to determine the GHC version to use on the project.
7077-- If it is a stack-cradle, we have to use @"stack path --compiler-exe"@
7178-- otherwise we may ask @ghc@ directly what version it is.
72- isCabalCradle :: Cradle -> Bool
73- isCabalCradle =
74- ( `elem`
75- [ " cabal "
76- , " Cabal-Helper-Cabal-V1 "
77- , " Cabal-Helper-Cabal-V2 "
78- , " Cabal-Helper-Cabal-V1-Dir "
79- , " Cabal-Helper-Cabal-V2-Dir "
80- , " Cabal-Helper-Cabal-V2-None "
81- , " Cabal-Helper-Cabal-None "
82- ]
83- )
84- . BIOS. actionName
85- . BIOS. cradleOptsProg
79+ isCabalCradle :: Cradle CabalHelper -> Bool
80+ isCabalCradle crdl = Bios. isCabalCradle crdl || cabalHelperCabalCradle crdl
81+ where
82+ cabalHelperCabalCradle =
83+ ( `elem` [ Bios. Other CabalV2 , Bios. Other CabalNone ])
84+ . Bios. actionName
85+ . Bios. cradleOptsProg
86+
87+ data CabalHelper
88+ = Stack
89+ | StackNone
90+ | CabalV2
91+ | CabalNone
92+ deriving ( Show , Eq , Ord )
8693
8794-- | Execute @ghc@ that is based on the given cradle.
8895-- Output must be a single line. If an error is raised, e.g. the command
@@ -91,7 +98,7 @@ isCabalCradle =
9198--
9299-- E.g. for a stack cradle, we use @stack ghc@ and for a cabal cradle
93100-- we are taking the @ghc@ that is on the path.
94- execProjectGhc :: Cradle -> [String ] -> IO (Maybe String )
101+ execProjectGhc :: Cradle CabalHelper -> [String ] -> IO (Maybe String )
95102execProjectGhc crdl args = do
96103 isStackInstalled <- isJust <$> findExecutable " stack"
97104 -- isCabalInstalled <- isJust <$> findExecutable "cabal"
@@ -147,7 +154,7 @@ tryCommand cmd = do
147154
148155
149156-- | Get the directory of the libdir based on the project ghc.
150- getProjectGhcLibDir :: Cradle -> IO (Maybe FilePath )
157+ getProjectGhcLibDir :: Cradle CabalHelper -> IO (Maybe FilePath )
151158getProjectGhcLibDir crdl =
152159 execProjectGhc crdl [" --print-libdir" ] >>= \ case
153160 Nothing -> do
@@ -444,7 +451,7 @@ the compiler options obtained from Cabal-Helper are relative to the package
444451source directory, which is "\/Repo\/SubRepo".
445452
446453-}
447- cabalHelperCradle :: FilePath -> IO Cradle
454+ cabalHelperCradle :: FilePath -> IO ( Cradle CabalHelper )
448455cabalHelperCradle file = do
449456 projM <- findCabalHelperEntryPoint file
450457 case projM of
@@ -454,7 +461,7 @@ cabalHelperCradle file = do
454461 return
455462 Cradle { cradleRootDir = cwd
456463 , cradleOptsProg =
457- CradleAction { actionName = " Direct"
464+ CradleAction { actionName = Bios. Direct
458465 , runCradle = \ _ _ ->
459466 return
460467 $ CradleSuccess
@@ -470,7 +477,7 @@ cabalHelperCradle file = do
470477 let root = projectRootDir proj
471478 -- Create a suffix for the cradle name.
472479 -- Purpose is mainly for easier debugging.
473- let actionNameSuffix = projectSuffix proj
480+ let actionNameSuffix = projectType proj
474481 debugm $ " Cabal-Helper dirs: " ++ show [root, file]
475482 let dist_dir = getDefaultDistDir proj
476483 env <- mkQueryEnv proj dist_dir
@@ -487,9 +494,7 @@ cabalHelperCradle file = do
487494 return
488495 Cradle { cradleRootDir = root
489496 , cradleOptsProg =
490- CradleAction { actionName = " Cabal-Helper-"
491- ++ actionNameSuffix
492- ++ " -None"
497+ CradleAction { actionName = Bios. Other (projectNoneType proj)
493498 , runCradle = \ _ _ -> return CradleNone
494499 }
495500 }
@@ -504,8 +509,7 @@ cabalHelperCradle file = do
504509 return
505510 Cradle { cradleRootDir = normalisedPackageLocation
506511 , cradleOptsProg =
507- CradleAction { actionName =
508- " Cabal-Helper-" ++ actionNameSuffix
512+ CradleAction { actionName = Bios. Other actionNameSuffix
509513 , runCradle = \ _ fp -> cabalHelperAction
510514 (Ex proj)
511515 env
@@ -754,12 +758,19 @@ projectRootDir ProjLocV2File { plProjectDirV2 } = plProjectDirV2
754758projectRootDir ProjLocV2Dir { plProjectDirV2 } = plProjectDirV2
755759projectRootDir ProjLocStackYaml { plStackYaml } = takeDirectory plStackYaml
756760
757- projectSuffix :: ProjLoc qt -> FilePath
758- projectSuffix ProjLocV1CabalFile {} = " Cabal-V1"
759- projectSuffix ProjLocV1Dir {} = " Cabal-V1-Dir"
760- projectSuffix ProjLocV2File {} = " Cabal-V2"
761- projectSuffix ProjLocV2Dir {} = " Cabal-V2-Dir"
762- projectSuffix ProjLocStackYaml {} = " Stack"
761+ projectType :: ProjLoc qt -> CabalHelper
762+ projectType ProjLocV1CabalFile {} = CabalV2
763+ projectType ProjLocV1Dir {} = CabalV2
764+ projectType ProjLocV2File {} = CabalV2
765+ projectType ProjLocV2Dir {} = CabalV2
766+ projectType ProjLocStackYaml {} = Stack
767+
768+ projectNoneType :: ProjLoc qt -> CabalHelper
769+ projectNoneType ProjLocV1CabalFile {} = CabalNone
770+ projectNoneType ProjLocV1Dir {} = CabalNone
771+ projectNoneType ProjLocV2File {} = CabalNone
772+ projectNoneType ProjLocV2Dir {} = CabalNone
773+ projectNoneType ProjLocStackYaml {} = StackNone
763774
764775-- ----------------------------------------------------------------------------
765776--
@@ -870,18 +881,25 @@ relativeTo file sourceDirs =
870881
871882-- | Returns a user facing display name for the cradle type,
872883-- e.g. "Stack project" or "GHC session"
873- cradleDisplay :: IsString a => BIOS. Cradle -> a
884+ cradleDisplay :: IsString a => Cradle CabalHelper -> a
874885cradleDisplay cradle = fromString result
875- where
876- result
877- | " stack" `isInfixOf` name = " Stack project"
878- | " cabal-v1" `isInfixOf` name = " Cabal (V1) project"
879- | " cabal" `isInfixOf` name = " Cabal project"
880- | " direct" `isInfixOf` name = " GHC session"
881- | " multi" `isInfixOf` name = " Multi Component project"
882- | otherwise = " project"
883- name = map toLower $ BIOS. actionName (BIOS. cradleOptsProg cradle)
884-
886+ where
887+ result
888+ | Bios. isStackCradle cradle
889+ || name
890+ `elem` [Bios. Other Stack , Bios. Other StackNone ]
891+ = " Stack project"
892+ | Bios. isCabalCradle cradle
893+ || name
894+ `elem` [Bios. Other CabalV2 , Bios. Other CabalNone ]
895+ = " Cabal project"
896+ | Bios. isDirectCradle cradle
897+ = " GHC session"
898+ | Bios. isMultiCradle cradle
899+ = " Multi Component project"
900+ | otherwise
901+ = " project"
902+ name = Bios. actionName (Bios. cradleOptsProg cradle)
885903
886904-- ---------------------------------------------------------------------
887905
0 commit comments