chore: leverage latest from igniter

This commit is contained in:
Zach Daniel 2024-07-02 16:55:05 -04:00
parent 6db94e810c
commit bb73544e09
7 changed files with 159 additions and 213 deletions

View file

@ -678,7 +678,11 @@ defmodule Ash.Actions.Read do
expr
|> Ash.Filter.used_aggregates(:*, true)
|> Enum.map(fn %Ash.Query.Ref{attribute: aggregate, relationship_path: relationship_path} ->
%{aggregate | resource: query.resource, relationship_path: relationship_path ++ aggregate.relationship_path}
%{
aggregate
| resource: query.resource,
relationship_path: relationship_path ++ aggregate.relationship_path
}
end)
end)
|> Enum.concat(Map.values(query.aggregates))

View file

@ -2,89 +2,75 @@ defmodule Ash.Domain.Igniter do
@moduledoc "Codemods for working with Ash.Domain modules"
def add_resource_reference(igniter, domain, resource) do
igniter
|> Igniter.update_elixir_file(Igniter.Code.Module.proper_location(domain), fn zipper ->
case Igniter.Code.Module.move_to_module_using(zipper, Ash.Domain) do
Igniter.Code.Module.find_and_update_module!(igniter, domain, fn zipper ->
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resources,
1
) do
:error ->
{:error, "Could not find module using Ash.Domain"}
code =
"""
resources do
resource #{inspect(resource)}
end
"""
Igniter.Code.Common.add_code(zipper, code)
{:ok, zipper} ->
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resources,
1
) do
:error ->
code =
"""
resources do
resource #{inspect(resource)}
end
"""
Igniter.Code.Common.add_code(zipper, code)
{:ok, zipper} ->
with {:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper),
:error <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resource,
1,
fn call ->
Igniter.Code.Function.argument_matches_predicate?(
call,
0,
&Igniter.Code.Common.nodes_equal?(&1, resource)
)
end
) do
Igniter.Code.Common.add_code(zipper, "resource #{inspect(resource)}")
else
_ ->
{:ok, zipper}
end
with {:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper),
:error <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resource,
1,
fn call ->
Igniter.Code.Function.argument_matches_predicate?(
call,
0,
&Igniter.Code.Common.nodes_equal?(&1, resource)
)
end
) do
{:ok, Igniter.Code.Common.add_code(zipper, "resource #{inspect(resource)}")}
else
_ ->
{:ok, zipper}
end
end
end)
end
def remove_resource_reference(igniter, domain, resource) do
igniter
|> Igniter.update_elixir_file(Igniter.Code.Module.proper_location(domain), fn zipper ->
case Igniter.Code.Module.move_to_module_using(zipper, Ash.Domain) do
Igniter.Code.Module.find_and_update_module!(igniter, domain, fn zipper ->
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resources,
1
) do
:error ->
{:error, "Could not find module using Ash.Domain"}
zipper
{:ok, zipper} ->
case Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resources,
1
) do
:error ->
zipper
{:ok, zipper} ->
with {:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper),
{:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resource,
1,
fn call ->
Igniter.Code.Function.argument_matches_predicate?(
call,
0,
&Igniter.Code.Common.nodes_equal?(&1, resource)
)
end
) do
{:ok, Sourceror.Zipper.remove(zipper)}
else
_ ->
{:ok, zipper}
end
with {:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper),
{:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:resource,
1,
fn call ->
Igniter.Code.Function.argument_matches_predicate?(
call,
0,
&Igniter.Code.Common.nodes_equal?(&1, resource)
)
end
) do
{:ok, Sourceror.Zipper.remove(zipper)}
else
_ ->
{:ok, zipper}
end
end
end)

View file

@ -1,79 +1,44 @@
defmodule Ash.Resource.Igniter do
@moduledoc "Codemods for working with Ash.Resource modules"
def move_to_resource(zipper) do
app_name = Igniter.Project.Application.app_name()
resources = [
Ash.Resource | Application.get_env(app_name, :base_resources) || []
]
case Igniter.Code.Module.move_to_module_using(zipper, resources) do
:error ->
{:error,
"""
Could not find module using Ash.Resource or any base resource.
To configure base resources, use `config :#{app_name}, base_resources: [...]`
"""}
{:ok, zipper} ->
{:ok, zipper}
end
end
def add_attribute(igniter, resource, attribute) do
igniter
|> Igniter.update_elixir_file(Igniter.Code.Module.proper_location(resource), fn zipper ->
case move_to_resource(zipper) do
{:ok, zipper} ->
with {:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:attributes,
1
),
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper) do
Igniter.Code.Common.add_code(zipper, attribute)
else
_ ->
attributes_with_attribute = """
attributes do
#{attribute}
end
"""
Igniter.Code.Common.add_code(zipper, attributes_with_attribute)
Igniter.Code.Module.find_and_update_module!(igniter, resource, fn zipper ->
with {:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(
zipper,
:attributes,
1
),
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper) do
Igniter.Code.Common.add_code(zipper, attribute)
else
_ ->
attributes_with_attribute = """
attributes do
#{attribute}
end
"""
{:error, error} ->
{:error, error}
Igniter.Code.Common.add_code(zipper, attributes_with_attribute)
end
end)
end
def add_action(igniter, resource, action) do
igniter
|> Igniter.update_elixir_file(Igniter.Code.Module.proper_location(resource), fn zipper ->
case move_to_resource(zipper) do
{:ok, zipper} ->
with {:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(zipper, :actions, 1),
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper) do
Igniter.Code.Common.add_code(zipper, action)
else
_ ->
actions_with_action = """
actions do
#{action}
end
"""
Igniter.Code.Common.add_code(zipper, actions_with_action)
Igniter.Code.Module.find_and_update_module!(igniter, resource, fn zipper ->
with {:ok, zipper} <-
Igniter.Code.Function.move_to_function_call_in_current_scope(zipper, :actions, 1),
{:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper) do
Igniter.Code.Common.add_code(zipper, action)
else
_ ->
actions_with_action = """
actions do
#{action}
end
"""
{:error, error} ->
{:error, error}
Igniter.Code.Common.add_code(zipper, actions_with_action)
end
end)
end

View file

@ -197,7 +197,7 @@ defmodule Mix.Tasks.Ash.Gen.Resource do
defp extend(igniter, resource, extensions, argv) do
Igniter.compose_task(
igniter,
"ash.extend",
"ash.patch.extend",
[inspect(resource), Enum.join(extensions, ",")] ++ argv
)
end

View file

@ -1,8 +1,8 @@
defmodule Mix.Tasks.Ash.Extend do
defmodule Mix.Tasks.Ash.Patch.Extend do
@moduledoc """
Adds an extension or extensions to the domain/resource
For example: `mix ash.extend My.Domain.Resource Ash.Policy.Authorizer`
For example: `mix ash.patch.extend My.Domain.Resource Ash.Policy.Authorizer`
"""
@shortdoc "Adds an extension or extensions to the given domain/resource"
require Igniter.Code.Common
@ -22,61 +22,53 @@ defmodule Mix.Tasks.Ash.Extend do
Enum.reduce(opts[:subjects], igniter, fn subject, igniter ->
subject = Igniter.Code.Module.parse(subject)
path_to_thing = Igniter.Code.Module.proper_location(subject)
kind_of_thing = kind_of_thing(igniter, path_to_thing)
case Igniter.Code.Module.find_module(igniter, subject) do
{:error, igniter} ->
Igniter.add_issue(igniter, "Could not find module to extend: #{subject}")
# we currently require that the packages required are already installed
# probably pretty low hanging fruit to adjust that
{igniter, patchers, _install} =
Enum.reduce(extensions, {igniter, [], []}, fn extension, {igniter, patchers, install} ->
case patcher(kind_of_thing, subject, extension, path_to_thing, argv) do
{fun, new_install} when is_function(fun, 1) ->
{igniter, [fun | patchers], install ++ new_install}
{:ok, {igniter, source, zipper}} ->
case kind_of_thing(zipper) do
{:ok, kind_of_thing} ->
{igniter, patchers, _install} =
Enum.reduce(extensions, {igniter, [], []}, fn extension,
{igniter, patchers, install} ->
case patcher(kind_of_thing, subject, extension, source.path, argv) do
{fun, new_install} when is_function(fun, 1) ->
{igniter, [fun | patchers], install ++ new_install}
{:error, error} ->
{Igniter.add_issue(igniter, error), patchers, install}
{:error, error} ->
{Igniter.add_issue(igniter, error), patchers, install}
end
end)
Enum.reduce(patchers, igniter, fn patcher, igniter ->
patcher.(igniter)
end)
:error ->
Igniter.add_issue(
igniter,
"Could not determine whether #{subject} is an `Ash.Resource` or an `Ash.Domain`."
)
end
end)
# unless Enum.empty?(install) do
# Mix.Shell.info("""
# Before proceeding, we must install the following packages:
# """)
# Igniter.Install.install(install, argv)
# end
Enum.reduce(patchers, igniter, fn patcher, igniter ->
patcher.(igniter)
end)
end
end)
end
defp kind_of_thing(igniter, path) do
igniter = Igniter.include_existing_elixir_file(igniter, path)
zipper =
igniter.rewrite
|> Rewrite.source!(path)
|> Rewrite.Source.get(:quoted)
|> Sourceror.Zipper.zip()
with {_, :error} <-
{Ash.Resource, Igniter.Code.Module.move_to_module_using(zipper, Ash.Resource)},
{_, :error} <- {Ash.Domain, Igniter.Code.Module.move_to_module_using(zipper, Ash.Domain)} do
raise ArgumentError, """
Could not determine whether the thing at #{path} is an `Ash.Resource` or an `Ash.Domain`.
It is a current limitation of `mix ash.extend` that it requires the module in question to be
defined at the "standard" path.
For example:
`YourApp.Foo.Bar` -> `lib/your_app/foo/bar.ex`
"""
defp kind_of_thing(zipper) do
with {:ok, zipper} <- Igniter.Code.Common.move_to_do_block(zipper) do
with {_, :error} <-
{Ash.Resource, Igniter.Code.Module.move_to_using(zipper, Ash.Resource)},
{_, :error} <-
{Ash.Domain, Igniter.Code.Module.move_to_using(zipper, Ash.Domain)} do
:error
else
{kind_of_thing, {:ok, _}} ->
{:ok, kind_of_thing}
end
else
{kind_of_thing, {:ok, _}} ->
kind_of_thing
_ -> :error
end
end
@ -101,7 +93,7 @@ defmodule Mix.Tasks.Ash.Extend do
{[], Ash.DataLayer.Mnesia}
{Ash.Resource, "embedded", _} ->
{[], &embedded_patcher(&1, module, path)}
{[], &embedded_patcher(&1, module)}
{Ash.Resource, "json_api", _} ->
{[:ash_json_api], AshJsonApi.Resource}
@ -129,10 +121,10 @@ defmodule Mix.Tasks.Ash.Extend do
if function_exported?(extension, :install, 4) do
fn igniter ->
extension.install(igniter, module, kind_of_thing, path, argv)
|> simple_add_extension(kind_of_thing, path, extension)
|> simple_add_extension(kind_of_thing, module, extension)
end
else
&simple_add_extension(&1, kind_of_thing, path, extension)
&simple_add_extension(&1, kind_of_thing, module, extension)
end
{fun, install}
@ -167,7 +159,7 @@ defmodule Mix.Tasks.Ash.Extend do
end
end
defp embedded_patcher(igniter, resource, path) do
defp embedded_patcher(igniter, resource) do
domain =
resource
|> Module.split()
@ -175,52 +167,50 @@ defmodule Mix.Tasks.Ash.Extend do
|> Module.concat()
igniter
|> remove_domain_option(path)
|> Spark.Igniter.add_extension(path, Ash.Resource, :data_layer, :embedded, true)
|> remove_domain_option(resource)
|> Spark.Igniter.add_extension(resource, Ash.Resource, :data_layer, :embedded, true)
|> Ash.Domain.Igniter.remove_resource_reference(domain, resource)
|> Spark.Igniter.update_dsl(
Ash.Resource,
path,
resource,
[{:section, :actions}, {:option, :defaults}],
[:read, :destroy, create: [], update: []],
fn x -> x end
[:read, :destroy, create: :*, update: :*],
fn x -> {:ok, x} end
)
end
defp remove_domain_option(igniter, path) do
Igniter.update_elixir_file(igniter, path, fn zipper ->
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Ash.Resource),
{:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ash.Resource),
defp remove_domain_option(igniter, module) do
Igniter.Code.Module.find_and_update_module!(igniter, module, fn zipper ->
with {:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ash.Resource),
{:ok, zipper} <-
Igniter.Code.Function.update_nth_argument(zipper, 1, fn values_zipper ->
values_zipper
|> Igniter.Code.Keyword.remove_keyword_key(:domain)
Igniter.Code.Keyword.remove_keyword_key(values_zipper, :domain)
end) do
zipper
Igniter.Util.Debug.puts_code_at_node(zipper)
{:ok, zipper}
else
_ ->
zipper
{:ok, zipper}
end
end)
end
defp simple_add_extension(igniter, Ash.Resource, path, extension) do
defp simple_add_extension(igniter, Ash.Resource, module, extension) do
cond do
Spark.implements_behaviour?(extension, Ash.DataLayer) ->
Spark.Igniter.add_extension(igniter, path, Ash.Resource, :data_layer, extension, true)
Spark.Igniter.add_extension(igniter, module, Ash.Resource, :data_layer, extension, true)
Spark.implements_behaviour?(extension, Ash.Notifier) ->
Spark.Igniter.add_extension(igniter, path, Ash.Resource, :notifiers, extension)
Spark.Igniter.add_extension(igniter, module, Ash.Resource, :notifiers, extension)
Spark.implements_behaviour?(extension, Ash.Authorizer) ->
Spark.Igniter.add_extension(igniter, path, Ash.Resource, :authorizers, extension)
Spark.Igniter.add_extension(igniter, module, Ash.Resource, :authorizers, extension)
true ->
igniter
end
end
defp simple_add_extension(igniter, type, path, extension) do
Spark.Igniter.add_extension(igniter, path, type, :extensions, extension)
defp simple_add_extension(igniter, type, module, extension) do
Spark.Igniter.add_extension(igniter, module, type, :extensions, extension)
end
end

View file

@ -335,7 +335,7 @@ defmodule Ash.MixProject do
defp deps do
[
# DSLs
{:spark, "~> 2.1 and >= 2.1.18"},
{:spark, "~> 2.1 and >= 2.2.7"},
# Ash resources are backed by ecto scheams
{:ecto, "~> 3.7"},
# Used by the ETS data layer
@ -360,7 +360,8 @@ defmodule Ash.MixProject do
{:simple_sat, "~> 0.1 and >= 0.1.1", optional: true},
# Code Generators
{:igniter, "~> 0.2.5"},
# {:igniter, "~> 0.2.5"},
{:igniter, path: "../igniter", override: true},
# Dev/Test dependencies
{:eflame, "~> 1.0", only: [:dev, :test]},

View file

@ -1,7 +1,7 @@
%{
"benchee": {:hex, :benchee, "1.3.1", "c786e6a76321121a44229dde3988fc772bca73ea75170a73fd5f4ddf1af95ccf", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "76224c58ea1d0391c8309a8ecbfe27d71062878f59bd41a390266bf4ac1cc56d"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"},
"castore": {:hex, :castore, "1.0.8", "dedcf20ea746694647f883590b82d9e96014057aff1d44d03ec90f36a5c0dc6e", [:mix], [], "hexpm", "0b2b66d2ee742cb1d9cb8c8be3b43c3a70ee8651f37b75a8b982e036752983f1"},
"comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"},
"credo": {:hex, :credo, "1.7.7", "771445037228f763f9b2afd612b6aa2fd8e28432a95dbbc60d8e03ce71ba4446", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8bc87496c9aaacdc3f90f01b7b0582467b69b4bd2441fe8aae3109d843cc2f2e"},
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
@ -45,7 +45,7 @@
"simple_sat": {:hex, :simple_sat, "0.1.3", "f650fc3c184a5fe741868b5ac56dc77fdbb428468f6dbf1978e14d0334497578", [:mix], [], "hexpm", "a54305066a356b7194dc81db2a89232bacdc0b3edaef68ed9aba28dcbc34887b"},
"sobelow": {:hex, :sobelow, "0.13.0", "218afe9075904793f5c64b8837cc356e493d88fddde126a463839351870b8d1e", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cd6e9026b85fc35d7529da14f95e85a078d9dd1907a9097b3ba6ac7ebbe34a0d"},
"sourceror": {:hex, :sourceror, "1.4.0", "be87319b1579191e25464005d465713079b3fd7124a3938a1e6cf4def39735a9", [:mix], [], "hexpm", "16751ca55e3895f2228938b703ad399b0b27acfe288eff6c0e629ed3e6ec0358"},
"spark": {:hex, :spark, "2.2.6", "4f160462f45c0be2bccdc4700e7ffc6b2e97b4e38f57eed2349bc9dab4aaa66c", [:mix], [{:igniter, "~> 0.2", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "1e0e012978be808232a502a116d4b99b5059ab3760453438b155ac048f82ce20"},
"spark": {:hex, :spark, "2.2.7", "b9f430ec0e05e3e719dc9a3e6b466a1e8122d361ddf495b191e84db41999a461", [:mix], [{:igniter, "~> 0.2.6", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "50d63a6d58f6fdf7ed67e38e18ba1380a981398c0b4882b580834e6689f0a0f5"},
"spitfire": {:hex, :spitfire, "0.1.3", "7ea0f544005dfbe48e615ed90250c9a271bfe126914012023fd5e4b6b82b7ec7", [:mix], [], "hexpm", "d53b5107bcff526a05c5bb54c95e77b36834550affd5830c9f58760e8c543657"},
"splode": {:hex, :splode, "0.2.4", "71046334c39605095ca4bed5d008372e56454060997da14f9868534c17b84b53", [:mix], [], "hexpm", "ca3b95f0d8d4b482b5357954fec857abd0fa3ea509d623334c1328e7382044c2"},
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},