forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrelude.hs
More file actions
378 lines (343 loc) · 12.5 KB
/
Prelude.hs
File metadata and controls
378 lines (343 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
module Stack.Prelude
( withSystemTempDir
, withKeepSystemTempDir
, sinkProcessStderrStdout
, sinkProcessStdout
, logProcessStderrStdout
, readProcessNull
, withProcessContext
, stripCR
, prompt
, promptPassword
, promptBool
, FirstTrue (..)
, fromFirstTrue
, defaultFirstTrue
, FirstFalse (..)
, fromFirstFalse
, defaultFirstFalse
, writeBinaryFileAtomic
, bugReport
, bugPrettyReport
, blankLine
, putUtf8Builder
, putBuilder
, ppException
, prettyThrowIO
, prettyThrowM
, mcons
, MungedPackageId (..)
, MungedPackageName (..)
, LibraryName (..)
, module X
-- * Re-exports from the rio-pretty print package
, HasStylesUpdate (..)
, HasTerm (..)
, Pretty (..)
, PrettyException (..)
, PrettyRawSnapshotLocation (..)
, StyleDoc
, Style (..)
, StyleSpec
, StylesUpdate (..)
, (<+>)
, align
, bulletedList
, debugBracket
, defaultStyles
, displayWithColor
, encloseSep
, fill
, fillSep
, foldr'
, fromPackageId
, fromPackageName
, flow
, hang
, hcat
, hsep
, indent
, line
, logLevelToStyle
, mkNarrativeList
, parens
, parseStylesUpdateFromString
, prettyDebug
, prettyDebugL
, prettyError
, prettyErrorL
, prettyGeneric
, prettyInfo
, prettyInfoL
, prettyInfoS
, prettyNote
, prettyNoteL
, prettyNoteS
, prettyWarn
, prettyWarnL
, prettyWarnNoIndent
, prettyWarnS
, punctuate
, sep
, softbreak
, softline
, spacedBulletedList
, string
, style
, vsep
) where
import Data.Monoid as X
( Any (..), Endo (..), First (..), Sum (..) )
import Data.Conduit as X ( ConduitM, runConduit, (.|) )
import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.List as CL
import Data.Conduit.Process.Typed
( byteStringInput, createSource, withLoggedProcess_ )
import Data.Foldable ( Foldable(foldr') )
import qualified Data.Text.IO as T
import Distribution.Types.LibraryName ( LibraryName (..) )
import Distribution.Types.MungedPackageId ( MungedPackageId (..) )
import Distribution.Types.MungedPackageName ( MungedPackageName (..) )
import Pantry as X hiding ( Package (..), loadSnapshot )
import Path as X
( Abs, Dir, File, Path, Rel, toFilePath )
import qualified Path.IO
import RIO as X
import RIO.File as X hiding ( writeBinaryFileAtomic )
import RIO.PrettyPrint
( HasStylesUpdate (..), HasTerm (..), Pretty (..), Style (..)
, StyleDoc, (<+>), align, blankLine, bulletedList
, debugBracket, displayWithColor, encloseSep, fill, fillSep
, flow, hang, hcat, hsep, indent, line, logLevelToStyle
, mkNarrativeList, parens, prettyDebug, prettyDebugL
, prettyError, prettyErrorL, prettyGeneric, prettyInfo
, prettyInfoL, prettyInfoS, prettyNote, prettyNoteL
, prettyNoteS, prettyWarn, prettyWarnL, prettyWarnNoIndent
, prettyWarnS, punctuate, sep, softbreak, softline
, spacedBulletedList, string, style, stylesUpdateL, useColorL
, vsep
)
import RIO.PrettyPrint.DefaultStyles (defaultStyles)
import RIO.PrettyPrint.PrettyException
( PrettyException (..), ppException, prettyThrowIO
, prettyThrowM
)
import RIO.PrettyPrint.StylesUpdate
( StylesUpdate (..), parseStylesUpdateFromString )
import RIO.PrettyPrint.Types ( StyleSpec )
import RIO.Process
( HasProcessContext (..), ProcessConfig, ProcessContext
, closed, getStderr, getStdout, proc, readProcess_, setStderr
, setStdin, setStdout, waitExitCode, withProcessWait_
, workingDirL
)
import qualified RIO.Text as T
import System.IO.Echo ( withoutInputEcho )
-- | Path version
withSystemTempDir :: MonadUnliftIO m => String -> (Path Abs Dir -> m a) -> m a
withSystemTempDir str inner = withRunInIO $ \run ->
Path.IO.withSystemTempDir str $ run . inner
-- | Like `withSystemTempDir`, but the temporary directory is not deleted.
withKeepSystemTempDir :: MonadUnliftIO m
=> String
-> (Path Abs Dir -> m a)
-> m a
withKeepSystemTempDir str inner = withRunInIO $ \run -> do
path <- Path.IO.getTempDir
dir <- Path.IO.createTempDir path str
run $ inner dir
-- | Consume the stdout and stderr of a process feeding strict 'ByteString's to
-- the consumers.
--
-- Throws a 'ReadProcessException' if unsuccessful in launching, or
-- 'ExitCodeException' if the process itself fails.
sinkProcessStderrStdout ::
forall e o env. (HasProcessContext env, HasLogFunc env, HasCallStack)
=> String -- ^ Command
-> [String] -- ^ Command line arguments
-> ConduitM ByteString Void (RIO env) e -- ^ Sink for stderr
-> ConduitM ByteString Void (RIO env) o -- ^ Sink for stdout
-> RIO env (e,o)
sinkProcessStderrStdout name args sinkStderr sinkStdout =
proc name args $ \pc0 -> do
let pc = setStdout createSource
$ setStderr createSource
-- Don't use closed, since that can break ./configure scripts
-- See https://github.com/commercialhaskell/stack/pull/4722
$ setStdin (byteStringInput "")
pc0
withProcessWait_ pc $ \p ->
(runConduit (getStderr p .| sinkStderr) `concurrently`
runConduit (getStdout p .| sinkStdout)) <* waitExitCode p
-- | Consume the stdout of a process feeding strict 'ByteString's to a consumer.
-- If the process fails, spits out stdout and stderr as error log
-- level. Should not be used for long-running processes or ones with
-- lots of output; for that use 'sinkProcessStderrStdout'.
--
-- Throws a 'ReadProcessException' if unsuccessful.
sinkProcessStdout ::
(HasProcessContext env, HasLogFunc env, HasCallStack)
=> String -- ^ Command
-> [String] -- ^ Command line arguments
-> ConduitM ByteString Void (RIO env) a -- ^ Sink for stdout
-> RIO env a
sinkProcessStdout name args sinkStdout =
proc name args $ \pc ->
withLoggedProcess_ (setStdin closed pc) $ \p -> runConcurrently
$ Concurrently (runConduit $ getStderr p .| CL.sinkNull)
*> Concurrently (runConduit $ getStdout p .| sinkStdout)
logProcessStderrStdout ::
(HasCallStack, HasProcessContext env, HasLogFunc env)
=> ProcessConfig stdin stdoutIgnored stderrIgnored
-> RIO env ()
logProcessStderrStdout pc = withLoggedProcess_ pc $ \p ->
let logLines = CB.lines .| CL.mapM_ (logInfo . displayBytesUtf8)
in runConcurrently
$ Concurrently (runConduit $ getStdout p .| logLines)
*> Concurrently (runConduit $ getStderr p .| logLines)
-- | Read from the process, ignoring any output.
--
-- Throws a 'ReadProcessException' exception if the process fails.
readProcessNull :: (HasProcessContext env, HasLogFunc env, HasCallStack)
=> String -- ^ Command
-> [String] -- ^ Command line arguments
-> RIO env ()
readProcessNull name args =
-- We want the output to appear in any exceptions, so we capture and drop it
void $ proc name args readProcess_
-- | Use the new 'ProcessContext', but retain the working directory
-- from the parent environment.
withProcessContext :: HasProcessContext env
=> ProcessContext
-> RIO env a
-> RIO env a
withProcessContext pcNew inner = do
pcOld <- view processContextL
let pcNew' = set workingDirL (view workingDirL pcOld) pcNew
local (set processContextL pcNew') inner
-- | Remove a trailing carriage pure if present
stripCR :: Text -> Text
stripCR = T.dropSuffix "\r"
-- | Prompt the user by sending text to stdout, and taking a line of
-- input from stdin.
prompt :: MonadIO m => Text -> m Text
prompt txt = liftIO $ do
T.putStr txt
hFlush stdout
T.getLine
-- | Prompt the user by sending text to stdout, and collecting a line
-- of input from stdin. While taking input from stdin, input echoing is
-- disabled, to hide passwords.
--
-- Based on code from cabal-install, Distribution.Client.Upload
promptPassword :: MonadIO m => Text -> m Text
promptPassword txt = liftIO $ do
T.putStr txt
hFlush stdout
-- Save/restore the terminal echoing status (no echoing for entering
-- the password).
password <- withoutInputEcho T.getLine
-- Since the user's newline is not echoed, one needs to be inserted.
T.putStrLn ""
pure password
-- | Prompt the user by sending text to stdout, and collecting a line of
-- input from stdin. If something other than "y" or "n" is entered, then
-- print a message indicating that "y" or "n" is expected, and ask
-- again.
promptBool :: MonadIO m => Text -> m Bool
promptBool txt = liftIO $ do
input <- prompt txt
case input of
"y" -> pure True
"n" -> pure False
_ -> do
T.putStrLn "Please press either 'y' or 'n', and then enter."
promptBool txt
-- | Like @First Bool@, but the default is @True@.
newtype FirstTrue
= FirstTrue { firstTrue :: Maybe Bool }
deriving (Eq, Ord, Show)
instance Semigroup FirstTrue where
FirstTrue (Just x) <> _ = FirstTrue (Just x)
FirstTrue Nothing <> x = x
instance Monoid FirstTrue where
mempty = FirstTrue Nothing
mappend = (<>)
-- | Get the 'Bool', defaulting to 'True'
fromFirstTrue :: FirstTrue -> Bool
fromFirstTrue = fromMaybe True . (.firstTrue)
-- | Helper for filling in default values
defaultFirstTrue :: FirstTrue -> Bool
defaultFirstTrue _ = True
-- | Like @First Bool@, but the default is @False@.
newtype FirstFalse
= FirstFalse { firstFalse :: Maybe Bool }
deriving (Eq, Ord, Show)
instance Semigroup FirstFalse where
FirstFalse (Just x) <> _ = FirstFalse (Just x)
FirstFalse Nothing <> x = x
instance Monoid FirstFalse where
mempty = FirstFalse Nothing
mappend = (<>)
-- | Get the 'Bool', defaulting to 'False'
fromFirstFalse :: FirstFalse -> Bool
fromFirstFalse = fromMaybe False . (.firstFalse)
-- | Helper for filling in default values
defaultFirstFalse :: FirstFalse -> Bool
defaultFirstFalse _ = False
-- | Write a @Builder@ to a file and atomically rename.
writeBinaryFileAtomic :: MonadIO m => Path absrel File -> Builder -> m ()
writeBinaryFileAtomic fp builder =
liftIO $
withBinaryFileAtomic (toFilePath fp) WriteMode (`hPutBuilder` builder)
newtype PrettyRawSnapshotLocation
= PrettyRawSnapshotLocation RawSnapshotLocation
instance Pretty PrettyRawSnapshotLocation where
pretty (PrettyRawSnapshotLocation (RSLCompiler compiler)) =
fromString $ T.unpack $ utf8BuilderToText $ display compiler
pretty (PrettyRawSnapshotLocation (RSLUrl url Nothing)) =
style Url (fromString $ T.unpack url)
pretty (PrettyRawSnapshotLocation (RSLUrl url (Just blob))) =
fillSep
[ style Url (fromString $ T.unpack url)
, parens $ fromString $ T.unpack $ utf8BuilderToText $ display blob
]
pretty (PrettyRawSnapshotLocation (RSLFilePath resolved)) =
style File (fromString $ show $ resolvedRelative resolved)
pretty (PrettyRawSnapshotLocation (RSLSynonym syn)) = fromString $ show syn
-- | Report a bug in Stack.
bugReport :: String -> String -> String
bugReport code msg =
"Error: " ++ code ++ "\n" ++
bugDeclaration ++ " " ++ msg ++ " " ++ bugRequest
-- | Report a pretty bug in Stack.
bugPrettyReport :: String -> StyleDoc -> StyleDoc
bugPrettyReport code msg =
"Error:" <+> fromString code
<> line
<> flow bugDeclaration <+> msg <+> flow bugRequest
-- | Bug declaration message.
bugDeclaration :: String
bugDeclaration = "The impossible happened!"
-- | Bug report message.
bugRequest :: String
bugRequest = "Please report this bug at Stack's repository."
-- | Maybe cons.
mcons :: Maybe a -> [a] -> [a]
mcons ma as = maybe as (:as) ma
-- | Write a 'Utf8Builder' to the standard output stream.
putUtf8Builder :: MonadIO m => Utf8Builder -> m ()
putUtf8Builder = putBuilder . getUtf8Builder
-- | Write a 'Builder' to the standard output stream.
putBuilder :: MonadIO m => Builder -> m ()
putBuilder = hPutBuilder stdout
-- | Convert a package identifier to a value of a string-like type.
fromPackageId :: IsString a => PackageIdentifier -> a
fromPackageId = fromString . packageIdentifierString
-- | Convert a package name to a value of a string-like type.
fromPackageName :: IsString a => PackageName -> a
fromPackageName = fromString . packageNameString