forked from ash-project/ash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathash.gen.resources.ex
More file actions
86 lines (70 loc) · 3.95 KB
/
ash.gen.resources.ex
File metadata and controls
86 lines (70 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
if Code.ensure_loaded?(Igniter) do
defmodule Mix.Tasks.Ash.Gen.Resources do
@example """
mix ash.gen.resources "Helpdesk.Support.Ticket --attribute subject:string:required --timestamps;Helpdesk.Support.Representative --uuid-primary-key id;Helpdesk.Support.Comment --attribute body:text --relationship belongs_to:ticket:Helpdesk.Support.Ticket"
"""
@moduledoc """
Generate and configure multiple Ash.Resource modules from a semicolon-separated list.
This task takes a semicolon-separated list of resource names with their individual options
and generates each one using the `ash.gen.resource` task.
Each resource entry in the list can have its own specific options. The format is:
`"ResourceName --option1 value1 --option2;AnotherResource --option3 value3"`
## Example
```bash
#{@example}
```
## Resource Options
Each resource supports all options from `mix ash.gen.resource`:
* `--attribute` or `-a` - An attribute or comma separated list of attributes to add, as `name:type`. Modifiers: `primary_key`, `array`, `public`, `sensitive`, and `required`. i.e `-a name:string:required`
* `--relationship` or `-r` - A relationship or comma separated list of relationships to add, as `type:name:dest`. Modifiers: `public`. `belongs_to` only modifiers: `primary_key`, `sensitive`, and `required`. i.e `-r belongs_to:author:MyApp.Accounts.Author:required`
* `--default-actions` - A csv list of default action types to add. The `create` and `update` actions accept the public attributes being added.
* `--uuid-primary-key` or `-u` - Adds a UUIDv4 primary key with that name. i.e `-u id`
* `--uuid-v7-primary-key` - Adds a UUIDv7 primary key with that name.
* `--integer-primary-key` or `-i` - Adds an integer primary key with that name. i.e `-i id`
* `--domain` or `-d` - The domain module to add the resource to. i.e `-d MyApp.MyDomain`. This defaults to the resource's module name, minus the last segment.
* `--extend` or `-e` - A comma separated list of modules or builtins to extend the resource with. i.e `-e postgres,Some.Extension`
* `--base` or `-b` - The base module to use for the resource. i.e `-b Ash.Resource`. Requires that the module is in `config :your_app, :base_resources`
* `--timestamps` or `-t` - If set adds `inserted_at` and `updated_at` timestamps to the resource.
* `--ignore-if-exists` - Does nothing if the resource already exists
* `--conflicts` - How to handle conflicts when the same attribute, relationship, or action already exists. Options: `ignore` (default), `replace`
`ignore` will ignore your addition for that attribute, relationship, or action. `replace` will remove the existing one in favor of yours.
"""
@shortdoc "Generate and configure multiple Ash.Resource modules from a semicolon-separated list."
use Igniter.Mix.Task
@impl Igniter.Mix.Task
def info(_argv, _parent) do
%Igniter.Mix.Task.Info{
positional: [:resources],
example: @example,
schema: [],
aliases: []
}
end
@impl Igniter.Mix.Task
def igniter(igniter) do
igniter.args.positional.resources
|> String.split(";", trim: true)
|> Enum.map(&String.trim/1)
|> Enum.reduce(igniter, fn resource_entry, igniter ->
argv = String.split(resource_entry, ~r/\s+/, trim: true)
Igniter.compose_task(igniter, "ash.gen.resource", argv)
end)
end
end
else
defmodule Mix.Tasks.Ash.Gen.Resources do
@moduledoc """
Generate and configure multiple Ash.Resource modules from a semicolon-separated list.
"""
@shortdoc "Generate and configure multiple Ash.Resource modules from a semicolon-separated list."
use Mix.Task
def run(_argv) do
Mix.shell().error("""
The task 'ash.gen.resources' requires igniter to be run.
Please install igniter and try again.
For more information, see: https://hexdocs.pm/igniter
""")
exit({:shutdown, 1})
end
end
end