Skip to content

Commit

Permalink
improvement: better step explanation in installer
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 5, 2025
1 parent 95e6ac4 commit 4f113f9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 28 deletions.
27 changes: 23 additions & 4 deletions installer/lib/mix/tasks/igniter.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ if !Code.ensure_loaded?(Mix.Tasks.Igniter.Install) do
@impl true
@shortdoc "Install a package or packages, and run any associated installers."
def run(argv) do
message =
cond do
"--igniter-repeat" in argv ->
"setting up igniter"

"--from-igniter-new" in argv ->
"installing igniter"

true ->
"checking for igniter in project"
end

Igniter.Installer.Task.with_spinner(
"deps.compile",
message,
fn ->
Mix.Task.run("deps.compile")

Expand All @@ -49,6 +61,8 @@ if !Code.ensure_loaded?(Mix.Tasks.Igniter.Install) do
verbose?: "--verbose" in argv
)

argv = Enum.reject(argv, &(&1 in ["--igniter-repeat", "--from-igniter-new"]))

if Code.ensure_loaded?(Igniter.Util.Install) do
{argv, positional} = extract_positional_args(argv)

Expand All @@ -63,7 +77,12 @@ if !Code.ensure_loaded?(Mix.Tasks.Igniter.Install) do

Application.ensure_all_started(:rewrite)

apply(Igniter.Util.Install, :install, [Enum.join(packages, ","), argv])
apply(Igniter.Util.Install, :install, [
Enum.join(packages, ","),
argv,
Igniter.new(),
from_igniter_new?: true
])
else
if File.exists?("mix.exs") do
contents =
Expand Down Expand Up @@ -121,7 +140,7 @@ if !Code.ensure_loaded?(Mix.Tasks.Igniter.Install) do
System.cmd("mix", ["deps.get"])

Igniter.Installer.Task.with_spinner(
"deps.compile",
"compiling igniter",
fn ->
for task <- @tasks, do: Mix.Task.reenable(task)

Expand All @@ -133,7 +152,7 @@ if !Code.ensure_loaded?(Mix.Tasks.Igniter.Install) do
)

Mix.Task.reenable("igniter.install")
Mix.Task.run("igniter.install", argv)
Mix.Task.run("igniter.install", argv ++ ["--igniter-repeat"])
end
else
Mix.shell().error("""
Expand Down
12 changes: 11 additions & 1 deletion installer/lib/mix/tasks/igniter.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ defmodule Mix.Tasks.Igniter.New do
with_args =
[name | OptionParser.split(options[:with_args] || "")]

with_args =
if install_with == "phx.new" do
with_args ++ ["--install", "--from-elixir-install"]
else
with_args
end

with_args =
if options[:module] do
with_args ++ ["--module", options[:module]]
Expand Down Expand Up @@ -199,7 +206,10 @@ defmodule Mix.Tasks.Igniter.New do
_ -> []
end

Mix.Task.run("igniter.install", install_args ++ rest_args ++ ["--yes-to-deps"])
Mix.Task.run(
"igniter.install",
install_args ++ rest_args ++ ["--yes-to-deps", "--from-igniter-new"]
)
end

:ok
Expand Down
4 changes: 2 additions & 2 deletions installer/lib/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ defmodule Igniter.Installer.Task do
|> Stream.map(fn next ->
receive do
{:stop, pid, ref} ->
IO.puts("\r\e[K" <> task_name <> ": " <> "#{IO.ANSI.green()}#{IO.ANSI.reset()}")
IO.puts("\r\e[K" <> task_name <> " " <> "#{IO.ANSI.green()}#{IO.ANSI.reset()}")
send(pid, {:loader_stopped, ref})
exit(:normal)
after
0 ->
IO.write("\r\e[K" <> task_name <> ": " <> next)
IO.write("\r\e[K" <> task_name <> " " <> next)
:timer.sleep(50)
end
end)
Expand Down
10 changes: 8 additions & 2 deletions lib/igniter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,10 @@ defmodule Igniter do
|> accepted_once()

if Keyword.get(opts, :fetch?, true) do
Igniter.Util.Install.get_deps!(igniter, opts)
Igniter.Util.Install.get_deps!(
igniter,
Keyword.put_new(opts, :operation, "installing new dependencies")
)
else
igniter
end
Expand Down Expand Up @@ -864,7 +867,10 @@ defmodule Igniter do
end

igniter =
Igniter.Util.Install.get_deps!(igniter, opts)
Igniter.Util.Install.get_deps!(
igniter,
Keyword.put_new(opts, :operation, "installing new dependencies")
)

%{igniter | rewrite: rewrite}
else
Expand Down
18 changes: 15 additions & 3 deletions lib/igniter/util/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,40 @@ defmodule Igniter.Util.Info do

case schema.installs do
[] ->
names = Enum.map_join(List.wrap(schema.adds_deps), ", ", &elem(&1, 0))

igniter =
igniter
|> add_deps(
List.wrap(schema.adds_deps),
opts
)
|> Igniter.apply_and_fetch_dependencies(opts)
|> Igniter.apply_and_fetch_dependencies(
Keyword.put(opts, :operation, "compiling #{names}")
)

raise_on_conflicts!(schema, argv)

{igniter, Enum.uniq(acc), validate!(argv, schema, task_name)}

installs ->
schema = %{schema | installs: []}
install_names = Keyword.keys(installs)
install_names = Enum.map(installs, &elem(&1, 0))

names_message =
Enum.join(
Enum.uniq(Enum.map(List.wrap(schema.adds_deps), &elem(&1, 0)) ++ install_names),
", "
)

igniter
|> add_deps(
List.wrap(installs),
opts
)
|> Igniter.apply_and_fetch_dependencies(opts)
|> Igniter.apply_and_fetch_dependencies(
Keyword.put(opts, :operation, "compiling #{names_message}")
)
|> maybe_set_only(install_names, argv, task_name)
|> compose_install_and_validate!(
argv,
Expand Down
44 changes: 30 additions & 14 deletions lib/igniter/util/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,36 @@ defmodule Igniter.Util.Install do
append?: Keyword.get(opts, :append?, false)
)

igniter = Igniter.apply_and_fetch_dependencies(igniter, options)
installing_names = Enum.join(installing, ", ")

{available_tasks, available_task_sources} =
igniter =
Igniter.apply_and_fetch_dependencies(
igniter,
Keyword.put(options, :operation, "compiling #{installing_names}")
)

available_tasks =
Enum.zip(installing, Enum.map(installing, &Mix.Task.get("#{&1}.install")))
|> Enum.filter(fn {_desired_task, source_task} -> source_task end)
|> Enum.unzip()

case available_tasks do
[] ->
:ok

[task] ->
[{name, _} = tasks] ->
run_installers(
igniter,
available_task_sources,
"The following installer was found and executed: `#{task}`",
tasks,
"The following installer was found and executed: `#{name}.install`",
argv,
options
)

tasks ->
run_installers(
igniter,
available_task_sources,
"The following installers were found and executed: #{Enum.map_join(tasks, ", ", &"`#{&1}`")}",
tasks,
"The following installers were found and executed: #{Enum.map_join(tasks, ", ", &"`#{elem(&1, 0)}.install`")}",
argv,
options
)
Expand All @@ -124,8 +129,10 @@ defmodule Igniter.Util.Install do

defp run_installers(igniter, igniter_task_sources, title, argv, options) do
igniter_task_sources
|> Enum.reduce(igniter, fn igniter_task_source, igniter ->
Igniter.compose_task(igniter, igniter_task_source, argv)
|> Enum.reduce(igniter, fn {name, task}, igniter ->
igniter = Igniter.compose_task(igniter, task, argv)
Mix.shell().info("`#{name}.install` #{IO.ANSI.green()}#{IO.ANSI.reset()}")
igniter
end)
|> Igniter.do_or_dry_run(Keyword.put(options, :title, title))

Expand Down Expand Up @@ -175,8 +182,12 @@ defmodule Igniter.Util.Install do
end

if Keyword.get(opts, :compile_deps?, true) do
Igniter.Util.Loading.with_spinner("deps.compile", fn ->
Igniter.Util.DepsCompile.run(recompile_igniter?: true, force: opts[:force?])
Igniter.Util.Loading.with_spinner(opts[:operation] || "deps.compile", fn ->
Igniter.Util.DepsCompile.run(
recompile_igniter?: !opts[:from_igniter_new?],
force: opts[:force?]
)

Mix.Task.run("compile")
end)
end
Expand All @@ -186,7 +197,7 @@ defmodule Igniter.Util.Install do
{output, exit_code} ->
case handle_error(output, exit_code, igniter, opts) do
{:ok, igniter} ->
get_deps!(igniter, opts)
get_deps!(igniter, Keyword.put(opts, :name, "applying dependency conflict changes"))

:error ->
Mix.shell().info("""
Expand Down Expand Up @@ -310,7 +321,12 @@ defmodule Igniter.Util.Install do
{:ok,
Igniter.apply_and_fetch_dependencies(
igniter,
Keyword.merge(opts, compile_deps?: false, message: message, error_on_abort?: true)
Keyword.merge(opts,
compile_deps?: false,
operation: "recompiling conflicts",
message: message,
error_on_abort?: true
)
)}
end
else
Expand Down
4 changes: 2 additions & 2 deletions lib/igniter/util/loading.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ defmodule Igniter.Util.Loading do
|> Stream.map(fn next ->
receive do
{:stop, pid, ref} ->
IO.puts("\r\e[K" <> task_name <> ": " <> "#{IO.ANSI.green()}#{IO.ANSI.reset()}")
IO.puts("\r\e[K" <> task_name <> " " <> "#{IO.ANSI.green()}#{IO.ANSI.reset()}")
send(pid, {:loader_stopped, ref})
exit(:normal)
after
0 ->
IO.write("\r\e[K" <> task_name <> ": " <> next)
IO.write("\r\e[K" <> task_name <> " " <> next)
:timer.sleep(50)
end
end)
Expand Down
2 changes: 2 additions & 0 deletions lib/mix/tasks/igniter.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ defmodule Mix.Tasks.Igniter.Install do
verbose?: "--verbose" in argv
)

argv = Enum.reject(argv, &(&1 == "--from-igniter-new"))

{argv, positional} = extract_positional_args(argv)

packages =
Expand Down

0 comments on commit 4f113f9

Please sign in to comment.