diff --git a/include/mad.hrl b/include/mad.hrl index e3bb78f..32e179b 100644 --- a/include/mad.hrl +++ b/include/mad.hrl @@ -1 +1 @@ --define(VERSION,"1b48b6"). +-define(VERSION,"ca88d4"). diff --git a/mad b/mad index 0b4f409..ad061b1 100755 Binary files a/mad and b/mad differ diff --git a/priv/web/apps/sample/rebar.config b/priv/web/apps/sample/rebar.config index d49a4e1..2cce55f 100644 --- a/priv/web/apps/sample/rebar.config +++ b/priv/web/apps/sample/rebar.config @@ -5,3 +5,10 @@ {source_ext, ".html"}, {module_ext, "_view"} ]}. +{jaderl_opts, [ + {doc_root, "priv/templates"}, + {out_dir, "ebin"}, + {compiler_options, [report, return, debug_info]}, + {source_ext, ".jade"}, + {module_ext, "_view"} +]}. \ No newline at end of file diff --git a/src/compile/mad_compile.erl b/src/compile/mad_compile.erl index aed985c..1a0829a 100644 --- a/src/compile/mad_compile.erl +++ b/src/compile/mad_compile.erl @@ -67,10 +67,11 @@ dep(Cwd, _Conf, ConfigFile, Name) -> Opts = mad_utils:get_value(erl_opts, Conf1, []), FilesStatus = compile_files(Files,IncDir, EbinDir, Opts,Includes), DTLStatus = mad_dtl:compile(DepPath,Conf1), + JADEStatus = mad_jade:compile(DepPath, Conf1), PortStatus = lists:any(fun(X)->X end,mad_port:compile(DepPath,Conf1)), % io:format("Status: ~p~n",[[Name,FilesStatus,DTLStatus,PortStatus,DepsRes]]), put(Name, compiled), - case (DepsRes orelse FilesStatus orelse DTLStatus orelse PortStatus) andalso filelib:is_dir(Name)==false of + case (DepsRes orelse FilesStatus orelse DTLStatus orelse JADEStatus orelse PortStatus) andalso filelib:is_dir(Name)==false of true -> {error,Name}; false -> {ok,Name} end end. diff --git a/src/compile/mad_jade.erl b/src/compile/mad_jade.erl new file mode 100644 index 0000000..2f00a8a --- /dev/null +++ b/src/compile/mad_jade.erl @@ -0,0 +1,59 @@ +-module(mad_jade). +-copyright('Sina Samavati'). +-copyright('Igor Savchuk'). +-compile(export_all). + +compile(Dir,Config) -> + case mad_utils:get_value(jaderl_opts, Config, []) of + [] -> false; + X -> compile_jaderl_files(validate_jaderl_opts(Dir,X)) end. + +get_kv(K, Opts, Default) -> + V = mad_utils:get_value(K, Opts, Default), + KV = {K, V}, + {KV, Opts -- [KV]}. + +file_to_beam(Bin, Filename) -> filename:join(Bin, filename:basename(Filename) ++ ".beam"). + +validate_jaderl_opts(Cwd, Opts) -> + DefaultDocRoot = filename:join("priv", "templates"), + {DocRoot, Opts1} = get_kv(doc_root, Opts, DefaultDocRoot), + {OutDir, Opts2} = get_kv(out_dir, Opts1, "ebin"), + {CompilerOpts, Opts3} = get_kv(compiler_options, Opts2, []), + {SourceExt, Opts4} = get_kv(source_ext, Opts3, ".jade"), + {ModuleExt, Opts5} = get_kv(module_ext, Opts4, ""), + + {_, DocRootDir} = DocRoot, + DocRoot1 = {doc_root, filename:join(Cwd, DocRootDir)}, + {_, OutDir1} = OutDir, + OutDir2 = {out_dir, filename:join(Cwd, OutDir1)}, + + [DocRoot1, OutDir2, CompilerOpts, SourceExt, ModuleExt|Opts5]. + +module_name(File, Ext, NewExt) -> + list_to_atom(filename:basename(File, Ext) ++ NewExt). + +compile_jaderl_files(Opts) -> + + {{_, DocRoot}, Opts1} = get_kv(doc_root, Opts, ""), + {{_, SourceExt}, Opts2} = get_kv(source_ext, Opts1, ""), + {{_, ModuleExt}, Opts3} = get_kv(module_ext, Opts2, ""), + {{_, OutDir}, _} = get_kv(out_dir, Opts3, ""), + + Files = filelib:fold_files(DocRoot, SourceExt, true, + fun(F, Acc) -> [F|Acc] end, []), + + Compile = fun(F) -> + ModuleName = module_name(F, SourceExt, ModuleExt), + BeamFile = file_to_beam(OutDir, atom_to_list(ModuleName)), + Compiled = mad_compile:is_compiled(BeamFile, F), + case Compiled of false -> + mad:info("JADE Compiling ~s~n", [F -- mad_utils:cwd()]), + Res = jaderl:compile(F, ModuleName, Opts3), + file:change_time(BeamFile, calendar:local_time()), + case Res of {error,Error} -> mad:info("Error: ~p~n",[Error]); + OK -> OK end; + true -> ok end + end, + + lists:any(fun({error,_}) -> true; (ok) -> false end,[Compile(F) || F <- Files]).