Skip to content

Commit 75227d7

Browse files
committed
Move Logger into Cradle
1 parent 6f4551d commit 75227d7

File tree

8 files changed

+119
-119
lines changed

8 files changed

+119
-119
lines changed

exe/Main.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,28 @@ main = do
6363
hSetEncoding stdout utf8
6464
cwd <- getCurrentDirectory
6565
cmd <- execParser progInfo
66-
cradle <-
67-
-- find cradle does a takeDirectory on the argument, so make it into a file
68-
findCradle (cwd </> "File.hs") >>= \case
69-
Just yaml -> loadCradle yaml
70-
Nothing -> loadImplicitCradle (cwd </> "File.hs")
71-
7266
let
7367
printLog (L.WithSeverity l sev) = "[" ++ show sev ++ "] " ++ show (pretty l)
7468
logger :: forall a . Pretty a => L.LogAction IO (L.WithSeverity a)
7569
logger = L.cmap printLog L.logStringStderr
7670

71+
cradle <-
72+
-- find cradle does a takeDirectory on the argument, so make it into a file
73+
findCradle (cwd </> "File.hs") >>= \case
74+
Just yaml -> loadCradle logger yaml
75+
Nothing -> loadImplicitCradle logger (cwd </> "File.hs")
76+
7777
res <- case cmd of
78-
Check targetFiles -> checkSyntax logger logger cradle targetFiles
78+
Check targetFiles -> checkSyntax logger cradle targetFiles
7979
Debug files -> case files of
80-
[] -> debugInfo logger (cradleRootDir cradle) cradle
81-
fp -> debugInfo logger fp cradle
80+
[] -> debugInfo (cradleRootDir cradle) cradle
81+
fp -> debugInfo fp cradle
8282
Flags files -> case files of
8383
-- TODO force optparse to acquire one
8484
[] -> error "too few arguments"
8585
_ -> do
8686
res <- forM files $ \fp -> do
87-
res <- getCompilerOptions logger fp [] cradle
87+
res <- getCompilerOptions fp [] cradle
8888
case res of
8989
CradleFail (CradleError _deps _ex err) ->
9090
return $ "Failed to show flags for \""
@@ -97,7 +97,7 @@ main = do
9797
CradleNone -> return $ "No flags/None Cradle: component " ++ fp ++ " should not be loaded"
9898
return (unlines res)
9999
ConfigInfo files -> configInfo files
100-
CradleInfo files -> cradleInfo files
100+
CradleInfo files -> cradleInfo logger files
101101
Root -> rootInfo cradle
102102
Version -> return progVersion
103103
putStr res

src/HIE/Bios/Cradle.hs

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,31 @@ findCradle wfile = do
8484
runMaybeT (yamlConfig wdir)
8585

8686
-- | Given root\/hie.yaml load the Cradle.
87-
loadCradle :: FilePath -> IO (Cradle Void)
88-
loadCradle = loadCradleWithOpts absurd
87+
loadCradle :: LogAction IO (WithSeverity Log) -> FilePath -> IO (Cradle Void)
88+
loadCradle l = loadCradleWithOpts l absurd
8989

9090
-- | Given root\/foo\/bar.hs, load an implicit cradle
91-
loadImplicitCradle :: Show a => FilePath -> IO (Cradle a)
92-
loadImplicitCradle wfile = do
91+
loadImplicitCradle :: Show a => LogAction IO (WithSeverity Log) -> FilePath -> IO (Cradle a)
92+
loadImplicitCradle l wfile = do
9393
let wdir = takeDirectory wfile
9494
cfg <- runMaybeT (implicitConfig wdir)
9595
case cfg of
96-
Just bc -> getCradle absurd bc
97-
Nothing -> return $ defaultCradle wdir
96+
Just bc -> getCradle l absurd bc
97+
Nothing -> return $ defaultCradle l wdir
9898

9999
-- | Finding 'Cradle'.
100100
-- Find a cabal file by tracing ancestor directories.
101101
-- Find a sandbox according to a cabal sandbox config
102102
-- in a cabal directory.
103-
loadCradleWithOpts :: (Yaml.FromJSON b, Show a) => (b -> CradleAction a) -> FilePath -> IO (Cradle a)
104-
loadCradleWithOpts buildCustomCradle wfile = do
103+
loadCradleWithOpts :: (Yaml.FromJSON b, Show a) => LogAction IO (WithSeverity Log) -> (b -> CradleAction a) -> FilePath -> IO (Cradle a)
104+
loadCradleWithOpts l buildCustomCradle wfile = do
105105
cradleConfig <- readCradleConfig wfile
106-
getCradle buildCustomCradle (cradleConfig, takeDirectory wfile)
106+
getCradle l buildCustomCradle (cradleConfig, takeDirectory wfile)
107107

108-
getCradle :: Show a => (b -> CradleAction a) -> (CradleConfig b, FilePath) -> IO (Cradle a)
109-
getCradle buildCustomCradle (cc, wdir) = do
108+
getCradle :: Show a => LogAction IO (WithSeverity Log) -> (b -> CradleAction a) -> (CradleConfig b, FilePath) -> IO (Cradle a)
109+
getCradle l buildCustomCradle (cc, wdir) = do
110110
rcs <- canonicalizeResolvedCradles wdir cs
111-
resolvedCradlesToCradle buildCustomCradle wdir rcs
111+
resolvedCradlesToCradle l buildCustomCradle wdir rcs
112112
where
113113
cs = resolveCradleTree wdir cc
114114

@@ -147,36 +147,34 @@ data ProgramVersions =
147147
, ghcVersion :: Maybe Version
148148
}
149149

150-
makeVersions :: ([String] -> IO (CradleLoadResult String)) -> IO ProgramVersions
151-
makeVersions ghc = do
152-
cabalVersion <- unsafeInterleaveIO getCabalVersion
153-
stackVersion <- unsafeInterleaveIO getStackVersion
150+
makeVersions :: LogAction IO (WithSeverity Log) -> FilePath -> ([String] -> IO (CradleLoadResult String)) -> IO ProgramVersions
151+
makeVersions l wdir ghc = do
152+
cabalVersion <- unsafeInterleaveIO (getCabalVersion l wdir)
153+
stackVersion <- unsafeInterleaveIO (getStackVersion l wdir)
154154
ghcVersion <- unsafeInterleaveIO (getGhcVersion ghc)
155155
pure ProgramVersions{..}
156156

157-
getCabalVersion :: IO (Maybe Version)
158-
getCabalVersion = do
159-
let p = proc "cabal" ["--numeric-version"]
160-
res <- optional $ readCreateProcessWithExitCode p ""
157+
getCabalVersion :: LogAction IO (WithSeverity Log) -> FilePath -> IO (Maybe Version)
158+
getCabalVersion l wdir = do
159+
res <- readProcessWithCwd l wdir "cabal" ["--numeric-version"] ""
161160
case res of
162-
Just (ExitSuccess,stdo,_) ->
161+
CradleSuccess stdo ->
163162
pure $ versionMaybe stdo
164163
_ -> pure Nothing
165164

166-
getStackVersion :: IO (Maybe Version)
167-
getStackVersion = do
168-
let p = proc "stack" ["--numeric-version"]
169-
res <- optional $ readCreateProcessWithExitCode p ""
165+
getStackVersion :: LogAction IO (WithSeverity Log) -> FilePath -> IO (Maybe Version)
166+
getStackVersion l wdir = do
167+
res <- readProcessWithCwd l wdir "stack" ["--numeric-version"] ""
170168
case res of
171-
Just (ExitSuccess,stdo,_) ->
169+
CradleSuccess stdo ->
172170
pure $ versionMaybe stdo
173171
_ -> pure Nothing
174172

175173
getGhcVersion :: ([String] -> IO (CradleLoadResult String)) -> IO (Maybe Version)
176174
getGhcVersion ghc = do
177-
res <- optional $ ghc ["--numeric-version"]
175+
res <- ghc ["--numeric-version"]
178176
case res of
179-
Just (CradleSuccess stdo) ->
177+
CradleSuccess stdo ->
180178
pure $ versionMaybe stdo
181179
_ -> pure Nothing
182180

@@ -194,9 +192,9 @@ addActionDeps deps =
194192
(\(ComponentOptions os' dir ds) -> CradleSuccess (ComponentOptions os' dir (ds `union` deps)))
195193

196194

197-
resolvedCradlesToCradle :: Show a => (b -> CradleAction a) -> FilePath -> [ResolvedCradle b] -> IO (Cradle a)
198-
resolvedCradlesToCradle buildCustomCradle root cs = mdo
199-
let run_ghc_cmd l args =
195+
resolvedCradlesToCradle :: Show a => LogAction IO (WithSeverity Log) -> (b -> CradleAction a) -> FilePath -> [ResolvedCradle b] -> IO (Cradle a)
196+
resolvedCradlesToCradle logger buildCustomCradle root cs = mdo
197+
let run_ghc_cmd args =
200198
-- We're being lazy here and just returning the ghc path for the
201199
-- first non-none cradle. This shouldn't matter in practice: all
202200
-- sub cradles should be using the same ghc version!
@@ -205,11 +203,10 @@ resolvedCradlesToCradle buildCustomCradle root cs = mdo
205203
(act:_) ->
206204
runGhcCmd
207205
act
208-
l
209206
args
210-
versions <- makeVersions (run_ghc_cmd mempty)
207+
versions <- makeVersions logger root run_ghc_cmd
211208
let rcs = ResolvedCradles root cs versions
212-
cradleActions = [ (c, resolveCradleAction buildCustomCradle rcs root c) | c <- cs ]
209+
cradleActions = [ (c, resolveCradleAction logger buildCustomCradle rcs root c) | c <- cs ]
213210
err_msg fp
214211
= ["Multi Cradle: No prefixes matched"
215212
, "pwd: " ++ root
@@ -218,13 +215,14 @@ resolvedCradlesToCradle buildCustomCradle root cs = mdo
218215
] ++ [show (prefix pf, actionName cc) | (pf, cc) <- cradleActions]
219216
pure $ Cradle
220217
{ cradleRootDir = root
218+
, cradleLogger = logger
221219
, cradleOptsProg = CradleAction
222220
{ actionName = multiActionName
223-
, runCradle = \l fp prev -> do
221+
, runCradle = \fp prev -> do
224222
absfp <- makeAbsolute fp
225223
case selectCradle (prefix . fst) absfp cradleActions of
226224
Just (rc, act) -> do
227-
addActionDeps (cradleDeps rc) <$> runCradle act l fp prev
225+
addActionDeps (cradleDeps rc) <$> runCradle act fp prev
228226
Nothing -> return $ CradleFail $ CradleError [] ExitSuccess (err_msg fp)
229227
, runGhcCmd = run_ghc_cmd
230228
}
@@ -266,13 +264,13 @@ resolvedCradlesToCradle buildCustomCradle root cs = mdo
266264
notNoneType _ = True
267265

268266

269-
resolveCradleAction :: (b -> CradleAction a) -> ResolvedCradles b -> FilePath -> ResolvedCradle b -> CradleAction a
270-
resolveCradleAction buildCustomCradle cs root cradle =
267+
resolveCradleAction :: LogAction IO (WithSeverity Log) -> (b -> CradleAction a) -> ResolvedCradles b -> FilePath -> ResolvedCradle b -> CradleAction a
268+
resolveCradleAction l buildCustomCradle cs root cradle =
271269
case concreteCradle cradle of
272-
ConcreteCabal t -> cabalCradle cs root (cabalComponent t) (projectConfigFromMaybe root (cabalProjectFile t))
273-
ConcreteStack t -> stackCradle root (stackComponent t) (projectConfigFromMaybe root (stackYaml t))
274-
ConcreteBios bios deps mbGhc -> biosCradle root bios deps mbGhc
275-
ConcreteDirect xs -> directCradle root xs
270+
ConcreteCabal t -> cabalCradle l cs root (cabalComponent t) (projectConfigFromMaybe root (cabalProjectFile t))
271+
ConcreteStack t -> stackCradle l root (stackComponent t) (projectConfigFromMaybe root (stackYaml t))
272+
ConcreteBios bios deps mbGhc -> biosCradle l root bios deps mbGhc
273+
ConcreteDirect xs -> directCradle l root xs
276274
ConcreteNone -> noneCradle
277275
ConcreteOther a -> buildCustomCradle a
278276

@@ -391,15 +389,16 @@ isOtherCradle crdl = case actionName (cradleOptsProg crdl) of
391389

392390
-- | Default cradle has no special options, not very useful for loading
393391
-- modules.
394-
defaultCradle :: FilePath -> Cradle a
395-
defaultCradle cur_dir =
392+
defaultCradle :: LogAction IO (WithSeverity Log) -> FilePath -> Cradle a
393+
defaultCradle l cur_dir =
396394
Cradle
397395
{ cradleRootDir = cur_dir
396+
, cradleLogger = l
398397
, cradleOptsProg = CradleAction
399398
{ actionName = Types.Default
400-
, runCradle = \_ _ _ ->
399+
, runCradle = \_ _ ->
401400
return (CradleSuccess (ComponentOptions argDynamic cur_dir []))
402-
, runGhcCmd = \l -> runGhcCmdOnPath l cur_dir
401+
, runGhcCmd = runGhcCmdOnPath l cur_dir
403402
}
404403
}
405404

@@ -410,8 +409,8 @@ noneCradle :: CradleAction a
410409
noneCradle =
411410
CradleAction
412411
{ actionName = Types.None
413-
, runCradle = \_ _ _ -> return CradleNone
414-
, runGhcCmd = \_ _ -> return CradleNone
412+
, runCradle = \_ _ -> return CradleNone
413+
, runGhcCmd = \_ -> return CradleNone
415414
}
416415

417416
---------------------------------------------------------------
@@ -435,13 +434,13 @@ selectCradle k cur_fp (c: css) =
435434

436435
-------------------------------------------------------------------------
437436

438-
directCradle :: FilePath -> [String] -> CradleAction a
439-
directCradle wdir args
437+
directCradle :: LogAction IO (WithSeverity Log) -> FilePath -> [String] -> CradleAction a
438+
directCradle l wdir args
440439
= CradleAction
441440
{ actionName = Types.Direct
442-
, runCradle = \_ _ _ ->
441+
, runCradle = \_ _ ->
443442
return (CradleSuccess (ComponentOptions (args ++ argDynamic) wdir []))
444-
, runGhcCmd = \l -> runGhcCmdOnPath l wdir
443+
, runGhcCmd = runGhcCmdOnPath l wdir
445444
}
446445

447446

@@ -450,12 +449,12 @@ directCradle wdir args
450449

451450
-- | Find a cradle by finding an executable `hie-bios` file which will
452451
-- be executed to find the correct GHC options to use.
453-
biosCradle :: FilePath -> Callable -> Maybe Callable -> Maybe FilePath -> CradleAction a
454-
biosCradle wdir biosCall biosDepsCall mbGhc
452+
biosCradle :: LogAction IO (WithSeverity Log) -> FilePath -> Callable -> Maybe Callable -> Maybe FilePath -> CradleAction a
453+
biosCradle l wdir biosCall biosDepsCall mbGhc
455454
= CradleAction
456455
{ actionName = Types.Bios
457-
, runCradle = biosAction wdir biosCall biosDepsCall
458-
, runGhcCmd = \l args -> readProcessWithCwd l wdir (fromMaybe "ghc" mbGhc) args ""
456+
, runCradle = biosAction wdir biosCall biosDepsCall l
457+
, runGhcCmd = \args -> readProcessWithCwd l wdir (fromMaybe "ghc" mbGhc) args ""
459458
}
460459

461460
biosWorkDir :: FilePath -> MaybeT IO FilePath
@@ -513,12 +512,12 @@ projectLocationOrDefault = \case
513512

514513
-- |Cabal Cradle
515514
-- Works for new-build by invoking `v2-repl`.
516-
cabalCradle :: ResolvedCradles b -> FilePath -> Maybe String -> CradleProjectConfig -> CradleAction a
517-
cabalCradle cs wdir mc projectFile
515+
cabalCradle :: LogAction IO (WithSeverity Log) -> ResolvedCradles b -> FilePath -> Maybe String -> CradleProjectConfig -> CradleAction a
516+
cabalCradle l cs wdir mc projectFile
518517
= CradleAction
519518
{ actionName = Types.Cabal
520-
, runCradle = \l fp -> runCradleResultT . cabalAction cs wdir mc l projectFile fp
521-
, runGhcCmd = \l args -> runCradleResultT $ do
519+
, runCradle = \fp -> runCradleResultT . cabalAction cs wdir mc l projectFile fp
520+
, runGhcCmd = \args -> runCradleResultT $ do
522521
buildDir <- liftIO $ cabalBuildDir wdir
523522
-- Workaround for a cabal-install bug on 3.0.0.0:
524523
-- ./dist-newstyle/tmp/environment.-24811: createDirectory: does not exist (No such file or directory)
@@ -906,12 +905,12 @@ stackYamlLocationOrDefault (ExplicitConfig yaml) = yaml
906905

907906
-- | Stack Cradle
908907
-- Works for by invoking `stack repl` with a wrapper script
909-
stackCradle :: FilePath -> Maybe String -> CradleProjectConfig -> CradleAction a
910-
stackCradle wdir mc syaml =
908+
stackCradle :: LogAction IO (WithSeverity Log) -> FilePath -> Maybe String -> CradleProjectConfig -> CradleAction a
909+
stackCradle l wdir mc syaml =
911910
CradleAction
912911
{ actionName = Types.Stack
913-
, runCradle = stackAction wdir mc syaml
914-
, runGhcCmd = \l args -> runCradleResultT $ do
912+
, runCradle = stackAction wdir mc syaml l
913+
, runGhcCmd = \args -> runCradleResultT $ do
915914
-- Setup stack silently, since stack might print stuff to stdout in some cases (e.g. on Win)
916915
-- Issue 242 from HLS: https://github.com/haskell/haskell-language-server/issues/242
917916
_ <- readProcessWithCwd_ l wdir "stack" (stackYamlProcessArgs syaml <> ["setup", "--silent"]) ""

src/HIE/Bios/Environment.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,18 @@ makeTargetIdAbsolute _ tid = tid
6868
--
6969
--
7070
-- Obtains libdir by calling 'runCradleGhc' on the provided cradle.
71-
getRuntimeGhcLibDir :: LogAction IO (WithSeverity Log)
72-
-> Cradle a
71+
getRuntimeGhcLibDir :: Cradle a
7372
-> IO (CradleLoadResult FilePath)
74-
getRuntimeGhcLibDir l cradle = fmap (fmap trim) $
75-
runGhcCmd (cradleOptsProg cradle) l ["--print-libdir"]
73+
getRuntimeGhcLibDir cradle = fmap (fmap trim) $
74+
runGhcCmd (cradleOptsProg cradle) ["--print-libdir"]
7675

7776
-- | Gets the version of ghc used when compiling the cradle. It is based off of
7877
-- 'getRuntimeGhcLibDir'. If it can't work out the verison reliably, it will
7978
-- return a 'CradleError'
80-
getRuntimeGhcVersion :: LogAction IO (WithSeverity Log)
81-
-> Cradle a
79+
getRuntimeGhcVersion :: Cradle a
8280
-> IO (CradleLoadResult String)
83-
getRuntimeGhcVersion l cradle =
84-
fmap (fmap trim) $ runGhcCmd (cradleOptsProg cradle) l ["--numeric-version"]
81+
getRuntimeGhcVersion cradle =
82+
fmap (fmap trim) $ runGhcCmd (cradleOptsProg cradle) ["--numeric-version"]
8583

8684
----------------------------------------------------------------
8785

src/HIE/Bios/Flags.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import Colog.Core (LogAction (..), WithSeverity (..), Severity (..), (<&))
77
-- | Initialize the 'DynFlags' relating to the compilation of a single
88
-- file or GHC session according to the provided 'Cradle'.
99
getCompilerOptions
10-
:: LogAction IO (WithSeverity Log)
11-
-> FilePath -- ^ The file we are loading it because of
10+
:: FilePath -- ^ The file we are loading it because of
1211
-> [FilePath] -- ^ previous files we might want to include in the build
1312
-> Cradle a
1413
-> IO (CradleLoadResult ComponentOptions)
15-
getCompilerOptions l fp fps cradle = do
16-
l <& LogProcessOutput "invoking build tool to determine build flags (this may take some time depending on the cache)" `WithSeverity` Info
17-
runCradle (cradleOptsProg cradle) l fp fps
14+
getCompilerOptions fp fps cradle = do
15+
(cradleLogger cradle) <& LogProcessOutput "invoking build tool to determine build flags (this may take some time depending on the cache)" `WithSeverity` Info
16+
runCradle (cradleOptsProg cradle) fp fps

src/HIE/Bios/Ghc/Api.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,22 @@ import HIE.Bios.Flags
3333
-- | Initialize a GHC session by loading a given file into a given cradle.
3434
initializeFlagsWithCradle ::
3535
GhcMonad m
36-
=> LogAction IO (WithSeverity Log)
37-
-> FilePath -- ^ The file we are loading the 'Cradle' because of
36+
=> FilePath -- ^ The file we are loading the 'Cradle' because of
3837
-> Cradle a -- ^ The cradle we want to load
3938
-> m (CradleLoadResult (m G.SuccessFlag, ComponentOptions))
40-
initializeFlagsWithCradle l = initializeFlagsWithCradleWithMessage l (Just Gap.batchMsg)
39+
initializeFlagsWithCradle = initializeFlagsWithCradleWithMessage (Just Gap.batchMsg)
4140

4241
-- | The same as 'initializeFlagsWithCradle' but with an additional argument to control
4342
-- how the loading progress messages are displayed to the user. In @haskell-ide-engine@
4443
-- the module loading progress is displayed in the UI by using a progress notification.
4544
initializeFlagsWithCradleWithMessage ::
4645
GhcMonad m
47-
=> LogAction IO (WithSeverity Log)
48-
-> Maybe G.Messager
46+
=> Maybe G.Messager
4947
-> FilePath -- ^ The file we are loading the 'Cradle' because of
5048
-> Cradle a -- ^ The cradle we want to load
5149
-> m (CradleLoadResult (m G.SuccessFlag, ComponentOptions)) -- ^ Whether we actually loaded the cradle or not.
52-
initializeFlagsWithCradleWithMessage l msg fp cradle =
53-
fmap (initSessionWithMessage msg) <$> liftIO (getCompilerOptions l fp [] cradle)
50+
initializeFlagsWithCradleWithMessage msg fp cradle =
51+
fmap (initSessionWithMessage msg) <$> liftIO (getCompilerOptions fp [] cradle)
5452

5553
-- | Actually perform the initialisation of the session. Initialising the session corresponds to
5654
-- parsing the command line flags, setting the targets for the session and then attempting to load

0 commit comments

Comments
 (0)