Skip to content

Commit

Permalink
Fixes difference in config values when recompiling project
Browse files Browse the repository at this point in the history
  • Loading branch information
szsoppa committed Oct 25, 2023
1 parent d48a72d commit 466c1e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
16 changes: 7 additions & 9 deletions lib/contexted/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ defmodule Contexted.Tracer do
This is useful when you want to ensure that certain modules do not reference each other directly within your application.
"""

@contexts Application.compile_env(:contexted, :contexts, [])
@exclude_paths Application.compile_env(:contexted, :exclude_paths, [])
alias Contexted.Utils

@doc """
Trace events are emitted during compilation.
Expand Down Expand Up @@ -52,15 +51,15 @@ defmodule Contexted.Tracer do

@spec extract_beam_files_folder :: String.t()
defp extract_beam_files_folder do
first_context = List.first(@contexts)
first_context = Utils.get_config_contexts() |> List.first()
compiled_file_path = :code.which(first_context) |> List.to_string()
compiled_file_name = Path.basename(compiled_file_path)
String.replace(compiled_file_path, compiled_file_name, "")
end

@spec remove_context_beams_and_return_module_paths :: list(String.t())
defp remove_context_beams_and_return_module_paths do
@contexts
Utils.get_config_contexts()
|> Enum.map(fn module ->
file_path = module.__info__(:compile)[:source] |> List.to_string()

Expand Down Expand Up @@ -96,7 +95,8 @@ defmodule Contexted.Tracer do

@spec map_module_to_context_module(module()) :: module() | nil
defp map_module_to_context_module(module) do
Enum.find(@contexts, fn context ->
Utils.get_config_contexts()
|> Enum.find(fn context ->
regex = build_regex(context)
stringified_module = Atom.to_string(module)

Expand All @@ -106,10 +106,8 @@ defmodule Contexted.Tracer do

@spec is_file_excluded_from_check?(String.t()) :: boolean()
defp is_file_excluded_from_check?(file) do
Enum.any?(
@exclude_paths,
&String.contains?(file, &1)
)
Utils.get_config_exclude_paths()
|> Enum.any?(&String.contains?(file, &1))
end

@spec build_regex(module()) :: Regex.t()
Expand Down
19 changes: 17 additions & 2 deletions lib/contexted/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ defmodule Contexted.Utils do
Checks is `enable_recompilation` option is set.
"""
@spec recompilation_enabled? :: boolean()
def recompilation_enabled? do
Application.get_env(:contexted, :enable_recompilation, false)
def recompilation_enabled?, do: get_from_config(:enable_recompilation, false)

@doc """
Returns `contexts` option value from contexted config or `[]` if it's not set.
"""
@spec get_config_contexts :: list(module())
def get_config_contexts, do: get_from_config(:contexts, [])

@doc """
Returns `exclude_paths` option value from contexted config or [] if it's not set.
"""
@spec get_config_exclude_paths :: list(String.t())
def get_config_exclude_paths, do: get_from_config(:exclude_paths, [])

@spec get_from_config(atom(), any()) :: any()
defp get_from_config(option_name, default_value) do
Application.get_env(:contexted, option_name, default_value)
end
end

0 comments on commit 466c1e6

Please sign in to comment.