This commit is contained in:
Zach Daniel 2024-06-13 15:31:22 -04:00
parent c0dd24dc66
commit fbe7caaf84
4 changed files with 44 additions and 35 deletions

View file

@ -401,7 +401,7 @@ defmodule AshPostgres.DataLayer do
A postgres data layer that leverages Ecto's postgres capabilities.
"""
require Igniter.Common
require Igniter.Code.Common
use Spark.Dsl.Extension,
sections: @sections,
@ -2953,11 +2953,11 @@ defmodule AshPostgres.DataLayer do
|> List.last()
|> Macro.underscore()
repo = Igniter.Module.module_name(Repo)
repo = Igniter.Code.Module.module_name(Repo)
igniter
|> Spark.Igniter.set_option(Ash.Resource, path, [:postgres, :table], table_name, & &1)
|> Spark.Igniter.set_option(Ash.Resource, path, [:postgres, :repo], repo, & &1)
|> Spark.Igniter.set_option(Ash.Resource, path, [:postgres, :table], table_name)
|> Spark.Igniter.set_option(Ash.Resource, path, [:postgres, :repo], repo)
end
def install(igniter, _, _, _), do: igniter

View file

@ -1,9 +1,9 @@
defmodule Mix.Tasks.AshPostgres.Install do
require Igniter.Common
require Igniter.Code.Common
use Igniter.Mix.Task
def igniter(igniter, argv) do
repo = Igniter.Module.module_name("Repo")
def igniter(igniter, _argv) do
repo = Igniter.Code.Module.module_name("Repo")
otp_app = Igniter.Application.app_name()
igniter
@ -13,7 +13,7 @@ defmodule Mix.Tasks.AshPostgres.Install do
|> configure_dev(otp_app, repo)
|> configure_test(otp_app, repo)
|> configure_runtime(repo, otp_app)
|> Igniter.Application.add_child(repo)
|> Igniter.Application.add_new_child(repo)
|> Igniter.add_task("ash.codegen", ["install_ash_postgres"])
end
@ -25,7 +25,10 @@ defmodule Mix.Tasks.AshPostgres.Install do
[:ecto_repos],
[repo],
fn zipper ->
Igniter.Common.prepend_new_to_list(zipper, repo, &Igniter.Common.equal_modules?/2)
Igniter.Code.List.prepend_new_to_list(
zipper,
repo
)
end
)
end
@ -67,16 +70,21 @@ defmodule Mix.Tasks.AshPostgres.Install do
]
zipper
|> Igniter.Common.match_pattern_in_scope(patterns)
|> Igniter.Code.Common.move_to_cursor_match_in_scope(patterns)
|> case do
{:ok, zipper} ->
case Igniter.Common.move_to_function_call_in_current_scope(zipper, :=, 2, fn call ->
Igniter.Common.argument_matches_predicate?(
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:=,
2,
fn call ->
Igniter.Code.Function.argument_matches_predicate?(
call,
0,
&match?({:database_url, _, Elixir}, &1)
)
end) do
end
) do
{:ok, zipper} ->
zipper
|> Igniter.Config.modify_configuration_code(
@ -95,7 +103,7 @@ defmodule Mix.Tasks.AshPostgres.Install do
)
{:error, _error} ->
Igniter.Common.add_code(zipper, """
Igniter.Code.Common.add_code(zipper, """
database_url =
System.get_env("DATABASE_URL") ||
raise \"\"\"
@ -110,7 +118,7 @@ defmodule Mix.Tasks.AshPostgres.Install do
end
{:error, _error} ->
Igniter.Common.add_code(zipper, """
Igniter.Code.Common.add_code(zipper, """
if config_env() == :prod do
database_url =
System.get_env("DATABASE_URL") ||
@ -174,7 +182,7 @@ defmodule Mix.Tasks.AshPostgres.Install do
end
defp setup_repo_module(igniter, otp_app, repo) do
path = Igniter.Module.proper_location(repo)
path = Igniter.Code.Module.proper_location(repo)
default_repo_contents =
"""
@ -202,10 +210,10 @@ defmodule Mix.Tasks.AshPostgres.Install do
end
defp use_ash_postgres_instead_of_ecto(zipper) do
with {:ok, zipper} <- Igniter.Common.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Common.move_to_use(zipper, Ecto.Repo),
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ecto.Repo),
{:ok, zipper} <-
Igniter.Common.update_nth_argument(zipper, 0, fn _ ->
Igniter.Code.Function.update_nth_argument(zipper, 0, fn _ ->
AshPostgres.Repo
end) do
zipper
@ -216,12 +224,12 @@ defmodule Mix.Tasks.AshPostgres.Install do
end
defp remove_adapter_option(zipper) do
with {:ok, zipper} <- Igniter.Common.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Common.move_to_use(zipper, Ecto.Repo),
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ecto.Repo),
{:ok, zipper} <-
Igniter.Common.update_nth_argument(zipper, 1, fn values_zipper ->
Igniter.Code.Function.update_nth_argument(zipper, 1, fn values_zipper ->
values_zipper
|> Igniter.Common.remove_keyword_key(:adapter)
|> Igniter.Code.Keyword.remove_keyword_key(:adapter)
end) do
zipper
else
@ -231,16 +239,16 @@ defmodule Mix.Tasks.AshPostgres.Install do
end
defp set_otp_app(zipper, otp_app) do
with {:ok, zipper} <- Igniter.Common.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Common.move_to_use(zipper, Ecto.Repo),
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Ecto.Repo),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ecto.Repo),
{:ok, zipper} <-
Igniter.Common.update_nth_argument(zipper, 0, fn _ ->
Igniter.Code.Function.update_nth_argument(zipper, 0, fn _ ->
AshPostgres.Repo
end),
{:ok, zipper} <-
Igniter.Common.update_nth_argument(zipper, 1, fn values_zipper ->
Igniter.Code.Function.update_nth_argument(zipper, 1, fn values_zipper ->
values_zipper
|> Igniter.Common.set_keyword_key(:otp_app, otp_app, fn x -> x end)
|> Igniter.Code.Keyword.set_keyword_key(:otp_app, otp_app, fn x -> x end)
end) do
zipper
else
@ -250,8 +258,8 @@ defmodule Mix.Tasks.AshPostgres.Install do
end
defp add_installed_extensions_function(zipper) do
with {:ok, zipper} <- Igniter.Common.move_to_module_using(zipper, Ecto.Repo) do
Igniter.Common.add_code(zipper, """
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Ecto.Repo) do
Igniter.Code.Common.add_code(zipper, """
def installed_extensions do
# Add extensions here, and the migration generator will install them.
["ash-functions"]

View file

@ -164,7 +164,7 @@ defmodule AshPostgres.MixProject do
[
{:ash, ash_version("~> 3.0 and >= 3.0.7")},
{:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.3")},
{:igniter, path: "../igniter", optional: true},
{:igniter, "~> 0.1"},
{:ecto_sql, "~> 3.9"},
{:ecto, "~> 3.9"},
{:jason, "~> 1.0"},

View file

@ -23,6 +23,7 @@
"git_ops": {:hex, :git_ops, "2.6.1", "cc7799a68c26cf814d6d1a5121415b4f5bf813de200908f930b27a2f1fe9dad5", [:mix], [{:git_cli, "~> 0.2", [hex: :git_cli, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "ce62d07e41fe993ec22c35d5edb11cf333a21ddaead6f5d9868fcb607d42039e"},
"glob_ex": {:hex, :glob_ex, "0.1.7", "eae6b6377147fb712ac45b360e6dbba00346689a87f996672fe07e97d70597b1", [:mix], [], "hexpm", "decc1c21c0c73df3c9c994412716345c1692477b9470e337f628a7e08da0da6a"},
"hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"},
"igniter": {:hex, :igniter, "0.1.4", "1d746f9f8e845750a4bf3d208d0e7424eea95fb031bdcb4a7c9653ce80535008", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:req, "~> 0.4", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}], "hexpm", "2e81f6094523a51ac6d66211fdd9fcf2b2a1d59f53f06b31ad4ab54f7715ca36"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},