Skip to content

Commit 9f01ebc

Browse files
committed
Use enums instead of naked booleans
Add enum to control TH stripping messages. Add enum to control conversion of paths to absolute.
1 parent f2cbb71 commit 9f01ebc

File tree

2 files changed

+69
-39
lines changed

2 files changed

+69
-39
lines changed

src/Stack/Build/Execute.hs

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Stack.Build.Execute
1717
, ExecuteEnv
1818
, withExecuteEnv
1919
, withSingleContext
20+
, ExcludeTHLoading(..)
2021
) where
2122

2223
import Control.Applicative
@@ -450,7 +451,7 @@ withExecuteEnv menv bopts boptsCli baseConfigOpts locals globalPackages snapshot
450451
runResourceT
451452
$ CB.sourceFile (toFilePath filepath)
452453
$$ CT.decodeUtf8Lenient
453-
=$ mungeBuildOutput True True pkgDir
454+
=$ mungeBuildOutput ExcludeTHLoading ConvertPathsToAbsolute pkgDir
454455
=$ CL.mapM_ $logInfo
455456
$logInfo $ T.pack $ "\n-- End of log file: " ++ toFilePath filepath ++ "\n"
456457

@@ -808,7 +809,7 @@ ensureConfig :: (StackM env m, HasEnvConfig env)
808809
-> Path Abs Dir -- ^ package directory
809810
-> ExecuteEnv m
810811
-> m () -- ^ announce
811-
-> (Bool -> [String] -> m ()) -- ^ cabal
812+
-> (ExcludeTHLoading -> [String] -> m ()) -- ^ cabal
812813
-> Path Abs File -- ^ .cabal file
813814
-> m Bool
814815
ensureConfig newConfigCache pkgDir ExecuteEnv {..} announce cabal cabalfp = do
@@ -847,7 +848,7 @@ ensureConfig newConfigCache pkgDir ExecuteEnv {..} announce cabal cabalfp = do
847848
Just x -> return $ concat ["--with-", name, "=", toFilePath x]
848849
-- Configure cabal with arguments determined by
849850
-- Stack.Types.Build.configureOpts
850-
cabal False $ "configure" : concat
851+
cabal KeepTHLoading $ "configure" : concat
851852
[ concat exes
852853
, dirs
853854
, nodirs
@@ -875,7 +876,7 @@ announceTask task x = $logInfo $ T.concat
875876
-- custom setup is built.
876877
--
877878
-- * Provides the user a function with which run the Cabal process.
878-
withSingleContext :: (StackM env m, HasEnvConfig env)
879+
withSingleContext :: forall env m a. (StackM env m, HasEnvConfig env)
879880
=> (m () -> IO ())
880881
-> ActionContext
881882
-> ExecuteEnv m
@@ -885,14 +886,13 @@ withSingleContext :: (StackM env m, HasEnvConfig env)
885886
-- Nothing, just provide global and snapshot package
886887
-- databases.
887888
-> Maybe String
888-
-> ( Package -- Package info
889-
-> Path Abs File -- Cabal file path
890-
-> Path Abs Dir -- Package root directory file path
891-
-> (Bool -> [String] -> m ()) -- Function to run Cabal with args
892-
-- The Bool indicates if it's a build step, so strip TH stuff
893-
-> (Text -> m ()) -- An 'announce' function, for different build phases
894-
-> Bool -- Whether output should be directed to the console
895-
-> Maybe (Path Abs File, Handle) -- Log file
889+
-> ( Package -- Package info
890+
-> Path Abs File -- Cabal file path
891+
-> Path Abs Dir -- Package root directory file path
892+
-> (ExcludeTHLoading -> [String] -> m ()) -- Function to run Cabal with args
893+
-> (Text -> m ()) -- An 'announce' function, for different build phases
894+
-> Bool -- Whether output should be directed to the console
895+
-> Maybe (Path Abs File, Handle) -- Log file
896896
-> m a)
897897
-> m a
898898
withSingleContext runInBase ActionContext {..} ExecuteEnv {..} task@Task {..} mdeps msuffix inner0 =
@@ -946,6 +946,12 @@ withSingleContext runInBase ActionContext {..} ExecuteEnv {..} task@Task {..} md
946946
(liftIO . hClose)
947947
$ \h -> inner (Just (logPath, h))
948948

949+
withCabal
950+
:: Package
951+
-> Path Abs Dir
952+
-> Maybe (Path Abs File, Handle)
953+
-> ((ExcludeTHLoading -> [String] -> m ()) -> m a)
954+
-> m a
949955
withCabal package pkgDir mlogFile inner = do
950956
config <- view configL
951957

@@ -1103,14 +1109,18 @@ withSingleContext runInBase ActionContext {..} ExecuteEnv {..} task@Task {..} md
11031109
sinkProcessStderrStdoutHandle (Just pkgDir) menv (toFilePath exeName) fullArgs h h
11041110
Nothing ->
11051111
void $ sinkProcessStderrStdout (Just pkgDir) menv (toFilePath exeName) fullArgs
1106-
(outputSink False LevelWarn)
1112+
(outputSink KeepTHLoading LevelWarn)
11071113
(outputSink stripTHLoading LevelInfo)
1114+
outputSink :: ExcludeTHLoading -> LogLevel -> Sink S.ByteString IO ()
11081115
outputSink excludeTH level =
11091116
CT.decodeUtf8Lenient
11101117
=$ mungeBuildOutput excludeTH makeAbsolute pkgDir
11111118
=$ CL.mapM_ (runInBase . monadLoggerLog $(TH.location >>= liftLoc) "" level)
11121119
-- If users want control, we should add a config option for this
1113-
makeAbsolute = stripTHLoading
1120+
makeAbsolute :: ConvertPathsToAbsolute
1121+
makeAbsolute = case stripTHLoading of
1122+
ExcludeTHLoading -> ConvertPathsToAbsolute
1123+
KeepTHLoading -> KeepPathsAsIs
11141124

11151125
wc <- view $ actualCompilerVersionL.whichCompilerL
11161126
exeName <- case (esetupexehs, wc) of
@@ -1166,7 +1176,7 @@ withSingleContext runInBase ActionContext {..} ExecuteEnv {..} task@Task {..} md
11661176
-- local install directory. Note that this is literally invoking Cabal
11671177
-- with @copy@, and not the copying done by @stack install@ - that is
11681178
-- handled by 'copyExecutables'.
1169-
singleBuild :: (StackM env m, HasEnvConfig env)
1179+
singleBuild :: forall env m. (StackM env m, HasEnvConfig env)
11701180
=> (m () -> IO ())
11711181
-> ActionContext
11721182
-> ExecuteEnv m
@@ -1322,8 +1332,15 @@ singleBuild runInBase ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} in
13221332

13231333
initialBuildSteps cabal announce = do
13241334
() <- announce ("initial-build-steps" <> annSuffix)
1325-
cabal False ["repl", "stack-initial-build-steps"]
1326-
1335+
cabal KeepTHLoading ["repl", "stack-initial-build-steps"]
1336+
1337+
realBuild
1338+
:: ConfigCache
1339+
-> Package
1340+
-> Path Abs Dir
1341+
-> (ExcludeTHLoading -> [String] -> m ())
1342+
-> (Text -> m ())
1343+
-> m Installed
13271344
realBuild cache package pkgDir cabal announce = do
13281345
wc <- view $ actualCompilerVersionL.whichCompilerL
13291346

@@ -1363,7 +1380,10 @@ singleBuild runInBase ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} in
13631380
() <- announce ("build" <> annSuffix)
13641381
config <- view configL
13651382
extraOpts <- extraBuildOptions wc eeBuildOpts
1366-
cabal (configHideTHLoading config) (("build" :) $ (++ extraOpts) $
1383+
let stripTHLoading
1384+
| configHideTHLoading config = ExcludeTHLoading
1385+
| otherwise = KeepTHLoading
1386+
cabal stripTHLoading (("build" :) $ (++ extraOpts) $
13671387
case (taskType, taskAllInOne, isFinalBuild) of
13681388
(_, True, True) -> error "Invariant violated: cannot have an all-in-one build that also has a final build step."
13691389
(TTLocal lp, False, False) -> primaryComponentOptions lp
@@ -1391,22 +1411,23 @@ singleBuild runInBase ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} in
13911411
("Warning: haddock not generating hyperlinked sources because 'HsColour' not\n" <>
13921412
"found on PATH (use 'stack install hscolour' to install).")
13931413
return ["--hyperlink-source" | hscolourExists]
1394-
cabal False (concat [ ["haddock", "--html", "--html-location=../$pkg-$version/"]
1395-
, sourceFlag
1396-
, ["--internal" | boptsHaddockInternal eeBuildOpts]
1397-
, [ "--haddock-option=" <> opt
1398-
| opt <- hoAdditionalArgs (boptsHaddockOpts eeBuildOpts) ]
1399-
])
1414+
cabal KeepTHLoading $ concat
1415+
[ ["haddock", "--html", "--html-location=../$pkg-$version/"]
1416+
, sourceFlag
1417+
, ["--internal" | boptsHaddockInternal eeBuildOpts]
1418+
, [ "--haddock-option=" <> opt
1419+
| opt <- hoAdditionalArgs (boptsHaddockOpts eeBuildOpts) ]
1420+
]
14001421

14011422
let shouldCopy = not isFinalBuild && (packageHasLibrary package || not (Set.null (packageExes package)))
14021423
when shouldCopy $ withMVar eeInstallLock $ \() -> do
14031424
announce "copy/register"
1404-
eres <- try $ cabal False ["copy"]
1425+
eres <- try $ cabal KeepTHLoading ["copy"]
14051426
case eres of
14061427
Left err@CabalExitedUnsuccessfully{} ->
14071428
throwM $ CabalCopyFailed (packageBuildType package == Just C.Simple) (show err)
14081429
_ -> return ()
1409-
when (packageHasLibrary package) $ cabal False ["register"]
1430+
when (packageHasLibrary package) $ cabal KeepTHLoading ["register"]
14101431

14111432
let (installedPkgDb, installedDumpPkgsTVar) =
14121433
case taskLocation task of
@@ -1649,31 +1670,40 @@ singleBench runInBase beopts benchesToRun ac ee task installedMap = do
16491670
return True
16501671

16511672
when toRun $ do
1652-
announce "benchmarks"
1653-
cabal False ("bench" : args)
1673+
announce "benchmarks"
1674+
cabal KeepTHLoading ("bench" : args)
1675+
1676+
data ExcludeTHLoading = ExcludeTHLoading | KeepTHLoading
1677+
data ConvertPathsToAbsolute = ConvertPathsToAbsolute | KeepPathsAsIs
16541678

16551679
-- | Strip Template Haskell "Loading package" lines and making paths absolute.
1656-
mungeBuildOutput :: (MonadIO m, MonadCatch m, MonadBaseControl IO m)
1657-
=> Bool -- ^ exclude TH loading?
1658-
-> Bool -- ^ convert paths to absolute?
1659-
-> Path Abs Dir -- ^ package's root directory
1680+
mungeBuildOutput :: forall m. (MonadIO m, MonadCatch m, MonadBaseControl IO m)
1681+
=> ExcludeTHLoading -- ^ exclude TH loading?
1682+
-> ConvertPathsToAbsolute -- ^ convert paths to absolute?
1683+
-> Path Abs Dir -- ^ package's root directory
16601684
-> ConduitM Text Text m ()
16611685
mungeBuildOutput excludeTHLoading makeAbsolute pkgDir = void $
16621686
CT.lines
16631687
=$ CL.map stripCR
16641688
=$ CL.filter (not . isTHLoading)
1665-
=$ CL.mapM toAbsolutePath
1689+
=$ toAbsolute
16661690
where
16671691
-- | Is this line a Template Haskell "Loading package" line
16681692
-- ByteString
16691693
isTHLoading :: Text -> Bool
1670-
isTHLoading _ | not excludeTHLoading = False
1671-
isTHLoading bs =
1672-
"Loading package " `T.isPrefixOf` bs &&
1673-
("done." `T.isSuffixOf` bs || "done.\r" `T.isSuffixOf` bs)
1694+
isTHLoading = case excludeTHLoading of
1695+
KeepTHLoading -> const False
1696+
ExcludeTHLoading -> \bs ->
1697+
"Loading package " `T.isPrefixOf` bs &&
1698+
("done." `T.isSuffixOf` bs || "done.\r" `T.isSuffixOf` bs)
16741699

16751700
-- | Convert GHC error lines with file paths to have absolute file paths
1676-
toAbsolutePath bs | not makeAbsolute = return bs
1701+
toAbsolute :: ConduitM Text Text m ()
1702+
toAbsolute = case makeAbsolute of
1703+
KeepPathsAsIs -> awaitForever yield
1704+
ConvertPathsToAbsolute -> CL.mapM toAbsolutePath
1705+
1706+
toAbsolutePath :: Text -> m Text
16771707
toAbsolutePath bs = do
16781708
let (x, y) = T.break (== ':') bs
16791709
mabs <-

src/Stack/SDist.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ getSDistFileList lp =
266266
$ \ee ->
267267
withSingleContext runInBase ac ee task Nothing (Just "sdist") $ \_package cabalfp _pkgDir cabal _announce _console _mlogFile -> do
268268
let outFile = toFilePath tmpdir FP.</> "source-files-list"
269-
cabal False ["sdist", "--list-sources", outFile]
269+
cabal KeepTHLoading ["sdist", "--list-sources", outFile]
270270
contents <- liftIO (readFile outFile)
271271
return (contents, cabalfp)
272272
where

0 commit comments

Comments
 (0)