diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rebar.app.src | 3 | ||||
-rw-r--r-- | src/rebar3.erl | 12 | ||||
-rw-r--r-- | src/rebar_app_discover.erl | 13 | ||||
-rw-r--r-- | src/rebar_dir.erl | 2 | ||||
-rw-r--r-- | src/rebar_git_resource.erl | 11 | ||||
-rw-r--r-- | src/rebar_mustache.erl | 296 | ||||
-rw-r--r-- | src/rebar_otp_app.erl | 7 | ||||
-rw-r--r-- | src/rebar_packages.erl | 11 | ||||
-rw-r--r-- | src/rebar_prv_app_discovery.erl | 8 | ||||
-rw-r--r-- | src/rebar_prv_deps.erl | 119 | ||||
-rw-r--r-- | src/rebar_prv_erlydtl_compiler.erl | 270 | ||||
-rw-r--r-- | src/rebar_prv_install_deps.erl | 2 | ||||
-rw-r--r-- | src/rebar_prv_update.erl | 13 | ||||
-rw-r--r-- | src/rebar_prv_upgrade.erl | 1 | ||||
-rw-r--r-- | src/rebar_state.erl | 56 | ||||
-rw-r--r-- | src/rebar_templater.erl | 77 | ||||
-rw-r--r-- | src/rebar_utils.erl | 51 |
17 files changed, 497 insertions, 455 deletions
diff --git a/src/rebar.app.src b/src/rebar.app.src index b404c42..f753784 100644 --- a/src/rebar.app.src +++ b/src/rebar.app.src @@ -3,7 +3,7 @@ {application, rebar, [{description, "Rebar: Erlang Build Tool"}, - {vsn, "3.0.0-alpha-4"}, + {vsn, "3.0.0-alpha-5"}, {modules, []}, {registered, []}, {applications, [kernel, @@ -37,7 +37,6 @@ rebar_prv_dialyzer, rebar_prv_do, rebar_prv_edoc, - rebar_prv_erlydtl_compiler, rebar_prv_escriptize, rebar_prv_eunit, rebar_prv_help, diff --git a/src/rebar3.erl b/src/rebar3.erl index b0cc949..92803c5 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -127,19 +127,17 @@ run_aux(State, RawArgs) -> filename:join(filename:absname(rebar_state:dir(State2)), BaseDir)), {ok, Providers} = application:get_env(rebar, providers), - {ok, Resources} = application:get_env(rebar, resources), - State4 = rebar_state:resources(State3, Resources), - State5 = rebar_plugins:install(State4), + State4 = rebar_plugins:install(State3), %% Providers can modify profiles stored in opts, so set default after initializing providers - State6 = rebar_state:create_logic_providers(Providers, State5), - State7 = rebar_state:default(State6, rebar_state:opts(State6)), + State5 = rebar_state:create_logic_providers(Providers, State4), + State6 = rebar_state:default(State5, rebar_state:opts(State5)), {Task, Args} = parse_args(RawArgs), - State8 = rebar_state:code_paths(State7, default, code:get_path()), + State7 = rebar_state:code_paths(State6, default, code:get_path()), - rebar_core:init_command(rebar_state:command_args(State8, Args), Task). + rebar_core:init_command(rebar_state:command_args(State7, Args), Task). init_config() -> %% Initialize logging system diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl index 41f41f5..73401bc 100644 --- a/src/rebar_app_discover.erl +++ b/src/rebar_app_discover.erl @@ -159,7 +159,12 @@ find_app(AppDir, Validate) -> case Validate of V when V =:= invalid ; V =:= all -> AppInfo = create_app_info(AppDir, File), - {true, rebar_app_info:app_file_src(AppInfo, File)}; + case AppInfo of + {error, Reason} -> + throw({error, {invalid_app_file, File, Reason}}); + _ -> + {true, rebar_app_info:app_file_src(AppInfo, File)} + end; valid -> false end; @@ -175,7 +180,7 @@ find_app(AppDir, Validate) -> app_dir(AppFile) -> filename:join(rebar_utils:droplast(filename:split(filename:dirname(AppFile)))). --spec create_app_info(file:name(), file:name()) -> rebar_app_info:t() | error. +-spec create_app_info(file:name(), file:name()) -> rebar_app_info:t() | {error, term()}. create_app_info(AppDir, AppFile) -> case file:consult(AppFile) of {ok, [{application, AppName, AppDetails}]} -> @@ -193,8 +198,8 @@ create_app_info(AppDir, AppFile) -> false end, rebar_app_info:dir(rebar_app_info:valid(AppInfo1, Valid), AppDir); - _ -> - error + {error, Reason} -> + {error, Reason} end. dedup([]) -> []; diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index c0c6bb2..a94c72d 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -34,7 +34,7 @@ profile_dir(State, Profiles) -> ["default"] -> ["default"]; %% drop `default` from the profile dir if it's implicit and reverse order %% of profiles to match order passed to `as` - ["default"|Rest] -> lists:reverse(Rest) + ["default"|Rest] -> Rest end, ProfilesDir = string:join(ProfilesStrings, "+"), filename:join(rebar_state:get(State, base_dir, ?DEFAULT_BASE_DIR), ProfilesDir). diff --git a/src/rebar_git_resource.erl b/src/rebar_git_resource.erl index 3b77da2..07c9b4d 100644 --- a/src/rebar_git_resource.erl +++ b/src/rebar_git_resource.erl @@ -31,11 +31,14 @@ needs_update(Dir, {git, Url, {tag, Tag}}) -> ?DEBUG("Comparing git tag ~s with ~s", [Tag, Current1]), not ((Current1 =:= Tag) andalso compare_url(Dir, Url)); needs_update(Dir, {git, Url, {branch, Branch}}) -> - {ok, Current} = rebar_utils:sh(?FMT("git symbolic-ref -q --short HEAD", []), + %% Fetch remote so we can check if the branch has changed + {ok, _} = rebar_utils:sh(?FMT("git fetch origin ~s", [Branch]), + [{cd, Dir}]), + %% Check for new commits to origin/Branch + {ok, Current} = rebar_utils:sh(?FMT("git log HEAD..origin/~s --oneline", [Branch]), [{cd, Dir}]), - Current1 = string:strip(string:strip(Current, both, $\n), both, $\r), - ?DEBUG("Comparing git branch ~s with ~s", [Branch, Current1]), - not ((Current1 =:= Branch) andalso compare_url(Dir, Url)); + ?DEBUG("Checking git branch ~s for updates", [Branch]), + not ((Current =:= []) andalso compare_url(Dir, Url)); needs_update(Dir, {git, Url, "master"}) -> needs_update(Dir, {git, Url, {branch, "master"}}); needs_update(Dir, {git, Url, Ref}) -> diff --git a/src/rebar_mustache.erl b/src/rebar_mustache.erl new file mode 100644 index 0000000..af8a342 --- /dev/null +++ b/src/rebar_mustache.erl @@ -0,0 +1,296 @@ +%% The MIT License (MIT) +%% +%% Copyright (c) 2015 Hinagiku Soranoba +%% +%% 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. +%% +%% @doc Mustache template engine for Erlang/OTP. +-module(rebar_mustache). + +%%---------------------------------------------------------------------------------------------------------------------- +%% Exported API +%%---------------------------------------------------------------------------------------------------------------------- +-export([ + render/2, + parse_binary/1, + parse_file/1, + compile/2 + ]). + +-export_type([ + template/0, + data/0 + ]). + +%%---------------------------------------------------------------------------------------------------------------------- +%% Defines & Records & Types +%%---------------------------------------------------------------------------------------------------------------------- + +-define(PARSE_ERROR, incorrect_format). +-define(FILE_ERROR, file_not_found). +-define(COND(Cond, TValue, FValue), + case Cond of true -> TValue; false -> FValue end). + +-type key() :: binary(). +-type tag() :: {n, key()} | + {'&', key()} | + {'#', key(), [tag()], Source :: binary()} | + {'^', key(), [tag()]} | + binary(). + +-record(state, + { + dirname = <<>> :: file:filename_all(), + start = <<"{{">> :: binary(), + stop = <<"}}">> :: binary() + }). +-type state() :: #state{}. + +-record(?MODULE, + { + data :: [tag()] + }). + +-opaque template() :: #?MODULE{}. +%% @see parse_binary/1 +%% @see parse_file/1 +-ifdef(namespaced_types). +-type data() :: #{string() => data() | iodata() | fun((data(), function()) -> iodata())}. +-else. +-type data() :: dict(). +-endif. +%% @see render/2 +%% @see compile/2 +-type partial() :: {partial, {state(), EndTag :: binary(), LastTagSize :: non_neg_integer(), Rest :: binary(), [tag()]}}. + +%%---------------------------------------------------------------------------------------------------------------------- +%% Exported Functions +%%---------------------------------------------------------------------------------------------------------------------- + +%% @equiv compile(parse_binary(Bin), Map) +-spec render(binary(), data()) -> binary(). +render(Bin, Map) -> + compile(parse_binary(Bin), Map). + +%% @doc Create a {@link template/0} from a binary. +-spec parse_binary(binary()) -> template(). +parse_binary(Bin) when is_binary(Bin) -> + parse_binary_impl(#state{}, Bin). + +%% @doc Create a {@link template/0} from a file. +-spec parse_file(file:filename()) -> template(). +parse_file(Filename) -> + case file:read_file(Filename) of + {ok, Bin} -> parse_binary_impl(#state{dirname = filename:dirname(Filename)}, Bin); + _ -> error(?FILE_ERROR, [Filename]) + end. + +%% @doc Embed the data in the template. +-spec compile(template(), data()) -> binary(). +compile(#?MODULE{data = Tags}, Map) -> + ec_cnv:to_binary(lists:reverse(compile_impl(Tags, Map, []))). + +%%---------------------------------------------------------------------------------------------------------------------- +%% Internal Function +%%---------------------------------------------------------------------------------------------------------------------- + +%% @doc {@link compile/2} +%% +%% ATTENTION: The result is a list that is inverted. +-spec compile_impl(Template :: [tag()], data(), Result :: iodata()) -> iodata(). +compile_impl([], _, Result) -> + Result; +compile_impl([{n, Key} | T], Map, Result) -> + compile_impl(T, Map, [escape(to_binary(dict_get(binary_to_list(Key), Map, <<>>))) | Result]); +compile_impl([{'&', Key} | T], Map, Result) -> + compile_impl(T, Map, [to_binary(dict_get(binary_to_list(Key), Map, <<>>)) | Result]); +compile_impl([{'#', Key, Tags, Source} | T], Map, Result) -> + Value = dict_get(binary_to_list(Key), Map, undefined), + if + is_list(Value) -> compile_impl(T, Map, lists:foldl(fun(X, Acc) -> compile_impl(Tags, X, Acc) end, + Result, Value)); + Value =:= false; Value =:= undefined -> compile_impl(T, Map, Result); + is_function(Value, 2) -> compile_impl(T, Map, [Value(Source, fun(Text) -> render(Text, Map) end) | Result]); + %is_dict(Value) -> compile_impl(T, Map, compile_impl(Tags, Value, Result)); + true -> compile_impl(T, Map, compile_impl(Tags, Map, Result)) + end; +compile_impl([{'^', Key, Tags} | T], Map, Result) -> + Value = dict_get(binary_to_list(Key), Map, undefined), + case Value =:= undefined orelse Value =:= [] orelse Value =:= false of + true -> compile_impl(T, Map, compile_impl(Tags, Map, Result)); + false -> compile_impl(T, Map, Result) + end; +compile_impl([Bin | T], Map, Result) -> + compile_impl(T, Map, [Bin | Result]). + +%% @see parse_binary/1 +-spec parse_binary_impl(state(), Input :: binary()) -> template(). +parse_binary_impl(State, Input) -> + #?MODULE{data = parse(State, Input)}. + +%% @doc Analyze the syntax of the mustache. +-spec parse(state(), binary()) -> [tag()]. +parse(State, Bin) -> + case parse1(State, Bin, []) of + {partial, _} -> error(?PARSE_ERROR); + {_, Tags} -> lists:reverse(Tags) + end. + +%% @doc Part of the `parse/1' +%% +%% ATTENTION: The result is a list that is inverted. +-spec parse1(state(), Input :: binary(), Result :: [tag()]) -> {state(), [tag()]} | partial(). +parse1(#state{start = Start, stop = Stop} = State, Bin, Result) -> + case binary:split(Bin, Start) of + [] -> {State, Result}; + [B1] -> {State, [B1 | Result]}; + [B1, <<"{", B2/binary>>] -> parse2(State, binary:split(B2, <<"}", Stop/binary>>), [B1 | Result]); + [B1, B2] -> parse3(State, binary:split(B2, Stop), [B1 | Result]) + end. + +%% @doc Part of the `parse/1' +%% +%% ATTENTION: The result is a list that is inverted. +parse2(State, [B1, B2], Result) -> + parse1(State, B2, [{'&', remove_space_from_edge(B1)} | Result]); +parse2(_, _, _) -> + error(?PARSE_ERROR). + +%% @doc Part of the `parse/1' +%% +%% ATTENTION: The result is a list that is inverted. +parse3(State, [B1, B2], Result) -> + case remove_space_from_head(B1) of + <<"&", Tag/binary>> -> + parse1(State, B2, [{'&', remove_space_from_edge(Tag)} | Result]); + <<T, Tag/binary>> when T =:= $#; T =:= $^ -> + parse_loop(State, ?COND(T =:= $#, '#', '^'), remove_space_from_edge(Tag), B2, Result); + <<"=", Tag0/binary>> -> + Tag1 = remove_space_from_tail(Tag0), + Size = byte_size(Tag1) - 1, + case Size >= 0 andalso Tag1 of + <<Tag2:Size/binary, "=">> -> parse_delimiter(State, Tag2, B2, Result); + _ -> error(?PARSE_ERROR) + end; + <<"!", _/binary>> -> + parse1(State, B2, Result); + <<"/", Tag/binary>> -> + {partial, {State, remove_space_from_edge(Tag), byte_size(B1) + 4, B2, Result}}; + <<">", Tag/binary>> -> + parse_jump(State, remove_space_from_edge(Tag), B2, Result); + Tag -> + parse1(State, B2, [{n, remove_space_from_tail(Tag)} | Result]) + end; +parse3(_, _, _) -> + error(?PARSE_ERROR). + +%% @doc Loop processing part of the `parse/1' +%% +%% `{{# Tag}}' or `{{^ Tag}}' corresponds to this. +-spec parse_loop(state(), '#' | '^', Tag :: binary(), Input :: binary(), Result :: [tag()]) -> [tag()] | partial(). +parse_loop(State0, Mark, Tag, Input, Result0) -> + case parse1(State0, Input, []) of + {partial, {State, Tag, LastTagSize, Rest, Result1}} when is_list(Result1) -> + case Mark of + '#' -> Source = binary:part(Input, 0, byte_size(Input) - byte_size(Rest) - LastTagSize), + parse1(State, Rest, [{'#', Tag, lists:reverse(Result1), Source} | Result0]); + '^' -> parse1(State, Rest, [{'^', Tag, lists:reverse(Result1)} | Result0]) + end; + _ -> + error(?PARSE_ERROR) + end. + +%% @doc Partial part of the `parse/1' +-spec parse_jump(state(), Tag :: binary(), NextBin :: binary(), Result :: [tag()]) -> [tag()] | partial(). +parse_jump(#state{dirname = Dirname} = State0, Tag, NextBin, Result0) -> + Filename0 = <<Tag/binary, ".mustache">>, + Filename = filename:join(?COND(Dirname =:= <<>>, [Filename0], [Dirname, Filename0])), + case file:read_file(Filename) of + {ok, Bin} -> + case parse1(State0, Bin, Result0) of + {partial, _} -> error(?PARSE_ERROR); + {State, Result} -> parse1(State, NextBin, Result) + end; + _ -> + error(?FILE_ERROR, [Filename]) + end. + +%% @doc Update delimiter part of the `parse/1' +%% +%% Parse_BinaryDelimiterBin :: e.g. `{{=%% %%=}}' -> `%% %%' +-spec parse_delimiter(state(), Parse_BinaryDelimiterBin :: binary(), NextBin :: binary(), Result :: [tag()]) -> [tag()] | partial(). +parse_delimiter(State0, Parse_BinaryDelimiterBin, NextBin, Result) -> + case binary:match(Parse_BinaryDelimiterBin, <<"=">>) of + nomatch -> + case [X || X <- binary:split(Parse_BinaryDelimiterBin, <<" ">>, [global]), X =/= <<>>] of + [Start, Stop] -> parse1(State0#state{start = Start, stop = Stop}, NextBin, Result); + _ -> error(?PARSE_ERROR) + end; + _ -> + error(?PARSE_ERROR) + end. + +%% @doc Remove the space from the edge. +-spec remove_space_from_edge(binary()) -> binary(). +remove_space_from_edge(Bin) -> + remove_space_from_tail(remove_space_from_head(Bin)). + +%% @doc Remove the space from the head. +-spec remove_space_from_head(binary()) -> binary(). +remove_space_from_head(<<" ", Rest/binary>>) -> remove_space_from_head(Rest); +remove_space_from_head(Bin) -> Bin. + +%% @doc Remove the space from the tail. +-spec remove_space_from_tail(binary()) -> binary(). +remove_space_from_tail(<<>>) -> <<>>; +remove_space_from_tail(Bin) -> + PosList = binary:matches(Bin, <<" ">>), + LastPos = remove_space_from_tail_impl(lists:reverse(PosList), byte_size(Bin)), + binary:part(Bin, 0, LastPos). + +%% @see remove_space_from_tail/1 +-spec remove_space_from_tail_impl([{non_neg_integer(), pos_integer()}], non_neg_integer()) -> non_neg_integer(). +remove_space_from_tail_impl([{X, Y} | T], Size) when Size =:= X + Y -> + remove_space_from_tail_impl(T, X); +remove_space_from_tail_impl(_, Size) -> + Size. + +%% @doc Number to binary +-spec to_binary(number() | binary() | string()) -> binary() | string(). +to_binary(Integer) when is_integer(Integer) -> + ec_cnv:to_binary(Integer); +to_binary(Float) when is_float(Float) -> + io_lib:format("~p", [Float]); +to_binary(X) -> + X. + +%% @doc HTML Escape +-spec escape(iodata()) -> binary(). +escape(IoData) -> + Bin = ec_cnv:to_binary(IoData), + << <<(escape_char(X))/binary>> || <<X:8>> <= Bin >>. + +%% @see escape/1 +-spec escape_char(0..16#FFFF) -> binary(). +escape_char($<) -> <<"<">>; +escape_char($>) -> <<">">>; +escape_char($&) -> <<"&">>; +escape_char($") -> <<""">>; +escape_char($') -> <<"'">>; +escape_char(C) -> <<C:8>>. + +dict_get(Key, Dict, Default) -> + case dict:find(ec_cnv:to_atom(Key), Dict) of + {ok, Value} -> + Value; + error -> + Default + end. diff --git a/src/rebar_otp_app.erl b/src/rebar_otp_app.erl index 260343d..0bf27c9 100644 --- a/src/rebar_otp_app.erl +++ b/src/rebar_otp_app.erl @@ -104,7 +104,7 @@ preprocess(State, AppInfo, AppSrcFile) -> A1 = apply_app_vars(AppVars, AppData), %% AppSrcFile may contain instructions for generating a vsn number - Vsn = app_vsn(AppSrcFile), + Vsn = app_vsn(AppSrcFile, State), A2 = lists:keystore(vsn, 1, A1, {vsn, Vsn}), %% systools:make_relup/4 fails with {missing_param, registered} @@ -197,11 +197,12 @@ consult_app_file(Filename) -> end end. -app_vsn(AppFile) -> +app_vsn(AppFile, State) -> case consult_app_file(AppFile) of {ok, [{application, _AppName, AppData}]} -> AppDir = filename:dirname(filename:dirname(AppFile)), - rebar_utils:vcs_vsn(get_value(vsn, AppData, AppFile), AppDir); + Resources = rebar_state:resources(State), + rebar_utils:vcs_vsn(get_value(vsn, AppData, AppFile), AppDir, Resources); {error, Reason} -> ?ABORT("Failed to consult app file ~s: ~p\n", [AppFile, Reason]) end. diff --git a/src/rebar_packages.erl b/src/rebar_packages.erl index 8982573..fb2d094 100644 --- a/src/rebar_packages.erl +++ b/src/rebar_packages.erl @@ -73,14 +73,14 @@ find_highest_matching(Dep, Constraint, T) -> [{Dep, [[Vsn]]}] -> case ec_semver:pes(Vsn, Constraint) of true -> - Vsn; + {ok, Vsn}; false -> ?WARN("Only existing version of ~s is ~s which does not match constraint ~~> ~s. " "Using anyway, but it is not guarenteed to work.", [Dep, Vsn, Constraint]), - Vsn + {ok, Vsn} end; [{Dep, [[HeadVsn | VsnTail]]}] -> - lists:foldl(fun(Version, Highest) -> + {ok, lists:foldl(fun(Version, Highest) -> case ec_semver:pes(Version, Constraint) andalso ec_semver:gt(Version, Highest) of true -> @@ -88,5 +88,8 @@ find_highest_matching(Dep, Constraint, T) -> false -> Highest end - end, HeadVsn, VsnTail) + end, HeadVsn, VsnTail)}; + [] -> + ?WARN("Missing registry entry for package ~s", [Dep]), + none end. diff --git a/src/rebar_prv_app_discovery.erl b/src/rebar_prv_app_discovery.erl index 31c0f59..97862c1 100644 --- a/src/rebar_prv_app_discovery.erl +++ b/src/rebar_prv_app_discovery.erl @@ -45,5 +45,13 @@ do(State) -> -spec format_error(any()) -> iolist(). format_error({multiple_app_files, Files}) -> io_lib:format("Multiple app files found in one app dir: ~s", [string:join(Files, " and ")]); +format_error({invalid_app_file, File, Reason}) -> + case Reason of + {Line, erl_parse, Description} -> + io_lib:format("Invalid app file ~s at line ~b: ~p", + [File, Line, lists:flatten(Description)]); + _ -> + io_lib:format("Invalid app file ~s: ~p", [File, Reason]) + end; format_error(Reason) -> io_lib:format("~p", [Reason]). diff --git a/src/rebar_prv_deps.erl b/src/rebar_prv_deps.erl index 3627e91..be81c31 100644 --- a/src/rebar_prv_deps.erl +++ b/src/rebar_prv_deps.erl @@ -13,14 +13,18 @@ -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. init(State) -> - State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER}, - {module, ?MODULE}, - {bare, true}, - {deps, ?DEPS}, - {example, "rebar3 deps"}, - {short_desc, "List dependencies"}, - {desc, info("List dependencies")}, - {opts, []}])), + State1 = rebar_state:add_provider( + State, + providers:create([ + {name, ?PROVIDER}, + {module, ?MODULE}, + {bare, true}, + {deps, ?DEPS}, + {example, "rebar3 deps"}, + {short_desc, "List dependencies"}, + {desc, "List dependencies. Those not matching lock files " + "are followed by an asterisk (*)."}, + {opts, []}])), {ok, State1}. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. @@ -44,35 +48,55 @@ display(State, Profile, Deps) -> display_deps(State, Deps), ?CONSOLE("", []). -merge(Deps, Deps) -> - Deps; merge(Deps, SourceDeps) -> + merge1(dedup([normalize(Dep) || Dep <- Deps]), + [normalize(Dep) || Dep <- SourceDeps]). + +normalize(Name) when is_binary(Name) -> + Name; +normalize(Name) when is_atom(Name) -> + ec_cnv:to_binary(Name); +normalize(Dep) when is_tuple(Dep) -> + Name = element(1, Dep), + setelement(1, Dep, normalize(Name)). + +merge1(Deps, SourceDeps) -> + Names = [name(Dep) || Dep <- Deps], ToAdd = [Dep || Dep <- SourceDeps, - not lists:keymember(ec_cnv:to_binary(element(1,Dep)), 1, Deps)], + not lists:member(name(Dep), Names)], Deps ++ ToAdd. +%% Keep the latter one as locks come after regular deps in the list. +%% This is totally not safe as an assumption, but it's what we got. +%% We do this by comparing the current element and looking if a +%% similar named one happens later. If so, drop the current one. +dedup(Deps) -> dedup(Deps, [name(Dep) || Dep <- Deps]). + +dedup([], []) -> []; +dedup([Dep|Deps], [Name|DepNames]) -> + case lists:member(Name, DepNames) of + true -> dedup(Deps, DepNames); + false -> [Dep | dedup(Deps, DepNames)] + end. + +name(T) when is_tuple(T) -> element(1, T); +name(B) when is_binary(B) -> B. + + display_deps(State, Deps) -> lists:foreach(fun(Dep) -> display_dep(State, Dep) end, Deps). %% packages display_dep(_State, {Name, Vsn}) when is_list(Vsn) -> ?CONSOLE("~s* (package ~s)", [ec_cnv:to_binary(Name), ec_cnv:to_binary(Vsn)]); -display_dep(_State, Name) when is_atom(Name) -> - ?CONSOLE("~s* (package)", [ec_cnv:to_binary(Name)]); -%% git source -display_dep(_State, {Name, Source}) when is_tuple(Source), element(1, Source) =:= git -> - ?CONSOLE("~s* (git source)", [ec_cnv:to_binary(Name)]); -display_dep(_State, {Name, _Vsn, Source}) when is_tuple(Source), element(1, Source) =:= git -> - ?CONSOLE("~s* (git source)", [ec_cnv:to_binary(Name)]); -display_dep(_State, {Name, _Vsn, Source, _Opts}) when is_tuple(Source), element(1, Source) =:= git -> - ?CONSOLE("~s* (git soutce)", [ec_cnv:to_binary(Name)]); -%% unknown source +display_dep(_State, Name) when is_binary(Name) -> + ?CONSOLE("~s* (package)", [Name]); display_dep(_State, {Name, Source}) when is_tuple(Source) -> - ?CONSOLE("~s* (source ~p)", [ec_cnv:to_binary(Name), Source]); + ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]); display_dep(_State, {Name, _Vsn, Source}) when is_tuple(Source) -> - ?CONSOLE("~s* (source ~p)", [ec_cnv:to_binary(Name), Source]); + ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]); display_dep(_State, {Name, _Vsn, Source, _Opts}) when is_tuple(Source) -> - ?CONSOLE("~s* (source ~p)", [ec_cnv:to_binary(Name), Source]); + ?CONSOLE("~s* (~s source)", [ec_cnv:to_binary(Name), type(Source)]); %% Locked display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) -> DepsDir = rebar_dir:deps_dir(State), @@ -82,14 +106,6 @@ display_dep(State, {Name, Source={pkg, _, Vsn}, Level}) when is_integer(Level) - false -> "" end, ?CONSOLE("~s~s (locked package ~s)", [Name, NeedsUpdate, Vsn]); -display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Level), element(1, Source) =:= git -> - DepsDir = rebar_dir:deps_dir(State), - AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]), - NeedsUpdate = case rebar_fetch:needs_update(AppDir, Source, State) of - true -> "*"; - false -> "" - end, - ?CONSOLE("~s~s (locked git source)", [Name, NeedsUpdate]); display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Level) -> DepsDir = rebar_dir:deps_dir(State), AppDir = filename:join([DepsDir, ec_cnv:to_binary(Name)]), @@ -97,39 +113,6 @@ display_dep(State, {Name, Source, Level}) when is_tuple(Source), is_integer(Leve true -> "*"; false -> "" end, - ?CONSOLE("~s~s (locked ~p)", [Name, NeedsUpdate, Source]). - -info(Description) -> - io_lib:format("~s.~n" - "~n" - "Valid rebar.config options:~n" - " ~p~n" - " ~p~n" - "Valid command line options:~n" - " deps_dir=\"deps\" (override default or rebar.config deps_dir)~n", - [ - Description, - {deps_dir, "deps"}, - {deps, - [app_name, - {rebar, "1.0.*"}, - {rebar, ".*", - {git, "git://github.com/rebar/rebar.git"}}, - {rebar, ".*", - {git, "git://github.com/rebar/rebar.git", "Rev"}}, - {rebar, "1.0.*", - {git, "git://github.com/rebar/rebar.git", {branch, "master"}}}, - {rebar, "1.0.0", - {git, "git://github.com/rebar/rebar.git", {tag, "1.0.0"}}}, - {rebar, "", - {git, "git://github.com/rebar/rebar.git", {branch, "master"}}, - [raw]}, - {app_name, ".*", {hg, "https://www.example.org/url"}}, - {app_name, ".*", {rsync, "Url"}}, - {app_name, ".*", {svn, "https://www.example.org/url"}}, - {app_name, ".*", {svn, "svn://svn.example.org/url"}}, - {app_name, ".*", {bzr, "https://www.example.org/url", "Rev"}}, - {app_name, ".*", {fossil, "https://www.example.org/url"}}, - {app_name, ".*", {fossil, "https://www.example.org/url", "Vsn"}}, - {app_name, ".*", {p4, "//depot/subdir/app_dir"}}]} - ]). + ?CONSOLE("~s~s (locked ~s source)", [Name, NeedsUpdate, type(Source)]). + +type(Source) when is_tuple(Source) -> element(1, Source). diff --git a/src/rebar_prv_erlydtl_compiler.erl b/src/rebar_prv_erlydtl_compiler.erl deleted file mode 100644 index a7442af..0000000 --- a/src/rebar_prv_erlydtl_compiler.erl +++ /dev/null @@ -1,270 +0,0 @@ -%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- -%% ex: ts=4 sw=4 et -%% ------------------------------------------------------------------- -%% -%% rebar: Erlang Build Tools -%% -%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com), -%% Bryan Fink (bryan@basho.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. -%% ------------------------------------------------------------------- - -%% The rebar_erlydtl_compiler module is a plugin for rebar that compiles -%% ErlyDTL templates. By default, it compiles all templates/*.dtl -%% to ebin/*_dtl.beam. -%% -%% Configuration options should be placed in rebar.config under -%% 'erlydtl_opts'. It can be a list of name-value tuples or a list of -%% lists of name-value tuples if you have multiple template directories -%% that need to have different settings (see example below). -%% -%% Available options include: -%% -%% doc_root: where to find templates to compile -%% "templates" by default -%% -%% out_dir: where to put compiled template beam files -%% "ebin" by default -%% -%% source_ext: the file extension the template sources have -%% ".dtl" by default -%% -%% module_ext: characters to append to the template's module name -%% "_dtl" by default -%% -%% recursive: boolean that determines if doc_root(s) need to be -%% scanned recursively for matching template file names -%% (default: true). -%% For example, if you had: -%% /t_src/ -%% base.html -%% foo.html -%% -%% And you wanted them compiled to: -%% /priv/ -%% base.beam -%% foo.beam -%% -%% You would add to your rebar.config: -%% {erlydtl_opts, [ -%% {doc_root, "t_src"}, -%% {out_dir, "priv"}, -%% {source_ext, ".html"}, -%% {module_ext, ""} -%% ]}. -%% -%% The default settings are the equivalent of: -%% {erlydtl_opts, [ -%% {doc_root, "templates"}, -%% {out_dir, "ebin"}, -%% {source_ext, ".dtl"}, -%% {module_ext, "_dtl"} -%% ]}. -%% -%% The following example will compile the following templates: -%% "src/*.dtl" files into "ebin/*_dtl.beam" and -%% "templates/*.html" into "ebin/*.beam". Note that any tuple option -%% (such as 'out_dir') in the outer list is added to each inner list: -%% {erlydtl_opts, [ -%% {out_dir, "ebin"}, -%% {recursive, false}, -%% [ -%% {doc_root, "src"}, {module_ext, "_dtl"} -%% ], -%% [ -%% {doc_root, "templates"}, {module_ext, ""}, {source_ext, ".html"} -%% ] -%% ]}. --module(rebar_prv_erlydtl_compiler). - --behaviour(provider). - --export([init/1, - do/1, - format_error/1]). - --include("rebar.hrl"). --include_lib("providers/include/providers.hrl"). - --define(PROVIDER, compile). --define(DEPS, [{default, compile}]). - -%% =================================================================== -%% Public API -%% =================================================================== - --spec init(rebar_state:t()) -> {ok, rebar_state:t()}. -init(State) -> - State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER}, - {module, ?MODULE}, - {namespace, erlydtl}, - {bare, false}, - {deps, ?DEPS}, - {example, "rebar3 erlydtl compile"}, - {short_desc, "Compile erlydtl templates."}, - {desc, "Compile erlydtl templates."}, - {opts, []}])), - {ok, State1}. - -do(State) -> - ?INFO("Running erlydtl...", []), - case rebar_state:get(State, escript_main_app, undefined) of - undefined -> - Dir = rebar_state:dir(State), - case rebar_app_discover:find_app(Dir, all) of - {true, AppInfo} -> - AllApps = rebar_state:project_apps(State) ++ rebar_state:all_deps(State), - case rebar_app_utils:find(rebar_app_info:name(AppInfo), AllApps) of - {ok, AppInfo1} -> - %% Use the existing app info instead of newly created one - run_erlydtl(AppInfo1, State); - _ -> - run_erlydtl(AppInfo, State) - end, - {ok, State}; - _ -> - ?PRV_ERROR(no_main_app) - end; - Name -> - AllApps = rebar_state:project_apps(State) ++ rebar_state:all_deps(State), - {ok, App} = rebar_app_utils:find(Name, AllApps), - run_erlydtl(App, State), - {ok, State} - end. - -run_erlydtl(App, State) -> - Dir = rebar_state:dir(State), - DtlOpts = proplists:unfold(rebar_state:get(State, erlydtl_opts, [])), - TemplateDir = filename:join(Dir, option(doc_root, DtlOpts)), - DtlOpts2 = [{doc_root, TemplateDir} | proplists:delete(doc_root, DtlOpts)], - OutDir = rebar_app_info:ebin_dir(App), - filelib:ensure_dir(filename:join(OutDir, "dummy.beam")), - rebar_base_compiler:run(State, - [], - TemplateDir, - option(source_ext, DtlOpts2), - OutDir, - option(module_ext, DtlOpts2) ++ ".beam", - fun(S, T, C) -> - compile_dtl(C, S, T, DtlOpts2, Dir, OutDir) - end, - [{check_last_mod, false}, - {recursive, option(recursive, DtlOpts2)}]). - --spec format_error(any()) -> iolist(). -format_error(no_main_app) -> - "Erlydtl Error: Multiple project apps found and no {app, atom()} option found in erlydtl_opts."; -format_error(Reason) -> - io_lib:format("~p", [Reason]). - -%% =================================================================== -%% Internal functions -%% =================================================================== - -option(Opt, DtlOpts) -> - proplists:get_value(Opt, DtlOpts, default(Opt)). - -default(app) -> undefined; -default(doc_root) -> "priv/templates"; -default(source_ext) -> ".dtl"; -default(module_ext) -> "_dtl"; -default(custom_tags_dir) -> ""; -default(compiler_options) -> [return]; -default(recursive) -> true. - -compile_dtl(State, Source, Target, DtlOpts, Dir, OutDir) -> - case needs_compile(Source, Target, DtlOpts) of - true -> - do_compile(State, Source, Target, DtlOpts, Dir, OutDir); - false -> - skipped - end. - -do_compile(State, Source, Target, DtlOpts, Dir, OutDir) -> - CompilerOptions = option(compiler_options, DtlOpts), - - Sorted = proplists:unfold( - lists:sort( - [{out_dir, OutDir}, - {doc_root, filename:join(Dir, option(doc_root, DtlOpts))}, - {custom_tags_dir, option(custom_tags_dir, DtlOpts)}, - {compiler_options, CompilerOptions}])), - - %% ensure that doc_root and out_dir are defined, - %% using defaults if necessary - Opts = lists:ukeymerge(1, DtlOpts, Sorted), - ?DEBUG("Compiling \"~s\" -> \"~s\" with options:~n ~s", - [Source, Target, io_lib:format("~p", [Opts])]), - case erlydtl:compile_file(ec_cnv:to_list(Source), - list_to_atom(module_name(Target)), - Opts) of - {ok, _Mod} -> - ok; - {ok, _Mod, Ws} -> - rebar_base_compiler:ok_tuple(State, Source, Ws); - error -> - rebar_base_compiler:error_tuple(State, Source, [], [], Opts); - {error, Es, Ws} -> - rebar_base_compiler:error_tuple(State, Source, Es, Ws, Opts) - end. - -module_name(Target) -> - filename:rootname(filename:basename(Target), ".beam"). - -needs_compile(Source, Target, DtlOpts) -> - LM = filelib:last_modified(Target), - LM < filelib:last_modified(Source) orelse - lists:any(fun(D) -> LM < filelib:last_modified(D) end, - referenced_dtls(Source, DtlOpts)). - -referenced_dtls(Source, DtlOpts) -> - DtlOpts1 = lists:keyreplace(doc_root, 1, DtlOpts, - {doc_root, filename:dirname(Source)}), - Set = referenced_dtls1([Source], DtlOpts1, - sets:add_element(Source, sets:new())), - sets:to_list(sets:del_element(Source, Set)). - -referenced_dtls1(Step, DtlOpts, Seen) -> - ExtMatch = re:replace(option(source_ext, DtlOpts), "\.", "\\\\\\\\.", - [{return, list}]), - - ShOpts = [{use_stdout, false}, return_on_error], - AllRefs = - lists:append( - [begin - Cmd = lists:flatten(["grep -o [^\\\"]*\\", - ExtMatch, "[^\\\"]* ", F]), - case rebar_utils:sh(Cmd, ShOpts) of - {ok, Res} -> - string:tokens(Res, "\n"); - {error, _} -> - "" - end - end || F <- Step]), - DocRoot = option(doc_root, DtlOpts), - WithPaths = [ filename:join([DocRoot, F]) || F <- AllRefs ], - ?DEBUG("All deps: ~p\n", [WithPaths]), - Existing = [F || F <- WithPaths, filelib:is_regular(F)], - New = sets:subtract(sets:from_list(Existing), Seen), - case sets:size(New) of - 0 -> Seen; - _ -> referenced_dtls1(sets:to_list(New), DtlOpts, - sets:union(New, Seen)) - end. diff --git a/src/rebar_prv_install_deps.erl b/src/rebar_prv_install_deps.erl index 17346f4..5aa34c3 100644 --- a/src/rebar_prv_install_deps.erl +++ b/src/rebar_prv_install_deps.erl @@ -626,7 +626,7 @@ not_needs_compile(App) -> get_package(Dep, State) -> case rebar_state:registry(State) of {ok, T} -> - HighestDepVsn = rebar_packages:find_highest_matching(Dep, "0", T), + {ok, HighestDepVsn} = rebar_packages:find_highest_matching(Dep, "0", T), {Dep, HighestDepVsn}; error -> throw(?PRV_ERROR({load_registry_fail, Dep})) diff --git a/src/rebar_prv_update.erl b/src/rebar_prv_update.erl index d0ff889..973b275 100644 --- a/src/rebar_prv_update.erl +++ b/src/rebar_prv_update.erl @@ -52,7 +52,8 @@ do(State) -> write_registry(Dict, Graph, State), ok catch - _E:_C -> + _E:C -> + ?DEBUG("Error creating package index: ~p ~p", [C, erlang:get_stacktrace()]), throw(?PRV_ERROR(package_index_write)) end, @@ -94,9 +95,13 @@ update_graph(Pkg, PkgVsn, Deps, HexRegistry, Graph) -> lists:foldl(fun([Dep, DepVsn, false, _AppName | _], DepsListAcc) -> case DepVsn of <<"~> ", Vsn/binary>> -> - HighestDepVsn = rebar_packages:find_highest_matching(Dep, Vsn, HexRegistry), - digraph:add_edge(Graph, {Pkg, PkgVsn}, {Dep, HighestDepVsn}), - [{Dep, DepVsn} | DepsListAcc]; + case rebar_packages:find_highest_matching(Dep, Vsn, HexRegistry) of + {ok, HighestDepVsn} -> + digraph:add_edge(Graph, {Pkg, PkgVsn}, {Dep, HighestDepVsn}), + [{Dep, DepVsn} | DepsListAcc]; + none -> + DepsListAcc + end; Vsn -> digraph:add_edge(Graph, {Pkg, PkgVsn}, {Dep, Vsn}), [{Dep, Vsn} | DepsListAcc] diff --git a/src/rebar_prv_upgrade.erl b/src/rebar_prv_upgrade.erl index d7d2921..05845e4 100644 --- a/src/rebar_prv_upgrade.erl +++ b/src/rebar_prv_upgrade.erl @@ -140,4 +140,3 @@ info_useless(Old, New) -> || Name <- Old, not lists:member(Name, New)], ok. - diff --git a/src/rebar_state.erl b/src/rebar_state.erl index ddac9d2..7a6e60d 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -69,25 +69,28 @@ -spec new() -> t(). new() -> - #state_t{dir = rebar_dir:get_cwd()}. + BaseState = base_state(), + BaseState#state_t{dir = rebar_dir:get_cwd()}. -spec new(list()) -> t(). new(Config) when is_list(Config) -> + BaseState = base_state(), Deps = proplists:get_value(deps, Config, []), Opts = dict:from_list([{{deps, default}, Deps} | Config]), - #state_t { dir = rebar_dir:get_cwd(), - default = Opts, - opts = Opts }. + BaseState#state_t { dir = rebar_dir:get_cwd(), + default = Opts, + opts = Opts }. -spec new(t() | atom(), list()) -> t(). new(Profile, Config) when is_atom(Profile) , is_list(Config) -> + BaseState = base_state(), Deps = proplists:get_value(deps, Config, []), Opts = dict:from_list([{{deps, default}, Deps} | Config]), - #state_t { dir = rebar_dir:get_cwd(), - current_profiles = [Profile], - default = Opts, - opts = Opts }; + BaseState#state_t { dir = rebar_dir:get_cwd(), + current_profiles = [Profile], + default = Opts, + opts = Opts }; new(ParentState=#state_t{}, Config) -> %% Load terms from rebar.config, if it exists Dir = rebar_dir:get_cwd(), @@ -113,6 +116,15 @@ new(ParentState, Config, Dir) -> ,opts=NewOpts ,default=NewOpts}. +base_state() -> + case application:get_env(rebar, resources) of + undefined -> + Resources = []; + {ok, Resources} -> + Resources + end, + #state_t{resources=Resources}. + get(State, Key) -> {ok, Value} = dict:find(Key, State#state_t.opts), Value. @@ -229,16 +241,28 @@ apply_profiles(State, Profile) when not is_list(Profile) -> apply_profiles(State, [Profile]); apply_profiles(State, [default]) -> State; -apply_profiles(State=#state_t{opts=Opts, current_profiles=CurrentProfiles}, Profiles) -> +apply_profiles(State=#state_t{default = Defaults, current_profiles=CurrentProfiles}, Profiles) -> + AppliedProfiles = deduplicate(CurrentProfiles ++ Profiles), ConfigProfiles = rebar_state:get(State, profiles, []), - {Profiles1, NewOpts} = - lists:foldl(fun(default, {ProfilesAcc, OptsAcc}) -> - {ProfilesAcc, OptsAcc}; - (Profile, {ProfilesAcc, OptsAcc}) -> + NewOpts = + lists:foldl(fun(default, OptsAcc) -> + OptsAcc; + (Profile, OptsAcc) -> ProfileOpts = dict:from_list(proplists:get_value(Profile, ConfigProfiles, [])), - {[Profile]++ProfilesAcc, merge_opts(Profile, ProfileOpts, OptsAcc)} - end, {[], Opts}, Profiles), - State#state_t{current_profiles=CurrentProfiles++Profiles1, opts=NewOpts}. + merge_opts(Profile, ProfileOpts, OptsAcc) + end, Defaults, AppliedProfiles), + State#state_t{current_profiles = AppliedProfiles, opts=NewOpts}. + +deduplicate(Profiles) -> + do_deduplicate(lists:reverse(Profiles), []). + +do_deduplicate([], Acc) -> + Acc; +do_deduplicate([Head | Rest], Acc) -> + case lists:member(Head, Acc) of + true -> do_deduplicate(Rest, Acc); + false -> do_deduplicate(Rest, [Head | Acc]) + end. merge_opts(Profile, NewOpts, OldOpts) -> Opts = merge_opts(NewOpts, OldOpts), diff --git a/src/rebar_templater.erl b/src/rebar_templater.erl index 746e440..1bd4e09 100644 --- a/src/rebar_templater.erl +++ b/src/rebar_templater.erl @@ -29,14 +29,10 @@ -export([new/4, list_templates/1]). -%% API for other utilities that need templating functionality --export([resolve_variables/2, - render/2]). -include("rebar.hrl"). -define(TEMPLATE_RE, "^[^._].*\\.template\$"). --define(ERLYDTL_COMPILE_OPTS, [report_warnings, return_errors, {auto_escape, false}, {out_dir, false}]). %% =================================================================== %% Public API @@ -45,7 +41,7 @@ %% Apply a template new(Template, Vars, Force, State) -> {AvailTemplates, Files} = find_templates(State), - ?DEBUG("Looking for ~p~n", [Template]), + ?DEBUG("Looking for ~p", [Template]), case lists:keyfind(Template, 1, AvailTemplates) of false -> {not_found, Template}; TemplateTup -> create(TemplateTup, Files, Vars, Force, State) @@ -57,34 +53,6 @@ list_templates(State) -> [list_template(Files, Template, State) || Template <- AvailTemplates]. %% =================================================================== -%% Rendering API / legacy? -%% =================================================================== - -%% 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(Value0, Dict), - resolve_variables(Rest, dict:store(Key, Value, Dict)); -resolve_variables([{Key, {list, Dicts}} | Rest], Dict) when is_list(Dicts) -> - %% just un-tag it so erlydtl can use it - resolve_variables(Rest, dict:store(Key, Dicts, Dict)); -resolve_variables([_Pair | Rest], Dict) -> - resolve_variables(Rest, Dict). - -%% -%% Render a binary to a string, using erlydtl and the specified context -%% -render(Template, Context) when is_atom(Template) -> - Template:render(Context); -render(Template, Context) -> - Module = list_to_atom(Template++"_dtl"), - Module:render(Context). - - -%% =================================================================== %% Internal Functions %% =================================================================== @@ -191,7 +159,7 @@ create({Template, Type, File}, Files, UserVars, Force, State) -> %% Run template instructions one at a time. execute_template([], _, {Template,_,_}, _, _) -> - ?DEBUG("Template ~s applied~n", [Template]), + ?DEBUG("Template ~s applied", [Template]), ok; %% We can't execute the description execute_template([{description, _} | Terms], Files, Template, Vars, Force) -> @@ -201,13 +169,13 @@ execute_template([{variables, _} | Terms], Files, Template, Vars, Force) -> execute_template(Terms, Files, Template, Vars, Force); %% Create a directory execute_template([{dir, Path} | Terms], Files, Template, Vars, Force) -> - ?DEBUG("Creating directory ~p~n", [Path]), + ?DEBUG("Creating directory ~p", [Path]), case ec_file:mkdir_p(expand_path(Path, Vars)) of ok -> ok; {error, Reason} -> ?ABORT("Failed while processing template instruction " - "{dir, ~p}: ~p~n", [Path, Reason]) + "{dir, ~p}: ~p", [Path, Reason]) end, execute_template(Terms, Files, Template, Vars, Force); %% Change permissions on a file @@ -218,29 +186,28 @@ execute_template([{chmod, File, Perm} | Terms], Files, Template, Vars, Force) -> execute_template(Terms, Files, Template, Vars, Force); {error, Reason} -> ?ABORT("Failed while processing template instruction " - "{chmod, ~.8#, ~p}: ~p~n", [Perm, File, Reason]) + "{chmod, ~.8#, ~p}: ~p", [Perm, File, Reason]) end; %% Create a raw untemplated file execute_template([{file, From, To} | Terms], Files, {Template, Type, Cwd}, Vars, Force) -> - ?DEBUG("Creating file ~p~n", [To]), + ?DEBUG("Creating file ~p", [To]), Data = load_file(Files, Type, filename:join(Cwd, From)), Out = expand_path(To,Vars), case write_file(Out, Data, Force) of ok -> ok; - {error, exists} -> ?INFO("File ~p already exists.~n", [Out]) + {error, exists} -> ?INFO("File ~p already exists.", [Out]) end, execute_template(Terms, Files, {Template, Type, Cwd}, Vars, Force); %% Operate on a django template execute_template([{template, From, To} | Terms], Files, {Template, Type, Cwd}, Vars, Force) -> - ?DEBUG("Executing template file ~p~n", [From]), + ?DEBUG("Executing template file ~p", [From]), Out = expand_path(To, Vars), Tpl = load_file(Files, Type, filename:join(Cwd, From)), - TplName = make_template_name("rebar_template", Out), - {ok, Mod} = erlydtl:compile_template(Tpl, TplName, ?ERLYDTL_COMPILE_OPTS), - {ok, Output} = Mod:render(Vars), - case write_file(Out, Output, Force) of - ok -> ok; - {error, exists} -> ?INFO("File ~p already exists~n", [Out]) + case write_file(Out, render(Tpl, Vars), Force) of + ok -> + ok; + {error, exists} -> + ?INFO("File ~p already exists", [Out]) end, execute_template(Terms, Files, {Template, Type, Cwd}, Vars, Force); %% Unknown @@ -342,11 +309,11 @@ prioritize_templates([{Name, Type, File} | Rest], Valid) -> prioritize_templates(Rest, [{Name, Type, File} | Valid]); {_, escript, _} -> ?DEBUG("Skipping template ~p, due to presence of a built-in " - "template with the same name~n", [Name]), + "template with the same name", [Name]), prioritize_templates(Rest, Valid); {_, file, _} -> ?DEBUG("Skipping template ~p, due to presence of a custom " - "template at ~s~n", [Name, File]), + "template at ~s", [Name, File]), prioritize_templates(Rest, Valid) end. @@ -409,12 +376,8 @@ write_file(Output, Data, Force) -> {error, exists} end. --spec make_template_name(string(), term()) -> module(). -make_template_name(Base, Value) -> - %% Seed so we get different values each time - random:seed(os:timestamp()), - Hash = erlang:phash2(Value), - Ran = random:uniform(10000000), - erlang:list_to_atom(Base ++ "_" ++ - erlang:integer_to_list(Hash) ++ - "_" ++ erlang:integer_to_list(Ran)). +%% +%% Render a binary to a string, using mustache and the specified context +%% +render(Bin, Context) -> + rebar_mustache:render(ec_cnv:to_binary(Bin), dict:from_list(Context)). diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 49f7ad7..b7a9583 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -42,7 +42,7 @@ erl_to_mod/1, beams/1, find_executable/1, - vcs_vsn/2, + vcs_vsn/3, deprecated/3, deprecated/4, erl_opts/1, @@ -471,8 +471,8 @@ escript_foldl(Fun, Acc, File) -> Error end. -vcs_vsn(Vcs, Dir) -> - case vcs_vsn_cmd(Vcs, Dir) of +vcs_vsn(Vcs, Dir, Resources) -> + case vcs_vsn_cmd(Vcs, Dir, Resources) of {plain, VsnString} -> VsnString; {cmd, CmdString} -> @@ -484,23 +484,48 @@ vcs_vsn(Vcs, Dir) -> end. %% Temp work around for repos like relx that use "semver" -vcs_vsn_cmd(VCS, Dir) when VCS =:= semver ; VCS =:= "semver" -> - rebar_git_resource:make_vsn(Dir); -vcs_vsn_cmd(VCS, Dir) when VCS =:= git ; VCS =:= "git" -> - rebar_git_resource:make_vsn(Dir); -vcs_vsn_cmd(VCS, Dir) when VCS =:= pkg ; VCS =:= "pkg" -> - rebar_pkg_resource:make_vsn(Dir); -vcs_vsn_cmd({cmd, _Cmd}=Custom, _) -> +vcs_vsn_cmd(VCS, Dir, Resources) when VCS =:= semver ; VCS =:= "semver" -> + vcs_vsn_cmd(git, Dir, Resources); +vcs_vsn_cmd({cmd, _Cmd}=Custom, _, _) -> Custom; -vcs_vsn_cmd(Version, _) when is_list(Version) -> - {plain, Version}; -vcs_vsn_cmd(_, _) -> +vcs_vsn_cmd(VCS, Dir, Resources) when is_atom(VCS) -> + case find_resource_module(VCS, Resources) of + {ok, Module} -> + Module:make_vsn(Dir); + {error, _} -> + unknown + end; +vcs_vsn_cmd(VCS, Dir, Resources) when is_list(VCS) -> + try list_to_existing_atom(VCS) of + AVCS -> + case vcs_vsn_cmd(AVCS, Dir, Resources) of + unknown -> {plain, VCS}; + Other -> Other + end + catch + error:badarg -> + {plain, VCS} + end; +vcs_vsn_cmd(_, _, _) -> unknown. vcs_vsn_invoke(Cmd, Dir) -> {ok, VsnString} = rebar_utils:sh(Cmd, [{cd, Dir}, {use_stdout, false}]), string:strip(VsnString, right, $\n). +find_resource_module(Type, Resources) -> + case lists:keyfind(Type, 1, Resources) of + false -> + case code:which(Type) of + non_existing -> + {error, unknown}; + _ -> + {ok, Type} + end; + {Type, Module} -> + {ok, Module} + end. + %% %% Filter a list of erl_opts platform_define options such that only %% those which match the provided architecture regex are returned. |