Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/ash/domain/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,27 @@ defmodule Ash.Domain.Info do
end
end

@doc """
Returns a list of extensions in use by the domain.

## Options
- `:include_resource_extensions?` - If `true`, includes extensions defined on
resources in the domain. Defaults to `false`.
"""
@spec extensions(domain :: Ash.Domain.t(), opts :: [include_resource_extensions?: boolean()]) ::
list(module())
def extensions(domain, opts \\ []) do
if opts[:include_resource_extensions?] do
domain
|> resources()
|> Enum.flat_map(&Ash.Resource.Info.extensions/1)
|> Kernel.++(Spark.extensions(domain))
|> Enum.uniq()
else
Spark.extensions(domain)
end
end

@spec allowed?(mfa | nil, module()) :: boolean
defp allowed?({m, f, a}, resource) do
apply(m, f, List.wrap(a) ++ [resource])
Expand Down
28 changes: 28 additions & 0 deletions lib/ash/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,32 @@ defmodule Ash.Info do
)
end
end

@doc """
Returns a list of extensions in use by all the domains and resources in the
given application.
"""
@spec extensions_in_use(app :: Application.app()) :: [module()]
def extensions_in_use(app) do
app
|> domains()
|> Enum.flat_map(&Ash.Domain.Info.extensions(&1, include_resource_extensions?: true))
|> Enum.uniq()
end

@doc """
Returns a list of all defined extensions in the application.

> #### Speed {: .warning}
>
> This function will load all modules in the application, which can be slow.
> Do not call this function in time sensitive code paths, It is intended for
> mix tasks, introspection, and debugging purposes.
"""
@spec defined_extensions(app :: Application.app()) :: [module()]
def defined_extensions(app) do
app
|> Application.spec(:modules)
|> Enum.filter(&Spark.implements_behaviour?(&1, Spark.Dsl.Extension))
end
end
6 changes: 6 additions & 0 deletions lib/ash/resource/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -934,4 +934,10 @@ defmodule Ash.Resource.Info do
@doc "The data layer of the resource, or nil if it does not have one"
@spec data_layer(Ash.Resource.t()) :: Ash.DataLayer.t() | nil
defdelegate data_layer(resource), to: Ash.DataLayer

@doc """
Returns a list of extensions in use by the resource.
"""
@spec extensions(resource :: Ash.Resource.t()) :: list(module())
defdelegate extensions(resource), to: Spark
end
49 changes: 12 additions & 37 deletions lib/mix/tasks/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,23 @@ defmodule Ash.Mix.Tasks.Helpers do
@doc """
Gets all extensions in use by the current project's domains and resources
"""
@spec extensions!(argv :: [String.t()], opts :: [in_use?: boolean()]) :: [module()]
def extensions!(argv, opts \\ []) do
if opts[:in_use?] do
Mix.shell().info("Getting extensions in use by resources in current project...")
domains = Ash.Mix.Tasks.Helpers.domains!(argv)

resource_extensions =
domains
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
|> all_extensions()

domains
|> all_extensions()
|> Enum.concat(resource_extensions)
|> Enum.uniq()
|> case do
[] ->
Mix.shell().info("No extensions in use by resources in current project...")

extensions ->
extensions
extensions =
Enum.flat_map(
domains,
&Ash.Domain.Info.extensions(&1, include_resource_extensions?: true)
)

if extensions == [] do
Mix.shell().info("No extensions in use by resources in current project...")
end

extensions
else
Mix.shell().info("Getting extensions in current project...")

Expand All @@ -46,22 +42,7 @@ defmodule Ash.Mix.Tasks.Helpers do
apps()
|> Stream.concat(apps)
|> Stream.uniq()
|> Task.async_stream(
fn app ->
app
|> :application.get_key(:modules)
|> case do
:undefined ->
[]

{_, mods} ->
mods
|> List.wrap()
|> Enum.filter(&Spark.implements_behaviour?(&1, Spark.Dsl.Extension))
end
end,
timeout: :infinity
)
|> Task.async_stream(&Ash.Info.defined_extensions/1, timeout: :infinity)
|> Stream.map(&elem(&1, 1))
|> Stream.flat_map(& &1)
|> Stream.uniq()
Expand Down Expand Up @@ -140,12 +121,6 @@ defmodule Ash.Mix.Tasks.Helpers do
end
end

defp all_extensions(modules) do
modules
|> Enum.flat_map(&Spark.extensions/1)
|> Enum.uniq()
end

defp ensure_compiled(domain, args) do
if Code.ensure_loaded?(Mix.Tasks.App.Config) do
Mix.Task.run("app.config", args)
Expand Down
15 changes: 15 additions & 0 deletions test/domain/info_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Ash.Test.Domain.InfoTest do
@moduledoc false
use ExUnit.Case, async: true

alias Ash.Domain.Info
alias Ash.Test.Flow.Domain

describe "extensions/1" do
test "returns extensions in use by the domain" do
assert Ash.Domain.Dsl in Info.extensions(Domain)
refute Ash.DataLayer.Mnesia in Info.extensions(Domain)
assert Ash.DataLayer.Mnesia in Info.extensions(Domain, include_resource_extensions?: true)
end
end
end
20 changes: 20 additions & 0 deletions test/info_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Ash.Test.InfoTest do
@moduledoc false
use ExUnit.Case, async: true

alias Ash.Info

describe "extensions_in_use/1" do
test "returns extensions in use by all the domains and resources" do
assert Ash.Domain.Dsl in Info.extensions_in_use(:ash)
assert Ash.Resource.Dsl in Info.extensions_in_use(:ash)
end
end

describe "defined_extensions/1" do
test "returns all defined extensions in the application" do
assert Ash.Domain.Dsl in Info.defined_extensions(:ash)
assert Ash.Resource.Dsl in Info.defined_extensions(:ash)
end
end
end
6 changes: 6 additions & 0 deletions test/resource/info_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,10 @@ defmodule Ash.Test.Resource.InfoTest do
Info.public_relationship(Post, [:comments, :post])
end
end

describe "extensions/1" do
test "returns extensions in use by the resource" do
assert Ash.DataLayer.Ets in Info.extensions(Post)
end
end
end
Loading