Skip to content

Commit

Permalink
mix format phx.gen.context to avoid warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 13, 2023
1 parent f640d1c commit 9fb55d2
Showing 1 changed file with 76 additions and 37 deletions.
113 changes: 76 additions & 37 deletions lib/mix/tasks/phx.gen.context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,26 @@ defmodule Mix.Tasks.Phx.Gen.Context do
alias Mix.Phoenix.{Context, Schema}
alias Mix.Tasks.Phx.Gen

@switches [binary_id: :boolean, table: :string, web: :string,
schema: :boolean, context: :boolean, context_app: :string,
merge_with_existing_context: :boolean, prefix: :string, live: :boolean]
@switches [
binary_id: :boolean,
table: :string,
web: :string,
schema: :boolean,
context: :boolean,
context_app: :string,
merge_with_existing_context: :boolean,
prefix: :string,
live: :boolean
]

@default_opts [schema: true, context: true]

@doc false
def run(args) do
if Mix.Project.umbrella?() do
Mix.raise "mix phx.gen.context must be invoked from within your *_web application root directory"
Mix.raise(
"mix phx.gen.context must be invoked from within your *_web application root directory"
)
end

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

defp parse_opts(args) do
{opts, parsed, invalid} = OptionParser.parse(args, switches: @switches)

merged_opts =
@default_opts
|> Keyword.merge(opts)
|> put_context_app(opts[:context_app])

{merged_opts, parsed, invalid}
end

defp put_context_app(opts, nil), do: opts

defp put_context_app(opts, string) do
Keyword.put(opts, :context_app, String.to_atom(string))
end
Expand All @@ -154,15 +167,21 @@ defmodule Mix.Tasks.Phx.Gen.Context do
@doc false
def ensure_context_file_exists(%Context{file: file} = context, paths, binding) do
unless Context.pre_existing?(context) do
Mix.Generator.create_file(file, Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/context.ex", binding))
Mix.Generator.create_file(
file,
Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/context.ex", binding)
)
end
end

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

paths
|> Mix.Phoenix.eval_from("priv/templates/phx.gen.context/#{schema_access_template(context)}", binding)
|> Mix.Phoenix.eval_from(
"priv/templates/phx.gen.context/#{schema_access_template(context)}",
binding
)
|> inject_eex_before_final_end(file, binding)
end

Expand All @@ -173,7 +192,10 @@ defmodule Mix.Tasks.Phx.Gen.Context do
@doc false
def ensure_test_file_exists(%Context{test_file: test_file} = context, paths, binding) do
unless Context.pre_existing_tests?(context) do
Mix.Generator.create_file(test_file, Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/context_test.exs", binding))
Mix.Generator.create_file(
test_file,
Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/context_test.exs", binding)
)
end
end

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

@doc false
def ensure_test_fixtures_file_exists(%Context{test_fixtures_file: test_fixtures_file} = context, paths, binding) do
def ensure_test_fixtures_file_exists(
%Context{test_fixtures_file: test_fixtures_file} = context,
paths,
binding
) do
unless Context.pre_existing_test_fixtures?(context) do
Mix.Generator.create_file(test_fixtures_file, Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/fixtures_module.ex", binding))
Mix.Generator.create_file(
test_fixtures_file,
Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/fixtures_module.ex", binding)
)
end
end

defp inject_test_fixture(%Context{test_fixtures_file: test_fixtures_file} = context, paths, binding) do
defp inject_test_fixture(
%Context{test_fixtures_file: test_fixtures_file} = context,
paths,
binding
) do
ensure_test_fixtures_file_exists(context, paths, binding)

paths
Expand All @@ -214,20 +247,14 @@ defmodule Mix.Tasks.Phx.Gen.Context do
)

if Enum.any?(fixture_functions_needing_implementations) do
Mix.shell.info(
"""
Some of the generated database columns are unique. Please provide
unique implementations for the following fixture function(s) in
#{context.test_fixtures_file}:
#{
fixture_functions_needing_implementations
|> Enum.map_join(&indent(&1, 2))
|> String.trim_trailing()
}
"""
)
Mix.shell().info("""
Some of the generated database columns are unique. Please provide
unique implementations for the following fixture function(s) in
#{context.test_fixtures_file}:
#{fixture_functions_needing_implementations |> Enum.map_join(&indent(&1, 2)) |> String.trim_trailing()}
""")
end
end

Expand All @@ -237,11 +264,11 @@ defmodule Mix.Tasks.Phx.Gen.Context do
string
|> String.split("\n")
|> Enum.map_join(fn line ->
if String.trim(line) == "" do
"\n"
else
indent_string <> line <> "\n"
end
if String.trim(line) == "" do
"\n"
else
indent_string <> line <> "\n"
end
end)
end

Expand Down Expand Up @@ -283,27 +310,38 @@ defmodule Mix.Tasks.Phx.Gen.Context do
defp validate_args!([context, schema, _plural | _] = args, help) do
cond do
not Context.valid?(context) ->
help.raise_with_help "Expected the context, #{inspect context}, to be a valid module name"
help.raise_with_help(
"Expected the context, #{inspect(context)}, to be a valid module name"
)

not Schema.valid?(schema) ->
help.raise_with_help "Expected the schema, #{inspect schema}, to be a valid module name"
help.raise_with_help("Expected the schema, #{inspect(schema)}, to be a valid module name")

context == schema ->
help.raise_with_help "The context and schema should have different names"
help.raise_with_help("The context and schema should have different names")

context == Mix.Phoenix.base() ->
help.raise_with_help "Cannot generate context #{context} because it has the same name as the application"
help.raise_with_help(
"Cannot generate context #{context} because it has the same name as the application"
)

schema == Mix.Phoenix.base() ->
help.raise_with_help "Cannot generate schema #{schema} because it has the same name as the application"
help.raise_with_help(
"Cannot generate schema #{schema} because it has the same name as the application"
)

true ->
args
end
end

defp validate_args!(_, help) do
help.raise_with_help "Invalid arguments"
help.raise_with_help("Invalid arguments")
end

@doc false
def raise_with_help(msg) do
Mix.raise """
Mix.raise("""
#{msg}
mix phx.gen.html, phx.gen.json, phx.gen.live, and phx.gen.context
Expand All @@ -319,11 +357,12 @@ defmodule Mix.Tasks.Phx.Gen.Context do
The context serves as the API boundary for the given resource.
Multiple resources may belong to a context and a resource may be
split over distinct contexts (such as Accounts.User and Payments.User).
"""
""")
end

@doc false
def prompt_for_code_injection(%Context{generate?: false}), do: :ok

def prompt_for_code_injection(%Context{} = context) do
if Context.pre_existing?(context) && !merge_with_existing_context?(context) do
System.halt()
Expand Down

0 comments on commit 9fb55d2

Please sign in to comment.