-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMain.gren
More file actions
990 lines (901 loc) · 47.4 KB
/
Main.gren
File metadata and controls
990 lines (901 loc) · 47.4 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
module Main exposing (main)
import Meta
import Node
import ChildProcess
import Terminal.Parser as CliParser
import Terminal.PackageInstall
import Terminal.PackageUninstall
import Terminal.PackageOutdated
import Terminal.PackageBump
import Terminal.PackageDiff
import Terminal.PackageValidate
import Terminal.Paths
import Terminal.Init
import Terminal.Run
import Terminal.Help
import Terminal.Repl
import Terminal.Report exposing (Report)
import Compiler.Backend
import Compiler.PackageName as PackageName exposing (PackageName)
import Compiler.Paths
import Compiler.Outline as Outline exposing (Outline)
import Compiler.ModuleName as ModuleName exposing (ModuleName)
import Git
import Init
import Stream
import Stream.Log
import Task exposing (Task)
import Dict exposing (Dict)
import FileSystem
import FileSystem.Path as Path exposing (Path)
import HttpClient
import Bytes exposing (Bytes)
import Terminal
import CLI.Parser
import CLI.PrettyPrinter as PP
import SemanticVersion exposing (SemanticVersion)
import Json.Encode as Json
import Stream.Extra
import Process
main : Node.Program Model Msg
main =
Node.defineProgram
{ init = init
, update = update
, subscriptions = \_model -> Sub.none
}
type alias Model =
{ args : Array String
, stdout : Stream.Writable Bytes
, stderr : Stream.Writable Bytes
, stdin : Stream.Readable Bytes
, useColor : Bool
, interactive : Bool
, cacheRoot : Compiler.Paths.CacheRoot
, backendPath : Path
, fsPermission : FileSystem.Permission
, cpPermission : ChildProcess.Permission
, httpPermission : HttpClient.Permission
-- TODO: Remove
, pathToString : Path -> String
}
init : Node.Environment -> Init.Task { model : Model, command : Cmd Msg }
init env =
Init.await FileSystem.initialize <| \fsPermission ->
Init.await ChildProcess.initialize <| \cpPermission ->
Init.await HttpClient.initialize <| \httpPermission ->
Init.await Terminal.initialize <| \terminalConfig ->
Init.awaitTask Node.getEnvironmentVariables <| \envVars ->
Init.awaitTask (FileSystem.homeDirectory fsPermission) <| \homeDir ->
let
userArgs =
Array.dropFirst 2 env.args
interactiveTerminal =
terminalConfig /= Nothing
useColor =
interactiveTerminal && not (Dict.member "NO_COLOR" envVars)
cacheRoot =
Compiler.Paths.cacheRoot env.platform envVars homeDir Meta.version
backendBinary =
Compiler.Paths.backendBinary env.platform cacheRoot
maybePaths =
when { platform = env.platform, override = Dict.get "GREN_BIN" envVars } is
{ override = Just overridePath, platform = Node.Win32 } ->
Just <|
{ remotePath = Nothing
, localPath = Path.fromWin32String overridePath
}
{ override = Just overridePath } ->
Just <|
{ remotePath = Nothing
, localPath = Path.fromPosixString overridePath
}
_ ->
Compiler.Backend.downloadUrl env.platform env.cpuArchitecture
|> Result.map
(\url ->
{ remotePath = Just url
, localPath = backendBinary
}
)
|> Result.toMaybe
in
Node.startProgram
{ model =
{ args = userArgs
, stdout = env.stdout
, stderr = env.stderr
, stdin = env.stdin
, interactive = interactiveTerminal
, useColor = useColor
, cacheRoot = cacheRoot
, backendPath =
when maybePaths is
Just paths ->
paths.localPath
Nothing ->
backendBinary
, fsPermission = fsPermission
, cpPermission = cpPermission
, httpPermission = httpPermission
, pathToString =
if env.platform == Node.Win32 then
Path.toWin32String
else
Path.toPosixString
}
, command =
when maybePaths is
Just paths ->
FileSystem.checkAccess fsPermission [] paths.localPath
|> Task.attempt
(\result ->
ExistanceChecked
{ maybeRemotePath = paths.remotePath
, localPath = paths.localPath
, existanceResult = result
}
)
Nothing ->
Stream.Log.line env.stderr "We currently don't support this platform/arch."
|> Task.andThen (\_ -> Node.exitWithCode 1)
|> Task.execute
}
type Msg
= ExistanceChecked
{ maybeRemotePath : Maybe String
, localPath : Path
, existanceResult : Result FileSystem.Error Path
}
| CompilerDownloaded
{ localPath : Path
, downloadResult : Result (HttpClient.Error Bytes) (HttpClient.Response Bytes)
}
| CompilerInstalled (Result FileSystem.Error Path)
| CompilerInitialized { quiet : Bool, backendStreams : ChildProcess.StreamIO, encodedCommand : Bytes }
| CompilerRan Int
| CompiledForRun { path : Path, exitCode : Int }
| RunStarted Process.Id
| RunExited Int
| RedirectTerminalIO
(Result
{ error : Stream.Error
, source : Stream.Readable Bytes
, target : Stream.Writable Bytes
}
{ source : Stream.Readable Bytes
, target : Stream.Writable Bytes
}
)
| RunCmd (Cmd Msg) -- TODO: remove
update : Msg -> Model -> { model : Model, command : Cmd Msg }
update msg model =
let
endWithError errorStr =
Stream.Log.line model.stderr errorStr
|> Task.andThen (\_ -> Node.exitWithCode 1)
|> Task.execute
in
{ model = model
, command =
when msg is
ExistanceChecked { maybeRemotePath, localPath, existanceResult = Err _ } ->
when maybeRemotePath is
Just remotePath ->
Stream.Log.line model.stdout ("Compiler not found at " ++ model.pathToString localPath ++ ". Downloading...")
|> Task.andThen (\_ -> Compiler.Backend.download model.httpPermission remotePath)
|> Task.attempt (\result -> CompilerDownloaded { localPath = localPath, downloadResult = result })
Nothing ->
endWithError ("Compiler not found at " ++ model.pathToString localPath)
ExistanceChecked { existanceResult = Ok compilerPath } ->
parseUserArgs model compilerPath
CompilerDownloaded { localPath, downloadResult = Err ((HttpClient.BadStatus res) as err) } ->
if res.statusCode == 302 then
when Dict.get "location" res.headers is
Just [ location ] ->
Compiler.Backend.download model.httpPermission location
|> Task.attempt (\result -> CompilerDownloaded { localPath = localPath, downloadResult = result })
_ ->
endWithError "Missing, or vague, 'location' header in 302 response from server."
else
endWithError (HttpClient.errorToString err)
CompilerDownloaded { downloadResult = Err err } ->
endWithError (HttpClient.errorToString err)
CompilerDownloaded { localPath, downloadResult = Ok res } ->
let
cacheFolder =
Path.parentPath localPath
|> Maybe.withDefault Path.empty
in
FileSystem.makeDirectory model.fsPermission { recursive = True } cacheFolder
|> Task.andThen (\_cacheFolder -> FileSystem.writeFile model.fsPermission res.data localPath)
|> Task.andThen
(FileSystem.changeAccess
model.fsPermission
{ owner = [ FileSystem.Read, FileSystem.Write, FileSystem.Execute ]
, group = [ FileSystem.Read, FileSystem.Execute ]
, others = [ FileSystem.Read, FileSystem.Execute ]
}
)
|> Task.andThen
(\binPath ->
Stream.Log.line model.stdout "Downloaded"
|> Task.map (\_ -> binPath)
)
|> Task.attempt CompilerInstalled
CompilerInstalled (Err fsErr) ->
endWithError ("Failed to install binary after download, due to error: " ++ FileSystem.errorToString fsErr)
CompilerInstalled (Ok compilerPath) ->
parseUserArgs model compilerPath
CompilerInitialized { backendStreams, encodedCommand, quiet } ->
Cmd.batch
[ Stream.write encodedCommand backendStreams.input
|> Task.map (\_ -> { source = model.stdin, target = backendStreams.input })
|> Task.mapError
(\err ->
{ error = err
, source = model.stdin
, target = backendStreams.input
}
)
|> Task.attempt RedirectTerminalIO
, if quiet then
Cmd.none
else
Task.succeed { source = backendStreams.output, target = model.stdout }
|> Task.attempt RedirectTerminalIO
, Task.succeed { source = backendStreams.error, target = model.stderr }
|> Task.attempt RedirectTerminalIO
]
CompilerRan exitCode ->
Task.execute <| Node.exitWithCode exitCode
CompiledForRun { path, exitCode } ->
if exitCode == 0 then
Terminal.Run.run
{ cpPermission = model.cpPermission
, fsPermission = model.fsPermission
, pathToString = model.pathToString
, path = path
, onInit = \{ processId } -> RunStarted processId
, onExit = RunExited
}
|> Task.onError
(\error ->
Terminal.Run.prettifyError error
|> Terminal.Report.toString (Terminal.Report.Terminal { useColor = model.useColor })
|> Stream.Log.line model.stderr
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)
)
|> Task.perform RunCmd
else
Task.execute <| Node.exitWithCode exitCode
RunStarted _ ->
Cmd.none
RunExited exitCode ->
Task.execute <| Node.exitWithCode exitCode
RedirectTerminalIO (Ok streams) ->
Stream.read streams.source
|> Task.andThen (\str -> Stream.write str streams.target)
|> Task.map (\_ -> streams)
|> Task.mapError
(\err ->
{ error = err
, source = streams.source
, target = streams.target
}
)
|> Task.attempt RedirectTerminalIO
RedirectTerminalIO (Err { error, source, target }) ->
when error is
Stream.Closed ->
Cmd.none
_ ->
Stream.Log.line model.stderr ("Something went wrong when communicating with compiler backend: " ++ Stream.errorToString error)
|> Task.andThen (\_ -> Node.exitWithCode 1)
|> Task.execute
RunCmd cmd ->
cmd
}
parseUserArgs : Model -> Path -> Cmd Msg
parseUserArgs model compilerPath =
let
printOpts =
{ useColor = model.useColor
}
endWithError prettyErr =
Terminal.Help.prettyPrint printOpts prettyErr
|> Stream.Log.string model.stderr
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)
endWithErrorString stringErr =
Stream.Log.string model.stderr stringErr
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)
endWithErrorReport outputType report =
Terminal.Report.toString outputType report
|> endWithErrorString
in
when CLI.Parser.run model.args CliParser.parser is
CLI.Parser.UnknownCommand commandName ->
endWithErrorString ("I don't recognize this command: " ++ commandName)
|> Task.perform RunCmd
CLI.Parser.BadFlags err ->
err
|> CLI.Parser.flagErrorPrettified
|> endWithError
|> Task.perform RunCmd
CLI.Parser.BadArguments err ->
err
|> CLI.Parser.argumentErrorPrettified
|> endWithError
|> Task.perform RunCmd
CLI.Parser.HelpText prettifiedText ->
prettifiedText
|> Terminal.Help.prettyPrint printOpts
|> Stream.Log.line model.stdout
|> Task.execute
CLI.Parser.Success parsedCommand ->
let
initWithCommand backendCommand =
\cpOpts ->
when cpOpts.streams is
Nothing ->
RunCmd Cmd.none
Just streams ->
CompilerInitialized
{ backendStreams = streams
, quiet = False
, encodedCommand =
Compiler.Backend.encodeCommand
{ interactiveSession = model.interactive
, pathToString = model.pathToString
}
backendCommand
}
in
when parsedCommand is
CliParser.Init flags ->
Terminal.Init.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, stdout = model.stdout
, stdin = model.stdin
, interactive = model.interactive
, useColor = model.useColor
, package = flags.package
, platform = flags.platform
}
|> Task.map (\_ -> Cmd.none)
|> Task.mapError Terminal.Init.prettifyError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.Repl flags ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.onError
(\err ->
when err is
Terminal.PackageInstall.ReadProjectOutlineNoProject _ ->
Terminal.Repl.setupGlobalReplProject
{ stdout = model.stdout
, stdin = model.stdin
, fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, useColor = model.useColor
}
|> Task.mapError Terminal.Repl.prettifyError
_ ->
Task.fail err
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
)
|> Task.andThen
(\projectOutline ->
Terminal.PackageInstall.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
, strictAppResolution = True
}
projectOutline
|> Task.mapError Terminal.PackageInstall.prettifyError
)
|> Task.map
(\resolved ->
Compiler.Backend.run
model.cpPermission
{ useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, onInit =
initWithCommand <|
Compiler.Backend.Repl
{ interpreter = flags.interpreter
, projectPath = resolved.projectPath
, outline = resolved.outline
, rootSources = resolved.rootSources
, dependencies = resolved.dependencies
}
, onComplete = CompilerRan
}
)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.Make flags ->
let
determineOutputStreamForResolve =
when flags.output is
Just Compiler.Backend.StdOut ->
-- We only want to print the actual generated JS
-- in this case, ignoring everything else
Terminal.Help.getNullStream
|> Task.map Stream.writable
_ ->
Task.succeed model.stdout
reportType =
when flags.report is
Just {} ->
Terminal.Report.Json
Nothing ->
Terminal.Report.Terminal printOpts
in
determineOutputStreamForResolve
|> Task.andThen (\output -> resolveProject { model | stdout = output })
|> Task.andThen (verifyMakeFlags model.fsPermission flags.entryPoints flags.output)
|> Task.map
(\resolved ->
Compiler.Backend.run
model.cpPermission
{ useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, onInit = initWithCommand <|
Compiler.Backend.Make
{ optimize = flags.optimize
, sourcemaps = flags.sourcemaps
, output = flags.output
, report = flags.report
, projectPath = resolved.projectPath
, entryPoints = flags.entryPoints
, outline = resolved.outline
, rootSources = resolved.rootSources
, dependencies = resolved.dependencies
}
, onComplete = CompilerRan
}
)
|> Task.onError (endWithErrorReport reportType)
|> Task.perform RunCmd
CliParser.Run opts ->
Terminal.Run.make
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, moduleName = opts.moduleName
, package = opts.package
, onBackendInitialized =
(\{ backendStreams, encodedCommand } ->
when backendStreams is
Nothing ->
RunCmd Cmd.none
Just streams ->
CompilerInitialized
{ backendStreams = streams
, encodedCommand = encodedCommand
, quiet = True
}
)
, onCompiled =
(\outputPath exitCode ->
CompiledForRun
{ path = outputPath
, exitCode = exitCode
}
)
}
|> Task.mapError Terminal.Run.prettifyError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.Docs flags ->
let
determineOutputStreamForResolve =
when flags.output is
Just Compiler.Backend.DocsStdOut ->
-- We only want to print the actual generated JS
-- in this case, ignoring everything else
Terminal.Help.getNullStream
|> Task.map Stream.writable
_ ->
Task.succeed model.stdout
reportType =
when flags.report is
Just {} ->
Terminal.Report.Json
Nothing ->
Terminal.Report.Terminal printOpts
in
determineOutputStreamForResolve
|> Task.andThen (\output -> resolveProject { model | stdout = output })
|> Task.map
(\resolved ->
Compiler.Backend.run
model.cpPermission
{ useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, onInit =
initWithCommand <|
Compiler.Backend.Docs
{ output = flags.output
, report = flags.report
, projectPath = resolved.projectPath
, outline = resolved.outline
, rootSources = resolved.rootSources
, dependencies = resolved.dependencies
}
, onComplete = CompilerRan
}
)
|> Task.onError (endWithErrorReport reportType)
|> Task.perform RunCmd
CliParser.PackageInstall Nothing ->
resolveProject model
|> Task.andThen
(\res ->
Terminal.PackageInstall.cleanPackageDirectory model.fsPermission res
|> Task.mapError (\_ -> Terminal.Report.empty)
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.PackageInstall (Just requestedPackage) ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\projectOutline ->
Terminal.PackageInstall.addPackage
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
, strictAppResolution = True
}
projectOutline
requestedPackage
|> Task.mapError Terminal.PackageInstall.prettifyAddPackageError
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.PackageUninstall packageName ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\projectOutline ->
Terminal.PackageUninstall.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
projectOutline
packageName
|> Task.mapError Terminal.PackageUninstall.prettifyError
|> Task.andThen
(\res ->
Terminal.PackageInstall.cleanPackageDirectory model.fsPermission res
|> Task.mapError (\_ -> Terminal.Report.empty)
)
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.PackageOutdated ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\projectOutline ->
Terminal.PackageOutdated.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
projectOutline
|> Task.mapError Terminal.PackageOutdated.prettifyError
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.PackageValidate ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\projectOutline ->
Terminal.PackageValidate.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
projectOutline
|> Task.mapError Terminal.PackageValidate.prettifyError
)
|> Task.map
(\backendFlags ->
Compiler.Backend.run
model.cpPermission
{ onInit =
initWithCommand <|
Compiler.Backend.PackageValidate backendFlags
, useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, onComplete = CompilerRan
}
)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.PackageBump ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\projectOutline ->
Terminal.PackageBump.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
projectOutline
|> Task.mapError Terminal.PackageBump.prettifyError
)
|> Task.map
(\backendFlags ->
Compiler.Backend.run
model.cpPermission
{ onInit =
initWithCommand <|
Compiler.Backend.PackageBump backendFlags
, useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, onComplete = CompilerRan
}
)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.PackageDiff args ->
let
diffTask =
when args is
CliParser.DiffLatest ->
Terminal.PackageDiff.runLocal
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
Nothing
|> Task.mapError Terminal.PackageDiff.prettifyLocalError
CliParser.DiffVersion version ->
Terminal.PackageDiff.runLocal
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
(Just version)
|> Task.mapError Terminal.PackageDiff.prettifyLocalError
CliParser.DiffRange { lower, upper } ->
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\{ outline } ->
when outline is
Outline.App _ ->
-- TODO
Terminal.PackageDiff.LocalCannotDiffApplication
|> Terminal.PackageDiff.prettifyLocalError
|> Task.fail
Outline.Pkg pkgOutline ->
Terminal.PackageDiff.runGlobal
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
pkgOutline.name
lower
upper
|> Task.mapError Terminal.PackageDiff.prettifyGlobalError
)
CliParser.DiffGlobal { package, lower, upper } ->
Terminal.PackageDiff.runGlobal
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
}
package
lower
upper
|> Task.mapError Terminal.PackageDiff.prettifyGlobalError
in
diffTask
|> Task.map
(\backendFlags ->
Compiler.Backend.run
model.cpPermission
{ onInit =
initWithCommand <|
Compiler.Backend.PackageDiff backendFlags
, useColor = model.useColor
, compilerPath = compilerPath
, pathToString = model.pathToString
, onComplete = CompilerRan
}
)
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd
CliParser.Paths opts ->
Terminal.Paths.run
{ fsPermission = model.fsPermission
, stdout = model.stdout
, pathToString = model.pathToString
, cacheRoot = model.cacheRoot
, backendPath = model.backendPath
}
opts
|> Task.map (\_ -> Cmd.none)
|> Task.mapError Terminal.Paths.errorToString
|> Task.onError endWithErrorString
|> Task.perform RunCmd
resolveProject : Model -> Task Report Terminal.PackageInstall.PackageResolution
resolveProject model =
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
|> Task.andThen
(\projectOutline ->
Terminal.PackageInstall.run
{ fsPermission = model.fsPermission
, cpPermission = model.cpPermission
, cacheRoot = model.cacheRoot
, interactive = model.interactive
, useColor = model.useColor
, stdout = model.stdout
, stdin = model.stdin
, strictAppResolution = True
}
projectOutline
|> Task.mapError Terminal.PackageInstall.prettifyError
)
-- TODO: Move to gren-lang/compiler-node
verifyMakeFlags : FileSystem.Permission -> Array ModuleName -> Maybe Compiler.Backend.MakeOutput -> Terminal.PackageInstall.PackageResolution -> Task Report Terminal.PackageInstall.PackageResolution
verifyMakeFlags fsPermission entryPoints maybeOutput resolution =
let
maybeOutputPath =
when maybeOutput is
Nothing ->
Nothing
Just Compiler.Backend.StdOut ->
Nothing
Just Compiler.Backend.DevNull ->
Nothing
Just (Compiler.Backend.Html strPath) ->
Just strPath
Just (Compiler.Backend.Js strPath) ->
Just strPath
Just (Compiler.Backend.Exe strPath) ->
Just strPath
compatibleOutputCheck =
when maybeOutputPath is
Nothing ->
Task.succeed {}
Just strPath ->
Node.getPlatform
|> Task.map
(\platform ->
when platform is
Node.Win32 ->
Path.fromWin32String strPath
_ ->
Path.fromPosixString strPath
)
|> Task.andThen
(\path ->
FileSystem.metadata fsPermission { resolveLink = False } path
|> Task.map Just
|> Task.onError
(\fsError ->
if FileSystem.errorIsNoSuchFileOrDirectory fsError then
Task.succeed Nothing
else
Task.fail <|
Terminal.Report.create
"CANNOT ACCESS OUTPUT PATH"
(Just path)
(PP.verticalBlock
[ PP.words
( "You have told me to compile this project to "
++ Path.toPosixString path
++ ", but I cannot access this path for some reason!"
)
]
)
)
|> Task.andThen
(\maybeMeta ->
when maybeMeta is
Nothing ->
Task.succeed {}
Just metadata ->
if metadata.entityType == FileSystem.Directory then
Terminal.Report.create
"OUTPUT PATH IS A DIRECTORY"
(Just path)
(PP.verticalBlock
[ PP.words
( "You have told me to compile this project to "
++ Path.toPosixString path
++ ", but this is a directory!"
)
]
)
|> Task.fail
else
Task.succeed {}
)
)
in
compatibleOutputCheck
|> Task.andThen
(\{} ->
when Array.findFirst (\moduleName -> not <| Dict.member (ModuleName.toString moduleName) resolution.rootSources) entryPoints is
Nothing ->
Task.succeed resolution
Just { value = moduleName } ->
Task.fail <|
Terminal.Report.create
"MODULE DOES NOT EXIST"
Nothing
(PP.verticalBlock
[ PP.words
( "You have told me to compile "
++ ModuleName.toString moduleName
++ ", but this module isn't defined in this project!"
)
]
)
)