Skip to content

Commit

Permalink
fix: simplify how we get tasks to run
Browse files Browse the repository at this point in the history
fix: don't try to format after editing `mix.exs`
  • Loading branch information
zachdaniel committed Jul 9, 2024
1 parent ea66fac commit 73f4893
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 54 deletions.
13 changes: 4 additions & 9 deletions installer/lib/mix/tasks/igniter.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ defmodule Mix.Tasks.Igniter.New do

install_with = options[:with] || "new"

unless install_with in ["phx.new", "new"] do
if String.match?(install_with, ~r/\s/) do
raise ArgumentError, "The --with option must not contain any spaces, got: #{install_with}"
end
if String.match?(install_with, ~r/\s/) do
raise ArgumentError, "The --with option must not contain any spaces, got: #{install_with}"
end

install =
Expand Down Expand Up @@ -77,9 +75,6 @@ defmodule Mix.Tasks.Igniter.New do
|> Code.format_string!()

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

Mix.Task.run("format")
Mix.Task.reenable("format")
end

unless Enum.empty?(install) do
Expand Down Expand Up @@ -130,8 +125,8 @@ defmodule Mix.Tasks.Igniter.New do
defp dont_consolidate_protocols_in_dev(contents) do
String.replace(
contents,
"elixir: \"~> 1.17\",\n",
"elixir: \"~> 1.17\",\n consolidate_protocols: Mix.env() != :dev,\n"
"start_permanent: Mix.env() == :prod,\n",
"start_permanent: Mix.env() == :prod,\n consolidate_protocols: Mix.env() != :dev,\n"
)
end

Expand Down
48 changes: 3 additions & 45 deletions lib/igniter/util/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,9 @@ defmodule Igniter.Util.Install do
igniter = Igniter.apply_and_fetch_dependencies(igniter, options)

igniter_tasks =
Mix.Task.load_all()
|> Stream.map(fn item ->
Code.ensure_compiled!(item)
item
end)
|> Stream.filter(&implements_behaviour?(&1, Igniter.Mix.Task))
|> Enum.filter(&(Mix.Task.task_name(&1) in desired_tasks))
|> Enum.sort_by(
&Enum.find_index(desired_tasks, fn e -> e == Mix.Task.task_name(&1) end),
&<=/2
)
desired_tasks
|> Enum.map(&Mix.Task.get/1)
|> Enum.filter(& &1)

title =
case desired_tasks do
Expand All @@ -81,40 +73,6 @@ defmodule Igniter.Util.Install do
:ok
end

def implements_behaviour?(module, behaviour) do
:attributes
|> module.module_info()
|> Enum.any?(fn
{:behaviour, ^behaviour} ->
true

# optimizations, probably extremely minor but this is in a tight loop in some places
{:behaviour, [^behaviour | _]} ->
true

{:behaviour, [_, ^behaviour | _]} ->
true

{:behaviour, [_, _, ^behaviour | _]} ->
true

# never seen a module with three behaviours in real life, let alone four.
{:behaviour, behaviours} when is_list(behaviours) ->
module in behaviours

_ ->
false
end)
rescue
_ ->
false
end

# defp local_dep?(install) do
# config = Mix.Project.config()[:deps][install]
# Keyword.keyword?(config) && config[:path]
# end

defp determine_dep_type_and_version(requirement) do
case String.split(requirement, "@", trim: true, parts: 2) do
[package] ->
Expand Down

0 comments on commit 73f4893

Please sign in to comment.