Skip to content

Commit c722afe

Browse files
committed
Define Gradient.Debug for convenient Erlang / Elixir AST lookups
1 parent f18113f commit c722afe

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/gradient/debug.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule Gradient.Debug do
2+
@moduledoc false
3+
4+
def elixir_ast(mod) do
5+
{:ok, {_, [{:debug_info, {:debug_info_v1, :elixir_erl, abstract_code}}]}} =
6+
:beam_lib.chunks(get_beam_path_as_charlist(mod), [:debug_info])
7+
{:ok, _forms} = :elixir_erl.debug_info(:elixir_v1, :module_name, abstract_code, [])
8+
end
9+
10+
def erlang_ast(mod) do
11+
{:ok, _forms} = get_beam_path_as_charlist(mod) |> Gradient.ElixirFileUtils.get_forms_from_beam()
12+
end
13+
14+
def print_erlang(mod) do
15+
{:ok, forms} = erlang_ast(mod)
16+
IO.puts(:erl_prettypr.format(:erl_syntax.form_list(forms)))
17+
end
18+
19+
def get_beam_path_as_charlist(mod) when is_list(mod), do: mod
20+
def get_beam_path_as_charlist(mod) when is_atom(mod), do: :code.which(mod)
21+
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Gradient.MixProject do
1616
# Run "mix help compile.app" to learn about applications.
1717
def application do
1818
[
19-
extra_applications: [:logger]
19+
extra_applications: [:logger, :syntax_tools]
2020
]
2121
end
2222

0 commit comments

Comments
 (0)