Skip to content

Commit e4e8ec2

Browse files
authored
Merge pull request #336 from gren-lang/push-wylptozpxlyz
Implement --report=json flag for make and docs commands.
2 parents bba6f2c + bf15eb4 commit e4e8ec2

13 files changed

+231
-125
lines changed

src/Git.gren

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Task exposing (Task)
2020
import Terminal.Help as Help
2121
import CLI.PrettyPrinter as PP
2222
import Compiler.Paths
23+
import Terminal.Report as Report exposing (Report)
2324

2425

2526
type Error
@@ -29,11 +30,11 @@ type Error
2930
| FailedCommand { args : Array String, message : String }
3031

3132

32-
report : String -> String -> Error -> PP.Document
33+
report : String -> String -> Error -> Report
3334
report title context err =
3435
when err is
3536
NoVersions ->
36-
Help.report
37+
Report.create
3738
title
3839
Nothing
3940
( PP.verticalBlock
@@ -49,7 +50,7 @@ report title context err =
4950
)
5051

5152
NoSuchRepo ->
52-
Help.report
53+
Report.create
5354
title
5455
Nothing
5556
( PP.verticalBlock
@@ -64,7 +65,7 @@ report title context err =
6465
)
6566

6667
NoSuchRepoOrVersion vsn ->
67-
Help.report
68+
Report.create
6869
title
6970
Nothing
7071
( PP.verticalBlock
@@ -78,7 +79,7 @@ report title context err =
7879
)
7980

8081
FailedCommand { args, message } ->
81-
Help.report
82+
Report.create
8283
title
8384
Nothing
8485
( PP.verticalBlock

src/Main.gren

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Terminal.Init
1515
import Terminal.Run
1616
import Terminal.Help
1717
import Terminal.Repl
18+
import Terminal.Report exposing (Report)
1819
import Compiler.Backend
1920
import Compiler.PackageName as PackageName exposing (PackageName)
2021
import Compiler.Paths
@@ -293,7 +294,7 @@ update msg model =
293294
|> Task.onError
294295
(\error ->
295296
Terminal.Run.prettifyError error
296-
|> Terminal.Help.prettyPrint { useColor = model.useColor }
297+
|> Terminal.Report.toString (Terminal.Report.Terminal { useColor = model.useColor })
297298
|> Stream.Log.line model.stderr
298299
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)
299300
)
@@ -351,6 +352,10 @@ parseUserArgs model compilerPath =
351352
endWithErrorString stringErr =
352353
Stream.Log.string model.stderr stringErr
353354
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)
355+
356+
endWithErrorReport outputType report =
357+
Terminal.Report.toString outputType report
358+
|> endWithErrorString
354359
in
355360
when CLI.Parser.run model.args CliParser.parser is
356361
CLI.Parser.UnknownCommand commandName ->
@@ -410,7 +415,7 @@ parseUserArgs model compilerPath =
410415
}
411416
|> Task.map (\_ -> Cmd.none)
412417
|> Task.mapError Terminal.Init.prettifyError
413-
|> Task.onError endWithError
418+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
414419
|> Task.perform RunCmd
415420

416421
CliParser.Repl flags ->
@@ -467,7 +472,7 @@ parseUserArgs model compilerPath =
467472
, onComplete = CompilerRan
468473
}
469474
)
470-
|> Task.onError endWithError
475+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
471476
|> Task.perform RunCmd
472477

473478
CliParser.Make flags ->
@@ -482,6 +487,14 @@ parseUserArgs model compilerPath =
482487

483488
_ ->
484489
Task.succeed model.stdout
490+
491+
reportType =
492+
when flags.report is
493+
Just {} ->
494+
Terminal.Report.Json
495+
496+
Nothing ->
497+
Terminal.Report.Terminal printOpts
485498
in
486499
determineOutputStreamForResolve
487500
|> Task.andThen (\output -> resolveProject { model | stdout = output })
@@ -508,7 +521,7 @@ parseUserArgs model compilerPath =
508521
, onComplete = CompilerRan
509522
}
510523
)
511-
|> Task.onError endWithError
524+
|> Task.onError (endWithErrorReport reportType)
512525
|> Task.perform RunCmd
513526

514527
CliParser.Run opts ->
@@ -543,7 +556,7 @@ parseUserArgs model compilerPath =
543556
)
544557
}
545558
|> Task.mapError Terminal.Run.prettifyError
546-
|> Task.onError endWithError
559+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
547560
|> Task.perform RunCmd
548561

549562
CliParser.Docs flags ->
@@ -558,6 +571,14 @@ parseUserArgs model compilerPath =
558571

559572
_ ->
560573
Task.succeed model.stdout
574+
575+
reportType =
576+
when flags.report is
577+
Just {} ->
578+
Terminal.Report.Json
579+
580+
Nothing ->
581+
Terminal.Report.Terminal printOpts
561582
in
562583
determineOutputStreamForResolve
563584
|> Task.andThen (\output -> resolveProject { model | stdout = output })
@@ -581,18 +602,18 @@ parseUserArgs model compilerPath =
581602
, onComplete = CompilerRan
582603
}
583604
)
584-
|> Task.onError endWithError
605+
|> Task.onError (endWithErrorReport reportType)
585606
|> Task.perform RunCmd
586607

587608
CliParser.PackageInstall Nothing ->
588609
resolveProject model
589610
|> Task.andThen
590611
(\res ->
591612
Terminal.PackageInstall.cleanPackageDirectory model.fsPermission res
592-
|> Task.mapError (\_ -> PP.empty)
613+
|> Task.mapError (\_ -> Terminal.Report.empty)
593614
)
594615
|> Task.map (\_ -> Cmd.none)
595-
|> Task.onError endWithError
616+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
596617
|> Task.perform RunCmd
597618

598619
CliParser.PackageInstall (Just requestedPackage) ->
@@ -615,7 +636,7 @@ parseUserArgs model compilerPath =
615636
|> Task.mapError Terminal.PackageInstall.prettifyAddPackageError
616637
)
617638
|> Task.map (\_ -> Cmd.none)
618-
|> Task.onError endWithError
639+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
619640
|> Task.perform RunCmd
620641

621642
CliParser.PackageUninstall packageName ->
@@ -638,11 +659,11 @@ parseUserArgs model compilerPath =
638659
|> Task.andThen
639660
(\res ->
640661
Terminal.PackageInstall.cleanPackageDirectory model.fsPermission res
641-
|> Task.mapError (\_ -> PP.empty)
662+
|> Task.mapError (\_ -> Terminal.Report.empty)
642663
)
643664
)
644665
|> Task.map (\_ -> Cmd.none)
645-
|> Task.onError endWithError
666+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
646667
|> Task.perform RunCmd
647668

648669
CliParser.PackageOutdated ->
@@ -662,7 +683,7 @@ parseUserArgs model compilerPath =
662683
|> Task.mapError Terminal.PackageOutdated.prettifyError
663684
)
664685
|> Task.map (\_ -> Cmd.none)
665-
|> Task.onError endWithError
686+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
666687
|> Task.perform RunCmd
667688

668689
CliParser.PackageValidate ->
@@ -695,7 +716,7 @@ parseUserArgs model compilerPath =
695716
, onComplete = CompilerRan
696717
}
697718
)
698-
|> Task.onError endWithError
719+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
699720
|> Task.perform RunCmd
700721

701722
CliParser.PackageBump ->
@@ -728,7 +749,7 @@ parseUserArgs model compilerPath =
728749
, onComplete = CompilerRan
729750
}
730751
)
731-
|> Task.onError endWithError
752+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
732753
|> Task.perform RunCmd
733754

734755
CliParser.PackageDiff args ->
@@ -818,7 +839,7 @@ parseUserArgs model compilerPath =
818839
, onComplete = CompilerRan
819840
}
820841
)
821-
|> Task.onError endWithError
842+
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
822843
|> Task.perform RunCmd
823844

824845
CliParser.Paths opts ->
@@ -836,7 +857,7 @@ parseUserArgs model compilerPath =
836857
|> Task.perform RunCmd
837858

838859

839-
resolveProject : Model -> Task PP.Document Terminal.PackageInstall.PackageResolution
860+
resolveProject : Model -> Task Report Terminal.PackageInstall.PackageResolution
840861
resolveProject model =
841862
Terminal.PackageInstall.readProjectOutline model.fsPermission
842863
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
@@ -858,7 +879,7 @@ resolveProject model =
858879

859880

860881
-- TODO: Move to gren-lang/compiler-node
861-
verifyMakeFlags : FileSystem.Permission -> Array ModuleName -> Maybe Compiler.Backend.MakeOutput -> Terminal.PackageInstall.PackageResolution -> Task PP.Document Terminal.PackageInstall.PackageResolution
882+
verifyMakeFlags : FileSystem.Permission -> Array ModuleName -> Maybe Compiler.Backend.MakeOutput -> Terminal.PackageInstall.PackageResolution -> Task Report Terminal.PackageInstall.PackageResolution
862883
verifyMakeFlags fsPermission entryPoints maybeOutput resolution =
863884
let
864885
maybeOutputPath =
@@ -908,7 +929,7 @@ verifyMakeFlags fsPermission entryPoints maybeOutput resolution =
908929

909930
else
910931
Task.fail <|
911-
Terminal.Help.report
932+
Terminal.Report.create
912933
"CANNOT ACCESS OUTPUT PATH"
913934
(Just path)
914935
(PP.verticalBlock
@@ -928,7 +949,7 @@ verifyMakeFlags fsPermission entryPoints maybeOutput resolution =
928949

929950
Just metadata ->
930951
if metadata.entityType == FileSystem.Directory then
931-
Terminal.Help.report
952+
Terminal.Report.create
932953
"OUTPUT PATH IS A DIRECTORY"
933954
(Just path)
934955
(PP.verticalBlock
@@ -955,7 +976,7 @@ verifyMakeFlags fsPermission entryPoints maybeOutput resolution =
955976

956977
Just { value = moduleName } ->
957978
Task.fail <|
958-
Terminal.Help.report
979+
Terminal.Report.create
959980
"MODULE DOES NOT EXIST"
960981
Nothing
961982
(PP.verticalBlock

src/Terminal/Help.gren

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Terminal.Help exposing
22
( confirm
33
, prettyPrint
4-
, report
54
, makeLink
65
, getNullStream
76
)
@@ -73,42 +72,6 @@ prettyPrint { useColor } doc =
7372
)
7473

7574

76-
report : String -> Maybe Path -> PP.Document -> PP.Document
77-
report title maybePath message =
78-
let
79-
makeDashes n =
80-
String.repeat (max 1 (80 - n)) "-"
81-
82-
errorBarEnd =
83-
when maybePath is
84-
Nothing ->
85-
makeDashes (4 + String.unitLength title)
86-
87-
Just path ->
88-
-- TODO: platform toString
89-
let
90-
pathStr =
91-
Path.toPosixString path
92-
in
93-
makeDashes (5 + String.unitLength title + String.unitLength pathStr) ++ " " ++ pathStr
94-
95-
errorBar =
96-
PP.block
97-
[ PP.text "--"
98-
, PP.text title
99-
, PP.text errorBarEnd
100-
]
101-
|> PP.color PP.Cyan
102-
in
103-
PP.verticalBlock
104-
[ errorBar
105-
, PP.empty
106-
, message
107-
, PP.empty
108-
, PP.empty
109-
]
110-
111-
11275
makeLink : String -> PP.Document
11376
makeLink filename =
11477
PP.block

src/Terminal/Init.gren

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import SemanticVersionRange
2828
import Dict
2929
import Meta
3030
import Terminal.PackageInstall as PackageInstall
31+
import Terminal.Report as Report exposing (Report)
3132

3233

3334
type alias Config =
@@ -243,11 +244,11 @@ generateGrenJson config projectPath deps =
243244
)
244245

245246

246-
prettifyError : Error -> PP.Document
247+
prettifyError : Error -> Report
247248
prettifyError err =
248249
when err is
249250
AlreadyInitialized ->
250-
Terminal.Help.report
251+
Report.create
251252
"EXISTING PROJECT"
252253
Nothing
253254
(PP.verticalBlock
@@ -263,7 +264,7 @@ prettifyError err =
263264
)
264265

265266
FileSystemFailure fsErr ->
266-
Terminal.Help.report
267+
Report.create
267268
"FILESYSTEM ERROR"
268269
-- TODO: (Just <| FileSystem.errorPath fsErr)
269270
Nothing
@@ -281,7 +282,7 @@ prettifyError err =
281282
)
282283

283284
PromptError streamError ->
284-
Terminal.Help.report
285+
Report.create
285286
"STREAM ERROR"
286287
Nothing
287288
(PP.verticalBlock
@@ -304,7 +305,7 @@ prettifyError err =
304305
error
305306

306307
NoVersionFound packageName ->
307-
Terminal.Help.report
308+
Report.create
308309
"FAILED TO LOAD DEPENDENCIES"
309310
Nothing
310311
(PP.verticalBlock

0 commit comments

Comments
 (0)