Browse Source

Add {out_dir, false} option to explicitly disable generating a .beam.

Fixes #128.
Andreas Stenius 11 years ago
parent
commit
ca93672ec8

+ 2 - 1
README.markdown

@@ -52,7 +52,8 @@ Result:
 Options is a proplist possibly containing:
 
 * `out_dir` - Directory to store generated .beam files. If not
-  specified, no .beam files will be created.
+  specified, no .beam files will be created and a warning is
+  emitted. To silence the warning, use `{out_dir, false}`.
 
 * `doc_root` - Included template paths will be relative to this
   directory; defaults to the compiled template's directory.

+ 2 - 0
rebar.config

@@ -1,2 +1,4 @@
+%% -*- mode: erlang -*-
+
 {erl_opts, [debug_info]}.
 {yrl_opts, [{includefile, "include/erlydtl_preparser.hrl"}]}.

+ 32 - 12
src/erlydtl_compiler.erl

@@ -145,14 +145,21 @@ process_opts(File, Module, Options0) ->
                  update_defaults(Options0),
                  [{aliases, [{outdir, out_dir}]}
                  ]),
-    Source = case File of
-                 undefined ->
-                     filename:join(
-                       [proplists:get_value(out_dir, Options1, ""),
-                        Module]);
-                 {dir, Dir} -> filename:absname(Dir);
-                 _ -> File
-             end,
+    Source0 = filename:absname(
+                case File of
+                    undefined ->
+                        filename:join(
+                          [case proplists:get_value(out_dir, Options1, false) of
+                               false -> ".";
+                               OutDir -> OutDir
+                           end,
+                           Module]);
+                    {dir, Dir} ->
+                        Dir;
+                    _ ->
+                        File
+                end),
+    Source = shorten_filename(Source0),
     Options = [{compiler_options, [{source, Source}]}
                |compiler_opts(Options1, [])],
     case File of
@@ -182,6 +189,12 @@ compiler_opts([], Acc) ->
 update_defaults(Options) ->
     maybe_add_env_default_opts(Options).
 
+maybe_add_env_default_opts(Options) ->
+    case proplists:get_bool(no_env, Options) of
+        true -> Options;
+        _ -> Options ++ env_default_opts()
+    end.
+
 %% shamelessly borrowed from:
 %% https://github.com/erlang/otp/blob/21095e6830f37676dd29c33a590851ba2c76499b/\
 %% lib/compiler/src/compile.erl#L128
@@ -205,10 +218,16 @@ env_default_opts() ->
             end
     end.
 
-maybe_add_env_default_opts(Options) ->
-    case proplists:get_bool(no_env, Options) of
-        true -> Options;
-        _ -> Options ++ env_default_opts()
+%% 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) ->
@@ -356,6 +375,7 @@ compile_forms(Forms, Context) ->
 
 maybe_write(Module, Bin, Context) ->
     case proplists:get_value(out_dir, Context#dtl_context.all_options) of
+        false -> Context;
         undefined ->
             add_warning(no_out_dir, Context);
         OutDir ->

+ 8 - 7
tests/src/erlydtl_functional_tests.erl

@@ -196,8 +196,10 @@ setup(_) ->
 
 run_tests() ->
     io:format("Running functional tests...~n"),
+    file:set_cwd(erlydtl_deps:get_base_dir()),
     case [filelib:ensure_dir(
             filename:join([templates_dir(Dir), "foo"]))
+
           || Dir <- ["output", "expect"]] -- [ok,ok]
     of
         [] ->
@@ -264,12 +266,11 @@ test_compile_render(Name) ->
                             io:format("missing error"),
                             {error, "compiling should have failed :" ++ File}
                     end;
-                {error, _, _}=Err ->
-                    if CompileStatus =:= Err -> io:format("ok");
-                       true ->
-                            io:format("failed"),
-                            {compile_error, io_lib:format("~p", [Err])}
-                    end
+                {error, _, _}=CompileStatus ->
+                    io:format("ok");
+                Err ->
+                    io:format("failed"),
+                    {compile_error, io_lib:format("Actual: ~p, Expected: ~p", [Err, CompileStatus])}
             end;
         skip -> io:format("skipped")
     end.
@@ -333,4 +334,4 @@ get_expected_result(Name) ->
     end.
 
 templates_docroot() -> templates_dir("input").
-templates_dir(Name) -> filename:join([erlydtl_deps:get_base_dir(), "tests", Name]).
+templates_dir(Name) -> filename:join(["tests", Name]).

+ 2 - 2
tests/src/erlydtl_unittests.erl

@@ -281,7 +281,7 @@ tests() ->
                <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
                [{'list', [["One", "two"], ["One", "two"]]}], [], [], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>,
                %% the warnings we get from the erlang compiler still needs some care..
-               [error_info("/erlydtl_running_test", [{0, erl_lint, {unused_var, 'Var_inner/1_1:31'}}, no_out_dir])]},
+               [error_info("erlydtl_running_test", [{0, erl_lint, {unused_var, 'Var_inner/1_1:31'}}, no_out_dir])]},
               {"If changed",
                <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
                [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
@@ -1448,6 +1448,6 @@ error_info({Line, Module, ErrorDesc})
   when is_integer(Line), is_atom(Module) ->
     {Line, Module, ErrorDesc};
 error_info(Ws) when is_list(Ws) ->
-    error_info("/erlydtl_running_test", Ws);
+    error_info("erlydtl_running_test", Ws);
 error_info(ErrorDesc) ->
     {none, erlydtl_compiler, ErrorDesc}.