From 7ac39172221d5c25369d5dc7bb01408f6d78daff Mon Sep 17 00:00:00 2001 From: Kyle Boe Date: Tue, 9 Jan 2024 17:41:31 -0800 Subject: [PATCH 1/4] First pass at depends-on flag --- installer/lib/mix/tasks/phx.new.ex | 1 + installer/lib/phx_new/generator.ex | 2 ++ installer/lib/phx_new/project.ex | 2 ++ installer/lib/phx_new/single.ex | 2 ++ installer/lib/phx_new/umbrella.ex | 2 ++ installer/lib/phx_new/web.ex | 7 +++++-- installer/templates/phx_test/support/conn_case.ex | 2 +- .../templates/phx_umbrella/apps/app_name_web/mix.exs | 4 ++-- installer/templates/phx_web/telemetry.ex | 10 +++++----- 9 files changed, 22 insertions(+), 10 deletions(-) diff --git a/installer/lib/mix/tasks/phx.new.ex b/installer/lib/mix/tasks/phx.new.ex index 88c0592d5c..d7b86baf71 100644 --- a/installer/lib/mix/tasks/phx.new.ex +++ b/installer/lib/mix/tasks/phx.new.ex @@ -134,6 +134,7 @@ defmodule Mix.Tasks.Phx.New do html: :boolean, gettext: :boolean, umbrella: :boolean, + depends_on: :string, verbose: :boolean, live: :boolean, dashboard: :boolean, diff --git a/installer/lib/phx_new/generator.ex b/installer/lib/phx_new/generator.ex index de8284e2e5..34312cb2cd 100644 --- a/installer/lib/phx_new/generator.ex +++ b/installer/lib/phx_new/generator.ex @@ -219,6 +219,8 @@ defmodule Phx.New.Generator do signing_salt: random_string(8), lv_signing_salt: random_string(8), in_umbrella: project.in_umbrella?, + depends_on_app: project.depends_on_app, + depends_on_mod: inspect(project.depends_on_mod) || inspect(project.app_mod), asset_builders: Enum.filter([tailwind && :tailwind, esbuild && :esbuild], & &1), javascript: esbuild, css: tailwind, diff --git a/installer/lib/phx_new/project.ex b/installer/lib/phx_new/project.ex index da093e465a..cc4f35ddbf 100644 --- a/installer/lib/phx_new/project.ex +++ b/installer/lib/phx_new/project.ex @@ -15,6 +15,8 @@ defmodule Phx.New.Project do web_path: nil, opts: :unset, in_umbrella?: false, + depends_on_app: nil, + depends_on_mod: nil, binding: [], generators: [timestamp_type: :utc_datetime] diff --git a/installer/lib/phx_new/single.ex b/installer/lib/phx_new/single.ex index 989c4d7255..2d293691cb 100644 --- a/installer/lib/phx_new/single.ex +++ b/installer/lib/phx_new/single.ex @@ -125,6 +125,8 @@ defmodule Phx.New.Single do %Project{ project | web_app: app, + depends_on_app: app, + depends_on_mod: project.root_mod, lib_web_name: "#{app}_web", web_namespace: Module.concat(["#{project.root_mod}Web"]), web_path: project.base_path diff --git a/installer/lib/phx_new/umbrella.ex b/installer/lib/phx_new/umbrella.ex index 7a309478d2..ed7fbe0edd 100644 --- a/installer/lib/phx_new/umbrella.ex +++ b/installer/lib/phx_new/umbrella.ex @@ -40,6 +40,8 @@ defmodule Phx.New.Umbrella do | web_app: web_app, lib_web_name: web_app, web_namespace: web_namespace, + depends_on_app: app, + depends_on_mod: project.app_mod, generators: [context_app: :"#{app}"], web_path: Path.join(project.project_path, "apps/#{web_app}/") } diff --git a/installer/lib/phx_new/web.ex b/installer/lib/phx_new/web.ex index e991afc97f..92e77514a9 100644 --- a/installer/lib/phx_new/web.ex +++ b/installer/lib/phx_new/web.ex @@ -56,9 +56,10 @@ defmodule Phx.New.Web do "phx_web/components/layouts/app.html.heex": "lib/:web_app/components/layouts/app.html.heex"} ]) - def prepare_project(%Project{app: app} = project) when not is_nil(app) do + def prepare_project(%Project{app: app, opts: opts} = project) when not is_nil(app) do web_path = Path.expand(project.base_path) project_path = Path.dirname(Path.dirname(web_path)) + depends_on = Keyword.get(opts, :depends_on, false) %Project{ project @@ -66,7 +67,9 @@ defmodule Phx.New.Web do project_path: project_path, web_path: web_path, web_app: app, - generators: [context_app: false], + depends_on_app: depends_on, + depends_on_mod: depends_on && Module.concat([Macro.camelize(depends_on)]), + generators: [context_app: :"#{depends_on}"], web_namespace: project.app_mod } end diff --git a/installer/templates/phx_test/support/conn_case.ex b/installer/templates/phx_test/support/conn_case.ex index a6f5df8292..0a61412ae6 100644 --- a/installer/templates/phx_test/support/conn_case.ex +++ b/installer/templates/phx_test/support/conn_case.ex @@ -32,7 +32,7 @@ defmodule <%= @web_namespace %>.ConnCase do end<%= if @ecto do %> setup tags do - <%= @app_module %>.DataCase.setup_sandbox(tags) + <%= @depends_on_mod %>.DataCase.setup_sandbox(tags) {:ok, conn: Phoenix.ConnTest.build_conn()} end<% else %> diff --git a/installer/templates/phx_umbrella/apps/app_name_web/mix.exs b/installer/templates/phx_umbrella/apps/app_name_web/mix.exs index 166512faa2..f1d83d6d1e 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/mix.exs +++ b/installer/templates/phx_umbrella/apps/app_name_web/mix.exs @@ -54,8 +54,8 @@ defmodule <%= @web_namespace %>.MixProject do depth: 1},<% end %> {:telemetry_metrics, "~> 0.6"}, {:telemetry_poller, "~> 1.0"},<%= if @gettext do %> - {:gettext, "~> 0.20"},<% end %><%= if @app_name != @web_app_name do %> - {:<%= @app_name %>, in_umbrella: true},<% end %> + {:gettext, "~> 0.20"},<% end %><%= if @in_umbrella do %> + {:<%= @depends_on_app %>, in_umbrella: true},<% end %> {:jason, "~> 1.2"}, {<%= inspect @web_adapter_app %>, "<%= @web_adapter_vsn %>"} ] diff --git a/installer/templates/phx_web/telemetry.ex b/installer/templates/phx_web/telemetry.ex index 4edb792901..37f3e72525 100644 --- a/installer/templates/phx_web/telemetry.ex +++ b/installer/templates/phx_web/telemetry.ex @@ -52,23 +52,23 @@ defmodule <%= @web_namespace %>.Telemetry do ),<%= if @ecto do %> # Database Metrics - summary("<%= @app_name %>.repo.query.total_time", + summary("<%= @depends_on_app %>.repo.query.total_time", unit: {:native, :millisecond}, description: "The sum of the other measurements" ), - summary("<%= @app_name %>.repo.query.decode_time", + summary("<%= @depends_on_app %>.repo.query.decode_time", unit: {:native, :millisecond}, description: "The time spent decoding the data received from the database" ), - summary("<%= @app_name %>.repo.query.query_time", + summary("<%= @depends_on_app %>.repo.query.query_time", unit: {:native, :millisecond}, description: "The time spent executing the query" ), - summary("<%= @app_name %>.repo.query.queue_time", + summary("<%= @depends_on_app %>.repo.query.queue_time", unit: {:native, :millisecond}, description: "The time spent waiting for a database connection" ), - summary("<%= @app_name %>.repo.query.idle_time", + summary("<%= @depends_on_app %>.repo.query.idle_time", unit: {:native, :millisecond}, description: "The time the connection spent waiting before being checked out for the query" From 2b292418ff3879c5706f012b27c63ae67ede3668 Mon Sep 17 00:00:00 2001 From: Kyle Boe Date: Tue, 9 Jan 2024 17:53:23 -0800 Subject: [PATCH 2/4] update injected config to include dependent umbrella modules --- .../phx_umbrella/apps/app_name_web/config/config.exs | 4 ++-- .../phx_umbrella/apps/app_name_web/config/runtime.exs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/installer/templates/phx_umbrella/apps/app_name_web/config/config.exs b/installer/templates/phx_umbrella/apps/app_name_web/config/config.exs index 5bfc72189e..490da301dc 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/config/config.exs +++ b/installer/templates/phx_umbrella/apps/app_name_web/config/config.exs @@ -3,7 +3,7 @@ import Config <%= if @namespaced? || @ecto || @generators do %> config :<%= @web_app_name %><%= if @namespaced? do %>, namespace: <%= @web_namespace %><% end %><%= if @ecto do %>, - ecto_repos: [<%= @app_module %>.Repo]<% end %><%= if @generators do %>, + ecto_repos: [<%= @depends_on_mod %>.Repo]<% end %><%= if @generators do %>, generators: <%= inspect @generators %><% end %> <% end %># Configures the endpoint @@ -14,7 +14,7 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>, formats: [<%= if @html do%>html: <%= @web_namespace %>.ErrorHTML, <% end %>json: <%= @web_namespace %>.ErrorJSON], layout: false ], - pubsub_server: <%= @app_module %>.PubSub, + pubsub_server: <%= @depends_on_mod %>.PubSub, live_view: [signing_salt: "<%= @lv_signing_salt %>"]<%= if @javascript do %> # Configure esbuild (the version is required) diff --git a/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs b/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs index 57bd5a022e..2c566da289 100644 --- a/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs +++ b/installer/templates/phx_umbrella/apps/app_name_web/config/runtime.exs @@ -69,7 +69,7 @@ config :<%= @web_app_name %>, <%= @endpoint_module %>, # Also, you may need to configure the Swoosh API client of your choice if you # are not using SMTP. Here is an example of the configuration: # -# config :<%= @app_name %>, <%= @app_module %>.Mailer, +# config :<%= @app_name %>, <%= @depends_on_mod %>.Mailer, # adapter: Swoosh.Adapters.Mailgun, # api_key: System.get_env("MAILGUN_API_KEY"), # domain: System.get_env("MAILGUN_DOMAIN") From a4d99a2143d71eff16207d89972e9366059bbcd4 Mon Sep 17 00:00:00 2001 From: Kyle Boe Date: Tue, 9 Jan 2024 18:02:14 -0800 Subject: [PATCH 3/4] default to app_mod --- installer/lib/phx_new/web.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/installer/lib/phx_new/web.ex b/installer/lib/phx_new/web.ex index 92e77514a9..9675aeec12 100644 --- a/installer/lib/phx_new/web.ex +++ b/installer/lib/phx_new/web.ex @@ -67,8 +67,9 @@ defmodule Phx.New.Web do project_path: project_path, web_path: web_path, web_app: app, - depends_on_app: depends_on, - depends_on_mod: depends_on && Module.concat([Macro.camelize(depends_on)]), + depends_on_app: depends_on || app, + depends_on_mod: + (depends_on && Module.concat([Macro.camelize(depends_on)])) || project.app_mod, generators: [context_app: :"#{depends_on}"], web_namespace: project.app_mod } From e8a1dcb960547ff6546caf78b74a056cd38448ed Mon Sep 17 00:00:00 2001 From: Kyle Boe Date: Thu, 11 Jan 2024 14:41:34 -0800 Subject: [PATCH 4/4] Use dependency when defining ecto repo config --- installer/lib/phx_new/generator.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/installer/lib/phx_new/generator.ex b/installer/lib/phx_new/generator.ex index 34312cb2cd..d7e23831da 100644 --- a/installer/lib/phx_new/generator.ex +++ b/installer/lib/phx_new/generator.ex @@ -186,7 +186,7 @@ defmodule Phx.New.Generator do # means creating a database like FoO is the same as foo in # some storages. {adapter_app, adapter_module, adapter_config} = - get_ecto_adapter(db, String.downcase(project.app), project.app_mod) + get_ecto_adapter(db, String.downcase(project.app), project.depends_on_mod) {web_adapter_app, web_adapter_vsn, web_adapter_module} = get_web_adapter(web_adapter)