-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmodule_discovery_service.ex
More file actions
67 lines (55 loc) · 1.95 KB
/
Copy pathmodule_discovery_service.ex
File metadata and controls
67 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
defmodule LiveDebugger.Services.ModuleDiscoveryService do
@moduledoc """
This module provides functions to discover LiveViews and LiveComponents in the current application.
"""
alias LiveDebugger.Services.System.ModuleService
@live_view_behaviour Phoenix.LiveView
@live_component_behaviour Phoenix.LiveComponent
@spec all_modules() :: [module()]
def all_modules() do
ModuleService.all()
|> Enum.map(fn {module_charlist, _, _} ->
module_charlist |> to_string |> String.to_atom()
end)
end
@doc """
Accepts a list of all modules from ModuleService.all/0
Returns a list of loaded LiveView modules.
"""
@spec live_view_modules(modules :: [module()]) :: [module()]
def live_view_modules(modules) do
find_modules_by_behaviour(modules, @live_view_behaviour)
end
@doc """
Accepts a list of all modules from ModuleService.all/0
Returns a list of loaded LiveComponent modules.
## Examples
iex> services = LiveDebugger.Services.ModuleService.all()
[{MyAppWeb.LiveComponent, 'lib/my_app_web/live_component.ex'}, ...]
iex> LiveDebugger.Services.ModuleDiscoveryService.live_view_modules(services)
[MyAppWeb.LiveComponent, ...]
"""
@spec live_component_modules(modules :: [module()]) :: [module()]
def live_component_modules(modules) do
find_modules_by_behaviour(modules, @live_component_behaviour)
end
defp find_modules_by_behaviour(modules, behaviour) do
modules
|> Enum.filter(&ModuleService.loaded?/1)
|> Enum.reject(&debugger?/1)
|> Enum.filter(&behaviour?(&1, behaviour))
end
defp behaviour?(module, behaviour_to_find) do
module_behaviours = ModuleService.behaviours(module)
Enum.member?(module_behaviours, behaviour_to_find)
end
defp debugger?(module) do
stringified_module = Atom.to_string(module)
String.starts_with?(stringified_module, [
"Elixir.LiveDebugger.",
"Elixir.LiveDebuggerWeb.",
"LiveDebugger.",
"LiveDebuggerWeb."
])
end
end