diff --git a/src/aeso_aci.erl b/src/aeso_aci.erl index eced0b4..29f9ea6 100644 --- a/src/aeso_aci.erl +++ b/src/aeso_aci.erl @@ -9,7 +9,9 @@ -module(aeso_aci). --export([ contract_interface/2 +-export([ file/2 + , file/3 + , contract_interface/2 , contract_interface/3 , render_aci_json/1 @@ -22,6 +24,18 @@ -type json_text() :: binary(). %% External API +-spec file(aci_type(), string()) -> {ok, json() | string()} | {error, term()}. +file(Type, File) -> + file(Type, File, []). + +file(Type, File, Options0) -> + Options = aeso_compiler:add_include_path(File, Options0), + case file:read_file(File) of + {ok, BinCode} -> + do_contract_interface(Type, binary_to_list(BinCode), Options); + {error, _} = Err -> Err + end. + -spec contract_interface(aci_type(), string()) -> {ok, json() | string()} | {error, term()}. contract_interface(Type, ContractString) -> diff --git a/src/aeso_compiler.erl b/src/aeso_compiler.erl index 8f7ec6e..cd8a9d7 100644 --- a/src/aeso_compiler.erl +++ b/src/aeso_compiler.erl @@ -21,6 +21,7 @@ , decode_calldata/3 %% deprecated , decode_calldata/4 , parse/2 + , add_include_path/2 ]). -include_lib("aebytecode/include/aeb_opcodes.hrl"). @@ -65,12 +66,11 @@ version() -> -spec file(string()) -> {ok, map()} | {error, binary()}. file(Filename) -> - Dir = filename:dirname(Filename), - {ok, Cwd} = file:get_cwd(), - file(Filename, [{include, {file_system, [Cwd, Dir]}}]). + file(Filename, []). -spec file(string(), options()) -> {ok, map()} | {error, binary()}. -file(File, Options) -> +file(File, Options0) -> + Options = add_include_path(File, Options0), case read_contract(File) of {ok, Bin} -> from_string(Bin, [{src_file, File} | Options]); {error, Error} -> @@ -78,6 +78,15 @@ file(File, Options) -> {error, join_errors("File errors", [ErrorString], fun(E) -> E end)} end. +add_include_path(File, Options) -> + case lists:keymember(include, 1, Options) of + true -> Options; + false -> + Dir = filename:dirname(File), + {ok, Cwd} = file:get_cwd(), + [{include, {file_system, [Cwd, Dir]}} | Options] + end. + -spec from_string(binary() | string(), options()) -> {ok, map()} | {error, binary()}. from_string(Contract, Options) -> from_string(proplists:get_value(backend, Options, aevm), Contract, Options).