Skip to content

Commit

Permalink
improvement: add create_module utility
Browse files Browse the repository at this point in the history
fix: loadpaths after compiling deps
  • Loading branch information
zachdaniel committed Jul 31, 2024
1 parent c905ead commit c769fe1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
6 changes: 4 additions & 2 deletions installer/lib/mix/tasks/igniter.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ defmodule Mix.Tasks.Igniter.New do
"--example"
end

install_args = Enum.filter([Enum.join(install, ","), "--yes", example], & &1)
install_args =
Enum.filter([Enum.join(install, ","), "--yes", example], & &1)

System.cmd("mix", ["deps.get"])
System.cmd("mix", ["igniter.install" | install_args], use_stdio: false)
System.cmd("mix", ["deps.compile"])
System.cmd("mix", ["igniter.install" | install_args])
end

:ok
Expand Down
2 changes: 2 additions & 0 deletions installer/lib/private/deps_compile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ defmodule Igniter.Util.DepsCompile do
[include_children: true]

compile(filter_available_and_local_deps(deps), opts)
Mix.Task.reenable("deps.loadpaths")
Mix.Task.run("deps.loadpaths", ["--no-deps-check"])
end

@doc false
Expand Down
39 changes: 22 additions & 17 deletions lib/igniter/code/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,7 @@ defmodule Igniter.Code.Module do
igniter

{:error, igniter} ->
contents =
"""
defmodule #{inspect(module_name)} do
#{contents}
end
"""

location =
case Keyword.get(opts, :path, nil) do
nil ->
proper_location(module_name)

path ->
path
end

Igniter.create_new_elixir_file(igniter, location, contents)
create_module(igniter, module_name, contents, opts)
end
end

Expand All @@ -63,6 +47,27 @@ defmodule Igniter.Code.Module do
find_and_update_or_create_module(igniter, module_name, contents, updater, path: path)
end

@doc "Creates a new file & module in its appropriate location."
def create_module(igniter, module_name, contents, opts \\ []) do
contents =
"""
defmodule #{inspect(module_name)} do
#{contents}
end
"""

location =
case Keyword.get(opts, :path, nil) do
nil ->
proper_location(module_name)

path ->
path
end

Igniter.create_new_elixir_file(igniter, location, contents)
end

@doc "Checks if a module is defined somewhere in the project. The returned igniter should not be discarded."
def module_exists?(igniter, module_name) do
case find_module(igniter, module_name) do
Expand Down

0 comments on commit c769fe1

Please sign in to comment.