Skip to content

Commit

Permalink
Check for file conflicts before copying. Closes phoenixframework#2193 (
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord authored Mar 17, 2017
1 parent e550b2b commit 751516a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
22 changes: 22 additions & 0 deletions lib/mix/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,28 @@ defmodule Mix.Phoenix do
Path.join(web_prefix(), rel_path)
end

@doc """
Prompts to continue if any files exist.
"""
def prompt_for_conflicts(generator_files) do
file_paths = Enum.map(generator_files, fn {_, _, path} -> path end)

case Enum.filter(file_paths, &File.exists?(&1)) do
[] -> :ok
conflicts ->
Mix.shell.info"""
The following files conflict with new files to be generated:
#{conflicts |> Enum.map(&" * #{&1}") |> Enum.join("\n")}
See the --web option to namespace similarly named resources
"""
unless Mix.shell.yes?("Proceeed with interactive overwrite?") do
System.halt()
end
end
end

defp web_module(base) do
if base |> to_string() |> String.ends_with?(".Web") do
Module.concat(base, nil)
Expand Down
20 changes: 18 additions & 2 deletions lib/mix/tasks/phx.gen.context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ defmodule Mix.Tasks.Phx.Gen.Context do
binding = [context: context, schema: schema]
paths = Mix.Phoenix.generator_paths()

prompt_for_conflicts(context)

context
|> copy_new_files(paths, binding)
|> print_shell_instructions()
end

defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Mix.Phoenix.prompt_for_conflicts()
end

def build(args) do
switches = [binary_id: :boolean, table: :string, web: :string]
{opts, parsed, _} = OptionParser.parse(args, switches: switches)
Expand All @@ -91,6 +99,10 @@ defmodule Mix.Tasks.Phx.Gen.Context do
{context, schema}
end

def files_to_be_generated(%Context{schema: schema}) do
Gen.Schema.files_to_be_generated(schema)
end

def copy_new_files(%Context{schema: schema} = context, paths, binding) do
Gen.Schema.copy_new_files(schema, paths, binding)
inject_schema_access(context, paths, binding)
Expand All @@ -100,7 +112,9 @@ defmodule Mix.Tasks.Phx.Gen.Context do
end

defp inject_schema_access(%Context{file: file} = context, paths, binding) do
unless Context.pre_existing?(context) do
if Context.pre_existing?(context) do
Mix.shell.info([:green, "* injecting ", :reset, Path.relative_to_cwd(file)])
else
Mix.Generator.create_file(file, Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/context.ex", binding))
end

Expand All @@ -114,7 +128,9 @@ defmodule Mix.Tasks.Phx.Gen.Context do
end

defp inject_tests(%Context{test_file: test_file} = context, paths, binding) do
unless Context.pre_existing_tests?(context) do
if Context.pre_existing_tests?(context) do
Mix.shell.info([:green, "* injecting ", :reset, Path.relative_to_cwd(test_file)])
else
Mix.Generator.create_file(test_file, Mix.Phoenix.eval_from(paths, "priv/templates/phx.gen.context/context_test.exs", binding))
end

Expand Down
19 changes: 16 additions & 3 deletions lib/mix/tasks/phx.gen.html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,25 @@ defmodule Mix.Tasks.Phx.Gen.Html do
binding = [context: context, schema: schema, inputs: inputs(schema)]
paths = Mix.Phoenix.generator_paths()

prompt_for_conflicts(context)

context
|> copy_new_files(paths, binding)
|> print_shell_instructions()
end

def copy_new_files(%Context{schema: schema} = context, paths, binding) do
defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Kernel.++(Gen.Context.files_to_be_generated(context))
|> Mix.Phoenix.prompt_for_conflicts()
end

def files_to_be_generated(%Context{schema: schema}) do
web_prefix = Mix.Phoenix.web_prefix()
test_prefix = Mix.Phoenix.test_prefix()
web_path = to_string(schema.web_path)

Mix.Phoenix.copy_from paths, "priv/templates/phx.gen.html", "", binding, [
[
{:eex, "controller.ex", Path.join([web_prefix, "controllers", web_path, "#{schema.singular}_controller.ex"])},
{:eex, "edit.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "edit.html.eex"])},
{:eex, "form.html.eex", Path.join([web_prefix, "templates", web_path, schema.singular, "form.html.eex"])},
Expand All @@ -104,7 +112,12 @@ defmodule Mix.Tasks.Phx.Gen.Html do
{:eex, "view.ex", Path.join([web_prefix, "views", web_path, "#{schema.singular}_view.ex"])},
{:eex, "controller_test.exs", Path.join([test_prefix,"controllers", web_path, "#{schema.singular}_controller_test.exs"])},
]
end


def copy_new_files(%Context{} = context, paths, binding) do
files = files_to_be_generated(context)
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.html", "", binding, files)
Gen.Context.copy_new_files(context, paths, binding)
context
end
Expand Down
18 changes: 16 additions & 2 deletions lib/mix/tasks/phx.gen.json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,39 @@ defmodule Mix.Tasks.Phx.Gen.Json do
binding = [context: context, schema: schema]
paths = Mix.Phoenix.generator_paths()

prompt_for_conflicts(context)

context
|> copy_new_files(paths, binding)
|> print_shell_instructions()
end

def copy_new_files(%Context{schema: schema} = context, paths, binding) do
defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Kernel.++(Gen.Context.files_to_be_generated(context))
|> Mix.Phoenix.prompt_for_conflicts()
end

def files_to_be_generated(%Context{schema: schema}) do
web_prefix = Mix.Phoenix.web_prefix()
test_prefix = Mix.Phoenix.test_prefix()
web_path = to_string(schema.web_path)

Mix.Phoenix.copy_from paths, "priv/templates/phx.gen.json", "", binding, [
[
{:eex, "controller.ex", Path.join([web_prefix, "controllers", web_path, "#{schema.singular}_controller.ex"])},
{:eex, "view.ex", Path.join([web_prefix, "views", web_path, "#{schema.singular}_view.ex"])},
{:eex, "controller_test.exs", Path.join([test_prefix, "controllers", web_path, "#{schema.singular}_controller_test.exs"])},
{:new_eex, "changeset_view.ex", Path.join([web_prefix, "views/changeset_view.ex"])},
{:new_eex, "fallback_controller.ex", Path.join([web_prefix, "controllers/fallback_controller.ex"])},
]
end

def copy_new_files(%Context{} = context, paths, binding) do
files = files_to_be_generated(context)
Mix.Phoenix.copy_from paths, "priv/templates/phx.gen.json", "", binding, files
Gen.Context.copy_new_files(context, paths, binding)

context
end

Expand Down
17 changes: 14 additions & 3 deletions lib/mix/tasks/phx.gen.schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ defmodule Mix.Tasks.Phx.Gen.Schema do
schema = build(args, [])
paths = Mix.Phoenix.generator_paths()

prompt_for_conflicts(schema)

schema
|> copy_new_files(paths, schema: schema)
|> print_shell_instructions()
end

defp prompt_for_conflicts(schema) do
schema
|> files_to_be_generated()
|> Mix.Phoenix.prompt_for_conflicts()
end

def build(args, parent_opts, help \\ __MODULE__) do
{schema_opts, parsed, _} = OptionParser.parse(args, switches: @switches)
[schema_name, plural | attrs] = validate_args!(parsed, help)
Expand All @@ -107,6 +115,10 @@ defmodule Mix.Tasks.Phx.Gen.Schema do
schema
end

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

def copy_new_files(%Schema{} = schema, paths, binding) do
migration =
schema.module
Expand All @@ -117,9 +129,8 @@ defmodule Mix.Tasks.Phx.Gen.Schema do
|> Phoenix.Naming.underscore()
|> String.replace("/", "_")

Mix.Phoenix.copy_from paths, "priv/templates/phx.gen.schema", "", binding, [
{:eex, "schema.ex", schema.file}
]
files = files_to_be_generated(schema)
Mix.Phoenix.copy_from(paths,"priv/templates/phx.gen.schema", "", binding, files)

if schema.migration? do
Mix.Phoenix.copy_from paths, "priv/templates/phx.gen.schema", "", binding, [
Expand Down

0 comments on commit 751516a

Please sign in to comment.