Skip to content

Commit

Permalink
Merge pull request #145 from danschultzer/warn-no-templates-generation
Browse files Browse the repository at this point in the history
Warn if no template files will be generated for templates
  • Loading branch information
danschultzer committed Mar 17, 2019
2 parents e79ac7e + 6c0dccf commit 51d9630
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v1.0.5 (TBA)

* Added `extension_messages/1` to extension controllers and callbacks
* Improved feedback for when no templates are generated for an extension with `mix pow.extension.phoenix.gen.templates` and `mix pow.extension.phoenix.mailer.gen.templates` tasks

## v1.0.4 (2019-03-13)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ defmodule Mix.Tasks.Pow.Extension.Phoenix.Gen.Templates do
|> print_shell_instructions()
end

@extension_templates [
{PowResetPassword, [
@extension_templates %{
PowResetPassword => [
{"reset_password", ~w(new edit)}
]},
{PowInvitation, [
],
PowInvitation => [
{"invitation", ~w(new show edit)}
]}
]
]
}
defp create_template_files({config, _parsed, _invalid}) do
structure = Phoenix.parse_structure(config)
web_module = structure[:web_module]
Expand All @@ -49,19 +49,27 @@ defmodule Mix.Tasks.Pow.Extension.Phoenix.Gen.Templates do
extensions =
config
|> Extension.extensions(web_app)
|> Enum.filter(&Keyword.has_key?(@extension_templates, &1))
|> Enum.map(&{&1, @extension_templates[&1]})
|> Enum.map(fn extension ->
templates = Map.get(@extension_templates, extension, [])

Enum.each(extensions, fn {module, templates} ->
Enum.each(templates, fn {name, actions} ->
Phoenix.create_view_file(module, name, web_module, web_prefix)
Phoenix.create_templates(module, name, web_prefix, actions)
create_views_and_templates(extension, templates, web_module, web_prefix)

extension
end)
end)

%{extensions: extensions, web_app: web_app, structure: structure}
end

defp create_views_and_templates(extension, [], _web_module, _web_prefix) do
Mix.shell().info("Warning: No view or template files generated for #{inspect extension} as no templates has been defined for it.")
end
defp create_views_and_templates(extension, templates, web_module, web_prefix) do
Enum.each(templates, fn {name, actions} ->
Phoenix.create_view_file(extension, name, web_module, web_prefix)
Phoenix.create_templates(extension, name, web_prefix, actions)
end)
end

defp print_shell_instructions(%{extensions: [], web_app: web_app}) do
Extension.no_extensions_error(web_app)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ defmodule Mix.Tasks.Pow.Extension.Phoenix.Mailer.Gen.Templates do
|> print_shell_instructions()
end

@extension_templates [
{PowResetPassword, [
@extension_templates %{
PowResetPassword => [
{"mailer", ~w(reset_password)}
]},
{PowEmailConfirmation, [
],
PowEmailConfirmation => [
{"mailer", ~w(email_confirmation)}
]},
{PowInvitation, [
],
PowInvitation => [
{"mailer", ~w(invitation)}
]}
]
]
}
defp create_template_files({config, _parsed, _invalid}) do
structure = Phoenix.parse_structure(config)
web_module = structure[:web_module]
Expand All @@ -50,20 +50,30 @@ defmodule Mix.Tasks.Pow.Extension.Phoenix.Mailer.Gen.Templates do
extensions =
config
|> Extension.extensions(web_app)
|> Enum.filter(&Keyword.has_key?(@extension_templates, &1))
|> Enum.map(&{&1, @extension_templates[&1]})

Enum.each(extensions, fn {module, templates} ->
Enum.each(templates, fn {name, mails} ->
mails = Enum.map(mails, &String.to_atom/1)
Mailer.create_view_file(module, name, web_module, web_prefix, mails)
Mailer.create_templates(module, name, web_prefix, mails)
|> Enum.map(fn extension ->
templates = Map.get(@extension_templates, extension, [])

create_views_and_templates(extension, templates, web_module, web_prefix)

extension
end)
end)

%{extensions: extensions, web_app: web_app, structure: structure}
end


defp create_views_and_templates(extension, [], _web_module, _web_prefix) do
Mix.shell().info("Warning: No mailer view or template files generated for #{inspect extension} as no mailer templates has been defined for it.")
end
defp create_views_and_templates(extension, templates, web_module, web_prefix) do
Enum.each(templates, fn {name, mails} ->
mails = Enum.map(mails, &String.to_atom/1)

Mailer.create_view_file(extension, name, web_module, web_prefix, mails)
Mailer.create_templates(extension, name, web_prefix, mails)
end)
end

defp print_shell_instructions(%{extensions: [], web_app: web_app}) do
Extension.no_extensions_error(web_app)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ defmodule Mix.Tasks.Pow.Extension.Phoenix.Gen.TemplatesTest do
end)
end

test "warns no templates" do
File.cd!(@tmp_path, fn ->
Templates.run(~w(--extension PowPersistentSession))

assert_received {:mix_shell, :info, ["Warning: No view or template files generated for PowPersistentSession as no templates has been defined for it."]}
end)
end

describe "with extensions in env config" do
setup do
Application.put_env(:pow, :pow, extensions: Enum.map(@expected_template_files, &elem(&1, 0)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ defmodule Mix.Tasks.Pow.Extension.Phoenix.Mailer.Gen.TemplatesTest do
end)
end

test "warns no mailer templates" do
File.cd!(@tmp_path, fn ->
Templates.run(~w(--extension PowPersistentSession))

assert_received {:mix_shell, :info, ["Warning: No mailer view or template files generated for PowPersistentSession as no mailer templates has been defined for it."]}
end)
end

describe "with extensions in env config" do
setup do
Application.put_env(:pow, :pow, extensions: Enum.map(@expected_template_files, &elem(&1, 0)))
Expand Down

0 comments on commit 51d9630

Please sign in to comment.