Skip to content

Commit

Permalink
Support older put_root_layouts API
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella committed Jul 31, 2024
1 parent 3cd5f4e commit ec5ef59
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/mix/tasks/lvn.setup.config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,12 @@ defmodule Mix.Tasks.Lvn.Setup.Config do

%{node: {:plug, _, [{:__block__, _, [:put_root_layout]}, quoted_root_layouts]} = quoted_plug}->
range = Sourceror.get_range(quoted_plug)
old_root_layouts = Code.eval_quoted(quoted_root_layouts) |> elem(0)
old_root_layouts =
(Code.eval_quoted(quoted_root_layouts) |> elem(0))
|> case do
old_root_layout when is_tuple(old_root_layout) -> [html: old_root_layout]
old_root_layouts -> old_root_layouts
end

root_layouts =
(old_root_layouts ++ new_root_layouts)
Expand Down
37 changes: 36 additions & 1 deletion test/mix/tasks/lvn.setup_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ defmodule Mix.Tasks.Lvn.SetupTest do
"""
end

test "when :phonex config exists the :template_engines list is updated and duplicates are removed" do
test "when :phoenix config exists the :template_engines list is updated and duplicates are removed" do
config = """
config :phoenix, :template_engines, [
other: Other.Engine,
Expand All @@ -337,4 +337,39 @@ defmodule Mix.Tasks.Lvn.SetupTest do
"""
end
end

describe "router codgen scenarios" do
test "patch_layouts when this old style of router layout option is being used, rewrite as the new keyword list with html" do
config = """
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, {LiveViewNativeWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
"""

{_, {result, _}} = Config.patch_root_layouts({%{}, {config, "live_view_native_web/router.ex"}})

assert result =~ """
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout,
gameboy: {LiveViewNativeWeb.Layouts.GameBoy, :root},
html: {LiveViewNativeWeb.Layouts, :root},
switch: {LiveViewNativeWeb.Layouts.Switch, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
"""
end
end
end

0 comments on commit ec5ef59

Please sign in to comment.