2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2010-01-08 05:18:55 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% rebar: Erlang Build Tools
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
|
|
|
|
%%
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
%%
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
-module(rebar_templater).
|
|
|
|
|
2010-01-08 17:54:43 +00:00
|
|
|
-export(['create-app'/2,
|
2010-01-08 19:16:57 +00:00
|
|
|
'create-node'/2,
|
2010-08-20 15:30:37 +00:00
|
|
|
'list-templates'/2,
|
2010-01-08 17:54:43 +00:00
|
|
|
create/2]).
|
2010-01-08 05:18:55 +00:00
|
|
|
|
2011-04-10 22:01:34 +00:00
|
|
|
%% API for other utilities that need templating functionality
|
|
|
|
-export([resolve_variables/2,
|
|
|
|
render/2]).
|
|
|
|
|
2010-01-08 05:18:55 +00:00
|
|
|
-include("rebar.hrl").
|
|
|
|
|
2010-01-08 17:54:43 +00:00
|
|
|
-define(TEMPLATE_RE, ".*\\.template\$").
|
|
|
|
|
2010-01-08 05:18:55 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2010-01-08 17:54:43 +00:00
|
|
|
'create-app'(Config, File) ->
|
|
|
|
%% Alias for create w/ template=simpleapp
|
|
|
|
rebar_config:set_global(template, "simpleapp"),
|
|
|
|
create(Config, File).
|
|
|
|
|
2010-01-08 19:16:57 +00:00
|
|
|
'create-node'(Config, File) ->
|
|
|
|
%% Alias for create w/ template=simplenode
|
|
|
|
rebar_config:set_global(template, "simplenode"),
|
|
|
|
create(Config, File).
|
|
|
|
|
2010-08-20 15:30:37 +00:00
|
|
|
'list-templates'(_Config, _File) ->
|
|
|
|
%% Load a list of all the files in the escript -- cache it in the pdict
|
|
|
|
%% since we'll potentially need to walk it several times over the course
|
|
|
|
%% of a run.
|
|
|
|
cache_escript_files(),
|
|
|
|
|
|
|
|
%% Build a list of available templates
|
|
|
|
AvailTemplates = find_disk_templates() ++ find_escript_templates(),
|
|
|
|
?CONSOLE("Available templates:\n", []),
|
2010-10-10 20:11:13 +00:00
|
|
|
_ = [begin
|
|
|
|
BaseName = filename:basename(F, ".template"),
|
2012-06-30 20:14:08 +00:00
|
|
|
TemplateTerms = consult(load_file(Type, F)),
|
|
|
|
{_, VarList} = lists:keyfind(variables, 1, TemplateTerms),
|
2012-06-11 17:32:41 +00:00
|
|
|
Vars = lists:foldl(fun({V,_}, Acc) ->
|
|
|
|
[atom_to_list(V) | Acc]
|
|
|
|
end, [], VarList),
|
2012-06-30 20:28:30 +00:00
|
|
|
?CONSOLE(" * ~s: ~s (~p) (variables: ~p)\n",
|
2012-06-11 17:32:41 +00:00
|
|
|
[BaseName, F, Type, string:join(Vars, ", ")])
|
2010-10-10 20:11:13 +00:00
|
|
|
end || {Type, F} <- AvailTemplates],
|
2010-08-20 15:30:37 +00:00
|
|
|
ok.
|
|
|
|
|
|
|
|
|
2010-01-08 05:18:55 +00:00
|
|
|
create(_Config, _) ->
|
2010-01-08 17:54:43 +00:00
|
|
|
%% Load a list of all the files in the escript -- cache it in the pdict
|
|
|
|
%% since we'll potentially need to walk it several times over the course
|
|
|
|
%% of a run.
|
|
|
|
cache_escript_files(),
|
|
|
|
|
|
|
|
%% Build a list of available templates
|
|
|
|
AvailTemplates = find_disk_templates() ++ find_escript_templates(),
|
|
|
|
?DEBUG("Available templates: ~p\n", [AvailTemplates]),
|
|
|
|
|
2011-01-09 11:29:41 +00:00
|
|
|
TemplateId = template_id(),
|
|
|
|
|
2010-01-08 17:54:43 +00:00
|
|
|
%% Using the specified template id, find the matching template file/type.
|
|
|
|
%% Note that if you define the same template in both ~/.rebar/templates
|
|
|
|
%% that is also present in the escript, the one on the file system will
|
|
|
|
%% be preferred.
|
2011-01-09 11:29:41 +00:00
|
|
|
{Type, Template} = select_template(AvailTemplates, TemplateId),
|
2010-01-08 17:54:43 +00:00
|
|
|
|
|
|
|
%% Load the template definition as is and get the list of variables the
|
|
|
|
%% template requires.
|
|
|
|
TemplateTerms = consult(load_file(Type, Template)),
|
2010-10-25 22:38:51 +00:00
|
|
|
case lists:keyfind(variables, 1, TemplateTerms) of
|
|
|
|
{variables, Vars} ->
|
2010-01-08 17:54:43 +00:00
|
|
|
case parse_vars(Vars, dict:new()) of
|
|
|
|
{error, Entry} ->
|
|
|
|
Context0 = undefined,
|
2010-02-21 03:15:07 +00:00
|
|
|
?ABORT("Failed while processing variables from template ~p."
|
|
|
|
"Variable definitions must follow form of "
|
|
|
|
"[{atom(), term()}]. Failed at: ~p\n",
|
2011-01-09 11:29:41 +00:00
|
|
|
[TemplateId, Entry]);
|
2010-01-08 17:54:43 +00:00
|
|
|
Context0 ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
false ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?WARN("No variables section found in template ~p; "
|
|
|
|
"using empty context.\n", [TemplateId]),
|
2010-01-08 17:54:43 +00:00
|
|
|
Context0 = dict:new()
|
|
|
|
end,
|
|
|
|
|
2011-03-30 03:38:14 +00:00
|
|
|
%% Load variables from disk file, if provided
|
|
|
|
Context1 = case rebar_config:get_global(template_vars, undefined) of
|
|
|
|
undefined ->
|
|
|
|
Context0;
|
|
|
|
File ->
|
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
%% TODO: Cleanup/merge with similar code in rebar_reltool
|
|
|
|
M = fun(_Key, _Base, Override) -> Override end,
|
|
|
|
dict:merge(M, Context0, dict:from_list(Terms));
|
|
|
|
{error, Reason} ->
|
|
|
|
?ABORT("Unable to load template_vars from ~s: ~p\n",
|
|
|
|
[File, Reason])
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2011-01-28 15:08:27 +00:00
|
|
|
%% For each variable, see if it's defined in global vars -- if it is,
|
|
|
|
%% prefer that value over the defaults
|
2011-03-30 03:38:14 +00:00
|
|
|
Context2 = update_vars(dict:fetch_keys(Context1), Context1),
|
2011-01-09 11:29:41 +00:00
|
|
|
?DEBUG("Template ~p context: ~p\n", [TemplateId, dict:to_list(Context1)]),
|
Support single level of nested template variables
Add support for defining template variables of the following form:
{variables, [{appid, "mochiwebapp"},
{author, "Mochi Media <dev@mochimedia.com>"},
{year, "2010"},
{version, "0.1"},
{port, 8080},
{dest, "{{appid}}"}]}.
Where dest may be overridden on the commandline but will default to
being the appid. Mochiweb uses this so that we can create new
projects from the template in a configurable directory.
So
$ rebar create template=mochiwebapp dest=foo appid=bar
I thought about special casing dest but figured it might be generally
useful to be able to nest template vars.
However this patch only does one level of resolution. So if
{variables, [{foo, "{{bar}}"},
{bar, "{{foo}}"}]}.
then bar will end up being the literal string {{bar}} and foo the
literal string {{foo}}.
2010-11-11 17:41:13 +00:00
|
|
|
|
|
|
|
%% Handle variables that possibly include other variables in their
|
|
|
|
%% definition
|
2011-04-10 22:01:34 +00:00
|
|
|
Context = resolve_variables(dict:to_list(Context2), Context2),
|
Support single level of nested template variables
Add support for defining template variables of the following form:
{variables, [{appid, "mochiwebapp"},
{author, "Mochi Media <dev@mochimedia.com>"},
{year, "2010"},
{version, "0.1"},
{port, 8080},
{dest, "{{appid}}"}]}.
Where dest may be overridden on the commandline but will default to
being the appid. Mochiweb uses this so that we can create new
projects from the template in a configurable directory.
So
$ rebar create template=mochiwebapp dest=foo appid=bar
I thought about special casing dest but figured it might be generally
useful to be able to nest template vars.
However this patch only does one level of resolution. So if
{variables, [{foo, "{{bar}}"},
{bar, "{{foo}}"}]}.
then bar will end up being the literal string {{bar}} and foo the
literal string {{foo}}.
2010-11-11 17:41:13 +00:00
|
|
|
|
2011-01-28 15:08:27 +00:00
|
|
|
?DEBUG("Resolved Template ~p context: ~p\n",
|
2011-03-30 03:38:14 +00:00
|
|
|
[TemplateId, dict:to_list(Context)]),
|
2010-01-08 17:54:43 +00:00
|
|
|
|
2011-01-28 15:08:27 +00:00
|
|
|
%% Now, use our context to process the template definition -- this
|
|
|
|
%% permits us to use variables within the definition for filenames.
|
2010-01-08 17:54:43 +00:00
|
|
|
FinalTemplate = consult(render(load_file(Type, Template), Context)),
|
2011-01-09 11:29:41 +00:00
|
|
|
?DEBUG("Final template def ~p: ~p\n", [TemplateId, FinalTemplate]),
|
2010-01-08 17:54:43 +00:00
|
|
|
|
|
|
|
%% Execute the instructions in the finalized template
|
2010-01-30 16:56:06 +00:00
|
|
|
Force = rebar_config:get_global(force, "0"),
|
|
|
|
execute_template(FinalTemplate, Type, Template, Context, Force, []).
|
2010-01-08 17:54:43 +00:00
|
|
|
|
2010-01-08 05:18:55 +00:00
|
|
|
|
2011-04-10 22:01:34 +00:00
|
|
|
%%
|
|
|
|
%% Given a list of key value pairs, for each string value attempt to
|
|
|
|
%% render it using Dict as the context. Storing the result in Dict as Key.
|
|
|
|
%%
|
|
|
|
resolve_variables([], Dict) ->
|
|
|
|
Dict;
|
|
|
|
resolve_variables([{Key, Value0} | Rest], Dict) when is_list(Value0) ->
|
|
|
|
Value = render(list_to_binary(Value0), Dict),
|
|
|
|
resolve_variables(Rest, dict:store(Key, Value, Dict));
|
|
|
|
resolve_variables([_Pair | Rest], Dict) ->
|
|
|
|
resolve_variables(Rest, Dict).
|
|
|
|
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Render a binary to a string, using mustache and the specified context
|
|
|
|
%%
|
|
|
|
render(Bin, Context) ->
|
|
|
|
%% Be sure to escape any double-quotes before rendering...
|
|
|
|
ReOpts = [global, {return, list}],
|
|
|
|
Str0 = re:replace(Bin, "\\\\", "\\\\\\", ReOpts),
|
|
|
|
Str1 = re:replace(Str0, "\"", "\\\\\"", ReOpts),
|
|
|
|
mustache:render(Str1, Context).
|
2010-01-08 05:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2010-01-08 17:54:43 +00:00
|
|
|
%%
|
|
|
|
%% Scan the current escript for available files and cache in pdict.
|
|
|
|
%%
|
|
|
|
cache_escript_files() ->
|
2010-03-05 21:56:31 +00:00
|
|
|
{ok, Files} = rebar_utils:escript_foldl(
|
2011-01-28 15:08:27 +00:00
|
|
|
fun(Name, _, GetBin, Acc) ->
|
|
|
|
[{Name, GetBin()} | Acc]
|
|
|
|
end,
|
|
|
|
[], rebar_config:get_global(escript, undefined)),
|
2010-01-08 17:54:43 +00:00
|
|
|
erlang:put(escript_files, Files).
|
|
|
|
|
|
|
|
|
|
|
|
template_id() ->
|
|
|
|
case rebar_config:get_global(template, undefined) of
|
|
|
|
undefined ->
|
|
|
|
?ABORT("No template specified.\n", []);
|
|
|
|
TemplateId ->
|
|
|
|
TemplateId
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_escript_templates() ->
|
|
|
|
[{escript, Name} || {Name, _Bin} <- erlang:get(escript_files),
|
|
|
|
re:run(Name, ?TEMPLATE_RE, [{capture, none}]) == match].
|
|
|
|
|
|
|
|
find_disk_templates() ->
|
2010-08-28 14:04:47 +00:00
|
|
|
OtherTemplates = find_other_templates(),
|
2011-07-17 16:38:59 +00:00
|
|
|
HomeFiles = rebar_utils:find_files(filename:join([os:getenv("HOME"),
|
|
|
|
".rebar", "templates"]),
|
2011-01-28 15:08:27 +00:00
|
|
|
?TEMPLATE_RE),
|
2010-02-04 19:30:10 +00:00
|
|
|
LocalFiles = rebar_utils:find_files(".", ?TEMPLATE_RE),
|
2010-08-28 14:04:47 +00:00
|
|
|
[{file, F} || F <- OtherTemplates ++ HomeFiles ++ LocalFiles].
|
|
|
|
|
|
|
|
find_other_templates() ->
|
|
|
|
case rebar_config:get_global(template_dir, undefined) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
TemplateDir ->
|
|
|
|
rebar_utils:find_files(TemplateDir, ?TEMPLATE_RE)
|
|
|
|
end.
|
2010-01-08 17:54:43 +00:00
|
|
|
|
|
|
|
select_template([], Template) ->
|
|
|
|
?ABORT("Template ~s not found.\n", [Template]);
|
|
|
|
select_template([{Type, Avail} | Rest], Template) ->
|
|
|
|
case filename:basename(Avail, ".template") == Template of
|
|
|
|
true ->
|
|
|
|
{Type, Avail};
|
|
|
|
false ->
|
|
|
|
select_template(Rest, Template)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Read the contents of a file from the appropriate source
|
|
|
|
%%
|
|
|
|
load_file(escript, Name) ->
|
|
|
|
{Name, Bin} = lists:keyfind(Name, 1, erlang:get(escript_files)),
|
|
|
|
Bin;
|
|
|
|
load_file(file, Name) ->
|
|
|
|
{ok, Bin} = file:read_file(Name),
|
|
|
|
Bin.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Parse/validate variables out from the template definition
|
|
|
|
%%
|
|
|
|
parse_vars([], Dict) ->
|
|
|
|
Dict;
|
|
|
|
parse_vars([{Key, Value} | Rest], Dict) when is_atom(Key) ->
|
|
|
|
parse_vars(Rest, dict:store(Key, Value, Dict));
|
|
|
|
parse_vars([Other | _Rest], _Dict) ->
|
|
|
|
{error, Other};
|
|
|
|
parse_vars(Other, _Dict) ->
|
|
|
|
{error, Other}.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Given a list of keys in Dict, see if there is a corresponding value defined
|
|
|
|
%% in the global config; if there is, update the key in Dict with it
|
|
|
|
%%
|
|
|
|
update_vars([], Dict) ->
|
|
|
|
Dict;
|
|
|
|
update_vars([Key | Rest], Dict) ->
|
|
|
|
Value = rebar_config:get_global(Key, dict:fetch(Key, Dict)),
|
|
|
|
update_vars(Rest, dict:store(Key, Value, Dict)).
|
|
|
|
|
|
|
|
|
|
|
|
%%
|
2012-06-30 20:13:26 +00:00
|
|
|
%% Given a string or binary, parse it into a list of terms, ala file:consult/1
|
2010-01-08 17:54:43 +00:00
|
|
|
%%
|
|
|
|
consult(Str) when is_list(Str) ->
|
|
|
|
consult([], Str, []);
|
|
|
|
consult(Bin) when is_binary(Bin)->
|
|
|
|
consult([], binary_to_list(Bin), []).
|
|
|
|
|
|
|
|
consult(Cont, Str, Acc) ->
|
|
|
|
case erl_scan:tokens(Cont, Str, 0) of
|
|
|
|
{done, Result, Remaining} ->
|
|
|
|
case Result of
|
|
|
|
{ok, Tokens, _} ->
|
|
|
|
{ok, Term} = erl_parse:parse_term(Tokens),
|
|
|
|
consult([], Remaining, [Term | Acc]);
|
|
|
|
{eof, _Other} ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
{error, Info, _} ->
|
|
|
|
{error, Info}
|
|
|
|
end;
|
|
|
|
{more, Cont1} ->
|
|
|
|
consult(Cont1, eof, Acc)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-02-13 19:14:29 +00:00
|
|
|
write_file(Output, Data, Force) ->
|
|
|
|
%% determine if the target file already exists
|
2011-01-07 15:32:36 +00:00
|
|
|
FileExists = filelib:is_regular(Output),
|
2010-01-30 16:56:06 +00:00
|
|
|
|
2010-02-13 19:14:29 +00:00
|
|
|
%% perform the function if we're allowed,
|
|
|
|
%% otherwise just process the next template
|
2011-01-07 13:58:30 +00:00
|
|
|
case Force =:= "1" orelse FileExists =:= false of
|
|
|
|
true ->
|
2010-10-10 20:11:13 +00:00
|
|
|
ok = filelib:ensure_dir(Output),
|
2011-01-09 11:29:41 +00:00
|
|
|
case {Force, FileExists} of
|
|
|
|
{"1", true} ->
|
2010-02-13 19:14:29 +00:00
|
|
|
?CONSOLE("Writing ~s (forcibly overwriting)~n",
|
|
|
|
[Output]);
|
2011-01-09 11:29:41 +00:00
|
|
|
_ ->
|
2010-01-30 16:56:06 +00:00
|
|
|
?CONSOLE("Writing ~s~n", [Output])
|
|
|
|
end,
|
2010-02-13 19:14:29 +00:00
|
|
|
case file:write_file(Output, Data) of
|
2010-01-30 16:56:06 +00:00
|
|
|
ok ->
|
2010-02-13 19:14:29 +00:00
|
|
|
ok;
|
2010-01-30 16:56:06 +00:00
|
|
|
{error, Reason} ->
|
2010-02-13 19:14:29 +00:00
|
|
|
?ABORT("Failed to write output file ~p: ~p\n",
|
|
|
|
[Output, Reason])
|
2010-01-30 16:56:06 +00:00
|
|
|
end;
|
2011-01-07 13:58:30 +00:00
|
|
|
false ->
|
2010-02-13 19:14:29 +00:00
|
|
|
{error, exists}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Execute each instruction in a template definition file.
|
|
|
|
%%
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template([], _TemplateType, _TemplateName, _Context,
|
|
|
|
_Force, ExistingFiles) ->
|
2010-10-25 22:38:51 +00:00
|
|
|
case ExistingFiles of
|
|
|
|
[] ->
|
2010-02-13 19:14:29 +00:00
|
|
|
ok;
|
|
|
|
_ ->
|
2011-01-28 15:08:27 +00:00
|
|
|
Msg = lists:flatten([io_lib:format("\t* ~p~n", [F]) ||
|
|
|
|
F <- lists:reverse(ExistingFiles)]),
|
|
|
|
Help =
|
|
|
|
"To force overwriting, specify force=1 on the command line.\n",
|
|
|
|
?ERROR("One or more files already exist on disk and "
|
|
|
|
"were not generated:~n~s~s", [Msg , Help])
|
2010-02-13 19:14:29 +00:00
|
|
|
end;
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template([{template, Input, Output} | Rest], TemplateType,
|
|
|
|
TemplateName, Context, Force, ExistingFiles) ->
|
2010-02-13 19:14:29 +00:00
|
|
|
InputName = filename:join(filename:dirname(TemplateName), Input),
|
2011-01-28 15:08:27 +00:00
|
|
|
case write_file(Output, render(load_file(TemplateType, InputName), Context),
|
|
|
|
Force) of
|
2010-02-13 19:14:29 +00:00
|
|
|
ok ->
|
|
|
|
execute_template(Rest, TemplateType, TemplateName, Context,
|
|
|
|
Force, ExistingFiles);
|
|
|
|
{error, exists} ->
|
|
|
|
execute_template(Rest, TemplateType, TemplateName, Context,
|
|
|
|
Force, [Output|ExistingFiles])
|
|
|
|
end;
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template([{file, Input, Output} | Rest], TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles) ->
|
2010-02-13 19:14:29 +00:00
|
|
|
InputName = filename:join(filename:dirname(TemplateName), Input),
|
|
|
|
case write_file(Output, load_file(TemplateType, InputName), Force) of
|
|
|
|
ok ->
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles);
|
2010-02-13 19:14:29 +00:00
|
|
|
{error, exists} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
|
|
|
Context, Force, [Output|ExistingFiles])
|
2010-01-08 17:54:43 +00:00
|
|
|
end;
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template([{dir, Name} | Rest], TemplateType, TemplateName, Context,
|
|
|
|
Force, ExistingFiles) ->
|
2010-01-08 17:54:43 +00:00
|
|
|
case filelib:ensure_dir(filename:join(Name, "dummy")) of
|
|
|
|
ok ->
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles);
|
2010-01-08 17:54:43 +00:00
|
|
|
{error, Reason} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Failed while processing template instruction "
|
|
|
|
"{dir, ~s}: ~p\n", [Name, Reason])
|
2011-07-04 22:17:44 +00:00
|
|
|
end;
|
|
|
|
execute_template([{copy, Input, Output} | Rest], TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles) ->
|
|
|
|
InputName = filename:join(filename:dirname(TemplateName), Input),
|
2011-07-18 16:52:32 +00:00
|
|
|
try rebar_file_utils:cp_r([InputName ++ "/*"], Output) of
|
2011-07-04 22:17:44 +00:00
|
|
|
ok ->
|
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
2011-07-18 16:52:32 +00:00
|
|
|
Context, Force, ExistingFiles)
|
|
|
|
catch _:_ ->
|
2011-07-04 22:17:44 +00:00
|
|
|
?ABORT("Failed while processing template instruction "
|
2012-02-17 10:03:32 +00:00
|
|
|
"{copy, ~s, ~s}~n", [Input, Output])
|
2010-01-08 17:54:43 +00:00
|
|
|
end;
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template([{chmod, Mod, File} | Rest], TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles) when is_integer(Mod) ->
|
2010-02-04 22:45:05 +00:00
|
|
|
case file:change_mode(File, Mod) of
|
|
|
|
ok ->
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles);
|
2010-02-04 22:45:05 +00:00
|
|
|
{error, Reason} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Failed while processing template instruction "
|
2012-02-17 10:03:32 +00:00
|
|
|
"{chmod, ~b, ~s}: ~p~n", [Mod, File, Reason])
|
2010-02-04 22:45:05 +00:00
|
|
|
end;
|
2011-10-21 15:59:29 +00:00
|
|
|
execute_template([{symlink, Existing, New} | Rest], TemplateType, TemplateName,
|
2011-09-28 22:23:08 +00:00
|
|
|
Context, Force, ExistingFiles) ->
|
2011-10-21 15:59:29 +00:00
|
|
|
case file:make_symlink(Existing, New) of
|
2011-09-28 22:23:08 +00:00
|
|
|
ok ->
|
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles);
|
|
|
|
{error, Reason} ->
|
|
|
|
?ABORT("Failed while processing template instruction "
|
2011-10-21 15:59:29 +00:00
|
|
|
"{symlink, ~s, ~s}: ~p~n", [Existing, New, Reason])
|
2011-09-28 22:23:08 +00:00
|
|
|
end;
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template([{variables, _} | Rest], TemplateType, TemplateName, Context,
|
|
|
|
Force, ExistingFiles) ->
|
|
|
|
execute_template(Rest, TemplateType, TemplateName,
|
|
|
|
Context, Force, ExistingFiles);
|
|
|
|
execute_template([Other | Rest], TemplateType, TemplateName, Context,
|
|
|
|
Force, ExistingFiles) ->
|
2010-01-08 17:54:43 +00:00
|
|
|
?WARN("Skipping unknown template instruction: ~p\n", [Other]),
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_template(Rest, TemplateType, TemplateName, Context,
|
|
|
|
Force, ExistingFiles).
|