Skip to content

Commit 9d31e67

Browse files
committed
Clean up and simplify
1 parent f9e38db commit 9d31e67

File tree

4 files changed

+249
-27
lines changed

4 files changed

+249
-27
lines changed

elm-open-api-codegen/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,14 @@ code based on the `format` listed in your OpenAPI schema.
1111
See the [example of using this package][example], located in the `-cli`
1212
repository.
1313

14+
When running `elm-codegen`--or in any other way modifying this package's
15+
`codegen/elm.codegen.json` file--be sure to run this script afterward:
16+
17+
./scripts/sync-elm-codegen
18+
19+
This moves the `Gen.*` modules to the `src/` folder, which is necessary because
20+
the code in this project is part of an Elm _package_, and unlike Elm
21+
applications, Elm packages do not support specifying extra source directories.
22+
1423
[elm-open-api-cli]: https://github.com/wolfadex/elm-open-api-cli
1524
[example]: https://github.com/wolfadex/elm-open-api-cli/tree/main/example-using-api

elm-open-api-codegen/codegen/Generate.elm

Lines changed: 0 additions & 24 deletions
This file was deleted.

elm-open-api-codegen/scripts/sync-elm-codegen

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ set -e
66
rm -rf src/Gen 2>/dev/null
77
npx --no -- elm-codegen install
88
mv codegen/Gen src/
9-
10-
# Avoid clashing module name with root project.
11-
rm src/Gen/CodeGen/Generate.elm
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
port module Gen.CodeGen.Generate exposing
2+
( run, fromJson, fromText, fromDirectory, directoryDecoder
3+
, withFeedback, Error
4+
, File, Directory(..)
5+
, error, files, info
6+
)
7+
8+
{-|
9+
10+
11+
# Simple API
12+
13+
@docs run, fromJson, fromText, fromDirectory, directoryDecoder
14+
15+
16+
# With errors
17+
18+
@docs withFeedback, Error
19+
20+
21+
# Types
22+
23+
@docs File, Directory
24+
25+
26+
# Flexible API
27+
28+
This is the low level API if you need more control over the generation process proceeds.
29+
30+
@docs error, files, info
31+
32+
-}
33+
34+
import Dict exposing (Dict)
35+
import Json.Decode exposing (Decoder)
36+
import Json.Encode as Json
37+
38+
39+
{-| Produce a list of files without taking input.
40+
-}
41+
run : List File -> Program flags () ()
42+
run f =
43+
Platform.worker
44+
{ init =
45+
\_ ->
46+
( ()
47+
, files f
48+
)
49+
, update =
50+
\_ model ->
51+
( model, Cmd.none )
52+
, subscriptions = \_ -> Sub.none
53+
}
54+
55+
56+
{-| Given the flags, decoded from JSON, produce a list of files as output.
57+
-}
58+
fromJson : Decoder flags -> (flags -> List File) -> Program Json.Value () ()
59+
fromJson decoder f =
60+
Platform.worker
61+
{ init =
62+
\flags ->
63+
case Json.Decode.decodeValue decoder flags of
64+
Ok input ->
65+
( ()
66+
, files (f input)
67+
)
68+
69+
Err e ->
70+
( ()
71+
, error
72+
[ { title = "Error decoding flags"
73+
, description = Json.Decode.errorToString e
74+
}
75+
]
76+
)
77+
, update =
78+
\_ model ->
79+
( model, Cmd.none )
80+
, subscriptions = \_ -> Sub.none
81+
}
82+
83+
84+
{-| Given the file passed in with `--flags-from` as String, produce a list of files as output.
85+
-}
86+
fromText : (String -> List File) -> Program String () ()
87+
fromText f =
88+
Platform.worker
89+
{ init =
90+
\flags ->
91+
( ()
92+
, files (f flags)
93+
)
94+
, update =
95+
\_ model ->
96+
( model, Cmd.none )
97+
, subscriptions = \_ -> Sub.none
98+
}
99+
100+
101+
{-| Given the directory passed in with `--flags-from` as a file tree, produce a list of files as output.
102+
-}
103+
fromDirectory : (Directory -> List File) -> Program Json.Value () ()
104+
fromDirectory f =
105+
Platform.worker
106+
{ init =
107+
\flags ->
108+
case Json.Decode.decodeValue directoryDecoder flags of
109+
Ok input ->
110+
( ()
111+
, files (f input)
112+
)
113+
114+
Err e ->
115+
( ()
116+
, error
117+
[ { title = "Error decoding flags"
118+
, description = Json.Decode.errorToString e
119+
}
120+
]
121+
)
122+
, update =
123+
\_ model ->
124+
( model, Cmd.none )
125+
, subscriptions = \_ -> Sub.none
126+
}
127+
128+
129+
directoryDecoder : Decoder Directory
130+
directoryDecoder =
131+
Json.Decode.lazy
132+
(\_ ->
133+
Json.Decode.oneOf
134+
[ Json.Decode.map Ok Json.Decode.string
135+
, Json.Decode.map Err directoryDecoder
136+
]
137+
|> Json.Decode.dict
138+
|> Json.Decode.map
139+
(\entries ->
140+
entries
141+
|> Dict.toList
142+
|> List.foldl
143+
(\( name, entry ) ( dirAcc, fileAcc ) ->
144+
case entry of
145+
Ok file ->
146+
( dirAcc, ( name, file ) :: fileAcc )
147+
148+
Err directory ->
149+
( ( name, directory ) :: dirAcc, fileAcc )
150+
)
151+
( [], [] )
152+
|> (\( dirAcc, fileAcc ) ->
153+
Directory
154+
{ directories = Dict.fromList dirAcc
155+
, files = Dict.fromList fileAcc
156+
}
157+
)
158+
)
159+
)
160+
161+
162+
type Directory
163+
= Directory
164+
{ files : Dict String String
165+
, directories : Dict String Directory
166+
}
167+
168+
169+
type alias File =
170+
{ path : String
171+
, contents : String
172+
, warnings :
173+
List
174+
{ declaration : String
175+
, warning : String
176+
}
177+
}
178+
179+
180+
type alias Error =
181+
{ title : String
182+
, description : String
183+
}
184+
185+
186+
withFeedback :
187+
(flags
188+
-> Result (List Error) { info : List String, files : List File }
189+
)
190+
-> Program flags () ()
191+
withFeedback f =
192+
Platform.worker
193+
{ init =
194+
\flags ->
195+
( ()
196+
, case f flags of
197+
Ok result ->
198+
Cmd.batch <|
199+
List.map info result.info
200+
++ [ files result.files ]
201+
202+
Err errors ->
203+
error errors
204+
)
205+
, update =
206+
\_ model ->
207+
( model, Cmd.none )
208+
, subscriptions = \_ -> Sub.none
209+
}
210+
211+
212+
{-| Provide the list of files to be generated.
213+
These files will be generated and the script will end.
214+
-}
215+
files : List File -> Cmd msg
216+
files list =
217+
onSuccessSend list
218+
219+
220+
{-| Report an error. The script will end.
221+
-}
222+
error : List Error -> Cmd msg
223+
error errs =
224+
onFailureSend errs
225+
226+
227+
{-| Report some info. The script will continue to run.
228+
-}
229+
info : String -> Cmd msg
230+
info err =
231+
onInfoSend err
232+
233+
234+
port onSuccessSend : List File -> Cmd msg
235+
236+
237+
port onFailureSend : List Error -> Cmd msg
238+
239+
240+
port onInfoSend : String -> Cmd msg

0 commit comments

Comments
 (0)