Skip to content

Commit

Permalink
fix: don't raise error on codegen with no domains
Browse files Browse the repository at this point in the history
fixes #83
  • Loading branch information
zachdaniel committed Sep 23, 2024
1 parent 007df2c commit 9b7c3d4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
69 changes: 42 additions & 27 deletions lib/mix/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,64 @@ defmodule AshSqlite.Mix.Helpers do
|> Enum.map(&ensure_compiled(&1, args))
|> case do
[] ->
raise "must supply the --domains argument, or set `config :my_app, ash_domains: [...]` in config"
[]

domains ->
domains
end
end

def repos!(opts, args) do
domains = domains!(opts, args)
if opts[:domains] && opts[:domains] != "" do
domains = domains!(opts, args)

resources =
domains
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
|> Enum.filter(&(Ash.DataLayer.data_layer(&1) == AshSqlite.DataLayer))
resources =
domains
|> Enum.flat_map(&Ash.Domain.Info.resources/1)
|> Enum.filter(&(Ash.DataLayer.data_layer(&1) == AshSqlite.DataLayer))
|> case do
[] ->
raise """
No resources with `data_layer: AshSqlite.DataLayer` found in the domains #{Enum.map_join(domains, ",", &inspect/1)}.
Must be able to find at least one resource with `data_layer: AshSqlite.DataLayer`.
"""

resources ->
resources
end

resources
|> Enum.map(&AshSqlite.DataLayer.Info.repo/1)
|> Enum.uniq()
|> case do
[] ->
raise """
No resources with `data_layer: AshSqlite.DataLayer` found in the domains #{Enum.map_join(domains, ",", &inspect/1)}.
Must be able to find at least one resource with `data_layer: AshSqlite.DataLayer`.
"""
No repos could be found configured on the resources in the domains: #{Enum.map_join(domains, ",", &inspect/1)}
resources ->
resources
end

resources
|> Enum.map(&AshSqlite.DataLayer.Info.repo(&1))
|> Enum.uniq()
|> case do
[] ->
raise """
No repos could be found configured on the resources in the domains: #{Enum.map_join(domains, ",", &inspect/1)}
At least one resource must have a repo configured.
At least one resource must have a repo configured.
The following resources were found with `data_layer: AshSqlite.DataLayer`:
The following resources were found with `data_layer: AshSqlite.DataLayer`:
#{Enum.map_join(resources, "\n", &"* #{inspect(&1)}")}
"""

#{Enum.map_join(resources, "\n", &"* #{inspect(&1)}")}
"""
repos ->
repos
end
else
if Code.ensure_loaded?(Mix.Tasks.App.Config) do
Mix.Task.run("app.config", args)
else
Mix.Task.run("loadpaths", args)
"--no-compile" not in args && Mix.Task.run("compile", args)
end

repos ->
repos
Mix.Project.config()[:app]
|> Application.get_env(:ecto_repos, [])
|> Enum.filter(fn repo ->
Spark.implements_behaviour?(repo, AshSqlite.Repo)
end)
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/mix/tasks/ash_sqlite.generate_migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ defmodule Mix.Tasks.AshSqlite.GenerateMigrations do

domains = AshSqlite.Mix.Helpers.domains!(opts, args)

if Enum.empty?(domains) && !opts[:snapshots_only] do
IO.warn("""
No domains found, so no resource-related migrations will be generated.
Pass the `--domains` option or configure `config :your_app, ash_domains: [...]`
""")
end

opts =
opts
|> Keyword.put(:format, !opts[:no_format])
Expand Down

0 comments on commit 9b7c3d4

Please sign in to comment.