Skip to content

Commit

Permalink
Changes implementation of how beam folder path is fetched in Tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
szsoppa committed Nov 8, 2024
1 parent 460142d commit 51cf78b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ App.Account.find_user(1)

Both docs and specs are attached as metadata of module once it's compiled and saved as `.beam`. In reference to the example of `App.Account` context, it's possible that `App.Account.Users` will not be saved in `.beam` file before the `delegate_all` macro is executed. Therefore, first, all of the modules have to be compiled, and saved to `.beam` and only then we can create `@doc` and `@spec` of each delegated function.

As a workaround, in `Contexted.Tracer.after_compiler/1` all of the contexts `.beam` files are first deleted and then recompiled. This is an opt-in functionality, as it extends compilation time. If you want to enable it, set the following config value:
As a workaround, in `Contexted.Tracer.after_compiler/1` all of the contexts `.beam` files are first deleted and then recompiled. This is an opt-in functionality, as it extends compilation time. If you want to enable it, set the following config values:

```elixir
config :contexted,
app: :your_app_name, # replace 'your_app_name' with your real app name
enable_recompilation: true
```

Expand Down
16 changes: 8 additions & 8 deletions lib/contexted/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ defmodule Contexted.Tracer do
"""
@spec after_compiler(tuple()) :: tuple()
def after_compiler({status, diagnostics}) do
beam_files_folder = extract_beam_files_folder()
file_paths = remove_context_beams_and_return_module_paths()
beam_folder = get_beam_files_folder_path()

silence_recompilation_warnings(fn ->
Kernel.ParallelCompiler.compile_to_path(file_paths, beam_files_folder)
Kernel.ParallelCompiler.compile_to_path(file_paths, beam_folder)
end)

{status, diagnostics}
end

@spec extract_beam_files_folder :: String.t()
defp extract_beam_files_folder do
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, "")
@spec get_beam_files_folder_path() :: String.t()
def get_beam_files_folder_path() do
build_sub_path = Mix.Project.build_path()
app_sub_path = Utils.get_config_app() |> Atom.to_string()

Path.join([build_sub_path, "lib", app_sub_path, "ebin"])
end

@spec remove_context_beams_and_return_module_paths :: list(String.t())
Expand Down
6 changes: 5 additions & 1 deletion lib/contexted/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule Contexted.Utils do
Checks is `enable_recompilation` option is set.
"""
@spec recompilation_enabled? :: boolean()
def recompilation_enabled?, do: get_from_config(:enable_recompilation, false)
def recompilation_enabled?,
do: get_from_config(:enable_recompilation, false) && get_from_config(:app, false)

@doc """
Returns `contexts` option value from contexted config or `[]` if it's not set.
Expand All @@ -21,6 +22,9 @@ defmodule Contexted.Utils do
@spec get_config_exclude_paths :: list(String.t())
def get_config_exclude_paths, do: get_from_config(:exclude_paths, [])

@spec get_config_app :: :atom
def get_config_app, do: get_from_config(:app, nil)

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

0 comments on commit 51cf78b

Please sign in to comment.