-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.hs
executable file
·373 lines (334 loc) · 12 KB
/
manage.hs
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
#!/usr/bin/env stack
{- stack
script
--ghc-options -threaded
--ghc-options -Wall
--ghc-options -Wcompat
--ghc-options -Wincomplete-record-updates
--ghc-options -Wincomplete-uni-patterns
--ghc-options -Wredundant-constraints
--resolver lts-13.18
--package ansi-terminal
--package async
--package directory
--package text
--package typed-process
-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{- | This script manages building our project. It is a stack script, so it
does not require a local stack project. You can run it with @stack script
manage.hs@ or @./manage.hs@.
To build the server & client for production, run @./manage.hs build@. To
watch the source files and automatically build & run the server and client,
run @./manage.hs watch@. To remove all the build artifacts, run
@./manage.hs clean@. When run without an argument, the script will make
a production build.
Note: @pscid@ will be installed with the client dependencies, but you need
@ghcid@ on your path. The simplest way is to run @stack install ghcid@
& make sure @~/.local/bin/@ is in your @$PATH@.
TODO:
* Review manage.hs script for SESE website & turn common code into a package.
* Currently pscid steals all input, see how to avoid this. Setting it's
stdin to closed or a pipe makes it not work :/ Open issues about that.
* What's needed to run ghcid as a "server" we can parse the output of
& print successes, warnings, & errors?
* ghcid & pscid like to clear the screen or output many newlines, clean
that up or make feature requests.
* Use a logging framework instead of directly printing.
* Use @nvm@ for client commands.
-}
module Main where
import Control.Concurrent.Async ( async
, waitAnyCancel
)
import Control.Monad ( void
, when
)
import Control.Monad.IO.Class ( MonadIO
, liftIO
)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import GHC.IO.Handle ( BufferMode(LineBuffering)
, Handle
, hSetBuffering
, hIsEOF
)
import GHC.IO.Handle.FD ( stdout
, stderr
)
import System.Environment ( getArgs )
import System.Console.ANSI ( Color(..)
, ColorIntensity(Vivid)
, ConsoleIntensity
( BoldIntensity
)
, ConsoleLayer(Foreground)
, SGR(..)
, setSGR
)
import System.Exit ( ExitCode(..)
, exitFailure
)
import System.Process.Typed ( Process
, createPipe
, setWorkingDir
, setStdout
, setStderr
, getStdout
, getStderr
, proc
, withProcess
, waitExitCode
)
main :: IO ()
main = do
hSetBuffering stdout LineBuffering
hSetBuffering stderr LineBuffering
getArgs >>= \case
[] -> watch
["build"] -> build
["watch"] -> watch
["clean"] -> clean
_ -> printLog Failed "Valid Commands are `watch`, `build` & `clean`"
-- Commands
-- | Make a production build.
build :: MonadIO m => m ()
build = clean >> initClient >> buildClient >> initServer >> buildServer
-- | Start the frontend & backend build servers.
watch :: MonadIO m => m ()
watch = do
initClient >> initServer
void . liftIO $ do
printLog Info "Starting Client Builder"
clientBuild <- async
$ run "yarn" ["run", "watch"] "./client/" clientOutput
printLog Info "Starting Client Dev Server"
clientServe <- async
$ run "yarn" ["run", "serve"] "./client/" clientOutput
printLog Info "Starting Server Builder"
server <- async $ run
"ghcid"
[ "--command"
, "stack ghci"
, "--run"
, "--reload=settings.yaml"
, "--restart=stack.yaml"
, "--restart=package.yaml"
, "--restart=default-settings.yaml"
, "--color=always"
]
"./server/"
serverOutput
waitAnyCancel [clientBuild, clientServe, server]
-- | Remove all the build artifacts & output.
clean :: MonadIO m => m ()
clean = do
printLog Info "Removing Client Files"
void $ run
"rm"
[ "-r"
, "-f"
, "output"
, "node_modules"
, "generated-docs"
, ".cache"
, "dist"
, ".spago"
]
"./client/"
clientOutput
printLog Info "Removing Server Files"
void $ run "rm" ["-r", "-f", ".stack-work"] "./server/" serverOutput
printLog Success "Project Cleaned"
-- Initialize / Build
-- | Install GHC & build the server dependencies.
initServer :: MonadIO m => m ()
initServer = do
installDependency "stack"
["setup", "--color", "always"]
"./server/"
serverOutput
"GHC"
installDependency
"stack"
[ "build"
, "--only-dependencies"
, "--haddock"
, "--ghc-options"
, "-O2"
, "--ghc-options"
, "-threaded"
, "--color"
, "always"
]
"./server/"
serverOutput
"Server Dependencies"
-- | Build the server for production.
buildServer :: MonadIO m => m ()
buildServer = do
printLog Info "Building Server"
run
"stack"
[ "build"
, "--pedantic"
, "--haddock"
, "--ghc-options"
, "-O2"
, "--ghc-options"
, "-threaded"
, "--color"
, "always"
]
"./server/"
serverOutput
>>= \case
ExitSuccess -> printLog Success "Server Build Completed"
ExitFailure _ ->
printLog Failed "Server Build Failed" >> liftIO exitFailure
-- | Install the NPM dependencies & the Purescript dependencies.
initClient :: MonadIO m => m ()
initClient = do
installDependency "yarn"
["install"]
"./client/"
clientOutput
"Javascript Dependencies"
installDependency "yarn"
["run", "spago", "install"]
"./client/"
clientOutput
"Purescript Dependencies"
-- | Build the client code using the package's @build@ script.
buildClient :: MonadIO m => m ()
buildClient = do
printLog Info "Building Client"
run "yarn" ["run", "build"] "./client/" clientOutput >>= \case
ExitSuccess -> printLog Success "Client Build Completed"
ExitFailure _ ->
printLog Failed "Client Build Failed" >> liftIO exitFailure
-- General Runners
-- | Install a dependency.
installDependency
:: MonadIO m
=> FilePath
-- ^ Command
-> [String]
-- ^ Arguments
-> String
-- ^ Working Directory
-> (Handle -> IO a)
-- ^ Output Handler
-> T.Text
-- ^ Dependency Description
-> m ()
installDependency cmd args workingDir outputLogger description = do
printLog Info $ "Installing " <> description
run cmd args workingDir outputLogger >>= printExitStatus
(description <> " Installed")
(description <> " Installation Failed")
-- | Run a command with arguments in a specific directory, printing out the
-- stdout & stderr with the given logger.
run
:: MonadIO m
=> FilePath
-- ^ The command
-> [String]
-- ^ It's arguments
-> String
-- ^ The directory to run the command in
-> (Handle -> IO a)
-- ^ The output logger
-> m ExitCode
-- ^ The result of running the command
run cmd args workingDir outputLogger =
let processConfig =
setWorkingDir workingDir
$ setStdout createPipe
$ setStderr createPipe
$ proc cmd args
in liftIO $ withProcess processConfig withOutputLogger
where
-- Run the output logger on the stderr & stdin.
withOutputLogger :: Process stdin Handle Handle -> IO ExitCode
withOutputLogger p = do
void $ outputLogger (getStdout p)
void $ outputLogger (getStderr p)
waitExitCode p
-- Logging
-- | Potential message types for logging output. These get assigned colors
-- and surrounded with brackets when we print them out.
data LogType
= Info
| Failed
| Success
| Client
| Server
deriving (Show, Read, Eq, Enum, Bounded)
-- | What's the maximum length of LogType's 'show' strings.
maxLogLength :: Int
maxLogLength =
succ $ maximum $ map (length . show) [minBound .. maxBound :: LogType]
-- | How much padding to add to a LogType to make it the 'maxLogLength'.
paddingLength :: LogType -> Int
paddingLength logType = maxLogLength - length (show logType)
-- | Print the LogType in brackets followed by a message.
printLog :: MonadIO m => LogType -> T.Text -> m ()
printLog logType message =
liftIO $ printInBrackets logType >> T.putStrLn message
-- | Print out the output of a server command.
serverOutput :: MonadIO m => Handle -> m ()
serverOutput = prependOutput Server
-- | Print out the output of a client command.
clientOutput :: MonadIO m => Handle -> m ()
clientOutput = prependOutput Client
-- | Print out each line of output from the handle prepended with
-- a LogType.
prependOutput :: MonadIO m => LogType -> Handle -> m ()
prependOutput logType handle =
liftIO
. void
. async
. whileM_ (not <$> hIsEOF handle)
$ T.hGetLine handle
>>= printLog logType
where
whileM_ :: Monad m => m Bool -> m () -> m ()
whileM_ predicateM actionM = do
predicate <- predicateM
when predicate $ actionM >> whileM_ predicateM actionM
-- | Print different text depending on a process's ExitCode.
printExitStatus
:: MonadIO m
=> T.Text
-- ^ The success message.
-> T.Text
-- ^ The failure message.
-> ExitCode
-> m ()
printExitStatus successText errorText = \case
ExitSuccess -> printLog Success successText
ExitFailure _ -> printLog Failed errorText
-- | Print the LogType in bold colors & surrounded by brackets. Does not
-- include a newline.
printInBrackets :: MonadIO m => LogType -> m ()
printInBrackets logType = liftIO $ do
T.putStr "["
setSGR [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid color]
T.putStr text
setSGR [Reset]
T.putStr $ "]" <> T.replicate padding " "
where
color :: Color
color = case logType of
Info -> Blue
Success -> Green
Failed -> Red
Client -> Cyan
Server -> Magenta
text :: T.Text
text = T.toUpper . T.pack $ show logType
padding :: Int
padding = paddingLength logType