ash_money/lib/mix/tasks/ash_money.add_to_ash_postgres.ex
Igor Barakaiev 5811d00dd3
improvement: add Igniter installer task (#61)
* improvement: add Igniter installer task

* check uniqueness when adding to list

* fix dialyzer warnings

* add better checks when adding to AshPostgres

* change append_ to prepend_new_to_list

* add info/2 and use find_and_update_or_create

* use new find_module utilities

* fix add_to_ash_postgres

* prefix unused variable

* use add_dep instead of add_dependency
2024-07-14 18:07:25 -04:00

54 lines
2.1 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.Module.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)
|> Igniter.add_task("ash.codegen", ["install_ash_money_extension"])
end
end