| 1 | %%% Copyright (c) Dominic Williams, Nicolas Charpentier |
|---|
| 2 | %%% All rights reserved. |
|---|
| 3 | %%% See file COPYING. |
|---|
| 4 | |
|---|
| 5 | -module (modules). |
|---|
| 6 | -export ([to_binary/1]). |
|---|
| 7 | -export ([to_file_name/2]). |
|---|
| 8 | -export ([forms_to_binary/1]). |
|---|
| 9 | -export ([compile/2, compile/1]). |
|---|
| 10 | -export ([module_name/1]). |
|---|
| 11 | -export ([locate/2]). |
|---|
| 12 | -export ([includes/1]). |
|---|
| 13 | |
|---|
| 14 | to_binary (File_name) -> |
|---|
| 15 | {ok, _, Binary, _} = compile (fun compile: file/2, File_name, []), |
|---|
| 16 | Binary. |
|---|
| 17 | |
|---|
| 18 | to_file_name (Module, Directory) -> |
|---|
| 19 | File_name = atom_to_list (Module) ++ ".erl", |
|---|
| 20 | filename: join (Directory, File_name). |
|---|
| 21 | |
|---|
| 22 | forms_to_binary (Forms) -> |
|---|
| 23 | {ok, _, Binary, _} = compile (fun compile: forms/2, Forms, []), |
|---|
| 24 | Binary. |
|---|
| 25 | |
|---|
| 26 | compile (Fun, Parameter, User_options) -> |
|---|
| 27 | Options = [binary, return, warn_unused_import, debug_info | User_options], |
|---|
| 28 | Fun (Parameter, Options). |
|---|
| 29 | |
|---|
| 30 | compile (File, Options) -> |
|---|
| 31 | compile (fun compile: file/2, File, Options). |
|---|
| 32 | |
|---|
| 33 | compile (File) -> |
|---|
| 34 | compile (File, []). |
|---|
| 35 | |
|---|
| 36 | module_name (Filename) -> |
|---|
| 37 | {extension, ".erl"} = {extension, filename: extension (Filename)}, |
|---|
| 38 | String = filename: rootname (filename: basename (Filename)), |
|---|
| 39 | list_to_atom (String). |
|---|
| 40 | |
|---|
| 41 | locate ({M, F, A}, Binary) -> |
|---|
| 42 | {ok, {M, Chunks}} = beam_lib: chunks (Binary, [abstract_code, compile_info]), |
|---|
| 43 | [{abstract_code, Code}, {compile_info, Info}] = Chunks, |
|---|
| 44 | {value, {source, Filename}} = lists: keysearch (source, 1, Info), |
|---|
| 45 | {raw_abstract_v1, Forms} = Code, |
|---|
| 46 | Line = locate_line (F, A, Forms), |
|---|
| 47 | {Filename, Line}. |
|---|
| 48 | |
|---|
| 49 | locate_line (Function, Arity, [{function, Line, Function, Arity, _} | _]) -> |
|---|
| 50 | Line; |
|---|
| 51 | locate_line (Function, Arity, [_ | Forms]) -> |
|---|
| 52 | locate_line (Function, Arity, Forms); |
|---|
| 53 | locate_line (_, _, []) -> |
|---|
| 54 | unknown. |
|---|
| 55 | |
|---|
| 56 | includes (File) -> |
|---|
| 57 | includes_from_forms (epp_dodger: parse_file (File)). |
|---|
| 58 | |
|---|
| 59 | includes_from_forms ({error, _}=E) -> |
|---|
| 60 | E; |
|---|
| 61 | includes_from_forms ({ok, Forms}) -> |
|---|
| 62 | Attributes = [A || {tree, attribute, _, A} <- Forms], |
|---|
| 63 | Includes = [I || {attribute, {atom, _, include}, [I]} <- Attributes], |
|---|
| 64 | [F || {string, _, F} <- Includes]. |
|---|