Skip to content

Commit

Permalink
finish site gen
Browse files Browse the repository at this point in the history
  • Loading branch information
APB9785 committed Jan 17, 2025
1 parent 728c82a commit a45e4ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/mix/tasks/beacon.gen.site.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule Mix.Tasks.Beacon.Gen.Site do
otp_app = Igniter.Project.Application.app_name(igniter)
web_module = Igniter.Libs.Phoenix.web_module(igniter)
{igniter, router} = Beacon.Igniter.select_router!(igniter)
{igniter, existing_endpoints} = Igniter.Libs.Phoenix.endpoints_for_router(igniter, router)
repo = Igniter.Project.Module.module_name(igniter, "Repo")

igniter
Expand All @@ -66,6 +67,8 @@ defmodule Mix.Tasks.Beacon.Gen.Site do
|> maybe_create_proxy_endpoint(host, signing_salt, secret_key_base)
|> maybe_create_new_endpoint(host, otp_app, web_module)
|> maybe_configure_new_endpoint(host, otp_app, port, secure_port, secret_key_base, signing_salt)
|> maybe_update_existing_endpoints(host, otp_app, existing_endpoints, signing_salt, secret_key_base)
|> maybe_update_session_options(host, otp_app, signing_salt)
|> maybe_add_new_endpoint_to_application(host, repo)
|> Igniter.add_notice("""
Site #{inspect(site)} generated successfully.
Expand Down Expand Up @@ -245,7 +248,7 @@ defmodule Mix.Tasks.Beacon.Gen.Site do
defp maybe_create_proxy_endpoint(igniter, nil, _, _), do: igniter

defp maybe_create_proxy_endpoint(igniter, _host, signing_salt, secret_key_base),
do: Igniter.compose_task(igniter, "beacon.gen.proxy_endpoint", signing_salt: signing_salt, secret_key_base: secret_key_base)
do: Igniter.compose_task(igniter, "beacon.gen.proxy_endpoint", ~w(--signing-salt #{signing_salt} --secret-key-base #{secret_key_base}))

defp maybe_create_new_endpoint(igniter, nil, _, _), do: igniter

Expand Down Expand Up @@ -370,6 +373,22 @@ defmodule Mix.Tasks.Beacon.Gen.Site do
)
end

defp maybe_update_existing_endpoints(igniter, nil, _, _, _, _), do: igniter

defp maybe_update_existing_endpoints(igniter, _host, otp_app, existing_endpoints, signing_salt, secret_key_base) do
Enum.reduce(existing_endpoints, igniter, fn endpoint, acc ->
acc
|> Igniter.Project.Config.configure("config.exs", otp_app, [endpoint, :live_view, :signing_salt], signing_salt)
|> Igniter.Project.Config.configure("dev.exs", otp_app, [endpoint, :secret_key_base], secret_key_base)
end)
end

defp maybe_update_session_options(igniter, nil, _, _), do: igniter

defp maybe_update_session_options(igniter, _host, otp_app, signing_salt) do
Igniter.Project.Config.configure(igniter, "config.exs", otp_app, [:session_options, :signing_salt], signing_salt)
end

defp maybe_add_new_endpoint_to_application(igniter, nil, _), do: igniter

defp maybe_add_new_endpoint_to_application(igniter, host, repo) do
Expand Down
16 changes: 15 additions & 1 deletion test/mix/tasks/gen_site_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
test "updates config.exs", %{project: project} do
project
|> Igniter.compose_task("beacon.gen.site", @opts_host)
# add config for new endpoint
|> assert_has_patch("config/config.exs", """
10 + |config :test, TestWeb.ExampleEndpoint,
11 + | url: [host: "localhost"],
Expand All @@ -225,11 +226,20 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
17 + | pubsub_server: Test.PubSub,
18 + | live_view: [signing_salt: "#{@signing_salt}"]
""")
# update signing salt for host app session_options
|> assert_has_patch("config/config.exs", """
28 + | signing_salt: "#{@signing_salt}",
""")
# update signing salt for existing endpoint
|> assert_has_patch("config/config.exs", """
41 + | live_view: [signing_salt: "#{@signing_salt}"]
""")
end

test "updates dev.exs", %{project: project} do
project
|> Igniter.compose_task("beacon.gen.site", @opts_host)
# add config for new endpoint
|> assert_has_patch("config/dev.exs", """
3 + |config :test, TestWeb.ExampleEndpoint,
4 + | http: [ip: {127, 0, 0, 1}, port: #{@port}],
Expand All @@ -243,6 +253,10 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
12 + | ]
13 + |
""")
# update secret key base for existing endpoint
|> assert_has_patch("config/dev.exs", """
33 + | secret_key_base: "#{@secret_key_base}",
""")
end

test "updates runtime.exs", %{project: project} do
Expand All @@ -256,7 +270,7 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
57 + |
""")
|> assert_has_patch("config/runtime.exs", """
48 + | check_origin: ["example.com"],
48 + | check_origin: [host, "example.com"],
""")
|> assert_has_patch("config/runtime.exs", """
2 + |config :beacon, my_site: [site: :my_site, repo: Test.Repo, endpoint: TestWeb.ExampleEndpoint, router: TestWeb.Router]
Expand Down

0 comments on commit a45e4ad

Please sign in to comment.