Skip to content

Commit

Permalink
Add --from-elixir-install
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Dec 2, 2024
1 parent e99f657 commit 9918ea6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
3 changes: 2 additions & 1 deletion installer/lib/mix/tasks/phx.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ defmodule Mix.Tasks.Phx.New do
install: :boolean,
prefix: :string,
mailer: :boolean,
adapter: :string
adapter: :string,
from_elixir_install: :boolean,
]

@impl true
Expand Down
39 changes: 35 additions & 4 deletions installer/lib/phx_new/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ defmodule Phx.New.Generator do
tailwind = Keyword.get(opts, :tailwind, assets)
mailer = Keyword.get(opts, :mailer, true)
dev = Keyword.get(opts, :dev, false)
from_elixir_install = Keyword.get(opts, :from_elixir_install, false)
phoenix_path = phoenix_path(project, dev, false)
phoenix_path_umbrella_root = phoenix_path(project, dev, true)

Expand All @@ -188,7 +189,8 @@ defmodule Phx.New.Generator do
{adapter_app, adapter_module, adapter_config} =
get_ecto_adapter(db, String.downcase(project.app), project.app_mod)

{web_adapter_app, web_adapter_vsn, web_adapter_module, web_adapter_docs} = get_web_adapter(web_adapter)
{web_adapter_app, web_adapter_vsn, web_adapter_module, web_adapter_docs} =
get_web_adapter(web_adapter)

pubsub_server = get_pubsub_server(project.app_mod)

Expand Down Expand Up @@ -238,12 +240,33 @@ defmodule Phx.New.Generator do
web_adapter_docs: web_adapter_docs,
generators: nil_if_empty(project.generators ++ adapter_generators(adapter_config)),
namespaced?: namespaced?(project),
dev: dev
dev: dev,
from_elixir_install: from_elixir_install,
elixir_install_otp_bin_path: from_elixir_install && elixir_install_otp_bin_path(),
elixir_install_bin_path: from_elixir_install && elixir_install_bin_path()
]

%Project{project | binding: binding}
end

def elixir_install_otp_bin_path do
"erl"
|> System.find_executable()
|> Path.split()
|> Enum.drop(-1)
|> Path.join()
|> Path.relative_to(System.user_home())
end

def elixir_install_bin_path do
"elixir"
|> System.find_executable()
|> Path.split()
|> Enum.drop(-1)
|> Path.join()
|> Path.relative_to(System.user_home())
end

defp namespaced?(project) do
Macro.camelize(project.app) != inspect(project.app_mod)
end
Expand Down Expand Up @@ -304,8 +327,16 @@ defmodule Phx.New.Generator do
Mix.raise("Unknown database #{inspect(db)}")
end

defp get_web_adapter("cowboy"), do: {:plug_cowboy, "~> 2.7", Phoenix.Endpoint.Cowboy2Adapter, "https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html"}
defp get_web_adapter("bandit"), do: {:bandit, "~> 1.5", Bandit.PhoenixAdapter, "https://hexdocs.pm/bandit/Bandit.html#t:options/0"}
defp get_web_adapter("cowboy"),
do:
{:plug_cowboy, "~> 2.7", Phoenix.Endpoint.Cowboy2Adapter,
"https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html"}

defp get_web_adapter("bandit"),
do:
{:bandit, "~> 1.5", Bandit.PhoenixAdapter,
"https://hexdocs.pm/bandit/Bandit.html#t:options/0"}

defp get_web_adapter(other), do: Mix.raise("Unknown web adapter #{inspect(other)}")

defp fs_db_config(app, module) do
Expand Down
71 changes: 70 additions & 1 deletion installer/templates/phx_web/controllers/page_html/home.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,76 @@
fill="#A41C42"
fill-opacity=".2"
/>
</svg>
</svg><%= if @from_elixir_install do %>
<div class="sm:py-28 xl:py-32">
<div class="relative px-8">
<div class="relative space-y-6 bg-black/50 px-6 pt-1 pb-6 rounded-xl">
<span class="flex absolute h-6 w-6 -top-1 -left-2 -mt-1 -mr-1">
<span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-white">
</span>
<span class="relative inline-flex rounded-full h-6 w-6 bg-white/80"></span>
</span>
<h2 class="text-4xl text-white font-semibold">
Finish your Elixir setup!
</h2>
<p class="text-lg text-gray-300"><%= if match?({:win32, _}, :os.type()) do %>
Elixir has been installed to
<code class="text-[1rem] px-2 py-1 rounded-md bg-black/50">
%USERPROFILE%\.elixir-install
</code>
and activated in your current shell. <br />
<br /> Add to your
<code class="text-[1rem] px-2 py-1 rounded-md bg-black/50">%PROFILE</code>
or similar to ensure it is always activated:<% else %>
Elixir has been installed to
<code class="text-[1rem] px-2 py-1 rounded-md bg-black/50">~/.elixir-install</code>
and activated in your current shell. <br />
<br /> Add to your
<code class="text-[1rem] px-2 py-1 rounded-md bg-black/50">~/.bashrc</code>
or similar to ensure it is always activated:<% end %>
</p>
<div class="relative text-md text-gray-300 p-2 rounded-md bg-black/50">
<button
class="absolute -right-2 -top-2 p-1 bg-white/70 hover:bg-white rounded-full"
onclick="navigator.clipboard.writeText(document.getElementById('setup-elixir').innerText)"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="size-5 stroke-black/80"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.666 3.888A2.25 2.25 0 0 0 13.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 0 1-.75.75H9a.75.75 0 0 1-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 0 1-2.25 2.25H6.75A2.25 2.25 0 0 1 4.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 0 1 1.927-.184"
/>
</svg>
</button>
<%= if match?({:win32, _}, :os.type()) do %><code
id="setup-elixir"
class="block px-1 overflow-x-auto whitespace-pre"
phx-no-format
># If you are using powershell, add:

$env:PATH = "$env:USERPROFILE\<%= @elixir_install_otp_bin_path %>;$env:PATH"
$env:PATH = "$env:USERPROFILE\<%= @elixir_install_bin_path %>;$env:PATH"

# If you are using cmd, add:

set PATH=%%USERPROFILE%%\<%= @elixir_install_otp_bin_path %>;%%PATH%%
set PATH=%%USERPROFILE%%\<%= @elixir_install_bin_path %>;%%PATH%%</code><% else %><code
id="setup-elixir"
class="block px-1 overflow-x-auto whitespace-pre"
phx-no-format
>export PATH=$HOME/<%= @elixir_install_otp_bin_path %>:$PATH
export PATH=$HOME/<%= @elixir_install_bin_path %>:$PATH</code><% end %>
</div>
</div>
</div>
</div><% end %>
</div>
<div class="px-4 py-10 sm:px-6 sm:py-28 lg:px-8 xl:px-28 xl:py-32">
<div class="mx-auto max-w-xl lg:mx-0">
Expand Down

0 comments on commit 9918ea6

Please sign in to comment.