2023-08-15 10:30:35 +12:00
|
|
|
defmodule Mix.Tasks.Ash.Codegen do
|
|
|
|
@moduledoc """
|
2024-03-28 09:06:40 +13:00
|
|
|
Runs all codegen tasks for any extension on any resource/domain in your application.
|
2023-08-15 10:30:35 +12:00
|
|
|
"""
|
|
|
|
use Mix.Task
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
@shortdoc "Runs all codegen tasks for any extension on any resource/domain in your application."
|
|
|
|
@doc @shortdoc
|
2023-08-15 10:30:35 +12:00
|
|
|
def run(argv) do
|
2024-04-10 12:24:08 +12:00
|
|
|
Mix.Task.run("compile")
|
2024-04-10 14:55:26 +12:00
|
|
|
|
2023-12-28 04:12:19 +13:00
|
|
|
{name, argv} =
|
|
|
|
case argv do
|
|
|
|
["-" <> _ | _] ->
|
|
|
|
{nil, argv}
|
|
|
|
|
|
|
|
[first | rest] ->
|
2024-05-03 11:14:10 +12:00
|
|
|
{String.trim(first), rest}
|
2023-12-28 04:12:19 +13:00
|
|
|
|
|
|
|
[] ->
|
|
|
|
{nil, []}
|
|
|
|
end
|
|
|
|
|
|
|
|
{opts, _} =
|
|
|
|
OptionParser.parse!(argv,
|
|
|
|
strict: [
|
|
|
|
name: :string,
|
|
|
|
no_format: :boolean,
|
|
|
|
dry_run: :boolean,
|
|
|
|
check: :boolean,
|
|
|
|
drop_columns: :boolean
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-12-28 10:38:41 +13:00
|
|
|
opts = Keyword.put_new(opts, :name, name)
|
|
|
|
|
2023-12-28 04:12:19 +13:00
|
|
|
if !opts[:name] && !opts[:dry_run] && !opts[:check] do
|
|
|
|
raise ArgumentError, """
|
|
|
|
Name must be provided when running `ash.codegen`, unless `--dry-run` or `--check` is also provided.
|
|
|
|
|
|
|
|
Please provide a name. for example:
|
|
|
|
|
|
|
|
mix ash.codegen add_feature_for_reticulating_splines #{Enum.join(argv, " ")}
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-08-15 10:30:35 +12:00
|
|
|
argv
|
|
|
|
|> Ash.Mix.Tasks.Helpers.extensions!()
|
|
|
|
|> Enum.map(fn extension ->
|
|
|
|
if function_exported?(extension, :codegen, 1) do
|
2024-04-11 11:02:27 +12:00
|
|
|
extension_name =
|
2023-08-15 10:30:35 +12:00
|
|
|
if function_exported?(extension, :name, 0) do
|
|
|
|
extension.name()
|
|
|
|
else
|
|
|
|
inspect(extension)
|
|
|
|
end
|
|
|
|
|
2024-04-11 11:02:27 +12:00
|
|
|
Mix.shell().info("Running codegen for #{extension_name}...")
|
2023-08-15 10:30:35 +12:00
|
|
|
|
2024-04-11 11:02:27 +12:00
|
|
|
extension.codegen(argv ++ ["--name", name])
|
2023-08-15 10:30:35 +12:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|