Skip to content

Commit

Permalink
improvement: support :kind in find_and_update_or_create_module/5 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibarakaiev authored Jul 10, 2024
1 parent b4b7655 commit 8e71283
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions lib/igniter/code/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@ defmodule Igniter.Code.Module do
alias Igniter.Code.Common
alias Sourceror.Zipper

@doc "Finds a module and updates its contents wherever it is. If it does not exist, it is created with the provided contents."
def find_and_update_or_create_module(igniter, module_name, contents, updater, path \\ nil) do
require Logger

@doc """
Finds a module and updates its contents wherever it is.
If the module does not yet exist, it is created with the provided contents. In that case,
the path is determined with `Igniter.Code.Module.proper_location/2`, but may optionally be overwritten with options below.
# Options
- `:path` - Path where to create the module, relative to the project root. Default: `nil` (uses `:kind` to determine the path).
- `:kind` - Type of module being created. Possible values: `:lib`, `:test`, `:test_support` (default: `:lib`). Ignored if `:path` is set.
"""
def find_and_update_or_create_module(igniter, module_name, contents, updater, opts \\ [])

def find_and_update_or_create_module(
igniter,
module_name,
contents,
updater,
opts
)
when is_list(opts) do
case find_and_update_module(igniter, module_name, updater) do
{:ok, igniter} ->
igniter
Expand All @@ -18,10 +39,35 @@ defmodule Igniter.Code.Module do
end
"""

Igniter.create_new_elixir_file(igniter, path || proper_location(module_name), contents)
location =
case Keyword.get(opts, :path, nil) do
nil ->
case Keyword.get(opts, :kind, :lib) do
:lib -> proper_location(module_name)
:test -> proper_test_location(module_name)
:test_support -> proper_test_support_location(module_name)
end

path ->
path
end

Igniter.create_new_elixir_file(igniter, location, contents)
end
end

def find_and_update_or_create_module(
igniter,
module_name,
contents,
updater,
path
)
when is_binary(path) do
Logger.warning("You should use `opts` instead of `path` and pass `path` as a keyword.")
find_and_update_or_create_module(igniter, module_name, contents, updater, path: path)
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 8e71283

Please sign in to comment.