Skip to content

Commit

Permalink
improvement: make installer use override: true on local dependency
Browse files Browse the repository at this point in the history
improvement: ensure dependencies are compiled after `mix deps.get`

(sorry about the dirty dirty hack required there)
  • Loading branch information
zachdaniel committed Jun 20, 2024
1 parent 0590e44 commit 338cb72
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
12 changes: 7 additions & 5 deletions installer/lib/mix/tasks/igniter.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Mix.Tasks.Igniter.New do
version_requirement =
if options[:local] do
local = Path.join(["..", Path.relative_to_cwd(options[:local])])
"path: #{inspect(local)}"
"path: #{inspect(local)}, override: true"
else
inspect(version_requirement())
end
Expand All @@ -58,20 +58,22 @@ defmodule Mix.Tasks.Igniter.New do
if String.contains?(contents, "{:igniter") do
Mix.shell().info("It looks like the project already exists and igniter is already installed, not adding it to deps.")
else
# the spaces are required here to avoid the need for a format
new_contents =
String.replace(contents, "defp deps do\n [\n", "defp deps do\n [\n{:igniter, #{version_requirement}}\n")
String.replace(contents, "defp deps do\n [\n", "defp deps do\n [\n {:igniter, #{version_requirement}}\n")

File.write!("mix.exs", new_contents)
end

Mix.shell().cmd("mix deps.get")
Mix.shell().cmd("mix compile")

unless Enum.empty?(install) do
Mix.shell().cmd("mix deps.get")
Mix.shell().cmd("mix compile")

example =
if options[:example] do
"--example"
end

Mix.shell().cmd("mix igniter.install #{Enum.join(install, ",")} --yes #{example}")
end

Expand Down
35 changes: 22 additions & 13 deletions lib/util/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Igniter.Util.Install do
]
]

# sobelow_skip ["DOS.StringToAtom"]
# sobelow_skip ["DOS.StringToAtom", "RCE.CodeModule"]
def install(install, argv) do
install_list =
if is_binary(install) do
Expand Down Expand Up @@ -100,28 +100,37 @@ defmodule Igniter.Util.Install do
Mix.Task.reenable("compile")
Mix.Task.run("compile")

Mix.Project.clear_deps_cache()
Mix.Project.pop()

"mix.exs"
|> File.read!()
|> Code.eval_string([], file: Path.expand("mix.exs"))

Mix.Task.run("deps.compile", Enum.map(install_list, &to_string/1))

Mix.Task.reenable("compile")
Mix.Task.run("compile")

exit_code ->
Mix.shell().info("""
mix deps.get returned exited with code: `#{exit_code}`
""")
end

all_tasks =
Enum.filter(Mix.Task.load_all(), &implements_behaviour?(&1, Igniter.Mix.Task))

igniter =
Igniter.new()
|> Igniter.assign(%{manually_installed: install_list})

install_list
|> Enum.flat_map(fn install ->
all_tasks
|> Enum.find(fn task ->
Mix.Task.task_name(task) == "#{install}.install" &&
implements_behaviour?(task, Igniter.Mix.Task)
end)
|> List.wrap()
desired_tasks = Enum.map(install_list, &"#{&1}.install")

Mix.Task.load_all()
|> Stream.map(fn item ->
Code.ensure_compiled!(item)
item
end)
|> Stream.filter(&implements_behaviour?(&1, Igniter.Mix.Task))
|> Stream.filter(&(Mix.Task.task_name(&1) in desired_tasks))
|> Enum.reduce(igniter, fn task, igniter ->
Igniter.compose_task(igniter, task, argv)
end)
Expand All @@ -131,7 +140,7 @@ defmodule Igniter.Util.Install do
:ok
end

defp implements_behaviour?(module, behaviour) do
def implements_behaviour?(module, behaviour) do
:attributes
|> module.module_info()
|> Enum.any?(fn
Expand Down

0 comments on commit 338cb72

Please sign in to comment.