Skip to content

Commit

Permalink
Merge pull request #43 from danschultzer/pow-1.0.4
Browse files Browse the repository at this point in the history
Use Pow 1.0.4
  • Loading branch information
danschultzer committed Mar 14, 2019
2 parents 0492e9c + 41991d5 commit 74cd7b6
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 123 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.2.1 (TBA)

* Improve mix task instructions

## v0.2.0 (2019-03-09)

### Changes
Expand Down
40 changes: 40 additions & 0 deletions lib/mix/pow_assent.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Mix.PowAssent do
@moduledoc """
Utilities module for mix tasks.
"""

@doc false
@spec validate_schema_args!([binary()], binary()) :: map() | no_return
def validate_schema_args!([schema, plural | _rest] = args, task) do
cond do
not schema_valid?(schema) ->
raise_invalid_schema_args_error!("Expected the schema argument, #{inspect schema}, to be a valid module name", task)
not plural_valid?(plural) ->
raise_invalid_schema_args_error!("Expected the plural argument, #{inspect plural}, to be all lowercase using snake_case convention", task)
true ->
schema_options_from_args(args)
end
end
def validate_schema_args!([_schema | _rest], task) do
raise_invalid_schema_args_error!("Invalid arguments", task)
end
def validate_schema_args!([], _task), do: schema_options_from_args()

defp schema_valid?(schema), do: schema =~ ~r/^[A-Z]\w*(\.[A-Z]\w*)*$/

defp plural_valid?(plural), do: plural =~ ~r/^[a-z\_]*$/

@spec raise_invalid_schema_args_error!(binary(), binary()) :: no_return()
defp raise_invalid_schema_args_error!(msg, task) do
Mix.raise("""
#{msg}
mix #{task} accepts both a module name and the plural of the resource:
mix #{task} UserIdentities.UserIdentity user_identities
""")
end

defp schema_options_from_args(_opts \\ [])
defp schema_options_from_args([schema, plural | _rest]), do: %{schema_name: schema, schema_plural: plural}
defp schema_options_from_args(_any), do: %{schema_name: "UserIdentities.UserIdentity", schema_plural: "user_identities"}
end
33 changes: 19 additions & 14 deletions lib/mix/tasks/ecto/pow_assent.ecto.gen.migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.Migration do
mix pow_assent.ecto.gen.migration -r MyApp.Repo
mix pow_assent.ecto.gen.migration -r MyApp.Repo CustomUserIdentity custom_user_identity
mix pow_assent.ecto.gen.migration -r MyApp.Repo Accounts.Identity identities
This generator will add a migration file in `priv/repo/migrations` for the
`user_identities` table
## Arguments
* `-r`, `--repo` - the repo module
* `--binary-id` - use binary id for primary key
* `--users-table` - what users table to reference, defaults to "users"
"""
use Mix.Task

alias PowAssent.Ecto.UserIdentities.Schema.Migration, as: UserIdentitiesMigration
alias Mix.{Ecto, Pow, Pow.Ecto.Migration}
alias Mix.{Ecto, Pow, Pow.Ecto.Migration, PowAssent}

@switches [binary_id: :boolean, users_table: :string]
@default_opts [binary_id: false, users_table: "users"]
@mix_task "pow_assent.ecto.gen.migration"

@doc false
@impl true
def run(args) do
Pow.no_umbrella!("pow_assent.ecto.gen.migration")
Pow.no_umbrella!(@mix_task)

args
|> Pow.parse_options(@switches, @default_opts)
Expand All @@ -27,13 +37,9 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.Migration do
end

defp parse({config, parsed, _invalid}) do
case parsed do
[_schema_name, schema_plural | _rest] ->
Map.merge(config, %{schema_plural: schema_plural})

_ ->
config
end
parsed
|> PowAssent.validate_schema_args!(@mix_task)
|> Map.merge(config)
end

defp create_migrations_files(config, args) do
Expand All @@ -44,9 +50,8 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.Migration do
|> Enum.each(&create_migration_files/1)
end

defp create_migration_files(%{repo: repo, binary_id: binary_id, users_table: users_table} = config) do
schema_plural = Map.get(config, :schema_plural, "user_identities")
context_base = Pow.context_app() |> Pow.context_base() |> Atom.to_string()
defp create_migration_files(%{repo: repo, binary_id: binary_id, users_table: users_table, schema_plural: schema_plural}) do
context_base = Pow.app_base(Pow.otp_app())
schema = UserIdentitiesMigration.new(context_base, schema_plural, repo: repo, binary_id: binary_id, users_table: users_table)
content = UserIdentitiesMigration.gen(schema)

Expand Down
69 changes: 39 additions & 30 deletions lib/mix/tasks/ecto/pow_assent.ecto.gen.schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.Schema do
mix pow_assent.ecto.gen.schema -r MyApp.Repo
mix pow_assent.ecto.gen.schema -r MyApp.Repo CustomUserIdentity custom_user_identity
mix pow_assent.ecto.gen.schema -r MyApp.Repo Accounts.Identity identities
This generator will add a schema module file in
`lib/my_app/user_identities/user_identity.ex`.
## Arguments
* `--binary-id` - use binary id for primary key and references
* `--context-app` - context app to use for path and module names
"""
use Mix.Task

alias PowAssent.Ecto.UserIdentities.Schema.Module, as: SchemaModule
alias Mix.{Generator, Pow}
alias Mix.{Generator, Pow, PowAssent}

@switches [context_app: :string, binary_id: :boolean]
@default_opts [binary_id: false]
@mix_task "pow_assent.ecto.gen.schema"

@doc false
@impl true
def run(args) do
Pow.no_umbrella!("pow_assent.ecto.gen.schema")
Pow.no_umbrella!(@mix_task)

args
|> Pow.parse_options(@switches, @default_opts)
Expand All @@ -27,34 +36,18 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.Schema do
end

defp parse({config, parsed, _invalid}) do
case parsed do
[schema_name, schema_plural | _rest] ->
Map.merge(config, %{schema_name: schema_name, schema_plural: schema_plural})

_ ->
config
end
parsed
|> PowAssent.validate_schema_args!(@mix_task)
|> Map.merge(config)
end

defp create_schema_file(%{binary_id: binary_id} = config) do
context_app = Map.get(config, :context_app, Pow.context_app())
context_base = Pow.context_base(context_app)
schema_name = Map.get(config, :schema_name, "UserIdentities.UserIdentity")
schema_plural = Map.get(config, :schema_plural, "user_identities")
schema = SchemaModule.new(context_base, schema_name, schema_plural, binary_id: binary_id)
content = SchemaModule.gen(schema)
dir_name =
schema.schema_name
|> String.split(".")
|> Enum.slice(0..-2)
|> Enum.join(".")
|> Macro.underscore()
file_name =
schema.module
|> Module.split()
|> List.last()
|> Macro.underscore()
|> Kernel.<>(".ex")
defp create_schema_file(%{binary_id: binary_id, schema_name: schema_name, schema_plural: schema_plural} = config) do
context_app = Map.get(config, :context_app) || Pow.otp_app()
context_base = Pow.app_base(context_app)
schema = SchemaModule.new(context_base, schema_name, schema_plural, binary_id: binary_id)
content = SchemaModule.gen(schema)
dir_name = dir_name(schema_name)
file_name = file_name(schema.module)

context_app
|> Pow.context_lib_path(dir_name)
Expand All @@ -64,6 +57,22 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.Schema do
|> Generator.create_file(content)
end

defp dir_name(schema_name) do
schema_name
|> String.split(".")
|> Enum.slice(0..-2)
|> Enum.join(".")
|> Macro.underscore()
end

defp file_name(module) do
module
|> Module.split()
|> List.last()
|> Macro.underscore()
|> Kernel.<>(".ex")
end

defp maybe_create_directory(path) do
Generator.create_directory(path)

Expand Down
20 changes: 17 additions & 3 deletions lib/mix/tasks/ecto/pow_assent.ecto.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@ defmodule Mix.Tasks.PowAssent.Ecto.Install do
Generates user identity schema and migration file.
mix pow_assent.ecto.install -r MyApp.Repo
mix pow_assent.ecto.install -r MyApp.Repo Accounts.Identity identities
See `Mix.Tasks.PowAssent.Ecto.Gen.Schema` and
`Mix.Tasks.PowAssent.Ecto.Gen.Migration` for more.
"""
use Mix.Task

alias Mix.Tasks.PowAssent.Ecto.Gen.Schema, as: SchemaTask
alias Mix.Tasks.PowAssent.Ecto.Gen.Migration, as: MigrationTask
alias Mix.Pow
alias Mix.{Pow, PowAssent}

@switches []
@default_opts []
@mix_task "pow_assent.ecto.install"

@doc false
@impl true
def run(args) do
Pow.no_umbrella!("pow_assent.ecto.install")
Pow.no_umbrella!(@mix_task)

args
|> Pow.parse_options(@switches, @default_opts)
|> parse()
|> run_gen_migration(args)
|> run_gen_schema(args)
end


defp parse({config, parsed, _invalid}) do
PowAssent.validate_schema_args!(parsed, @mix_task)

config
end

defp run_gen_migration(config, args) do
MigrationTask.run(args)

Expand Down
31 changes: 5 additions & 26 deletions lib/mix/tasks/phoenix/pow_assent.phoenix.gen.templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ defmodule Mix.Tasks.PowAssent.Phoenix.Gen.Templates do
mix pow_assent.phoenix.gen.templates
mix pow_assent.phoenix.gen.templates --context-app my_app
## Arguments
* `--context-app MyApp` app to use for path and module names
* `--context-app` app to use for path and module names
"""
use Mix.Task

Expand All @@ -17,14 +19,13 @@ defmodule Mix.Tasks.PowAssent.Phoenix.Gen.Templates do
@switches [context_app: :string]
@default_opts []

@doc false
@impl true
def run(args) do
Pow.no_umbrella!("pow_assent.phoenix.gen.templates")

args
|> Pow.parse_options(@switches, @default_opts)
|> create_template_files()
|> print_shell_instructions()
end

@templates [
Expand All @@ -33,7 +34,6 @@ defmodule Mix.Tasks.PowAssent.Phoenix.Gen.Templates do

defp create_template_files({config, _parsed, _invalid}) do
structure = Phoenix.parse_structure(config)
context_base = structure[:context_base]
web_module = structure[:web_module]
web_prefix = structure[:web_prefix]

Expand All @@ -42,27 +42,6 @@ defmodule Mix.Tasks.PowAssent.Phoenix.Gen.Templates do
Phoenix.create_templates(Elixir.PowAssent, name, web_prefix, actions)
end)

%{context_base: context_base, web_module: web_module}
end

defp print_shell_instructions(%{context_base: context_base, web_module: web_base}) do
Mix.shell.info("""
PowAssent Phoenix templates and views has been generated.
Please set `web_module: #{inspect(web_base)}` in your configuration.
defmodule #{inspect(web_base)}.Endpoint do
use #{inspect(web_base)}.Endpoint, otp_app: :#{Macro.underscore(context_base)}
# ...
plug #{inspect(web_base)}.Pow.Plug.Session,
repo: #{inspect(context_base)}.Repo,
user: #{inspect(context_base)}.Users.User,
web_module: #{inspect(web_base)}
# ...
end
""")
%{structure: structure}
end
end
12 changes: 11 additions & 1 deletion lib/mix/tasks/pow_assent.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ defmodule Mix.Tasks.PowAssent.Install do
Will generate PowAssent migration file.
mix pow_assent.install -r MyApp.Repo
mix pow_assent.install -r MyApp.Repo Accounts.Identity identities
See `Mix.Tasks.PowAssent.Ecto.Install` for more.
"""
use Mix.Task

alias Mix.Project
alias Mix.Tasks.PowAssent.Ecto.Install
@mix_task "pow_assent.install"

@doc false
def run(args) do
Expand All @@ -24,7 +29,12 @@ defmodule Mix.Tasks.PowAssent.Install do

defp no_umbrella! do
if Project.umbrella?() do
Mix.raise("mix pow_assent.install can't be used in umbrella apps. Run mix pow_assent.ecto.install in your ecto app directory.")
Mix.raise(
"""
mix #{@mix_task} has to be used inside an application directory, but this is an umbrella project.
Run mix pow_assent.ecto.install inside your Ecto application directory to create schema module and migrations.
""")
end

:ok
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule PowAssent.MixProject do

defp deps do
[
{:pow, "~> 1.0.3"},
{:pow, "~> 1.0.4"},

{:oauther, "~> 1.1"},

Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
"oauther": {:hex, :oauther, "1.1.1", "7d8b16167bb587ecbcddd3f8792beb9ec3e7b65c1f8ebd86b8dd25318d535752", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"phoenix": {:hex, :phoenix, "1.4.1", "801f9d632808657f1f7c657c8bbe624caaf2ba91429123ebe3801598aea4c3d9", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm"},
"phoenix": {:hex, :phoenix, "1.4.2", "3a1250f22010daeee265923bae02f10b5434b569b999c1b18100b5da05834d93", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_html": {:hex, :phoenix_html, "2.13.1", "fa8f034b5328e2dfa0e4131b5569379003f34bc1fafdaa84985b0b9d2f12e68b", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm"},
"plug": {:hex, :plug, "1.7.2", "d7b7db7fbd755e8283b6c0a50be71ec0a3d67d9213d74422d9372effc8e87fd1", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm"},
"plug_cowboy": {:hex, :plug_cowboy, "2.0.1", "d798f8ee5acc86b7d42dbe4450b8b0dadf665ce588236eb0a751a132417a980e", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.14.0", "f3d6ffea1ca8a156e0633900a5338a3d17b00435227726baed8982718232b694", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"pow": {:hex, :pow, "1.0.3", "35608eed84abbcfcff5bac05b722ed4a4bb865c5154066cda6816f5bcb8353c9", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3.0 or ~> 1.4.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and <= 3.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 1.8.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
"pow": {:hex, :pow, "1.0.4", "8186c3eca543b5c38511c3c684e8eec951a024a324cc7c2b07e712df8cb8c64b", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3.0 or ~> 1.4.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and <= 3.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 1.8.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.2.0", "5b40caa3efe4deb30fb12d7cd8ed4f556f6d6bd15c374c2366772161311ce377", [:mix], [], "hexpm"},
Expand Down
10 changes: 0 additions & 10 deletions test/mix/tasks/ecto/pow_assent.ecto.gen.schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ defmodule Mix.Tasks.PowAssent.Ecto.Gen.SchemaTest do
end)
end

test "generates with :context_app" do
options = ~w(--context-app pow_assent)

File.cd!(@tmp_path, fn ->
Schema.run(options)

assert File.exists?(@expected_file)
end)
end

test "generates with :binary_id" do
options = ~w(--binary-id)

Expand Down
Loading

0 comments on commit 74cd7b6

Please sign in to comment.