-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: make generators more consistent
- Loading branch information
1 parent
6b80d0f
commit 459dca3
Showing
11 changed files
with
201 additions
and
113 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
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,79 @@ | ||
defmodule AshPhoenix.Gen do | ||
@moduledoc false | ||
|
||
def docs do | ||
""" | ||
## Positional Arguments | ||
- `api` - The API (e.g. "Shop"). | ||
- `resource` - The resource (e.g. "Product"). | ||
## Options | ||
- `--resource-plural` - The plural resource name (e.g. "products") | ||
""" | ||
end | ||
|
||
def parse_opts(argv) do | ||
{api, resource, rest} = | ||
case argv do | ||
[api, resource | rest] -> | ||
{api, resource, rest} | ||
|
||
argv -> | ||
raise "Not enough arguments. Expected 2, got #{Enum.count(argv)}" | ||
end | ||
|
||
if String.starts_with?(api, "-") do | ||
raise "Expected first argument to be an api module, not an option" | ||
end | ||
|
||
if String.starts_with?(resource, "-") do | ||
raise "Expected second argument to be a resource module, not an option" | ||
end | ||
|
||
{parsed, _, _} = | ||
OptionParser.parse(rest, | ||
strict: [resource_plural: :string, actor: :string, no_actor: :boolean] | ||
) | ||
|
||
api = Module.concat([api]) | ||
resource = Module.concat([resource]) | ||
|
||
parsed = | ||
Keyword.put_new_lazy(rest, :resource_plural, fn -> | ||
plural_name!(resource, parsed) | ||
end) | ||
|
||
{api, resource, parsed, rest} | ||
end | ||
|
||
defp plural_name!(resource, opts) do | ||
plural_name = | ||
opts[:resource_plural] || | ||
Ash.Resource.Info.plural_name(resource) || | ||
Mix.shell().prompt( | ||
""" | ||
Please provide a plural_name for #{inspect(resource)}. For example the plural of tweet is tweets. | ||
This can also be configured on the resource. To do so, press enter to abort, | ||
and add the following configuration to your resource (using the proper plural name) | ||
resource do | ||
plural_name :tweets | ||
end | ||
> | ||
""" | ||
|> String.trim() | ||
) | ||
|> String.trim() | ||
|
||
case plural_name do | ||
empty when empty in ["", nil] -> | ||
raise("Must configure `plural_name` on resource or provide --resource-plural") | ||
|
||
plural_name -> | ||
to_string(plural_name) | ||
end | ||
end | ||
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
Oops, something went wrong.