mirror of
https://github.com/ash-project/ash_money.git
synced 2024-09-19 21:02:55 +12:00
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
This commit is contained in:
parent
f74ffde02b
commit
5811d00dd3
3 changed files with 119 additions and 1 deletions
54
lib/mix/tasks/ash_money.add_to_ash_postgres.ex
Normal file
54
lib/mix/tasks/ash_money.add_to_ash_postgres.ex
Normal file
|
@ -0,0 +1,54 @@
|
|||
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
|
63
lib/mix/tasks/ash_money.install.ex
Normal file
63
lib/mix/tasks/ash_money.install.ex
Normal file
|
@ -0,0 +1,63 @@
|
|||
defmodule Mix.Tasks.AshMoney.Install do
|
||||
@moduledoc "Installs AshMoney. Should be run with `mix igniter.install ash_money`"
|
||||
@shortdoc @moduledoc
|
||||
require Igniter.Code.Common
|
||||
use Igniter.Mix.Task
|
||||
|
||||
def igniter(igniter, _argv) do
|
||||
Igniter.compose_task(igniter, "ex_cldr.install", [], fn igniter, _argv ->
|
||||
cldr_module_name = Igniter.Code.Module.module_name("Cldr")
|
||||
|
||||
igniter
|
||||
|> Igniter.Code.Module.find_and_update_or_create_module(
|
||||
cldr_module_name,
|
||||
"""
|
||||
use Cldr,
|
||||
locales: ["en"],
|
||||
default_locale: "en"
|
||||
""",
|
||||
fn zipper -> {:ok, zipper} end
|
||||
)
|
||||
|> configure_cldr_config(cldr_module_name)
|
||||
end)
|
||||
|> configure_config()
|
||||
|> maybe_add_to_ash_postgres()
|
||||
end
|
||||
|
||||
defp configure_cldr_config(igniter, cldr_module_name) do
|
||||
Igniter.Project.Config.configure_new(
|
||||
igniter,
|
||||
"config.exs",
|
||||
:ex_cldr,
|
||||
[:default_backend],
|
||||
cldr_module_name
|
||||
)
|
||||
end
|
||||
|
||||
defp configure_config(igniter) do
|
||||
Igniter.Project.Config.configure(
|
||||
igniter,
|
||||
"config.exs",
|
||||
:ash,
|
||||
[:known_types],
|
||||
[AshMoney.Types.Money],
|
||||
updater: fn zipper ->
|
||||
Igniter.Code.List.append_new_to_list(zipper, AshMoney.Types.Money)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
defp maybe_add_to_ash_postgres(igniter) do
|
||||
repo_module_name = Igniter.Code.Module.module_name("Repo")
|
||||
|
||||
with {:ok, {igniter, _source, zipper}} <-
|
||||
Igniter.Code.Module.find_module(igniter, repo_module_name),
|
||||
{:ok, _zipper} <-
|
||||
Igniter.Code.Module.move_to_module_using(zipper, AshPostgres.Repo) do
|
||||
Igniter.compose_task(igniter, "ash_money.add_to_ash_postgres")
|
||||
else
|
||||
_ ->
|
||||
igniter
|
||||
end
|
||||
end
|
||||
end
|
3
mix.exs
3
mix.exs
|
@ -116,7 +116,8 @@ defmodule AshMoney.MixProject do
|
|||
{:sobelow, ">= 0.0.0", only: [:dev, :test], runtime: false},
|
||||
{:git_ops, "~> 2.5", only: [:dev, :test]},
|
||||
{:mix_test_watch, "~> 1.0", only: :dev, runtime: false},
|
||||
{:mix_audit, ">= 0.0.0", only: [:dev, :test], runtime: false}
|
||||
{:mix_audit, ">= 0.0.0", only: [:dev, :test], runtime: false},
|
||||
{:igniter, "~> 0.2.5"}
|
||||
]
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue