Permalink
Browse files

Property handle compiler return code

* Bug fix: When compiler_option includes 'return', properly handle Error
and Warnings returned by the Erlang compiler, so that the user still gets
'ok' instead of {ok, Module, Bin, {Errors, Warnings}}, which rebar doesn't
property handle.

* Add `verbose` compiler option.
  • Loading branch information...
saleyn committed Oct 19, 2012
1 parent 97657d3 commit f74b94f800bae7ab5eccd763d6d4ff064b8988e6
Showing with 32 additions and 12 deletions.
  1. +1 −0 README.markdown
  2. +31 −12 src/erlydtl_compiler.erl
View
@@ -102,6 +102,7 @@ Defaults to [].
* `binary_strings` - Whether to compile strings as binary terms (rather than * `binary_strings` - Whether to compile strings as binary terms (rather than
lists). Defaults to `true`. lists). Defaults to `true`.
* `verbose` - Enable verbose printing of compilation results.
Helper compilation Helper compilation
------------------ ------------------
View
@@ -86,7 +86,7 @@ compile(Binary, Module, Options) when is_binary(Binary) ->
{ok, DjangoParseTree} -> {ok, DjangoParseTree} ->
case compile_to_binary(File, DjangoParseTree, case compile_to_binary(File, DjangoParseTree,
init_dtl_context(File, Module, Options), CheckSum) of init_dtl_context(File, Module, Options), CheckSum) of
{ok, Module1, _} -> {ok, Module1, _, _} ->
{ok, Module1}; {ok, Module1};
Err -> Err ->
Err Err
@@ -102,8 +102,8 @@ compile(File, Module, Options) ->
ok; ok;
{ok, DjangoParseTree, CheckSum} -> {ok, DjangoParseTree, CheckSum} ->
case compile_to_binary(File, DjangoParseTree, Context, CheckSum) of case compile_to_binary(File, DjangoParseTree, Context, CheckSum) of
{ok, Module1, Bin} -> {ok, Module1, Bin, Warnings} ->
write_binary(Module1, Bin, Options); write_binary(Module1, Bin, Options, Warnings);
Err -> Err ->
Err Err
end; end;
@@ -143,8 +143,8 @@ compile_dir(Dir, Module, Options) ->
case ParserErrors of case ParserErrors of
[] -> [] ->
case compile_multiple_to_binary(Dir, ParserResults, Context) of case compile_multiple_to_binary(Dir, ParserResults, Context) of
{ok, Module1, Bin} -> {ok, Module1, Bin, Warnings} ->
write_binary(Module1, Bin, Options); write_binary(Module1, Bin, Options, Warnings);
Err -> Err ->
Err Err
end; end;
@@ -156,17 +156,31 @@ compile_dir(Dir, Module, Options) ->
%% Internal functions %% Internal functions
%%==================================================================== %%====================================================================
write_binary(Module1, Bin, Options) -> write_binary(Module1, Bin, Options, Warnings) ->
Verbose = proplists:get_value(verbose, Options, false),
case proplists:get_value(out_dir, Options) of case proplists:get_value(out_dir, Options) of
undefined -> undefined ->
Verbose =:= true andalso
io:format("Template module: ~w not saved (no out_dir option)\n", [Module1]),
ok; ok;
OutDir -> OutDir ->
BeamFile = filename:join([OutDir, atom_to_list(Module1) ++ ".beam"]), BeamFile = filename:join([OutDir, atom_to_list(Module1) ++ ".beam"]),
Verbose =:= true andalso
io:format("Template module: ~w -> ~s~s\n",
[Module1, BeamFile,
case Warnings of
[] -> "";
_ -> io_lib:format("\n Warnings: ~p", [Warnings])
end]),
case file:write_file(BeamFile, Bin) of case file:write_file(BeamFile, Bin) of
ok -> ok ->
ok; ok;
{error, Reason} -> {error, Reason} ->
{error, lists:concat(["beam generation failed (", Reason, "): ", BeamFile])} {error, lists:flatten(
io_lib:format("Beam generation of '~s' failed: ~p",
[BeamFile, file:format_error(Reason)]))}
end end
end. end.
@@ -205,17 +219,22 @@ compile_to_binary(File, DjangoParseTree, Context, CheckSum) ->
compile_forms_and_reload(File, Forms, CompilerOptions) -> compile_forms_and_reload(File, Forms, CompilerOptions) ->
case compile:forms(Forms, CompilerOptions) of case compile:forms(Forms, CompilerOptions) of
{ok, Module1, Bin} -> {ok, Module1, Bin} ->
code:purge(Module1), load_code(Module1, Bin, []);
case code:load_binary(Module1, atom_to_list(Module1) ++ ".erl", Bin) of {ok, Module1, Bin, Warnings} ->
{module, _} -> {ok, Module1, Bin}; load_code(Module1, Bin, Warnings);
_ -> {error, lists:concat(["code reload failed: ", Module1])}
end;
error -> error ->
{error, lists:concat(["compilation failed: ", File])}; {error, lists:concat(["compilation failed: ", File])};
OtherError -> OtherError ->
OtherError OtherError
end. end.
load_code(Module, Bin, Warnings) ->
code:purge(Module),
case code:load_binary(Module, atom_to_list(Module) ++ ".erl", Bin) of
{module, _} -> {ok, Module, Bin, Warnings};
_ -> {error, lists:concat(["code reload failed: ", Module])}
end.
init_dtl_context(File, Module, Options) when is_list(Module) -> init_dtl_context(File, Module, Options) when is_list(Module) ->
init_dtl_context(File, list_to_atom(Module), Options); init_dtl_context(File, list_to_atom(Module), Options);
init_dtl_context(File, Module, Options) -> init_dtl_context(File, Module, Options) ->

0 comments on commit f74b94f

Please sign in to comment.