fix: only use Mix.deps_tree if defined

fixes #1197
This commit is contained in:
Zach Daniel 2024-05-23 10:00:46 -04:00
parent 66de15db28
commit 73a2ef4852

View file

@ -3,6 +3,8 @@ defmodule Ash.Mix.Tasks.Helpers do
Helpers for Ash Mix tasks.
"""
require Logger
@doc """
Gets all extensions in use by the current project's domains and resources
"""
@ -41,17 +43,7 @@ defmodule Ash.Mix.Tasks.Helpers do
[]
end
# for our app, and all dependency apps, we want to find extensions
# the benefit of not just getting all loaded applications is that this
# is actually a surprisingly expensive thing to do for every single built
# in application for elixir/erlang. Instead we get anything w/ a dependency on ash or spark
# this could miss things, but its unlikely. And if it misses things, it actually should be
# fixed in the dependency that is relying on a transitive dependency :)
Mix.Project.deps_tree()
|> Stream.filter(fn {_, nested_deps} ->
Enum.any?(nested_deps, &(&1 == :spark || &1 == :ash))
end)
|> Stream.map(&elem(&1, 0))
apps()
|> Stream.concat(apps)
|> Stream.uniq()
|> Task.async_stream(
@ -71,6 +63,33 @@ defmodule Ash.Mix.Tasks.Helpers do
end
end
Code.ensure_loaded!(Mix.Project)
if function_exported?(Mix.Project, :deps_tree, 0) do
# for our app, and all dependency apps, we want to find extensions
# the benefit of not just getting all loaded applications is that this
# is actually a surprisingly expensive thing to do for every single built
# in application for elixir/erlang. Instead we get anything w/ a dependency on ash or spark
# this could miss things, but its unlikely. And if it misses things, it actually should be
# fixed in the dependency that is relying on a transitive dependency :)
defp apps do
Mix.Project.deps_tree()
|> Stream.filter(fn {_, nested_deps} ->
Enum.any?(nested_deps, &(&1 == :spark || &1 == :ash))
end)
|> Stream.map(&elem(&1, 0))
end
else
defp apps do
Logger.warning(
"Mix.Project.deps_tree/0 not available, falling back to loaded_applications/0. Upgrade to Elixir 1.15+ to make this *much* faster."
)
:application.loaded_applications()
|> Stream.map(&elem(&1, 0))
end
end
@doc """
Get all domains for the current project and ensure they are compiled.
"""