@@ -78,16 +78,26 @@ defmodule Mix.Tasks.Phx.Gen.Context do
78
78
alias Mix.Phoenix . { Context , Schema }
79
79
alias Mix.Tasks.Phx.Gen
80
80
81
- @ switches [ binary_id: :boolean , table: :string , web: :string ,
82
- schema: :boolean , context: :boolean , context_app: :string ,
83
- merge_with_existing_context: :boolean , prefix: :string , live: :boolean ]
81
+ @ switches [
82
+ binary_id: :boolean ,
83
+ table: :string ,
84
+ web: :string ,
85
+ schema: :boolean ,
86
+ context: :boolean ,
87
+ context_app: :string ,
88
+ merge_with_existing_context: :boolean ,
89
+ prefix: :string ,
90
+ live: :boolean
91
+ ]
84
92
85
93
@ default_opts [ schema: true , context: true ]
86
94
87
95
@ doc false
88
96
def run ( args ) do
89
97
if Mix.Project . umbrella? ( ) do
90
- Mix . raise "mix phx.gen.context must be invoked from within your *_web application root directory"
98
+ Mix . raise (
99
+ "mix phx.gen.context must be invoked from within your *_web application root directory"
100
+ )
91
101
end
92
102
93
103
{ context , schema } = build ( args )
@@ -120,14 +130,17 @@ defmodule Mix.Tasks.Phx.Gen.Context do
120
130
121
131
defp parse_opts ( args ) do
122
132
{ opts , parsed , invalid } = OptionParser . parse ( args , switches: @ switches )
133
+
123
134
merged_opts =
124
135
@ default_opts
125
136
|> Keyword . merge ( opts )
126
137
|> put_context_app ( opts [ :context_app ] )
127
138
128
139
{ merged_opts , parsed , invalid }
129
140
end
141
+
130
142
defp put_context_app ( opts , nil ) , do: opts
143
+
131
144
defp put_context_app ( opts , string ) do
132
145
Keyword . put ( opts , :context_app , String . to_atom ( string ) )
133
146
end
@@ -154,15 +167,21 @@ defmodule Mix.Tasks.Phx.Gen.Context do
154
167
@ doc false
155
168
def ensure_context_file_exists ( % Context { file: file } = context , paths , binding ) do
156
169
unless Context . pre_existing? ( context ) do
157
- Mix.Generator . create_file ( file , Mix.Phoenix . eval_from ( paths , "priv/templates/phx.gen.context/context.ex" , binding ) )
170
+ Mix.Generator . create_file (
171
+ file ,
172
+ Mix.Phoenix . eval_from ( paths , "priv/templates/phx.gen.context/context.ex" , binding )
173
+ )
158
174
end
159
175
end
160
176
161
177
defp inject_schema_access ( % Context { file: file } = context , paths , binding ) do
162
178
ensure_context_file_exists ( context , paths , binding )
163
179
164
180
paths
165
- |> Mix.Phoenix . eval_from ( "priv/templates/phx.gen.context/#{ schema_access_template ( context ) } " , binding )
181
+ |> Mix.Phoenix . eval_from (
182
+ "priv/templates/phx.gen.context/#{ schema_access_template ( context ) } " ,
183
+ binding
184
+ )
166
185
|> inject_eex_before_final_end ( file , binding )
167
186
end
168
187
@@ -173,7 +192,10 @@ defmodule Mix.Tasks.Phx.Gen.Context do
173
192
@ doc false
174
193
def ensure_test_file_exists ( % Context { test_file: test_file } = context , paths , binding ) do
175
194
unless Context . pre_existing_tests? ( context ) do
176
- Mix.Generator . create_file ( test_file , Mix.Phoenix . eval_from ( paths , "priv/templates/phx.gen.context/context_test.exs" , binding ) )
195
+ Mix.Generator . create_file (
196
+ test_file ,
197
+ Mix.Phoenix . eval_from ( paths , "priv/templates/phx.gen.context/context_test.exs" , binding )
198
+ )
177
199
end
178
200
end
179
201
@@ -186,13 +208,24 @@ defmodule Mix.Tasks.Phx.Gen.Context do
186
208
end
187
209
188
210
@ doc false
189
- def ensure_test_fixtures_file_exists ( % Context { test_fixtures_file: test_fixtures_file } = context , paths , binding ) do
211
+ def ensure_test_fixtures_file_exists (
212
+ % Context { test_fixtures_file: test_fixtures_file } = context ,
213
+ paths ,
214
+ binding
215
+ ) do
190
216
unless Context . pre_existing_test_fixtures? ( context ) do
191
- Mix.Generator . create_file ( test_fixtures_file , Mix.Phoenix . eval_from ( paths , "priv/templates/phx.gen.context/fixtures_module.ex" , binding ) )
217
+ Mix.Generator . create_file (
218
+ test_fixtures_file ,
219
+ Mix.Phoenix . eval_from ( paths , "priv/templates/phx.gen.context/fixtures_module.ex" , binding )
220
+ )
192
221
end
193
222
end
194
223
195
- defp inject_test_fixture ( % Context { test_fixtures_file: test_fixtures_file } = context , paths , binding ) do
224
+ defp inject_test_fixture (
225
+ % Context { test_fixtures_file: test_fixtures_file } = context ,
226
+ paths ,
227
+ binding
228
+ ) do
196
229
ensure_test_fixtures_file_exists ( context , paths , binding )
197
230
198
231
paths
@@ -214,20 +247,14 @@ defmodule Mix.Tasks.Phx.Gen.Context do
214
247
)
215
248
216
249
if Enum . any? ( fixture_functions_needing_implementations ) do
217
- Mix . shell . info (
218
- """
219
-
220
- Some of the generated database columns are unique. Please provide
221
- unique implementations for the following fixture function(s) in
222
- #{ context . test_fixtures_file } :
223
-
224
- #{
225
- fixture_functions_needing_implementations
226
- |> Enum . map_join ( & indent ( & 1 , 2 ) )
227
- |> String . trim_trailing ( )
228
- }
229
- """
230
- )
250
+ Mix . shell ( ) . info ( """
251
+
252
+ Some of the generated database columns are unique. Please provide
253
+ unique implementations for the following fixture function(s) in
254
+ #{ context . test_fixtures_file } :
255
+
256
+ #{ fixture_functions_needing_implementations |> Enum . map_join ( & indent ( & 1 , 2 ) ) |> String . trim_trailing ( ) }
257
+ """ )
231
258
end
232
259
end
233
260
@@ -237,11 +264,11 @@ defmodule Mix.Tasks.Phx.Gen.Context do
237
264
string
238
265
|> String . split ( "\n " )
239
266
|> Enum . map_join ( fn line ->
240
- if String . trim ( line ) == "" do
241
- "\n "
242
- else
243
- indent_string <> line <> "\n "
244
- end
267
+ if String . trim ( line ) == "" do
268
+ "\n "
269
+ else
270
+ indent_string <> line <> "\n "
271
+ end
245
272
end )
246
273
end
247
274
@@ -283,27 +310,38 @@ defmodule Mix.Tasks.Phx.Gen.Context do
283
310
defp validate_args! ( [ context , schema , _plural | _ ] = args , help ) do
284
311
cond do
285
312
not Context . valid? ( context ) ->
286
- help . raise_with_help "Expected the context, #{ inspect context } , to be a valid module name"
313
+ help . raise_with_help (
314
+ "Expected the context, #{ inspect ( context ) } , to be a valid module name"
315
+ )
316
+
287
317
not Schema . valid? ( schema ) ->
288
- help . raise_with_help "Expected the schema, #{ inspect schema } , to be a valid module name"
318
+ help . raise_with_help ( "Expected the schema, #{ inspect ( schema ) } , to be a valid module name" )
319
+
289
320
context == schema ->
290
- help . raise_with_help "The context and schema should have different names"
321
+ help . raise_with_help ( "The context and schema should have different names" )
322
+
291
323
context == Mix.Phoenix . base ( ) ->
292
- help . raise_with_help "Cannot generate context #{ context } because it has the same name as the application"
324
+ help . raise_with_help (
325
+ "Cannot generate context #{ context } because it has the same name as the application"
326
+ )
327
+
293
328
schema == Mix.Phoenix . base ( ) ->
294
- help . raise_with_help "Cannot generate schema #{ schema } because it has the same name as the application"
329
+ help . raise_with_help (
330
+ "Cannot generate schema #{ schema } because it has the same name as the application"
331
+ )
332
+
295
333
true ->
296
334
args
297
335
end
298
336
end
299
337
300
338
defp validate_args! ( _ , help ) do
301
- help . raise_with_help "Invalid arguments"
339
+ help . raise_with_help ( "Invalid arguments" )
302
340
end
303
341
304
342
@ doc false
305
343
def raise_with_help ( msg ) do
306
- Mix . raise """
344
+ Mix . raise ( """
307
345
#{ msg }
308
346
309
347
mix phx.gen.html, phx.gen.json, phx.gen.live, and phx.gen.context
@@ -319,11 +357,12 @@ defmodule Mix.Tasks.Phx.Gen.Context do
319
357
The context serves as the API boundary for the given resource.
320
358
Multiple resources may belong to a context and a resource may be
321
359
split over distinct contexts (such as Accounts.User and Payments.User).
322
- """
360
+ """ )
323
361
end
324
362
325
363
@ doc false
326
364
def prompt_for_code_injection ( % Context { generate?: false } ) , do: :ok
365
+
327
366
def prompt_for_code_injection ( % Context { } = context ) do
328
367
if Context . pre_existing? ( context ) && ! merge_with_existing_context? ( context ) do
329
368
System . halt ( )
0 commit comments