Skip to content

Commit

Permalink
Merge pull request #29 from tiagoefmoraes/add_surface
Browse files Browse the repository at this point in the history
Add Surface generator
  • Loading branch information
dbernheisel authored Jan 1, 2024
2 parents 3f7e762 + 20f0393 commit 2d1ac3f
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 12 deletions.
63 changes: 57 additions & 6 deletions lib/utility/gen_diff/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions lib/utility/gen_diff/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/utility/hex/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
92 changes: 91 additions & 1 deletion lib/utility/project_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
],
" && "
Expand Down Expand Up @@ -177,6 +177,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
Expand Down Expand Up @@ -222,6 +226,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)

Expand Down Expand Up @@ -357,6 +383,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"
Expand All @@ -368,4 +398,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<div class="px-4 py-5 sm:p-6">
<ul>
<li>
<code class="ghd-line-type-remove">VERSION=<%= @generator.from_version %> <%= @generator.command %> <%= Enum.join(@generator.default_flags || [], " ") %> <%= Enum.join(@generator.from_flags || [], " ") %></code>
<code class="ghd-line-type-remove">VERSION=<%= @generator.from_version %> <%= @generator.command %> <%= Enum.join(@generator.from_default_flags || [], " ") %> <%= Enum.join(@generator.from_flags || [], " ") %></code>
</li>

<li>
<code class="ghd-line-type-add">VERSION=<%= @generator.to_version %> <%= @generator.command %> <%= Enum.join(@generator.default_flags || [], " ") %> <%= Enum.join(@generator.to_flags || [], " ") %></code>
<code class="ghd-line-type-add">VERSION=<%= @generator.to_version %> <%= @generator.command %> <%= Enum.join(@generator.from_default_flags || [], " ") %> <%= Enum.join(@generator.to_flags || [], " ") %></code>
</li>
</ul>
</div>
Expand Down

0 comments on commit 2d1ac3f

Please sign in to comment.