-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
pow.ecto.install.ex
70 lines (52 loc) · 1.99 KB
/
pow.ecto.install.ex
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
defmodule Mix.Tasks.Pow.Ecto.Install do
@shortdoc "Generates user schema module and migration file(s)"
@moduledoc """
Generates a user schema module and migration file(s).
mix pow.ecto.install -r MyApp.Repo
mix pow.ecto.install -r MyApp.Repo Accounts.Organization organizations
See `Mix.Tasks.Pow.Ecto.Gen.Schema`, `Mix.Tasks.Pow.Ecto.Gen.Migration`
and `Mix.Tasks.Pow.Extension.Ecto.Gen.Migrations` for more.
## Arguments
* `--no-migrations` - don't generate migration files
* `--no-schema` - don't generate schema file
* `--extension` - extensions to generate migrations for
"""
use Mix.Task
alias Mix.Tasks.Pow.Ecto.Gen.Schema, as: SchemaTask
alias Mix.Tasks.Pow.Ecto.Gen.Migration, as: MigrationTask
alias Mix.Tasks.Pow.Extension.Ecto.Gen.Migrations, as: ExtensionMigrationsTask
alias Mix.Pow
@switches [migrations: :boolean, schema: :boolean, extension: :keep]
@default_opts [migrations: true, schema: true]
@mix_task "pow.ecto.install"
@impl true
def run(args) do
Pow.no_umbrella!(@mix_task)
Pow.ensure_ecto!(@mix_task, args)
args
|> Pow.parse_options(@switches, @default_opts)
|> parse()
|> maybe_run_gen_migration(args)
|> maybe_run_extension_gen_migrations(args)
|> maybe_run_gen_schema(args)
end
defp parse({config, parsed, _invalid}) do
Pow.validate_schema_args!(parsed, @mix_task)
config
end
defp maybe_run_gen_migration(%{migrations: true} = config, args) do
MigrationTask.run(args)
config
end
defp maybe_run_gen_migration(config, _args), do: config
defp maybe_run_extension_gen_migrations(%{migrations: true, extension: extensions} = config, args) when extensions != [] do
ExtensionMigrationsTask.run(args)
config
end
defp maybe_run_extension_gen_migrations(config, _args), do: config
defp maybe_run_gen_schema(%{schema: true} = config, args) do
SchemaTask.run(args ++ ~w(--no-migrations))
config
end
defp maybe_run_gen_schema(config, _args), do: config
end