From ec5ef59013a819c524ec95004819d9439b4d26bc Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Wed, 31 Jul 2024 14:39:34 -0400 Subject: [PATCH] Support older put_root_layouts API --- lib/mix/tasks/lvn.setup.config.ex | 7 +++++- test/mix/tasks/lvn.setup_test.exs | 37 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/lvn.setup.config.ex b/lib/mix/tasks/lvn.setup.config.ex index 4ce2fd8..7b0af23 100644 --- a/lib/mix/tasks/lvn.setup.config.ex +++ b/lib/mix/tasks/lvn.setup.config.ex @@ -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) diff --git a/test/mix/tasks/lvn.setup_test.exs b/test/mix/tasks/lvn.setup_test.exs index 48bae81..42b1931 100644 --- a/test/mix/tasks/lvn.setup_test.exs +++ b/test/mix/tasks/lvn.setup_test.exs @@ -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, @@ -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