5
5
6
6
module Haskell.Ide.Engine.Cradle where
7
7
8
- import HIE.Bios as BIOS
9
- import HIE.Bios.Types as BIOS
10
- import Haskell.Ide.Engine.MonadFunctions
8
+ import HIE.Bios as Bios
9
+ import qualified HIE.Bios.Cradle as Bios
10
+ import HIE.Bios.Types (Cradle (.. ), CradleAction (.. ))
11
+ import qualified HIE.Bios.Types as Bios
11
12
import Distribution.Helper (Package , projectPackages , pUnits ,
12
13
pSourceDir , ChComponentInfo (.. ),
13
14
unChModuleName , Ex (.. ), ProjLoc (.. ),
14
15
QueryEnv , mkQueryEnv , runQuery ,
15
16
Unit , unitInfo , uiComponents ,
16
17
ChEntrypoint (.. ), UnitInfo (.. ))
17
18
import Distribution.Helper.Discover (findProjects , getDefaultDistDir )
18
- import Data.Char (toLower )
19
19
import Data.Function ((&) )
20
- import Data.List (isPrefixOf , isInfixOf , sortOn , find )
20
+ import Data.List (isPrefixOf , sortOn , find )
21
21
import qualified Data.List.NonEmpty as NonEmpty
22
22
import Data.List.NonEmpty (NonEmpty )
23
23
import qualified Data.Map as Map
@@ -32,6 +32,8 @@ import System.Directory (getCurrentDirectory, canonicalizePath, findEx
32
32
import System.Exit
33
33
import System.Process (readCreateProcessWithExitCode , shell )
34
34
35
+ import Haskell.Ide.Engine.Logger
36
+
35
37
-- | Find the cradle that the given File belongs to.
36
38
--
37
39
-- First looks for a "hie.yaml" file in the directory of the file
@@ -42,44 +44,67 @@ import System.Process (readCreateProcessWithExitCode, shell)
42
44
-- If no "hie.yaml" can be found, the implicit config is used.
43
45
-- The implicit config uses different heuristics to determine the type
44
46
-- of the project that may or may not be accurate.
45
- findLocalCradle :: FilePath -> IO Cradle
47
+ findLocalCradle :: FilePath -> IO ( Cradle CabalHelper )
46
48
findLocalCradle fp = do
47
- cradleConf <- BIOS . findCradle fp
48
- crdl <- case cradleConf of
49
+ cradleConf <- Bios . findCradle fp
50
+ crdl <- case cradleConf of
49
51
Just yaml -> do
50
52
debugm $ " Found \" " ++ yaml ++ " \" for \" " ++ fp ++ " \" "
51
- BIOS. loadCradle yaml
52
- Nothing -> cabalHelperCradle fp
53
+ crdl <- Bios. loadCradle yaml
54
+ return $ relaxCradle crdl
55
+ Nothing -> cabalHelperCradle fp
53
56
logm $ " Module \" " ++ fp ++ " \" is loaded by Cradle: " ++ show crdl
54
57
return crdl
58
+ where
59
+ relaxCradle :: Cradle a -> Cradle CabalHelper
60
+ relaxCradle crdl =
61
+ let newActionName = case actionName $ cradleOptsProg crdl of
62
+ Bios. Other _ -> Bios. Other CabalNone
63
+ Bios. Stack -> Bios. Stack
64
+ Bios. Cabal -> Bios. Cabal
65
+ Bios. Direct -> Bios. Direct
66
+ Bios. None -> Bios. None
67
+ Bios. Multi -> Bios. Multi
68
+ Bios. Default -> Bios. Default
69
+ Bios. Bios -> Bios. Bios
70
+ in crdl
71
+ { cradleOptsProg = (cradleOptsProg crdl) { actionName = newActionName
72
+ }
73
+ }
55
74
56
75
-- | Check if the given cradle is a stack cradle.
57
76
-- This might be used to determine the GHC version to use on the project.
58
77
-- If it is a stack-cradle, we have to use @"stack path --compiler-exe"@
59
78
-- otherwise we may ask `ghc` directly what version it is.
60
- isStackCradle :: Cradle -> Bool
61
- isStackCradle = (`elem` [" stack" , " Cabal-Helper-Stack" , " Cabal-Helper-Stack-None" ])
62
- . BIOS. actionName
63
- . BIOS. cradleOptsProg
64
-
79
+ isStackCradle :: Cradle CabalHelper -> Bool
80
+ isStackCradle =
81
+ ( `elem`
82
+ [ Bios. Other Stack
83
+ , Bios. Other StackNone
84
+ ]
85
+ )
86
+ . Bios. actionName
87
+ . Bios. cradleOptsProg
65
88
-- | Check if the given cradle is a cabal cradle.
66
89
-- This might be used to determine the GHC version to use on the project.
67
90
-- If it is a stack-cradle, we have to use @"stack path --compiler-exe"@
68
91
-- otherwise we may ask @ghc@ directly what version it is.
69
- isCabalCradle :: Cradle -> Bool
92
+ isCabalCradle :: Cradle CabalHelper -> Bool
70
93
isCabalCradle =
71
- (`elem`
72
- [ " cabal"
73
- , " Cabal-Helper-Cabal-V1"
74
- , " Cabal-Helper-Cabal-V2"
75
- , " Cabal-Helper-Cabal-V1-Dir"
76
- , " Cabal-Helper-Cabal-V2-Dir"
77
- , " Cabal-Helper-Cabal-V2-None"
78
- , " Cabal-Helper-Cabal-None"
79
- ]
94
+ ( `elem`
95
+ [ Bios. Other CabalV2
96
+ , Bios. Other CabalNone
97
+ ]
80
98
)
81
- . BIOS. actionName
82
- . BIOS. cradleOptsProg
99
+ . Bios. actionName
100
+ . Bios. cradleOptsProg
101
+
102
+ data CabalHelper
103
+ = Stack
104
+ | StackNone
105
+ | CabalV2
106
+ | CabalNone
107
+ deriving (Show , Eq , Ord )
83
108
84
109
-- | Execute @ghc@ that is based on the given cradle.
85
110
-- Output must be a single line. If an error is raised, e.g. the command
@@ -88,7 +113,7 @@ isCabalCradle =
88
113
--
89
114
-- E.g. for a stack cradle, we use @stack ghc@ and for a cabal cradle
90
115
-- we are taking the @ghc@ that is on the path.
91
- execProjectGhc :: Cradle -> [String ] -> IO (Maybe String )
116
+ execProjectGhc :: Cradle CabalHelper -> [String ] -> IO (Maybe String )
92
117
execProjectGhc crdl args = do
93
118
isStackInstalled <- isJust <$> findExecutable " stack"
94
119
-- isCabalInstalled <- isJust <$> findExecutable "cabal"
@@ -144,7 +169,7 @@ tryCommand cmd = do
144
169
145
170
146
171
-- | Get the directory of the libdir based on the project ghc.
147
- getProjectGhcLibDir :: Cradle -> IO (Maybe FilePath )
172
+ getProjectGhcLibDir :: Cradle CabalHelper -> IO (Maybe FilePath )
148
173
getProjectGhcLibDir crdl =
149
174
execProjectGhc crdl [" --print-libdir" ] >>= \ case
150
175
Nothing -> do
@@ -441,7 +466,7 @@ the compiler options obtained from Cabal-Helper are relative to the package
441
466
source directory, which is "\/Repo\/SubRepo".
442
467
443
468
-}
444
- cabalHelperCradle :: FilePath -> IO Cradle
469
+ cabalHelperCradle :: FilePath -> IO ( Cradle CabalHelper )
445
470
cabalHelperCradle file = do
446
471
projM <- findCabalHelperEntryPoint file
447
472
case projM of
@@ -451,7 +476,7 @@ cabalHelperCradle file = do
451
476
return
452
477
Cradle { cradleRootDir = cwd
453
478
, cradleOptsProg =
454
- CradleAction { actionName = " Direct"
479
+ CradleAction { actionName = Bios. Direct
455
480
, runCradle = \ _ _ ->
456
481
return
457
482
$ CradleSuccess
@@ -484,9 +509,7 @@ cabalHelperCradle file = do
484
509
return
485
510
Cradle { cradleRootDir = root
486
511
, cradleOptsProg =
487
- CradleAction { actionName = " Cabal-Helper-"
488
- ++ actionNameSuffix
489
- ++ " -None"
512
+ CradleAction { actionName = Bios. None
490
513
, runCradle = \ _ _ -> return CradleNone
491
514
}
492
515
}
@@ -501,8 +524,7 @@ cabalHelperCradle file = do
501
524
return
502
525
Cradle { cradleRootDir = normalisedPackageLocation
503
526
, cradleOptsProg =
504
- CradleAction { actionName =
505
- " Cabal-Helper-" ++ actionNameSuffix
527
+ CradleAction { actionName = Bios. Other actionNameSuffix
506
528
, runCradle = \ _ fp -> cabalHelperAction
507
529
(Ex proj)
508
530
env
@@ -751,12 +773,12 @@ projectRootDir ProjLocV2File { plProjectDirV2 } = plProjectDirV2
751
773
projectRootDir ProjLocV2Dir { plProjectDirV2 } = plProjectDirV2
752
774
projectRootDir ProjLocStackYaml { plStackYaml } = takeDirectory plStackYaml
753
775
754
- projectSuffix :: ProjLoc qt -> FilePath
755
- projectSuffix ProjLocV1CabalFile {} = " Cabal-V1 "
756
- projectSuffix ProjLocV1Dir {} = " Cabal-V1-Dir "
757
- projectSuffix ProjLocV2File {} = " Cabal-V2 "
758
- projectSuffix ProjLocV2Dir {} = " Cabal-V2-Dir "
759
- projectSuffix ProjLocStackYaml {} = " Stack"
776
+ projectSuffix :: ProjLoc qt -> CabalHelper
777
+ projectSuffix ProjLocV1CabalFile {} = CabalV2
778
+ projectSuffix ProjLocV1Dir {} = CabalV2
779
+ projectSuffix ProjLocV2File {} = CabalV2
780
+ projectSuffix ProjLocV2Dir {} = CabalV2
781
+ projectSuffix ProjLocStackYaml {} = Stack
760
782
761
783
-- ----------------------------------------------------------------------------
762
784
--
@@ -867,14 +889,22 @@ relativeTo file sourceDirs =
867
889
868
890
-- | Returns a user facing display name for the cradle type,
869
891
-- e.g. "Stack project" or "GHC session"
870
- cradleDisplay :: IsString a => BIOS. Cradle -> a
892
+ cradleDisplay :: IsString a => Cradle CabalHelper -> a
871
893
cradleDisplay cradle = fromString result
872
- where
873
- result
874
- | " stack" `isInfixOf` name = " Stack project"
875
- | " cabal-v1" `isInfixOf` name = " Cabal (V1) project"
876
- | " cabal" `isInfixOf` name = " Cabal project"
877
- | " direct" `isInfixOf` name = " GHC session"
878
- | " multi" `isInfixOf` name = " Multi Component project"
879
- | otherwise = " project"
880
- name = map toLower $ BIOS. actionName (BIOS. cradleOptsProg cradle)
894
+ where
895
+ result
896
+ | Bios. isStackCradle cradle
897
+ || name
898
+ `elem` [Bios. Other Stack , Bios. Other StackNone ]
899
+ = " Stack project"
900
+ | Bios. isCabalCradle cradle
901
+ || name
902
+ `elem` [Bios. Other CabalV2 , Bios. Other CabalNone ]
903
+ = " Cabal project"
904
+ | Bios. isDirectCradle cradle
905
+ = " GHC session"
906
+ | Bios. isMultiCradle cradle
907
+ = " Multi Component project"
908
+ | otherwise
909
+ = " project"
910
+ name = Bios. actionName (Bios. cradleOptsProg cradle)
0 commit comments