@@ -23,14 +23,12 @@ defmodule JS2E do
23
23
alias JS2E.Parsers . { ParserWarning , ParserError }
24
24
alias JS2E.Printers.PrinterError
25
25
26
- @ spec main ( [ String . t ] ) :: :ok
26
+ @ spec main ( [ String . t ( ) ] ) :: :ok
27
27
def main ( args ) do
28
-
29
- { options , paths , errors } =
30
- OptionParser . parse ( args , switches: [ module_name: :string ] )
28
+ { options , paths , errors } = OptionParser . parse ( args , switches: [ module_name: :string ] )
31
29
32
30
if length ( paths ) == 0 do
33
- IO . puts @ moduledoc
31
+ IO . puts ( @ moduledoc )
34
32
exit ( :normal )
35
33
end
36
34
@@ -42,22 +40,21 @@ defmodule JS2E do
42
40
files = resolve_all_paths ( paths )
43
41
44
42
if length ( files ) == 0 do
45
- print_error ( "Error: Could not find any " <>
46
- "JSON files in path: #{ inspect paths } " )
43
+ print_error ( "Error: Could not find any " <> "JSON files in path: #{ inspect ( paths ) } " )
47
44
exit ( :no_files )
48
45
end
49
46
50
47
output_path = create_output_dir ( options )
51
48
JS2E . generate ( files , output_path )
52
49
end
53
50
54
- @ spec resolve_all_paths ( [ String . t ] ) :: [ Path . t ]
51
+ @ spec resolve_all_paths ( [ String . t ( ) ] ) :: [ Path . t ( ) ]
55
52
defp resolve_all_paths ( paths ) do
56
53
paths
57
54
|> Enum . filter ( & File . exists? / 1 )
58
- |> Enum . reduce ( [ ] , fn ( filename , files ) ->
55
+ |> Enum . reduce ( [ ] , fn filename , files ->
59
56
cond do
60
- File . dir? filename ->
57
+ File . dir? ( filename ) ->
61
58
walk_directory ( filename ) ++ files
62
59
63
60
String . ends_with? ( filename , ".json" ) ->
@@ -69,15 +66,15 @@ defmodule JS2E do
69
66
end )
70
67
end
71
68
72
- @ spec walk_directory ( String . t ) :: [ String . t ]
69
+ @ spec walk_directory ( String . t ( ) ) :: [ String . t ( ) ]
73
70
defp walk_directory ( dir ) do
74
71
dir
75
- |> File . ls!
72
+ |> File . ls! ( )
76
73
|> Enum . reduce ( [ ] , fn file , files ->
77
74
filename = "#{ dir } /#{ file } "
78
75
79
76
cond do
80
- File . dir? filename ->
77
+ File . dir? ( filename ) ->
81
78
walk_directory ( filename ) ++ files
82
79
83
80
String . ends_with? ( file , ".json" ) ->
@@ -89,58 +86,55 @@ defmodule JS2E do
89
86
end )
90
87
end
91
88
92
- @ spec create_output_dir ( list ) :: String . t
89
+ @ spec create_output_dir ( list ) :: String . t ( )
93
90
defp create_output_dir ( options ) do
94
-
95
- output_path = if Keyword . has_key? ( options , :module_name ) do
96
- Keyword . get ( options , :module_name )
97
- else
98
- "Domain"
99
- end
91
+ output_path =
92
+ if Keyword . has_key? ( options , :module_name ) do
93
+ Keyword . get ( options , :module_name )
94
+ else
95
+ "Domain"
96
+ end
100
97
101
98
output_path
102
99
|> File . mkdir_p! ( )
103
100
104
101
output_path
105
102
end
106
103
107
- @ spec generate ( [ String . t ] , String . t ) :: :ok
104
+ @ spec generate ( [ String . t ( ) ] , String . t ( ) ) :: :ok
108
105
def generate ( schema_paths , module_name ) do
109
-
110
- Logger . info "Parsing JSON schema files!"
106
+ Logger . info ( "Parsing JSON schema files!" )
111
107
parser_result = parse_schema_files ( schema_paths )
112
108
pretty_parser_warnings ( parser_result . warnings )
113
109
114
110
if length ( parser_result . errors ) > 0 do
115
111
pretty_parser_errors ( parser_result . errors )
116
-
117
112
else
118
- Logger . info "Converting to Elm code!"
113
+ Logger . info ( "Converting to Elm code!" )
119
114
printer_result = print_schemas ( parser_result . schema_dict , module_name )
120
115
121
116
if length ( printer_result . errors ) > 0 do
122
117
pretty_printer_errors ( printer_result . errors )
123
-
124
118
else
125
- Logger . info "Printing Elm code to file(s)!"
119
+ Logger . info ( "Printing Elm code to file(s)!" )
126
120
127
121
file_dict = printer_result . file_dict
122
+
128
123
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 } '"
124
+ { :ok , file } = File . open ( file_path , [ :write ] )
125
+ IO . binwrite ( file , file_content )
126
+ File . close ( file )
127
+ Logger . info ( "Created file '#{ file_path } '" )
133
128
end )
134
129
end
135
130
end
136
131
end
137
132
138
- @ spec pretty_parser_warnings ( [ ParserWarning . t ] ) :: :ok
133
+ @ spec pretty_parser_warnings ( [ ParserWarning . t ( ) ] ) :: :ok
139
134
defp pretty_parser_warnings ( warnings ) do
140
135
warnings
141
136
|> Enum . each ( fn { file_path , warnings } ->
142
137
if length ( warnings ) > 0 do
143
-
144
138
warning_header ( )
145
139
146
140
warnings
@@ -150,45 +144,50 @@ defmodule JS2E do
150
144
warning_type
151
145
|> to_string
152
146
|> String . replace ( "_" , " " )
153
- |> String . downcase
147
+ |> String . downcase ( )
154
148
155
- padding = String . duplicate ( "-" ,
156
- max ( 0 , 74 - String . length ( pretty_warning_type ) - String . length ( file_path ) ) )
149
+ padding =
150
+ String . duplicate (
151
+ "-" ,
152
+ max ( 0 , 74 - String . length ( pretty_warning_type ) - String . length ( file_path ) )
153
+ )
157
154
158
155
warnings
159
156
|> Enum . each ( fn warning ->
160
157
print_header ( "--- #{ pretty_warning_type } #{ padding } #{ file_path } \n " )
161
- IO . puts warning . message
158
+ IO . puts ( warning . message )
162
159
end )
163
160
end )
164
-
165
161
end
166
162
end )
163
+
167
164
:ok
168
165
end
169
166
170
- @ spec pretty_parser_errors ( [ ParserError . t ] ) :: :ok
167
+ @ spec pretty_parser_errors ( [ ParserError . t ( ) ] ) :: :ok
171
168
defp pretty_parser_errors ( errors ) do
172
169
errors
173
170
|> Enum . each ( fn { file_path , errors } ->
174
171
if length ( errors ) > 0 do
175
-
176
172
errors
177
173
|> Enum . group_by ( fn err -> err . error_type end )
178
174
|> Enum . each ( fn { error_type , errors } ->
179
175
pretty_error_type =
180
176
error_type
181
177
|> to_string
182
178
|> String . replace ( "_" , " " )
183
- |> String . upcase
179
+ |> String . upcase ( )
184
180
185
- padding = String . duplicate ( "-" ,
186
- max ( 0 , 74 - String . length ( pretty_error_type ) - String . length ( file_path ) ) )
181
+ padding =
182
+ String . duplicate (
183
+ "-" ,
184
+ max ( 0 , 74 - String . length ( pretty_error_type ) - String . length ( file_path ) )
185
+ )
187
186
188
187
errors
189
188
|> Enum . each ( fn error ->
190
189
print_header ( "--- #{ pretty_error_type } #{ padding } #{ file_path } \n " )
191
- IO . puts error . message
190
+ IO . puts ( error . message )
192
191
end )
193
192
end )
194
193
end
@@ -197,52 +196,49 @@ defmodule JS2E do
197
196
:ok
198
197
end
199
198
200
- @ spec pretty_printer_errors ( [ PrinterError . t ] ) :: :ok
199
+ @ spec pretty_printer_errors ( [ PrinterError . t ( ) ] ) :: :ok
201
200
defp pretty_printer_errors ( errors ) do
202
-
203
201
errors
204
202
|> Enum . each ( fn { file_path , errors } ->
205
203
if length ( errors ) > 0 do
206
-
207
204
errors
208
205
|> Enum . group_by ( fn err -> err . error_type end )
209
206
|> Enum . each ( fn { error_type , errors } ->
210
-
211
207
pretty_error_type =
212
208
error_type
213
209
|> to_string
214
210
|> String . replace ( "_" , " " )
215
- |> String . upcase
211
+ |> String . upcase ( )
216
212
217
- padding = String . duplicate ( "-" ,
218
- max ( 0 , 74 - String . length ( pretty_error_type ) - String . length ( file_path ) ) )
213
+ padding =
214
+ String . duplicate (
215
+ "-" ,
216
+ max ( 0 , 74 - String . length ( pretty_error_type ) - String . length ( file_path ) )
217
+ )
219
218
220
219
errors
221
220
|> Enum . each ( fn error ->
222
221
print_header ( "--- #{ pretty_error_type } #{ padding } #{ file_path } \n " )
223
- IO . puts error . message
222
+ IO . puts ( error . message )
224
223
end )
225
224
end )
226
-
227
225
end
228
226
end )
227
+
229
228
:ok
230
229
end
231
230
232
231
defp print_error ( str ) do
233
- IO . puts IO.ANSI . format ( [ :cyan , str ] )
232
+ IO . puts ( IO.ANSI . format ( [ :cyan , str ] ) )
234
233
end
235
234
236
235
defp print_header ( str ) do
237
- IO . puts IO.ANSI . format ( [ :cyan , str ] )
236
+ IO . puts ( IO.ANSI . format ( [ :cyan , str ] ) )
238
237
end
239
238
240
239
defp warning_header do
241
- header = String . duplicate ( "^" , 35 ) <>
242
- " WARNINGS " <>
243
- String . duplicate ( "^" , 35 )
240
+ header = String . duplicate ( "^" , 35 ) <> " WARNINGS " <> String . duplicate ( "^" , 35 )
244
241
245
- IO . puts IO.ANSI . format ( [ :yellow , header ] )
242
+ IO . puts ( IO.ANSI . format ( [ :yellow , header ] ) )
246
243
end
247
-
248
244
end
0 commit comments