Skip to content

Commit 80b7fc3

Browse files
authored
chore(tasks): make it work on umbrella child apps (#757)
1 parent 96b5706 commit 80b7fc3

File tree

7 files changed

+125
-51
lines changed

7 files changed

+125
-51
lines changed

lib/beacon/igniter.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,14 @@ if Code.ensure_loaded?(Igniter) do
8888
|> Rewrite.Source.diff()
8989
|> IO.iodata_to_binary()
9090
end
91+
92+
def config_file_path(igniter, file_name) do
93+
case igniter |> Igniter.Project.Application.config_path() |> Path.split() do
94+
[path] -> [path]
95+
path -> Enum.drop(path, -1)
96+
end
97+
|> Path.join()
98+
|> Path.join(file_name)
99+
end
91100
end
92101
end

lib/mix/tasks/beacon.gen.proxy_endpoint.ex

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ if Code.ensure_loaded?(Igniter) do
4343

4444
@moduledoc __MODULE__.Docs.long_doc()
4545

46+
@impl Igniter.Mix.Task
47+
def supports_umbrella?, do: true
48+
4649
@impl Igniter.Mix.Task
4750
def info(_argv, _composing_task) do
4851
%Igniter.Mix.Task.Info{
@@ -60,6 +63,16 @@ if Code.ensure_loaded?(Igniter) do
6063

6164
@impl Igniter.Mix.Task
6265
def igniter(igniter) do
66+
if Mix.Project.umbrella?() do
67+
Mix.shell().error("""
68+
Running 'mix beacon.gen.proxy_endpoint' in the root of Umbrella apps is not supported yet.
69+
70+
Please execute that task inside a child app.
71+
""")
72+
73+
exit({:shutdown, 1})
74+
end
75+
6376
options = igniter.args.options
6477
proxy_endpoint_module_name = Igniter.Libs.Phoenix.web_module_name(igniter, "ProxyEndpoint")
6578

@@ -85,11 +98,6 @@ if Code.ensure_loaded?(Igniter) do
8598
|> add_secret_key_base_to_dev_exs(secret_key_base)
8699
|> update_existing_endpoints(otp_app, existing_endpoints)
87100
|> configure_proxy_endpoint(otp_app, proxy_endpoint_module_name)
88-
|> Igniter.add_notice("""
89-
ProxyEndpoint generated successfully.
90-
91-
This enables your application to serve sites at multiple hosts, each with their own Endpoint.
92-
""")
93101
end
94102
end
95103

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

109117
defp add_signing_salt_to_config_exs(igniter, signing_salt) do
110-
Igniter.update_elixir_file(igniter, "config/config.exs", fn zipper ->
118+
default =
119+
"""
120+
import Config
121+
122+
signing_salt = \"#{signing_salt}\"
123+
"""
124+
125+
Igniter.create_or_update_elixir_file(igniter, Beacon.Igniter.config_file_path(igniter, "config.exs"), default, fn zipper ->
111126
case Beacon.Igniter.move_to_variable(zipper, :signing_salt) do
112127
{:ok, _already_exists} ->
113128
zipper
@@ -120,7 +135,14 @@ if Code.ensure_loaded?(Igniter) do
120135
end
121136

122137
defp add_secret_key_base_to_dev_exs(igniter, secret_key_base) do
123-
Igniter.update_elixir_file(igniter, "config/dev.exs", fn zipper ->
138+
default =
139+
"""
140+
import Config
141+
142+
secret_key_base = \"#{secret_key_base}\"
143+
"""
144+
145+
Igniter.create_or_update_elixir_file(igniter, Beacon.Igniter.config_file_path(igniter, "dev.exs"), default, fn zipper ->
124146
case Beacon.Igniter.move_to_variable(zipper, :secret_key_base) do
125147
{:ok, _already_exists} ->
126148
zipper
@@ -149,47 +171,44 @@ if Code.ensure_loaded?(Igniter) do
149171
signing_salt: signing_salt,
150172
same_site: "#{session_same_site}"
151173
]
152-
""")}
174+
""")},
175+
after: &match?({:=, _, [{:signing_salt, _, _}, _]}, &1.node)
153176
)
154177
end
155178

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

159182
igniter
160-
|> Igniter.update_elixir_file("config/config.exs", fn zipper ->
161-
{:ok,
162-
zipper
163-
|> Beacon.Igniter.move_to_variable!(:signing_salt)
164-
|> Igniter.Project.Config.modify_configuration_code(
165-
[proxy_endpoint_module_name],
166-
otp_app,
167-
Sourceror.parse_string!("""
168-
[
169-
adapter: Bandit.PhoenixAdapter,
170-
pubsub_server: #{inspect(pubsub)},
171-
live_view: [signing_salt: signing_salt]
172-
]
173-
""")
174-
)}
175-
end)
176-
|> Igniter.update_elixir_file("config/dev.exs", fn zipper ->
177-
{:ok,
178-
zipper
179-
|> Beacon.Igniter.move_to_variable!(:secret_key_base)
180-
|> Igniter.Project.Config.modify_configuration_code(
181-
[proxy_endpoint_module_name],
182-
otp_app,
183-
Sourceror.parse_string!("""
184-
[
185-
http: [ip: {127, 0, 0, 1}, port: 4000],
186-
check_origin: false,
187-
debug_errors: true,
188-
secret_key_base: secret_key_base
189-
]
190-
""")
191-
)}
192-
end)
183+
|> Igniter.Project.Config.configure(
184+
"config.exs",
185+
otp_app,
186+
[proxy_endpoint_module_name],
187+
{:code,
188+
Sourceror.parse_string!("""
189+
[
190+
adapter: Bandit.PhoenixAdapter,
191+
pubsub_server: #{inspect(pubsub)},
192+
live_view: [signing_salt: signing_salt]
193+
]
194+
""")},
195+
after: &match?({:=, _, [{:signing_salt, _, _}, _]}, &1.node)
196+
)
197+
|> Igniter.Project.Config.configure(
198+
"dev.exs",
199+
otp_app,
200+
[proxy_endpoint_module_name],
201+
{:code,
202+
Sourceror.parse_string!("""
203+
[
204+
http: [ip: {127, 0, 0, 1}, port: 4000],
205+
check_origin: false,
206+
debug_errors: true,
207+
secret_key_base: secret_key_base
208+
]
209+
""")},
210+
after: &match?({:=, _, [{:secret_key_base, _, _}, _]}, &1.node)
211+
)
193212
|> Igniter.Project.Config.configure_runtime_env(
194213
:prod,
195214
otp_app,
@@ -229,9 +248,16 @@ if Code.ensure_loaded?(Igniter) do
229248
"config.exs",
230249
otp_app,
231250
[endpoint, :live_view, :signing_salt],
232-
{:code, Sourceror.parse_string!("signing_salt")}
251+
{:code, Sourceror.parse_string!("signing_salt")},
252+
after: &match?({:=, _, [{:signing_salt, _, _}, _]}, &1.node)
253+
)
254+
|> Igniter.Project.Config.configure(
255+
"dev.exs",
256+
otp_app,
257+
[endpoint, :secret_key_base],
258+
{:code, Sourceror.parse_string!("secret_key_base")},
259+
after: &match?({:=, _, [{:secret_key_base, _, _}, _]}, &1.node)
233260
)
234-
|> Igniter.Project.Config.configure("dev.exs", otp_app, [endpoint, :secret_key_base], {:code, Sourceror.parse_string!("secret_key_base")})
235261
|> Igniter.Project.Config.configure("dev.exs", otp_app, [endpoint, :http], [],
236262
updater: fn zipper ->
237263
if port_matches_value?(zipper, 4000) do

lib/mix/tasks/beacon.gen.site.ex

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ if Code.ensure_loaded?(Igniter) do
5656

5757
@test? Beacon.Config.env_test?()
5858

59+
@impl Igniter.Mix.Task
60+
def supports_umbrella?, do: true
61+
5962
@impl Igniter.Mix.Task
6063
def info(_argv, _composing_task) do
6164
%Igniter.Mix.Task.Info{
@@ -79,6 +82,16 @@ if Code.ensure_loaded?(Igniter) do
7982

8083
@impl Igniter.Mix.Task
8184
def igniter(igniter) do
85+
if Mix.Project.umbrella?() do
86+
Mix.shell().error("""
87+
Running 'mix beacon.gen.site' in the root of Umbrella apps is not supported yet.
88+
89+
Please execute that task inside a child app.
90+
""")
91+
92+
exit({:shutdown, 1})
93+
end
94+
8295
options = igniter.args.options
8396
argv = igniter.args.argv
8497

@@ -390,7 +403,7 @@ if Code.ensure_loaded?(Igniter) do
390403
&if(Igniter.Project.Config.configures_key?(&1, "config.exs", otp_app, new_endpoint),
391404
do: &1,
392405
else:
393-
Igniter.update_elixir_file(&1, "config/config.exs", fn zipper ->
406+
Igniter.update_elixir_file(&1, Beacon.Igniter.config_file_path(igniter, "config.exs"), fn zipper ->
394407
{:ok,
395408
zipper
396409
|> Beacon.Igniter.move_to_variable!(:signing_salt)
@@ -417,7 +430,7 @@ if Code.ensure_loaded?(Igniter) do
417430
&if(Igniter.Project.Config.configures_key?(&1, "dev.exs", otp_app, new_endpoint),
418431
do: &1,
419432
else:
420-
Igniter.update_elixir_file(&1, "config/dev.exs", fn zipper ->
433+
Igniter.update_elixir_file(&1, Beacon.Igniter.config_file_path(igniter, "dev.exs"), fn zipper ->
421434
{:ok,
422435
zipper
423436
|> Beacon.Igniter.move_to_variable!(:secret_key_base)

lib/mix/tasks/beacon.gen.tailwind_config.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ if Code.ensure_loaded?(Igniter) do
3535

3636
@moduledoc __MODULE__.Docs.long_doc()
3737

38+
@impl Igniter.Mix.Task
39+
def supports_umbrella?, do: true
40+
3841
@impl Igniter.Mix.Task
3942
def info(_argv, _composing_task) do
4043
%Igniter.Mix.Task.Info{
@@ -46,6 +49,16 @@ if Code.ensure_loaded?(Igniter) do
4649

4750
@impl Igniter.Mix.Task
4851
def igniter(igniter) do
52+
if Mix.Project.umbrella?() do
53+
Mix.shell().error("""
54+
Running 'mix beacon.gen.tailwind_config' in the root of Umbrella apps is not supported yet.
55+
56+
Please execute that task inside a child app.
57+
""")
58+
59+
exit({:shutdown, 1})
60+
end
61+
4962
options = igniter.args.options
5063
site = Keyword.fetch!(options, :site) |> String.to_atom()
5164

lib/mix/tasks/beacon.install.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule Mix.Tasks.Beacon.Install.Docs do
2828
```
2929
3030
```bash
31-
"mix beacon.install --site my_site --path /
31+
mix beacon.install --site my_site --path /
3232
```
3333
3434
## Options
@@ -48,6 +48,9 @@ if Code.ensure_loaded?(Igniter) do
4848

4949
@moduledoc __MODULE__.Docs.long_doc()
5050

51+
@impl Igniter.Mix.Task
52+
def supports_umbrella?, do: true
53+
5154
@impl Igniter.Mix.Task
5255
def info(_argv, _composing_task) do
5356
%Igniter.Mix.Task.Info{
@@ -61,6 +64,16 @@ if Code.ensure_loaded?(Igniter) do
6164

6265
@impl Igniter.Mix.Task
6366
def igniter(igniter) do
67+
if Mix.Project.umbrella?() do
68+
Mix.shell().error("""
69+
Running 'mix beacon.install' in the root of Umbrella apps is not supported yet.
70+
71+
Please execute that task inside a child app.
72+
""")
73+
74+
exit({:shutdown, 1})
75+
end
76+
6477
argv = igniter.args.argv
6578
options = igniter.args.options
6679

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule Beacon.MixProject do
8080
{:solid, "~> 0.14"},
8181
{:tailwind, "~> 0.2"},
8282
{:esbuild, "~> 0.5"},
83-
{:igniter, "~> 0.5", optional: true},
83+
{:igniter, ">= 0.5.24", optional: true},
8484

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

0 commit comments

Comments
 (0)