Skip to content

Commit 9fb55d2

Browse files
committed
mix format phx.gen.context to avoid warnings
1 parent f640d1c commit 9fb55d2

File tree

1 file changed

+76
-37
lines changed

1 file changed

+76
-37
lines changed

lib/mix/tasks/phx.gen.context.ex

+76-37
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,26 @@ defmodule Mix.Tasks.Phx.Gen.Context do
7878
alias Mix.Phoenix.{Context, Schema}
7979
alias Mix.Tasks.Phx.Gen
8080

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+
]
8492

8593
@default_opts [schema: true, context: true]
8694

8795
@doc false
8896
def run(args) do
8997
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+
)
91101
end
92102

93103
{context, schema} = build(args)
@@ -120,14 +130,17 @@ defmodule Mix.Tasks.Phx.Gen.Context do
120130

121131
defp parse_opts(args) do
122132
{opts, parsed, invalid} = OptionParser.parse(args, switches: @switches)
133+
123134
merged_opts =
124135
@default_opts
125136
|> Keyword.merge(opts)
126137
|> put_context_app(opts[:context_app])
127138

128139
{merged_opts, parsed, invalid}
129140
end
141+
130142
defp put_context_app(opts, nil), do: opts
143+
131144
defp put_context_app(opts, string) do
132145
Keyword.put(opts, :context_app, String.to_atom(string))
133146
end
@@ -154,15 +167,21 @@ defmodule Mix.Tasks.Phx.Gen.Context do
154167
@doc false
155168
def ensure_context_file_exists(%Context{file: file} = context, paths, binding) do
156169
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+
)
158174
end
159175
end
160176

161177
defp inject_schema_access(%Context{file: file} = context, paths, binding) do
162178
ensure_context_file_exists(context, paths, binding)
163179

164180
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+
)
166185
|> inject_eex_before_final_end(file, binding)
167186
end
168187

@@ -173,7 +192,10 @@ defmodule Mix.Tasks.Phx.Gen.Context do
173192
@doc false
174193
def ensure_test_file_exists(%Context{test_file: test_file} = context, paths, binding) do
175194
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+
)
177199
end
178200
end
179201

@@ -186,13 +208,24 @@ defmodule Mix.Tasks.Phx.Gen.Context do
186208
end
187209

188210
@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
190216
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+
)
192221
end
193222
end
194223

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
196229
ensure_test_fixtures_file_exists(context, paths, binding)
197230

198231
paths
@@ -214,20 +247,14 @@ defmodule Mix.Tasks.Phx.Gen.Context do
214247
)
215248

216249
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+
""")
231258
end
232259
end
233260

@@ -237,11 +264,11 @@ defmodule Mix.Tasks.Phx.Gen.Context do
237264
string
238265
|> String.split("\n")
239266
|> 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
245272
end)
246273
end
247274

@@ -283,27 +310,38 @@ defmodule Mix.Tasks.Phx.Gen.Context do
283310
defp validate_args!([context, schema, _plural | _] = args, help) do
284311
cond do
285312
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+
287317
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+
289320
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+
291323
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+
293328
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+
295333
true ->
296334
args
297335
end
298336
end
299337

300338
defp validate_args!(_, help) do
301-
help.raise_with_help "Invalid arguments"
339+
help.raise_with_help("Invalid arguments")
302340
end
303341

304342
@doc false
305343
def raise_with_help(msg) do
306-
Mix.raise """
344+
Mix.raise("""
307345
#{msg}
308346
309347
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
319357
The context serves as the API boundary for the given resource.
320358
Multiple resources may belong to a context and a resource may be
321359
split over distinct contexts (such as Accounts.User and Payments.User).
322-
"""
360+
""")
323361
end
324362

325363
@doc false
326364
def prompt_for_code_injection(%Context{generate?: false}), do: :ok
365+
327366
def prompt_for_code_injection(%Context{} = context) do
328367
if Context.pre_existing?(context) && !merge_with_existing_context?(context) do
329368
System.halt()

0 commit comments

Comments
 (0)