mirror of
https://github.com/ash-project/ash_money.git
synced 2024-09-21 22:03:24 +12:00
54 lines
2 KiB
Elixir
54 lines
2 KiB
Elixir
defmodule Mix.Tasks.AshMoney.AddToAshPostgres do
|
|
@moduledoc "Adds AshMoney.AshPostgresExtension to installed_extensions and installs :ex_money_sql."
|
|
@shortdoc @moduledoc
|
|
require Igniter.Code.Common
|
|
use Igniter.Mix.Task
|
|
|
|
@impl Igniter.Mix.Task
|
|
def igniter(igniter, _argv) do
|
|
repo_module_name = Igniter.Code.Module.module_name("Repo")
|
|
|
|
igniter
|
|
|> Igniter.Project.Deps.add_dep({:ex_money_sql, "~> 1.0"})
|
|
|> Igniter.apply_and_fetch_dependencies()
|
|
|> Igniter.Code.Module.find_and_update_module!(repo_module_name, fn zipper ->
|
|
case Igniter.Code.Module.move_to_use(zipper, AshPostgres.Repo) do
|
|
# discarding since we just needed to check that `use AshPostgres.Repo` exists
|
|
{:ok, _zipper} ->
|
|
case Igniter.Code.Function.move_to_def(zipper, :installed_extensions, 0) do
|
|
{:ok, zipper} ->
|
|
case Igniter.Code.Common.move_right(zipper, &Igniter.Code.List.list?/1) do
|
|
{:ok, zipper} ->
|
|
case Igniter.Code.List.append_new_to_list(
|
|
zipper,
|
|
AshMoney.AshPostgresExtension
|
|
) do
|
|
{:ok, zipper} ->
|
|
{:ok, zipper}
|
|
|
|
_ ->
|
|
{:error,
|
|
"couldn't append `AshMoney.AshPostgresExtension` to #{inspect(repo_module_name)}.installed_extensions/0"}
|
|
end
|
|
|
|
:error ->
|
|
{:error,
|
|
"#{inspect(repo_module_name)}.installed_extensions/0 doesn't return a list"}
|
|
end
|
|
|
|
_ ->
|
|
Igniter.Code.Common.add_code(zipper, """
|
|
def installed_extensions do
|
|
# Add extensions here, and the migration generator will install them.
|
|
[AshMoney.AshPostgresExtension]
|
|
end
|
|
""")
|
|
end
|
|
|
|
_ ->
|
|
{:error, "Couldn't find `use AshPostgres.Repo` in #{inspect(repo_module_name)}"}
|
|
end
|
|
end)
|
|
|> Ash.Igniter.codegen("install_ash_money")
|
|
end
|
|
end
|