@@ -18,11 +18,13 @@ defmodule JS2E do
18
18
"""
19
19
20
20
require Logger
21
- alias JS2E . { Parser , Printer }
21
+ import JS2E.Parser , only: [ parse_schema_files: 1 ]
22
+ import JS2E.Printer , only: [ print_schemas: 2 ]
23
+ alias JS2E.Parsers . { ParserWarning , ParserError }
24
+ alias JS2E.Printers.PrinterError
22
25
23
26
@ spec main ( [ String . t ] ) :: :ok
24
27
def main ( args ) do
25
- Logger . debug "Arguments: #{ inspect args } "
26
28
27
29
{ options , paths , errors } =
28
30
OptionParser . parse ( args , switches: [ module_name: :string ] )
@@ -33,23 +35,23 @@ defmodule JS2E do
33
35
end
34
36
35
37
if length ( errors ) > 0 do
36
- IO . puts "Error: Found one or more errors in the supplied options"
38
+ print_error ( "Error: Found one or more errors in the supplied options" )
37
39
exit ( { :unknown_arguments , errors } )
38
40
end
39
41
40
42
files = resolve_all_paths ( paths )
41
- Logger . debug "Files: #{ inspect files } "
42
43
43
44
if length ( files ) == 0 do
44
- IO . puts "Error: Could not find any JSON files in path: #{ inspect paths } "
45
+ print_error ( "Error: Could not find any " <>
46
+ "JSON files in path: #{ inspect paths } " )
45
47
exit ( :no_files )
46
48
end
47
49
48
50
output_path = create_output_dir ( options )
49
51
JS2E . generate ( files , output_path )
50
52
end
51
53
52
- @ spec resolve_all_paths ( [ String . t ] ) :: [ String . t ]
54
+ @ spec resolve_all_paths ( [ String . t ] ) :: [ Path . t ]
53
55
defp resolve_all_paths ( paths ) do
54
56
paths
55
57
|> Enum . filter ( & File . exists? / 1 )
@@ -103,18 +105,144 @@ defmodule JS2E do
103
105
end
104
106
105
107
@ spec generate ( [ String . t ] , String . t ) :: :ok
106
- def generate ( json_schema_paths , module_name ) do
108
+ def generate ( schema_paths , module_name ) do
107
109
108
- schema_dict = Parser . parse_schema_files ( json_schema_paths , module_name )
109
- printed_schemas = Printer . print_schemas ( schema_dict )
110
+ Logger . info "Parsing JSON schema files!"
111
+ parser_result = parse_schema_files ( schema_paths )
112
+ pretty_parser_warnings ( parser_result . warnings )
110
113
111
- printed_schemas
112
- |> Enum . each ( fn { file_path , file_content } ->
113
- { :ok , file } = File . open file_path , [ :write ]
114
- IO . binwrite file , file_content
115
- File . close file
116
- Logger . info "Created file: #{ file_path } "
114
+ if length ( parser_result . errors ) > 0 do
115
+ pretty_parser_errors ( parser_result . errors )
116
+
117
+ else
118
+ Logger . info "Converting to Elm code!"
119
+ printer_result = print_schemas ( parser_result . schema_dict , module_name )
120
+
121
+ if length ( printer_result . errors ) > 0 do
122
+ pretty_printer_errors ( printer_result . errors )
123
+
124
+ else
125
+ Logger . info "Printing Elm code to file(s)!"
126
+
127
+ file_dict = printer_result . file_dict
128
+ Enum . each ( file_dict , fn { file_path , file_content } ->
129
+ { :ok , file } = File . open file_path , [ :write ]
130
+ IO . binwrite file , file_content
131
+ File . close file
132
+ Logger . info "Created file '#{ file_path } '"
133
+ end )
134
+ end
135
+ end
136
+ end
137
+
138
+ @ spec pretty_parser_warnings ( [ ParserWarning . t ] ) :: :ok
139
+ defp pretty_parser_warnings ( warnings ) do
140
+ warnings
141
+ |> Enum . each ( fn { file_path , warnings } ->
142
+ if length ( warnings ) > 0 do
143
+
144
+ warning_header ( )
145
+
146
+ warnings
147
+ |> Enum . group_by ( fn warning -> warning . warning_type end )
148
+ |> Enum . each ( fn { warning_type , warnings } ->
149
+ pretty_warning_type =
150
+ warning_type
151
+ |> to_string
152
+ |> String . replace ( "_" , " " )
153
+ |> String . downcase
154
+
155
+ padding = String . duplicate ( "-" ,
156
+ 74 - String . length ( pretty_warning_type ) - String . length ( file_path ) )
157
+
158
+ warnings
159
+ |> Enum . each ( fn warning ->
160
+ print_header ( "--- #{ pretty_warning_type } #{ padding } #{ file_path } \n " )
161
+ IO . puts warning . message
162
+ end )
163
+ end )
164
+
165
+ end
166
+ end )
167
+ :ok
168
+ end
169
+
170
+ @ spec pretty_parser_errors ( [ ParserError . t ] ) :: :ok
171
+ defp pretty_parser_errors ( errors ) do
172
+ errors
173
+ |> Enum . each ( fn { file_path , errors } ->
174
+ if length ( errors ) > 0 do
175
+
176
+ errors
177
+ |> Enum . group_by ( fn err -> err . error_type end )
178
+ |> Enum . each ( fn { error_type , errors } ->
179
+ pretty_error_type =
180
+ error_type
181
+ |> to_string
182
+ |> String . replace ( "_" , " " )
183
+ |> String . upcase
184
+
185
+ padding = String . duplicate ( "-" ,
186
+ 74 - String . length ( pretty_error_type ) - String . length ( file_path ) )
187
+
188
+ errors
189
+ |> Enum . each ( fn error ->
190
+ print_header ( "--- #{ pretty_error_type } #{ padding } #{ file_path } \n " )
191
+ IO . puts error . message
192
+ end )
193
+ end )
194
+ end
195
+ end )
196
+
197
+ :ok
198
+ end
199
+
200
+ @ spec pretty_printer_errors ( [ PrinterError . t ] ) :: :ok
201
+ defp pretty_printer_errors ( errors ) do
202
+
203
+ errors
204
+ |> Enum . each ( fn { file_path , errors } ->
205
+ if length ( errors ) > 0 do
206
+
207
+ errors
208
+ |> Enum . group_by ( fn err -> err . error_type end )
209
+ |> Enum . each ( fn { error_type , errors } ->
210
+
211
+ pretty_error_type =
212
+ error_type
213
+ |> to_string
214
+ |> String . replace ( "_" , " " )
215
+ |> String . upcase
216
+
217
+ padding = String . duplicate ( "-" ,
218
+ 74 - String . length ( pretty_error_type ) - String . length ( file_path ) )
219
+
220
+ errors
221
+ |> Enum . each ( fn error ->
222
+ print_header ( "--- #{ pretty_error_type } #{ padding } #{ file_path } \n " )
223
+ IO . puts error . message
224
+ end )
225
+ end )
226
+
227
+ end
117
228
end )
229
+ :ok
230
+ end
231
+
232
+ defp print_error ( str ) do
233
+ IO . puts IO.ANSI . format ( [ :cyan , str ] )
234
+ end
235
+
236
+ defp print_header ( str ) do
237
+ IO . puts IO.ANSI . format ( [ :cyan , str ] )
238
+ end
239
+
240
+ defp warning_header do
241
+ header = String . duplicate ( "^" , 35 ) <>
242
+ " WARNINGS " <>
243
+ String . duplicate ( "^" , 35 )
244
+
245
+ IO . puts IO.ANSI . format ( [ :yellow , header ] )
118
246
end
119
247
120
248
end
0 commit comments