diff --git a/lib/mix/tasks/phx.gen.context.ex b/lib/mix/tasks/phx.gen.context.ex index ef2fa5117a..2c8ae6f6a9 100644 --- a/lib/mix/tasks/phx.gen.context.ex +++ b/lib/mix/tasks/phx.gen.context.ex @@ -159,17 +159,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) @@ -311,12 +312,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 diff --git a/lib/mix/tasks/phx.gen.html.ex b/lib/mix/tasks/phx.gen.html.ex index 325cb3db09..31b9f21a3d 100644 --- a/lib/mix/tasks/phx.gen.html.ex +++ b/lib/mix/tasks/phx.gen.html.ex @@ -120,7 +120,6 @@ defmodule Mix.Tasks.Phx.Gen.Html do schema: schema, primary_key: schema.opts[:primary_key] || :id, scope: schema.scope, - inputs: inputs(schema), conn_scope: conn_scope, context_scope_prefix: context_scope_prefix ] @@ -137,18 +136,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 @@ -174,9 +165,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 @@ -202,7 +196,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 diff --git a/lib/mix/tasks/phx.gen.json.ex b/lib/mix/tasks/phx.gen.json.ex index 22f330c232..0d69dc003b 100644 --- a/lib/mix/tasks/phx.gen.json.ex +++ b/lib/mix/tasks/phx.gen.json.ex @@ -134,18 +134,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 @@ -166,9 +158,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 @@ -195,6 +188,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 diff --git a/lib/mix/tasks/phx.gen.live.ex b/lib/mix/tasks/phx.gen.live.ex index 07e3ceb6b1..0b3ee44421 100644 --- a/lib/mix/tasks/phx.gen.live.ex +++ b/lib/mix/tasks/phx.gen.live.ex @@ -133,7 +133,6 @@ defmodule Mix.Tasks.Phx.Gen.Live do schema: schema, primary_key: schema.opts[:primary_key] || :id, scope: schema.scope, - inputs: inputs(schema), socket_scope: socket_scope, context_scope_prefix: context_scope_prefix ] @@ -163,18 +162,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) @@ -194,18 +185,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 @@ -281,7 +273,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 diff --git a/lib/mix/tasks/phx.gen.schema.ex b/lib/mix/tasks/phx.gen.schema.ex index d58b51f1d7..8d4d39f991 100644 --- a/lib/mix/tasks/phx.gen.schema.ex +++ b/lib/mix/tasks/phx.gen.schema.ex @@ -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 @@ -159,7 +159,9 @@ defmodule Mix.Tasks.Phx.Gen.Schema do @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, []) @@ -207,17 +209,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) @@ -237,23 +248,25 @@ 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 @@ -261,21 +274,28 @@ defmodule Mix.Tasks.Phx.Gen.Schema do 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 @@ -283,13 +303,14 @@ defmodule Mix.Tasks.Phx.Gen.Schema do 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: <> defp pad(i), do: to_string(i) end