Skip to content

Commit 5ac10d0

Browse files
committed
Refactor Guida API to support guida.json and enhance error handling
1 parent b7f1679 commit 5ac10d0

File tree

6 files changed

+171
-58
lines changed

6 files changed

+171
-58
lines changed

lib/index.d.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
export interface GuidaConfig {
55
// XMLHttpRequest constructor for making HTTP requests.
6+
// @ts-ignore
67
XMLHttpRequest: typeof XMLHttpRequest;
78

89
// Write text or binary data to a path.
@@ -12,7 +13,7 @@ export interface GuidaConfig {
1213
readFile(path: string): Promise<string | ArrayBuffer | Uint8Array | Buffer | { buffer: ArrayBuffer }>;
1314

1415
// Read a directory and return a list of files.
15-
readDirectory(path: string): Promise<{ files: string[] }>;
16+
readDirectory(path: string): Promise<{ files: string[] | Buffer<ArrayBufferLike>[] }>;
1617

1718
// Create a directory.
1819
createDirectory(path: string): Promise<void>;
@@ -40,12 +41,42 @@ export interface MakeOptions {
4041
// nature of the results coming from the embedded Elm/runner process.
4142
export type GuidaResponse = any;
4243

43-
declare const _default: {
44-
make: (config: GuidaConfig, path: string, options?: MakeOptions) => Promise<GuidaResponse>;
45-
format: (config: GuidaConfig, content: string) => Promise<GuidaResponse>;
46-
install: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
47-
uninstall: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
48-
diagnostics: (config: GuidaConfig, args: { content: string } | { path: string }) => Promise<{ errors?: any }>;
49-
};
44+
export type Message = string | { bold: boolean, underline: boolean, color: null | string, string: string };
5045

51-
export default _default;
46+
export type Problem = {
47+
title: string;
48+
region: {
49+
start: { line: number; column: number; };
50+
end: { line: number; column: number; };
51+
};
52+
message: Message[];
53+
}
54+
55+
export type CompileError = {
56+
path: string;
57+
name: string;
58+
problems: Problem[];
59+
}
60+
61+
export type DiagnosticsResult =
62+
| null
63+
| {
64+
type: "content-error";
65+
error: Problem;
66+
}
67+
| {
68+
type: "compile-errors";
69+
errors: CompileError[];
70+
}
71+
| {
72+
type: "error";
73+
path: null | string;
74+
title: string;
75+
message: Message[];
76+
};
77+
78+
export declare const make: (config: GuidaConfig, path: string, options?: MakeOptions) => Promise<GuidaResponse>;
79+
export declare const format: (config: GuidaConfig, content: string) => Promise<GuidaResponse>;
80+
export declare const install: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
81+
export declare const uninstall: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
82+
export declare const diagnostics: (config: GuidaConfig, args: { content: string } | { path: string }) => Promise<DiagnosticsResult>;

src/API/Main.elm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ app =
7373
DiagnosticsArgs (DiagnosticsSourceContent src) ->
7474
case P.fromByteString (M.chompModule SV.Guida M.Application) E.ModuleBadEnd src of
7575
Ok _ ->
76-
exitWithResponse (Encode.object [])
76+
exitWithResponse Encode.null
7777

7878
Err err ->
7979
let
@@ -87,15 +87,20 @@ app =
8787
|> Decode.decodeString Decode.value
8888
|> Result.withDefault Encode.null
8989
in
90-
exitWithResponse (Encode.object [ ( "errors", Encode.list identity [ error ] ) ])
90+
exitWithResponse
91+
(Encode.object
92+
[ ( "type", Encode.string "content-error" )
93+
, ( "error", error )
94+
]
95+
)
9196

9297
DiagnosticsArgs (DiagnosticsSourcePath path) ->
9398
Make.run path (Make.Flags False False False)
9499
|> Task.bind
95100
(\result ->
96101
case result of
97102
Ok _ ->
98-
exitWithResponse (Encode.object [])
103+
exitWithResponse Encode.null
99104

100105
Err error ->
101106
exitWithResponse

src/Builder/Deps/Solver.elm

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,15 @@ getConstraints pkg vsn =
668668
home =
669669
Stuff.package cache pkg vsn
670670

671-
path : String
672-
path =
673-
home ++ "/elm.json"
671+
guidaPath : String
672+
guidaPath =
673+
home ++ "/guida.json"
674674
in
675-
-- FIXME extend this to guida!
676-
File.exists path
675+
File.exists guidaPath
677676
|> Task.bind
678-
(\outlineExists ->
679-
if outlineExists then
680-
File.readUtf8 path
677+
(\guidaOutlineExists ->
678+
if guidaOutlineExists then
679+
File.readUtf8 guidaPath
681680
|> Task.bind
682681
(\bytes ->
683682
case D.fromByteString constraintsDecoder bytes of
@@ -698,44 +697,103 @@ getConstraints pkg vsn =
698697
)
699698

700699
Err _ ->
701-
File.remove path
702-
|> Task.fmap (\_ -> ISErr (Exit.SolverBadCacheData pkg vsn))
700+
File.remove guidaPath
701+
|> Task.fmap (\_ -> ISErr (Exit.SolverBadCacheGuidaData pkg vsn))
703702
)
704703

705704
else
706-
case connection of
707-
Offline ->
708-
Task.pure (ISBack state)
709-
710-
Online manager ->
711-
Website.metadata pkg vsn "elm.json"
712-
|> Task.bind
713-
(\url ->
714-
Http.get manager url [] identity (Task.pure << Ok)
715-
|> Task.bind
716-
(\result ->
717-
case result of
718-
Err httpProblem ->
719-
Task.pure (ISErr (Exit.SolverBadHttp pkg vsn httpProblem))
720-
721-
Ok body ->
722-
case D.fromByteString constraintsDecoder body of
723-
Ok cs ->
724-
Utils.dirCreateDirectoryIfMissing True home
725-
|> Task.bind (\_ -> File.writeUtf8 path body)
726-
|> Task.fmap (\_ -> ISOk (toNewState cs) cs)
727-
728-
Err _ ->
729-
Task.pure (ISErr (Exit.SolverBadHttpData pkg vsn url))
730-
)
731-
)
705+
let
706+
elmPath : String
707+
elmPath =
708+
home ++ "/elm.json"
709+
in
710+
File.exists guidaPath
711+
|> Task.bind
712+
(\elmOutlineExists ->
713+
if elmOutlineExists then
714+
File.readUtf8 elmPath
715+
|> Task.bind
716+
(\bytes ->
717+
case D.fromByteString constraintsDecoder bytes of
718+
Ok cs ->
719+
case connection of
720+
Online _ ->
721+
Task.pure (ISOk (toNewState cs) cs)
722+
723+
Offline ->
724+
Utils.dirDoesDirectoryExist (Stuff.package cache pkg vsn ++ "/src")
725+
|> Task.fmap
726+
(\srcExists ->
727+
if srcExists then
728+
ISOk (toNewState cs) cs
729+
730+
else
731+
ISBack state
732+
)
733+
734+
Err _ ->
735+
File.remove elmPath
736+
|> Task.fmap (\_ -> ISErr (Exit.SolverBadCacheElmData pkg vsn))
737+
)
738+
739+
else
740+
case connection of
741+
Offline ->
742+
Task.pure (ISBack state)
743+
744+
Online manager ->
745+
Website.metadata pkg vsn "guida.json"
746+
|> Task.bind
747+
(\guidaUrl ->
748+
Http.get manager guidaUrl [] identity (Task.pure << Ok)
749+
|> Task.bind
750+
(\guidaResult ->
751+
case guidaResult of
752+
Err _ ->
753+
Website.metadata pkg vsn "elm.json"
754+
|> Task.bind
755+
(\elmUrl ->
756+
Http.get manager elmUrl [] identity (Task.pure << Ok)
757+
|> Task.bind
758+
(\elmResult ->
759+
case elmResult of
760+
Err elmHttpProblem ->
761+
Task.pure (ISErr (Exit.SolverBadHttp pkg vsn elmHttpProblem))
762+
763+
Ok body ->
764+
case D.fromByteString constraintsDecoder body of
765+
Ok cs ->
766+
Utils.dirCreateDirectoryIfMissing True home
767+
|> Task.bind (\_ -> File.writeUtf8 elmPath body)
768+
|> Task.fmap (\_ -> ISOk (toNewState cs) cs)
769+
770+
Err _ ->
771+
Task.pure (ISErr (Exit.SolverBadHttpElmData pkg vsn elmUrl))
772+
)
773+
)
774+
775+
Ok body ->
776+
case D.fromByteString constraintsDecoder body of
777+
Ok cs ->
778+
Utils.dirCreateDirectoryIfMissing True home
779+
|> Task.bind (\_ -> File.writeUtf8 guidaPath body)
780+
|> Task.fmap (\_ -> ISOk (toNewState cs) cs)
781+
782+
Err _ ->
783+
Task.pure (ISErr (Exit.SolverBadHttpGuidaData pkg vsn guidaUrl))
784+
)
785+
)
786+
)
732787
)
733788

734789

735790
constraintsDecoder : D.Decoder () Constraints
736791
constraintsDecoder =
737-
-- FIXME extend this to guida!
738-
D.mapError (\_ -> ()) Outline.elmDecoder
792+
D.oneOf
793+
[ Outline.guidaDecoder
794+
, Outline.elmDecoder
795+
]
796+
|> D.mapError (\_ -> ())
739797
|> D.bind
740798
(\outline ->
741799
case outline of

src/Builder/Reporting/Exit.elm

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,23 +1760,42 @@ uninstallToReport exit =
17601760

17611761

17621762
type Solver
1763-
= SolverBadCacheData Pkg.Name V.Version
1764-
| SolverBadHttpData Pkg.Name V.Version String
1763+
= SolverBadCacheGuidaData Pkg.Name V.Version
1764+
| SolverBadCacheElmData Pkg.Name V.Version
1765+
| SolverBadHttpGuidaData Pkg.Name V.Version String
1766+
| SolverBadHttpElmData Pkg.Name V.Version String
17651767
| SolverBadHttp Pkg.Name V.Version Http.Error
17661768

17671769

17681770
toSolverReport : Solver -> Help.Report
17691771
toSolverReport problem =
17701772
case problem of
1771-
SolverBadCacheData pkg vsn ->
1773+
SolverBadCacheGuidaData pkg vsn ->
1774+
Help.report "PROBLEM SOLVING PACKAGE CONSTRAINTS"
1775+
Nothing
1776+
("I need the guida.json of " ++ Pkg.toChars pkg ++ " " ++ V.toChars vsn ++ " to help me search for a set of compatible packages. I had it cached locally, but it looks like the file was corrupted!")
1777+
[ D.reflow <|
1778+
"I deleted the cached version, so the next run should download a fresh copy. Hopefully that will get you unstuck, but it will not resolve the root problem if a 3rd party tool is modifing cached files for some reason."
1779+
]
1780+
1781+
SolverBadCacheElmData pkg vsn ->
17721782
Help.report "PROBLEM SOLVING PACKAGE CONSTRAINTS"
17731783
Nothing
17741784
("I need the elm.json of " ++ Pkg.toChars pkg ++ " " ++ V.toChars vsn ++ " to help me search for a set of compatible packages. I had it cached locally, but it looks like the file was corrupted!")
17751785
[ D.reflow <|
17761786
"I deleted the cached version, so the next run should download a fresh copy. Hopefully that will get you unstuck, but it will not resolve the root problem if a 3rd party tool is modifing cached files for some reason."
17771787
]
17781788

1779-
SolverBadHttpData pkg vsn url ->
1789+
SolverBadHttpGuidaData pkg vsn url ->
1790+
Help.report "PROBLEM SOLVING PACKAGE CONSTRAINTS"
1791+
Nothing
1792+
("I need the guida.json of " ++ Pkg.toChars pkg ++ " " ++ V.toChars vsn ++ " to help me search for a set of compatible packages, but I ran into corrupted information from:")
1793+
[ D.indent 4 <| D.dullyellow <| D.fromChars url
1794+
, D.reflow <|
1795+
"Is something weird with your internet connection. We have gotten reports that schools, businesses, airports, etc. sometimes intercept requests and add things to the body or change its contents entirely. Could that be the problem?"
1796+
]
1797+
1798+
SolverBadHttpElmData pkg vsn url ->
17801799
Help.report "PROBLEM SOLVING PACKAGE CONSTRAINTS"
17811800
Nothing
17821801
("I need the elm.json of " ++ Pkg.toChars pkg ++ " " ++ V.toChars vsn ++ " to help me search for a set of compatible packages, but I ran into corrupted information from:")
@@ -1787,7 +1806,7 @@ toSolverReport problem =
17871806

17881807
SolverBadHttp pkg vsn httpError ->
17891808
toHttpErrorReport "PROBLEM SOLVING PACKAGE CONSTRAINTS" httpError <|
1790-
"I need the elm.json of "
1809+
"I need the guida.json (or elm.json) of "
17911810
++ Pkg.toChars pkg
17921811
++ " "
17931812
++ V.toChars vsn

src/Compiler/Generate/Html.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ sandwich moduleName javascript =
2929
try {
3030
""" ++ javascript ++ """
3131
32-
var app = Guida.""" ++ moduleName ++ """.init({ node: document.getElementById("guida") });
32+
var app = Elm.""" ++ moduleName ++ """.init({ node: document.getElementById("guida") });
3333
}
3434
catch (e)
3535
{

try/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ const config = {
1919

2020
window.addEventListener("load", async () => {
2121
await fs.createDirectory("root/src");
22-
await fs.writeFile("root/elm.json", `{
22+
await fs.writeFile("root/guida.json", `{
2323
"type": "application",
2424
"source-directories": [
2525
"src"
2626
],
27-
"elm-version": "0.19.1",
27+
"guida-version": "1.0.0",
2828
"dependencies": {
2929
"direct": {
3030
"elm/browser": "1.0.2",

0 commit comments

Comments
 (0)