Skip to content

Commit

Permalink
chore(tasks): make it work on umbrella child apps (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp authored Feb 12, 2025
1 parent 96b5706 commit 80b7fc3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 51 deletions.
9 changes: 9 additions & 0 deletions lib/beacon/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,14 @@ if Code.ensure_loaded?(Igniter) do
|> Rewrite.Source.diff()
|> IO.iodata_to_binary()
end

def config_file_path(igniter, file_name) do
case igniter |> Igniter.Project.Application.config_path() |> Path.split() do
[path] -> [path]
path -> Enum.drop(path, -1)
end
|> Path.join()
|> Path.join(file_name)
end
end
end
112 changes: 69 additions & 43 deletions lib/mix/tasks/beacon.gen.proxy_endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ if Code.ensure_loaded?(Igniter) do

@moduledoc __MODULE__.Docs.long_doc()

@impl Igniter.Mix.Task
def supports_umbrella?, do: true

@impl Igniter.Mix.Task
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
Expand All @@ -60,6 +63,16 @@ if Code.ensure_loaded?(Igniter) do

@impl Igniter.Mix.Task
def igniter(igniter) do
if Mix.Project.umbrella?() do
Mix.shell().error("""
Running 'mix beacon.gen.proxy_endpoint' in the root of Umbrella apps is not supported yet.
Please execute that task inside a child app.
""")

exit({:shutdown, 1})
end

options = igniter.args.options
proxy_endpoint_module_name = Igniter.Libs.Phoenix.web_module_name(igniter, "ProxyEndpoint")

Expand All @@ -85,11 +98,6 @@ if Code.ensure_loaded?(Igniter) do
|> add_secret_key_base_to_dev_exs(secret_key_base)
|> update_existing_endpoints(otp_app, existing_endpoints)
|> configure_proxy_endpoint(otp_app, proxy_endpoint_module_name)
|> Igniter.add_notice("""
ProxyEndpoint generated successfully.
This enables your application to serve sites at multiple hosts, each with their own Endpoint.
""")
end
end

Expand All @@ -107,7 +115,14 @@ if Code.ensure_loaded?(Igniter) do
end

defp add_signing_salt_to_config_exs(igniter, signing_salt) do
Igniter.update_elixir_file(igniter, "config/config.exs", fn zipper ->
default =
"""
import Config
signing_salt = \"#{signing_salt}\"
"""

Igniter.create_or_update_elixir_file(igniter, Beacon.Igniter.config_file_path(igniter, "config.exs"), default, fn zipper ->
case Beacon.Igniter.move_to_variable(zipper, :signing_salt) do
{:ok, _already_exists} ->
zipper
Expand All @@ -120,7 +135,14 @@ if Code.ensure_loaded?(Igniter) do
end

defp add_secret_key_base_to_dev_exs(igniter, secret_key_base) do
Igniter.update_elixir_file(igniter, "config/dev.exs", fn zipper ->
default =
"""
import Config
secret_key_base = \"#{secret_key_base}\"
"""

Igniter.create_or_update_elixir_file(igniter, Beacon.Igniter.config_file_path(igniter, "dev.exs"), default, fn zipper ->
case Beacon.Igniter.move_to_variable(zipper, :secret_key_base) do
{:ok, _already_exists} ->
zipper
Expand Down Expand Up @@ -149,47 +171,44 @@ if Code.ensure_loaded?(Igniter) do
signing_salt: signing_salt,
same_site: "#{session_same_site}"
]
""")}
""")},
after: &match?({:=, _, [{:signing_salt, _, _}, _]}, &1.node)
)
end

defp configure_proxy_endpoint(igniter, otp_app, proxy_endpoint_module_name) do
pubsub = Igniter.Project.Module.module_name(igniter, "PubSub")

igniter
|> Igniter.update_elixir_file("config/config.exs", fn zipper ->
{:ok,
zipper
|> Beacon.Igniter.move_to_variable!(:signing_salt)
|> Igniter.Project.Config.modify_configuration_code(
[proxy_endpoint_module_name],
otp_app,
Sourceror.parse_string!("""
[
adapter: Bandit.PhoenixAdapter,
pubsub_server: #{inspect(pubsub)},
live_view: [signing_salt: signing_salt]
]
""")
)}
end)
|> Igniter.update_elixir_file("config/dev.exs", fn zipper ->
{:ok,
zipper
|> Beacon.Igniter.move_to_variable!(:secret_key_base)
|> Igniter.Project.Config.modify_configuration_code(
[proxy_endpoint_module_name],
otp_app,
Sourceror.parse_string!("""
[
http: [ip: {127, 0, 0, 1}, port: 4000],
check_origin: false,
debug_errors: true,
secret_key_base: secret_key_base
]
""")
)}
end)
|> Igniter.Project.Config.configure(
"config.exs",
otp_app,
[proxy_endpoint_module_name],
{:code,
Sourceror.parse_string!("""
[
adapter: Bandit.PhoenixAdapter,
pubsub_server: #{inspect(pubsub)},
live_view: [signing_salt: signing_salt]
]
""")},
after: &match?({:=, _, [{:signing_salt, _, _}, _]}, &1.node)
)
|> Igniter.Project.Config.configure(
"dev.exs",
otp_app,
[proxy_endpoint_module_name],
{:code,
Sourceror.parse_string!("""
[
http: [ip: {127, 0, 0, 1}, port: 4000],
check_origin: false,
debug_errors: true,
secret_key_base: secret_key_base
]
""")},
after: &match?({:=, _, [{:secret_key_base, _, _}, _]}, &1.node)
)
|> Igniter.Project.Config.configure_runtime_env(
:prod,
otp_app,
Expand Down Expand Up @@ -229,9 +248,16 @@ if Code.ensure_loaded?(Igniter) do
"config.exs",
otp_app,
[endpoint, :live_view, :signing_salt],
{:code, Sourceror.parse_string!("signing_salt")}
{:code, Sourceror.parse_string!("signing_salt")},
after: &match?({:=, _, [{:signing_salt, _, _}, _]}, &1.node)
)
|> Igniter.Project.Config.configure(
"dev.exs",
otp_app,
[endpoint, :secret_key_base],
{:code, Sourceror.parse_string!("secret_key_base")},
after: &match?({:=, _, [{:secret_key_base, _, _}, _]}, &1.node)
)
|> Igniter.Project.Config.configure("dev.exs", otp_app, [endpoint, :secret_key_base], {:code, Sourceror.parse_string!("secret_key_base")})
|> Igniter.Project.Config.configure("dev.exs", otp_app, [endpoint, :http], [],
updater: fn zipper ->
if port_matches_value?(zipper, 4000) do
Expand Down
17 changes: 15 additions & 2 deletions lib/mix/tasks/beacon.gen.site.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ if Code.ensure_loaded?(Igniter) do

@test? Beacon.Config.env_test?()

@impl Igniter.Mix.Task
def supports_umbrella?, do: true

@impl Igniter.Mix.Task
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
Expand All @@ -79,6 +82,16 @@ if Code.ensure_loaded?(Igniter) do

@impl Igniter.Mix.Task
def igniter(igniter) do
if Mix.Project.umbrella?() do
Mix.shell().error("""
Running 'mix beacon.gen.site' in the root of Umbrella apps is not supported yet.
Please execute that task inside a child app.
""")

exit({:shutdown, 1})
end

options = igniter.args.options
argv = igniter.args.argv

Expand Down Expand Up @@ -390,7 +403,7 @@ if Code.ensure_loaded?(Igniter) do
&if(Igniter.Project.Config.configures_key?(&1, "config.exs", otp_app, new_endpoint),
do: &1,
else:
Igniter.update_elixir_file(&1, "config/config.exs", fn zipper ->
Igniter.update_elixir_file(&1, Beacon.Igniter.config_file_path(igniter, "config.exs"), fn zipper ->
{:ok,
zipper
|> Beacon.Igniter.move_to_variable!(:signing_salt)
Expand All @@ -417,7 +430,7 @@ if Code.ensure_loaded?(Igniter) do
&if(Igniter.Project.Config.configures_key?(&1, "dev.exs", otp_app, new_endpoint),
do: &1,
else:
Igniter.update_elixir_file(&1, "config/dev.exs", fn zipper ->
Igniter.update_elixir_file(&1, Beacon.Igniter.config_file_path(igniter, "dev.exs"), fn zipper ->
{:ok,
zipper
|> Beacon.Igniter.move_to_variable!(:secret_key_base)
Expand Down
13 changes: 13 additions & 0 deletions lib/mix/tasks/beacon.gen.tailwind_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ if Code.ensure_loaded?(Igniter) do

@moduledoc __MODULE__.Docs.long_doc()

@impl Igniter.Mix.Task
def supports_umbrella?, do: true

@impl Igniter.Mix.Task
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
Expand All @@ -46,6 +49,16 @@ if Code.ensure_loaded?(Igniter) do

@impl Igniter.Mix.Task
def igniter(igniter) do
if Mix.Project.umbrella?() do
Mix.shell().error("""
Running 'mix beacon.gen.tailwind_config' in the root of Umbrella apps is not supported yet.
Please execute that task inside a child app.
""")

exit({:shutdown, 1})
end

options = igniter.args.options
site = Keyword.fetch!(options, :site) |> String.to_atom()

Expand Down
15 changes: 14 additions & 1 deletion lib/mix/tasks/beacon.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Mix.Tasks.Beacon.Install.Docs do
```
```bash
"mix beacon.install --site my_site --path /
mix beacon.install --site my_site --path /
```
## Options
Expand All @@ -48,6 +48,9 @@ if Code.ensure_loaded?(Igniter) do

@moduledoc __MODULE__.Docs.long_doc()

@impl Igniter.Mix.Task
def supports_umbrella?, do: true

@impl Igniter.Mix.Task
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
Expand All @@ -61,6 +64,16 @@ if Code.ensure_loaded?(Igniter) do

@impl Igniter.Mix.Task
def igniter(igniter) do
if Mix.Project.umbrella?() do
Mix.shell().error("""
Running 'mix beacon.install' in the root of Umbrella apps is not supported yet.
Please execute that task inside a child app.
""")

exit({:shutdown, 1})
end

argv = igniter.args.argv
options = igniter.args.options

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Beacon.MixProject do
{:solid, "~> 0.14"},
{:tailwind, "~> 0.2"},
{:esbuild, "~> 0.5"},
{:igniter, "~> 0.5", optional: true},
{:igniter, ">= 0.5.24", optional: true},

# Dev, Test, Docs
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
Expand Down
Loading

0 comments on commit 80b7fc3

Please sign in to comment.