From 20f03931f370b71f5420296a78a0674931833683 Mon Sep 17 00:00:00 2001 From: Tiago Moraes Date: Tue, 26 Dec 2023 15:12:36 -0300 Subject: [PATCH] Add Surface generator --- lib/utility/gen_diff/data.ex | 63 +++++++++++-- lib/utility/gen_diff/generator.ex | 17 +++- lib/utility/hex/api.ex | 1 + lib/utility/project_builder.ex | 92 ++++++++++++++++++- .../gen_diff_html/diff_header.html.heex | 4 +- 5 files changed, 165 insertions(+), 12 deletions(-) diff --git a/lib/utility/gen_diff/data.ex b/lib/utility/gen_diff/data.ex index eee7b61..d3590b1 100644 --- a/lib/utility/gen_diff/data.ex +++ b/lib/utility/gen_diff/data.ex @@ -125,6 +125,34 @@ defmodule Utility.GenDiff.Data do } ] }, + "surface" => %{ + url: "https://hex.pm/packages/surface", + source: :hex, + generators: [ + %{ + command: "surface.init", + since: "0.6.0", + docs_url: "https://hexdocs.pm/surface/Mix.Tasks.Surface.Init.html", + default_flags: [ + {"--yes", [from: "0.6.0"]}, + {"--no-install", [from: "0.8.0"]}, + {"--no-dep-install", [from: "0.6.0", until: "0.8.0"]} + ], + help: + "Ran on Phoenix 1.7.7 project when version >= 0.10.0 and Phoenix 1.6.16 on prior versions, main also runs on Phoenix main.", + flags: [ + {"--catalogue", [from: "0.6.0"]}, + {"--demo", [from: "0.6.0"]}, + {"--layouts", [from: "0.8.0"]}, + {"--no-error-tag", [from: "0.6.0"]}, + {"--no-formatter", [from: "0.6.0"]}, + {"--no-js-hooks", [from: "0.6.0"]}, + {"--no-scoped-css", [from: "0.8.0"]}, + {"--tailwind", [from: "0.8.0", until: "0.9.4"]} + ] + } + ] + }, "rails" => %{ url: "https://guides.rubyonrails.org/", source: :gem, @@ -274,12 +302,32 @@ defmodule Utility.GenDiff.Data do end end - def default_flags_for_command(nil, _), do: [] + def default_flags_for_command(nil, _, _), do: [] + def default_flags_for_command(_, nil, _), do: [] - def default_flags_for_command(project, command) do - case get_by(project: project, command: command) do - %{default_flags: default_flags} -> default_flags - _ -> [] + def default_flags_for_command(project, command, version) do + version = if version in ["main", "master"], do: "9999.0.0", else: version + + case {version, get_by(project: project, command: command)} do + {nil, %{default_flags: [{_, _} | _] = default_flags}} -> + Enum.map(default_flags, &elem(&1, 0)) + + {_version, %{default_flags: [{_, _} | _] = default_flags}} -> + default_flags + |> Enum.filter(fn + {_flag, [from: from, until: until]} -> + Version.compare(version, from) != :lt && Version.compare(version, until) == :lt + + {_flag, [from: from]} -> + Version.compare(version, from) != :lt + end) + |> Enum.map(&elem(&1, 0)) + + {_version, %{default_flags: default_flags}} -> + default_flags + + _ -> + [] end end @@ -310,7 +358,10 @@ defmodule Utility.GenDiff.Data do defp limit_versions(versions, project, command) do case get_by(project: project, command: command) do %{since: since_version} -> - Enum.filter(versions, &(Version.compare(&1, since_version) != :lt)) + Enum.filter( + versions, + &(&1 in ["main", "master"] or Version.compare(&1, since_version) != :lt) + ) _ -> versions diff --git a/lib/utility/gen_diff/generator.ex b/lib/utility/gen_diff/generator.ex index b230fc7..12ecde9 100644 --- a/lib/utility/gen_diff/generator.ex +++ b/lib/utility/gen_diff/generator.ex @@ -20,7 +20,8 @@ defmodule Utility.GenDiff.Generator do field :id, :string field :url, :string field :docs_url, :string - field :default_flags, {:array, :string} + field :from_default_flags, {:array, :string} + field :to_default_flags, {:array, :string} field :flags, {:array, :string} field :help, :string field :since, :string @@ -146,15 +147,25 @@ defmodule Utility.GenDiff.Generator do def put_defaults_for_command(changeset) do if command = get_field(changeset, :command) do project = get_field(changeset, :project) + from_version = get_field(changeset, :from_version) + to_version = get_field(changeset, :to_version) changeset - |> put_change(:default_flags, Data.default_flags_for_command(project, command)) + |> put_change( + :from_default_flags, + Data.default_flags_for_command(project, command, from_version) + ) + |> put_change( + :to_default_flags, + Data.default_flags_for_command(project, command, to_version) + ) |> put_change(:flags, Data.flags_for_command(project, command)) |> put_change(:help, Data.help_for_command(project, command)) |> put_change(:docs_url, Data.docs_url_for_command(project, command)) else changeset - |> put_change(:default_flags, []) + |> put_change(:from_default_flags, []) + |> put_change(:to_default_flags, []) |> put_change(:flags, []) end end diff --git a/lib/utility/hex/api.ex b/lib/utility/hex/api.ex index 0b3e738..c833ca7 100644 --- a/lib/utility/hex/api.ex +++ b/lib/utility/hex/api.ex @@ -42,5 +42,6 @@ defmodule Utility.Hex.Api do defp get_nonhex_versions(versions, _), do: versions defp get_nonstandard_versions(versions, "phx_new"), do: versions ++ ["main"] + defp get_nonstandard_versions(versions, "surface"), do: versions ++ ["main"] defp get_nonstandard_versions(versions, _), do: versions end diff --git a/lib/utility/project_builder.ex b/lib/utility/project_builder.ex index 9ead14f..a2a400e 100644 --- a/lib/utility/project_builder.ex +++ b/lib/utility/project_builder.ex @@ -111,7 +111,7 @@ defmodule Utility.ProjectBuilder do run_command( command, version, - Data.default_flags_for_command(project, command) ++ flags + Data.default_flags_for_command(project, command, version) ++ flags ) ], " && " @@ -168,6 +168,10 @@ defmodule Utility.ProjectBuilder do "mix archive.install --force hex phx_new 1.5.7" end + def install_archive("surface", version) do + install_archive("phx_new", surface_phoenix_version(version)) + end + def install_archive("credo", _version), do: "true" def install_archive("rails", version) do @@ -213,6 +217,28 @@ defmodule Utility.ProjectBuilder do |> String.trim() end + def run_command("surface.init", version_string, flags) do + phoenix_version = surface_phoenix_version(version_string) + {extra_deps, extra_replaces} = surface_deps_changes(version_string) + + deps_replaces = + if version_string == "main" do + ~s|sed -i -E 's/\\{:phoenix, "~> (.*)"\\},/{:phoenix, github: "phoenixframework\\/phoenix", override: true}, # Manually changed#{extra_deps}\\n {:surface, github: "surface-ui\\/surface"}, # Manually changed/g' my_app/mix.exs| + else + ~s|sed -i 's/{:phoenix, "~> #{phoenix_version}"},/{:phoenix, "~> #{phoenix_version}"},#{extra_deps}\\n {:surface, "#{version_string}"}, # Manually added/g' my_app/mix.exs| + end + + """ + #{run_command("phx.new", phoenix_version, ["my_app", "--no-ecto", "--no-dashboard"])} && + #{deps_replaces} && + #{extra_replaces} + cd my_app && + mix deps.get && + mix surface.init #{Enum.join(flags, " ")} && + rm -rf _build deps mix.lock + """ + end + def run_command("phx.new", "master", flags), do: run_command("phx.new", "999.0.0", flags) def run_command("phx.new", "main", flags), do: run_command("phx.new", "999.0.0", flags) @@ -345,6 +371,10 @@ defmodule Utility.ProjectBuilder do end end + def docker_tag_for("surface.init", version) do + docker_tag_for("phx.new", surface_phoenix_version(version)) + end + def docker_tag_for("rails new", _version), do: "rails" def docker_tag_for("rails webpacker:install", _version), do: "rails" def docker_tag_for(_command, _version), do: "latest" @@ -356,4 +386,64 @@ defmodule Utility.ProjectBuilder do prefix <> Base.encode16(:crypto.strong_rand_bytes(4)) ]) end + + def surface_phoenix_version("main"), do: "main" + + def surface_phoenix_version(version) when is_binary(version) do + cond do + Version.compare(version, "0.10.0") != :lt -> "1.7.7" + true -> "1.6.16" + end + end + + def surface_deps_changes("main"), do: {nil, nil} + + def surface_deps_changes(version_string) do + version = Version.parse!(version_string) + + extra_deps = + cond do + Version.compare(version, "0.11.1") != :lt -> + nil + + Version.compare(version, "0.10.0") != :lt -> + ~s'\\n {:sourceror, "~> 0.12.0"}, # Manually added' + + Version.compare(version, "0.7.0") != :lt -> + ~s'\\n {:sourceror, "~> 0.11.0"}, # Manually added' + + true -> + ~s'\\n {:phoenix_html, "~> 3.2.0"}, # Manually added' + end + + extra_replaces = + cond do + Version.compare(version, "0.11.0") != :lt -> + nil + + Version.compare(version, "0.10.0") != :lt -> + """ + sed -i 's/{:phoenix_live_view, "~> 0.19.0"},/{:phoenix_live_view, "~> 0.18.18"}, # Manually changed/g' my_app/mix.exs && + """ + |> String.trim() + + Version.compare(version, "0.9.0") != :lt -> + """ + sed -i 's/{:phoenix_live_view, "~> 0.17.5"},/{:phoenix_live_view, "0.18.16"}, # Manually changed/g' my_app/mix.exs && + sed -i 's/import Phoenix.LiveView.Helpers/import Phoenix.LiveView.Helpers\\n import Phoenix.Component # Manually added/g' my_app/lib/my_app_web.ex && + """ + |> String.trim() + + Version.compare(version, "0.7.0") != :lt -> + nil + + true -> + """ + sed -i 's/{:phoenix_live_view, "~> 0.17.5"},/{:phoenix_live_view, "~> 0.16.0"}, # Manually changed/g' my_app/mix.exs && + """ + |> String.trim() + end + + {extra_deps, extra_replaces} + end end diff --git a/lib/utility_web/controllers/gen_diff_html/diff_header.html.heex b/lib/utility_web/controllers/gen_diff_html/diff_header.html.heex index c811da9..5414510 100644 --- a/lib/utility_web/controllers/gen_diff_html/diff_header.html.heex +++ b/lib/utility_web/controllers/gen_diff_html/diff_header.html.heex @@ -2,11 +2,11 @@