Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocate check for context files generation into context generator. #6010

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions lib/mix/tasks/phx.gen.context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,18 @@ defmodule Mix.Tasks.Phx.Gen.Context do
end

@doc false
def files_to_be_generated(%Context{generate?: false}), do: []

def files_to_be_generated(%Context{schema: schema}) do
if schema.generate? do
Gen.Schema.files_to_be_generated(schema)
else
[]
end
Gen.Schema.files_to_be_generated(schema)
end

@doc false
def copy_new_files(%Context{generate?: false} = context, _, _), do: context

def copy_new_files(%Context{schema: schema} = context, paths, binding) do
if schema.generate?, do: Gen.Schema.copy_new_files(schema, paths, binding)
Gen.Schema.copy_new_files(schema, paths, binding)

inject_schema_access(context, paths, binding)
inject_tests(context, paths, binding)
inject_test_fixture(context, paths, binding)
Expand Down Expand Up @@ -294,12 +295,10 @@ defmodule Mix.Tasks.Phx.Gen.Context do
end

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

def print_shell_instructions(%Context{schema: schema}) do
if schema.generate? do
Gen.Schema.print_shell_instructions(schema)
else
:ok
end
Gen.Schema.print_shell_instructions(schema)
end

defp schema_access_template(%Context{schema: schema}) do
Expand Down
19 changes: 7 additions & 12 deletions lib/mix/tasks/phx.gen.html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule Mix.Tasks.Phx.Gen.Html do
{context, schema} = Gen.Context.build(args)
Gen.Context.prompt_for_code_injection(context)

binding = [context: context, schema: schema, inputs: inputs(schema)]
binding = [context: context, schema: schema]
paths = Mix.Phoenix.generator_paths()

prompt_for_conflicts(context)
Expand All @@ -120,18 +120,10 @@ defmodule Mix.Tasks.Phx.Gen.Html do
defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Kernel.++(context_files(context))
|> Kernel.++(Gen.Context.files_to_be_generated(context))
|> Mix.Phoenix.prompt_for_conflicts()
end

defp context_files(%Context{generate?: true} = context) do
Gen.Context.files_to_be_generated(context)
end

defp context_files(%Context{generate?: false}) do
[]
end

@doc false
def files_to_be_generated(%Context{schema: schema, context_app: context_app}) do
singular = schema.singular
Expand All @@ -157,9 +149,12 @@ defmodule Mix.Tasks.Phx.Gen.Html do

@doc false
def copy_new_files(%Context{} = context, paths, binding) do
Gen.Context.copy_new_files(context, paths, binding)

binding = Keyword.merge(binding, inputs: inputs(context.schema))
files = files_to_be_generated(context)
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.html", binding, files)
if context.generate?, do: Gen.Context.copy_new_files(context, paths, binding)

context
end

Expand All @@ -185,7 +180,7 @@ defmodule Mix.Tasks.Phx.Gen.Html do
""")
end

if context.generate?, do: Gen.Context.print_shell_instructions(context)
Gen.Context.print_shell_instructions(context)
end

@doc false
Expand Down
15 changes: 4 additions & 11 deletions lib/mix/tasks/phx.gen.json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,10 @@ defmodule Mix.Tasks.Phx.Gen.Json do
defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Kernel.++(context_files(context))
|> Kernel.++(Gen.Context.files_to_be_generated(context))
|> Mix.Phoenix.prompt_for_conflicts()
end

defp context_files(%Context{generate?: true} = context) do
Gen.Context.files_to_be_generated(context)
end

defp context_files(%Context{generate?: false}) do
[]
end

@doc false
def files_to_be_generated(%Context{schema: schema, context_app: context_app}) do
singular = schema.singular
Expand All @@ -154,9 +146,10 @@ defmodule Mix.Tasks.Phx.Gen.Json do

@doc false
def copy_new_files(%Context{} = context, paths, binding) do
Gen.Context.copy_new_files(context, paths, binding)

files = files_to_be_generated(context)
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.json", binding, files)
if context.generate?, do: Gen.Context.copy_new_files(context, paths, binding)

context
end
Expand All @@ -183,6 +176,6 @@ defmodule Mix.Tasks.Phx.Gen.Json do
""")
end

if context.generate?, do: Gen.Context.print_shell_instructions(context)
Gen.Context.print_shell_instructions(context)
end
end
19 changes: 6 additions & 13 deletions lib/mix/tasks/phx.gen.live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ defmodule Mix.Tasks.Phx.Gen.Live do
{context, schema} = Gen.Context.build(args)
Gen.Context.prompt_for_code_injection(context)

binding = [context: context, schema: schema, inputs: inputs(schema)]
binding = [context: context, schema: schema]
paths = Mix.Phoenix.generator_paths()

prompt_for_conflicts(context)
Expand All @@ -132,18 +132,10 @@ defmodule Mix.Tasks.Phx.Gen.Live do
defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Kernel.++(context_files(context))
|> Kernel.++(Gen.Context.files_to_be_generated(context))
|> Mix.Phoenix.prompt_for_conflicts()
end

defp context_files(%Context{generate?: true} = context) do
Gen.Context.files_to_be_generated(context)
end

defp context_files(%Context{generate?: false}) do
[]
end

defp files_to_be_generated(%Context{schema: schema, context_app: context_app}) do
web_prefix = Mix.Phoenix.web_path(context_app)
test_prefix = Mix.Phoenix.web_test_path(context_app)
Expand All @@ -163,18 +155,19 @@ defmodule Mix.Tasks.Phx.Gen.Live do
end

defp copy_new_files(%Context{} = context, binding, paths) do
files = files_to_be_generated(context)
Gen.Context.copy_new_files(context, paths, binding)

binding =
Keyword.merge(binding,
inputs: inputs(context.schema),
assigns: %{
web_namespace: inspect(context.web_module),
gettext: true
}
)

files = files_to_be_generated(context)
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.live", binding, files)
if context.generate?, do: Gen.Context.copy_new_files(context, paths, binding)

context
end
Expand Down Expand Up @@ -250,7 +243,7 @@ defmodule Mix.Tasks.Phx.Gen.Live do
""")
end

if context.generate?, do: Gen.Context.print_shell_instructions(context)
Gen.Context.print_shell_instructions(context)
maybe_print_upgrade_info()
end

Expand Down
65 changes: 47 additions & 18 deletions lib/mix/tasks/phx.gen.schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Mix.Tasks.Phx.Gen.Schema do

The following types are supported:

#{for attr <- Mix.Phoenix.Schema.valid_types(), do: " * `#{inspect attr}`\n"}
#{for attr <- Mix.Phoenix.Schema.valid_types(), do: " * `#{inspect(attr)}`\n"}
* `:datetime` - An alias for `:naive_datetime`

The generator also supports references, which we will properly
Expand Down Expand Up @@ -152,14 +152,24 @@ defmodule Mix.Tasks.Phx.Gen.Schema do

alias Mix.Phoenix.Schema

@switches [migration: :boolean, binary_id: :boolean, table: :string, web: :string,
context_app: :string, prefix: :string, repo: :string, migration_dir: :string,
primary_key: :string]
@switches [
migration: :boolean,
binary_id: :boolean,
table: :string,
web: :string,
context_app: :string,
prefix: :string,
repo: :string,
migration_dir: :string,
primary_key: :string
]

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

schema = build(args, [])
Expand Down Expand Up @@ -201,17 +211,26 @@ defmodule Mix.Tasks.Phx.Gen.Schema do
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

@doc false
def files_to_be_generated(%Schema{generate?: false}), do: []

def files_to_be_generated(%Schema{} = schema) do
[{:eex, "schema.ex", schema.file}]
end

@doc false
def copy_new_files(%Schema{context_app: ctx_app, repo: repo, opts: opts} = schema, paths, binding) do
def copy_new_files(%Schema{generate?: false} = schema, _, _), do: schema

def copy_new_files(
%Schema{context_app: ctx_app, repo: repo, opts: opts} = schema,
paths,
binding
) do
files = files_to_be_generated(schema)
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.schema", binding, files)

Expand All @@ -231,59 +250,69 @@ defmodule Mix.Tasks.Phx.Gen.Schema do

migration_path = Path.join(migration_dir, "#{timestamp()}_create_#{schema.table}.exs")

Mix.Phoenix.copy_from paths, "priv/templates/phx.gen.schema", binding, [
{:eex, "migration.exs", migration_path},
]
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.schema", binding, [
{:eex, "migration.exs", migration_path}
])
end

schema
end

@doc false
def print_shell_instructions(%Schema{generate?: false}), do: :ok

def print_shell_instructions(%Schema{} = schema) do
if schema.migration? do
Mix.shell().info """
Mix.shell().info("""

Remember to update your repository by running migrations:

$ mix ecto.migrate
"""
""")
end
end

@doc false
def validate_args!([schema, plural | _] = args, help) do
cond do
not Schema.valid?(schema) ->
help.raise_with_help "Expected the schema argument, #{inspect schema}, to be a valid module name"
help.raise_with_help(
"Expected the schema argument, #{inspect(schema)}, to be a valid module name"
)

String.contains?(plural, ":") or plural != Phoenix.Naming.underscore(plural) ->
help.raise_with_help "Expected the plural argument, #{inspect plural}, to be all lowercase using snake_case convention"
help.raise_with_help(
"Expected the plural argument, #{inspect(plural)}, to be all lowercase using snake_case convention"
)

true ->
args
end
end

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

@doc false
@spec raise_with_help(String.t) :: no_return()
@spec raise_with_help(String.t()) :: no_return()
def raise_with_help(msg) do
Mix.raise """
Mix.raise("""
#{msg}

mix phx.gen.schema expects both a module name and
the plural of the generated resource followed by
any number of attributes:

mix phx.gen.schema Blog.Post blog_posts title:string
"""
""")
end

defp timestamp do
{{y, m, d}, {hh, mm, ss}} = :calendar.universal_time()
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
end
defp pad(i) when i < 10, do: << ?0, ?0 + i >>

defp pad(i) when i < 10, do: <<?0, ?0 + i>>
defp pad(i), do: to_string(i)
end
Loading