Skip to content

Commit

Permalink
Merge pull request #35 from curiosum-dev/fix/compile-to-runtime-config
Browse files Browse the repository at this point in the history
Fixes difference in config values when recompiling project
  • Loading branch information
szsoppa authored Oct 25, 2023
2 parents d48a72d + 82a90bb commit 187cb8e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Add the following to your `mix.exs` file:
```elixir
defp deps do
[
{:contexted, "~> 0.2.0"}
{:contexted, "~> 0.2.1"}
]
end
```
Expand Down
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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Contexted.MixProject do
app: :contexted,
description:
"Contexted is an Elixir library designed to streamline the management of complex Phoenix contexts in your projects, offering tools for module separation, subcontext creation, and auto-generating CRUD operations for improved code maintainability.",
version: "0.2.0",
version: "0.2.1",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down

0 comments on commit 187cb8e

Please sign in to comment.