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

0 comments on commit f74b94f

Please sign in to comment.