Browse Source

New debug options to not lean on the debug_info option (fixes #139)

Andreas Stenius 11 years ago
parent
commit
a0b58deeec
5 changed files with 76 additions and 40 deletions
  1. 3 2
      Makefile
  2. 11 4
      README.markdown
  3. 34 6
      src/erlydtl_beam_compiler.erl
  4. 1 13
      src/erlydtl_compiler.erl
  5. 27 15
      src/erlydtl_compiler_utils.erl

+ 3 - 2
Makefile

@@ -39,9 +39,7 @@ plt:
 clean:
 	@echo "Clean merl..." ; $(MAKE) -C deps/merl clean
 	@$(REBAR) clean
-	rm -fv ebintest/*
 	rm -fv erl_crash.dump
-	rm -fv tests/output/*
 
 # rebuild any .slex files as well..  not included by default to avoid
 # the slex dependency, which is only needed in case the .slex file has
@@ -54,3 +52,6 @@ slex-skip-deps: slex-compile
 
 slex-compile:
 	@$(REBAR) -C rebar-slex.config $(REBAR_DEPS) compile
+
+shell:
+	@$(ERL) -pz ebin deps/*/ebin

+ 11 - 4
README.markdown

@@ -118,11 +118,18 @@ Options is a proplist possibly containing:
   argument to the `render/2` call at render-time. (These may include
   any options, not just `locale` and `translation_fun`.)
 
+* `debug_compiler` - Enable compiler debug diagnostics.  Currently it
+  debug prints the options passed to `compile:forms` (i.e. if
+  verbosity is >= 2; that is, with two or more `verbose` options) and
+  enables the saving of the compiled template in source form to a .erl
+  file.
+
 * `debug_info` - This option is passed to `compile:forms` to include
-  debug information in the compiled module. It will also print all
-  options passed to `compile:forms` if verbosity is >= 2 (e.g. with
-  two or more `verbose` options) and save the compiled template in
-  source form to a .erl file.
+  debug information in the compiled module.
+
+* `debug_root` - The root directory for debug source dumps. If set to
+  `false`, no source dump will be saved. Defaults to `undefined`,
+  leaving the source dump next to the source template file.
 
 * `default_libraries` - A list of libraries that should be loaded by
   default when compiling a template. Libraries can be specified either

+ 34 - 6
src/erlydtl_beam_compiler.erl

@@ -63,7 +63,8 @@
          empty_scope/0, get_current_file/1, add_errors/2,
          add_warnings/2, merge_info/2, call_extension/3,
          init_treewalker/1, resolve_variable/2, resolve_variable/3,
-         reset_parse_trail/2, load_library/3, load_library/4]).
+         reset_parse_trail/2, load_library/3, load_library/4,
+         shorten_filename/2]).
 
 -include_lib("merl/include/merl.hrl").
 -include("erlydtl_ext.hrl").
@@ -263,17 +264,44 @@ load_code(Module, Bin, Context) ->
     end.
 
 maybe_debug_template(Forms, Context) ->
-    %% undocumented option to debug the compiled template
-    case proplists:get_bool(debug_info, Context#dtl_context.all_options) of
+    case proplists:get_bool(debug_compiler, Context#dtl_context.all_options) of
         false -> nop;
         true ->
             Options = Context#dtl_context.compiler_options,
             ?LOG_DEBUG("Compiler options: ~p~n", [Options], Context),
             try
                 Source = erl_prettypr:format(erl_syntax:form_list(Forms)),
-                File = lists:concat([proplists:get_value(source, Options), ".erl"]),
-                io:format("Saving template source to: ~s.. ~p~n",
-                          [File, file:write_file(File, Source)])
+                SourceFile = lists:concat(
+                               [proplists:get_value(source, Options),".erl"]),
+                File = case proplists:get_value(
+                              debug_root,
+                              Context#dtl_context.all_options) of
+                           false -> undefined;
+                           undefined -> SourceFile;
+                           Dir ->
+                               Abs = filename:absname(
+                                 shorten_filename(
+                                   SourceFile,
+                                   Context#dtl_context.doc_root),
+                                       Dir),
+                               case filelib:is_dir(Dir) of
+                                   true -> Abs;
+                                   false ->
+                                       case filelib:ensure_dir(Abs) of
+                                           ok -> Abs;
+                                           {error, Reason} ->
+                                               io:format(
+                                                 "Failed to ensure directories for file '~s': ~p~n",
+                                                 [Abs, Reason]),
+                                               undefined
+                                       end
+                               end
+                       end,
+                if File =/= undefined ->
+                        io:format("Saving template source to: ~s.. ~p~n",
+                                  [File, file:write_file(File, Source)]);
+                   true -> ok
+                end
             catch
                 error:Err ->
                     io:format("Pretty printing failed: ~p~n"

+ 1 - 13
src/erlydtl_compiler.erl

@@ -52,7 +52,7 @@
 
 -import(erlydtl_compiler_utils,
          [add_filters/2, add_tags/2, call_extension/3,
-         load_library/2]).
+         load_library/2, shorten_filename/1]).
 
 -include("erlydtl_ext.hrl").
 
@@ -173,18 +173,6 @@ env_default_opts() ->
             end
     end.
 
-%% shorten_filename/1 copied from Erlang/OTP lib/compiler/src/compile.erl
-shorten_filename(Name0) ->
-    {ok,Cwd} = file:get_cwd(),
-    case lists:prefix(Cwd, Name0) of
-        false -> Name0;
-        true ->
-            case lists:nthtail(length(Cwd), Name0) of
-                "/"++N -> N;
-                N -> N
-            end
-    end.
-
 compile(Context) ->
     Context1 = do_compile(Context),
     collect_result(Context1).

+ 27 - 15
src/erlydtl_compiler_utils.erl

@@ -48,30 +48,27 @@
 
 -export([
          add_error/3, add_errors/2,
-         add_filters/2, add_tags/2,
+         add_filters/2,
+         add_tags/2,
          add_warning/3, add_warnings/2,
+         begin_scope/1, begin_scope/2,
          call_extension/3,
+         empty_scope/0,
+         end_scope/4,
          format_error/1,
          full_path/2,
          get_current_file/1,
          init_treewalker/1,
-         load_library/2,
-         load_library/3,
-         load_library/4,
+         load_library/2, load_library/3, load_library/4,
          merge_info/2,
-         print/3,
-         print/4,
-         to_string/2,
-         unescape_string_literal/1,
-         reset_parse_trail/2,
-         resolve_variable/2,
-         resolve_variable/3,
+         print/3, print/4,
          push_scope/2,
+         reset_parse_trail/2,
+         resolve_variable/2, resolve_variable/3,
          restore_scope/2,
-         begin_scope/1,
-         begin_scope/2,
-         end_scope/4,
-         empty_scope/0
+         shorten_filename/1, shorten_filename/2,
+         to_string/2,
+         unescape_string_literal/1
         ]).
 
 -include("erlydtl_ext.hrl").
@@ -283,6 +280,21 @@ add_tags(Load, #dtl_context{ tags=Tags }=Context) ->
     ?LOG_TRACE("Load tags: ~p~n", [Load], Context),
     Context#dtl_context{ tags=Load ++ Tags }.
 
+%% shorten_filename/1 copied from Erlang/OTP lib/compiler/src/compile.erl
+shorten_filename(Name) ->
+    {ok, Cwd} = file:get_cwd(),
+    shorten_filename(Name, Cwd).
+
+shorten_filename(Name, Cwd) ->
+    case lists:prefix(Cwd, Name) of
+        false -> Name;
+        true ->
+            case lists:nthtail(length(Cwd), Name) of
+                "/"++N -> N;
+                N -> N
+            end
+    end.
+
 format_error({load_library, Name, Mod, Reason}) ->
     io_lib:format("Failed to load library '~p' (~p): ~p", [Name, Mod, Reason]);
 format_error({load_from, Name, Mod, Tag}) ->