-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0492e9c
commit 41991d5
Showing
12 changed files
with
171 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.