Просмотр исходного кода

"var" and "comment" tests working again

Roberto Saccon 17 лет назад
Родитель
Сommit
bab54b6601
3 измененных файлов с 43 добавлено и 37 удалено
  1. 5 5
      src/demo/erlydtl_demo.erl
  2. 6 6
      src/erlydtl/erlydtl_base.erl
  3. 32 26
      src/erlydtl/erlydtl_server.erl

+ 5 - 5
src/demo/erlydtl_demo.erl

@@ -50,8 +50,8 @@ compile() ->
         "\.html$|\.css$",
         true,
         fun(Path, _Acc) ->
-            Name = filename:rootname(filename:basename(Path)),
-            erlydtl_server:compile(Path, Name, DocRoot)
+            Module = filename:rootname(filename:basename(Path)),
+            erlydtl_server:compile(Path, DocRoot, Module)
         end,
         []).
 
@@ -95,9 +95,9 @@ compile(Name) ->
 %%--------------------------------------------------------------------       
 compile(Name, Ext) ->
     DocRoot = filename:join([filename:dirname(code:which(?MODULE)),"..", "demo", "templates"]),
-    Name2 = "test_" ++ Name,
-    Path = filename:join([DocRoot, Name2 ++ Ext]),
-    erlydtl_server:compile(Path, Name2, DocRoot).
+    Module = "test_" ++ Name,
+    Path = filename:join([DocRoot, Module ++ Ext]),
+    erlydtl_server:compile(Path, DocRoot, Module).
 
 
 %%--------------------------------------------------------------------

+ 6 - 6
src/erlydtl/erlydtl_base.erl

@@ -84,7 +84,7 @@ parse(File) ->
     
 
 parse_transform({var, Line, Val}, Var, Val) when is_atom(Var) ->
-    io:format("TRACE ~p:~p var_parse_transform: ~p~n",[?MODULE, ?LINE, Val]),
+    %io:format("TRACE ~p:~p var_parse_transform: ~p~n",[?MODULE, ?LINE, Val]),
     {var, Line, Var}.
     
 
@@ -102,17 +102,17 @@ parse_transform({var, _Line, Var}, Args) ->
     Var2 = list_to_atom(tl(atom_to_list(Var))),
     binary_string(proplists:get_value(Var2, Args));      
 parse_transform(Other, _) ->    
-    io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, other]),
+    %io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, other]),
     Other.
         
 
 parse_transform({block, _Line , _Name, [nil, T]}) ->
 	parse_transform(T); 
 parse_transform({var, _Line, Val}) ->
-    io:format("TRACE ~p:~p var_parse_transform: ~p~n",[?MODULE, ?LINE, Val]),    
+    %io:format("TRACE ~p:~p var_parse_transform: ~p~n",[?MODULE, ?LINE, Val]),    
     erl_syntax:variable(Val);
 parse_transform(Other) -> 
-    io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, other]),   
+    %io:format("TRACE ~p:~p ~p~n",[?MODULE, ?LINE, other]),   
     Other.   	
 
 
@@ -181,7 +181,7 @@ build_tree2(nil, [{for, _, It, Var, [HFor | TFor]}], #dtl{buffer = Buffer, props
     {regular, lists:flatten([Buffer1, Buffer]), Args1, Props};     
 
 build_tree2(nil, [Token], #dtl{buffer = Buffer, args = Args, props = Props}) ->
-    io:format("TRACE ~p:~p other1-Token: ~p~n",[?MODULE, ?LINE, Token]),
+    %io:format("TRACE ~p:~p other1-Token: ~p~n",[?MODULE, ?LINE, Token]),
     {regular, [Token | Buffer], Args, Props}; 
   
 build_tree2([H | T], [{var, _, Var}], #dtl{buffer = Buffer, var = Var} = Dtl) ->
@@ -218,7 +218,7 @@ build_tree2([H | T], [{for, _, It, Var, [HFor | TFor]}], #dtl{buffer = Buffer} =
     build_tree2(H, T, Dtl#dtl{buffer = lists:flatten([Buffer1, Buffer]), args = Args1});
         	
 build_tree2([H | T], [Token], #dtl{buffer = Buffer} = Dtl) ->
-    io:format("TRACE ~p:~p other2-Token: ~p~n",[?MODULE, ?LINE, Token]),
+    %io:format("TRACE ~p:~p other2-Token: ~p~n",[?MODULE, ?LINE, Token]),
     build_tree2(H, T, Dtl#dtl{buffer = [Token | Buffer]}).
     
 

+ 32 - 26
src/erlydtl/erlydtl_server.erl

@@ -42,7 +42,8 @@
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
          terminate/2, code_change/3]).
 
--record(state, {}).
+-record(state, {
+    reload = true}).
 
 
 %%====================================================================
@@ -58,8 +59,8 @@ start_link() ->
     
 
 %%--------------------------------------------------------------------
-%% @spec (File:string()) -> 
-%%     {Ok::atom, Ast::tuple() | {Error::atom(), Msg:string()}
+%% @spec (File::string()) -> 
+%%     {Ok::atom, Ast::tuple() | {Error::atom(), Msg::string()}
 %% @doc compiles a template to a beam file
 %% @end 
 %%--------------------------------------------------------------------
@@ -67,23 +68,23 @@ compile(File) ->
     compile(File, todo, todo).
         
 %%--------------------------------------------------------------------
-%% @spec (File:string(), ModuleName:string(), DocRoot:string()) -> 
+%% @spec (File::string(), DocRoot::string(), Module::string()) -> 
 %%     {Ok::atom, Ast::tuple() | {Error::atom(), Msg:string()}
 %% @doc compiles a template to a beam file
 %% @end 
 %%--------------------------------------------------------------------
-compile(File, ModuleName, DocRoot) ->
-    compile(File, ModuleName, DocRoot, "render").
+compile(File, DocRoot, Module) ->
+    compile(File, DocRoot, Module, "render").
     
 
 %%--------------------------------------------------------------------
-%% @spec (File:string(), ModuleName:string(), DocRoot:string(), FunctionName:atom()) -> 
+%% @spec (File::string(), DocRoot::string(), Module::string(), Function::atom()) -> 
 %%     {Ok::atom, Ast::tuple() | {Error::atom(), Msg:string()}
 %% @doc compiles a template to a beam file
 %% @end 
 %%--------------------------------------------------------------------
-compile(File, ModuleName, DocRoot, FunctionName) ->   
-    gen_server:call(?MODULE, {compile, File, ModuleName, DocRoot, FunctionName}).
+compile(File, DocRoot, Module, Function) ->   
+    gen_server:call(?MODULE, {compile, File, DocRoot, Module, Function}).
         
 
 %%====================================================================
@@ -112,13 +113,13 @@ init([]) ->
 %% @doc Handling call messages
 %% @end 
 %%--------------------------------------------------------------------
-handle_call({compile, File, ModuleName, DocRoot, FunctionName}, _From, State) ->
+handle_call({compile, File, DocRoot, Module, Function}, _From, State) ->
     Reply = case erlydtl_base:parse(File) of
         {ok, Ast} ->
-		    RelDocRoot = erlydtl_base:rel_dir(filename:dirname(File), DocRoot),
+		    DocRoot2 = erlydtl_base:rel_dir(filename:dirname(File), DocRoot),
 		    Ext = filename:extension(File),
-            compile(Ast, ModuleName, FunctionName, RelDocRoot, Ext);
-       Err ->
+            compile(Ast, Module, Function, DocRoot2, Ext, State#state.reload);
+        Err ->
             Err
     end,
     {reply, Reply, State};
@@ -170,20 +171,20 @@ code_change(_OldVsn, State, _Extra) ->
 %%====================================================================
 %% Internal functions
 %%====================================================================
-compile([H | T], ModuleName, FunctionName, DocRoot, Ext) ->
+compile([H | T], Module, Function, DocRoot, Ext, Reload) ->
     case erlydtl_base:build_tree(H, T, DocRoot, Ext) of
         {regular, Out, Args, _} ->
-            Out1 = [erlydtl_scanner:parse_transform(X) ||  X <- Out],
-            create_module(Out1, Args, ModuleName, FunctionName);
+            Out1 = [erlydtl_base:parse_transform(X) ||  X <- Out],
+            create_module(Out1, Args, Module, Function, Reload);
         {inherited, Out, Args, _} ->
-            create_module(Out, Args, ModuleName, FunctionName);
+            create_module(Out, Args, Module, Function, Reload);
         {error, Reason} ->
             % check whether Reason contains Linenumber
             {error, Reason}
     end.   
     
 
-create_module(List, Args, ModuleName, FunctionName) ->
+create_module(List, Args, Module, Function, Reload) ->
     {BodyAST, Args1} = case Args of 
         []  ->
             {[erl_syntax:list(List)], []};
@@ -201,19 +202,24 @@ create_module(List, Args, ModuleName, FunctionName) ->
             {BodyAST0, [Var]}
     end,
     ClauseAST = erl_syntax:clause(Args1, none, BodyAST),
-    FuncAST = erl_syntax:function(erl_syntax:atom(FunctionName), [ClauseAST]),
+    FuncAST = erl_syntax:function(erl_syntax:atom(Function), [ClauseAST]),
     [ModAST, CmpAST] = [erl_syntax:attribute(erl_syntax:atom(X), [erl_syntax:atom(Y)]) ||
-        {X, Y} <- [{"module", ModuleName}, {"compile", "export_all"}]],
+        {X, Y} <- [{"module", Module}, {"compile", "export_all"}]],
     Forms = [erl_syntax:revert(X) || X <- [ModAST, CmpAST, FuncAST]],
     case compile:forms(Forms) of
-        {ok, Module, Bin} ->
-            case erlydtl:write_beam(Module, Bin, "ebin") of
+        {ok, Module1, Bin} ->
+            case erlydtl:write_beam(Module1, Bin, "ebin") of
                 ok ->
-                    case erlydtl:reload(Module, Bin) of
-                        ok ->
-                            ok;
+                    case Reload of
+                        true ->
+                            case erlydtl:reload(Module1, Bin) of
+                                ok ->
+                                    ok;
+                                _ ->
+                                    {error, "code reload failed"}
+                            end;
                         _ ->
-                            {error, "code reload failed"}
+                            ok
                     end;
                 _ ->
                     {error, "beam generation failed"}