Pārlūkot izejas kodu

use git or hg configs if exist for default user and email in templates

pull/221/head
Tristan Sloughter pirms 10 gadiem
vecāks
revīzija
1b851267f8
2 mainītis faili ar 55 papildinājumiem un 4 dzēšanām
  1. +29
    -2
      src/rebar_templater.erl
  2. +26
    -2
      test/rebar_new_SUITE.erl

+ 29
- 2
src/rebar_templater.erl Parādīt failu

@ -132,14 +132,41 @@ override_vars([{Var, Default, Doc} | Rest], General) ->
%% Default variables, generated dynamically.
default_variables() ->
{DefaultAuthor, DefaultEmail} = default_author_and_email(),
{{Y,M,D},{H,Min,S}} = calendar:universal_time(),
[{date, lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w",[Y,M,D]))},
{datetime, lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0w+00:00",[Y,M,D,H,Min,S]))},
{author_name, "Anonymous"},
{author_email, "anonymous@example.org"},
{author_name, DefaultAuthor},
{author_email, DefaultEmail},
{copyright_year, integer_to_list(Y)},
{apps_dir, "apps", "Directory where applications will be created if needed"}].
default_author_and_email() ->
%% See if we can get a git user and email to use as defaults
case rebar_utils:sh("git config --global user.name", []) of
{ok, Name} ->
case rebar_utils:sh("git config --global user.email", []) of
{ok, Email} ->
{string:strip(Name, both, $\n), string:strip(Email, both, $\n)};
{error, _} ->
%% Use neither if one doesn't exist
{"Anonymous", "anonymous@example.org"}
end;
{error, _} ->
%% Ok, try mecurial
case rebar_utils:sh("hg showconfig ui.username", []) of
{ok, NameEmail} ->
case re:run(NameEmail, [{capture, [1,2], list}]) of
{match, [Name, Email]} ->
{Name, Email};
_ ->
{"Anonymous", "anonymous@example.org"}
end;
{error, _} ->
{"Anonymous", "anonymous@example.org"}
end
end.
%% Load variable definitions from the 'Globals' file in the home template
%% directory
global_variables(State) ->

+ 26
- 2
test/rebar_new_SUITE.erl Parādīt failu

@ -6,7 +6,7 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
all() -> [app].
all() -> [app, app_with_fallbacks].
init_per_testcase(Case, Config0) ->
@ -32,6 +32,10 @@ mock_empty_escript_templates() ->
meck:expect(rebar_utils, escript_foldl, fun(_,_,_) -> {ok, []} end).
app(Config) ->
meck:expect(rebar_utils, sh, fun("git config --global user.name", _) -> {ok, "gitname"};
("git config --global user.email", _) -> {ok, "git@email.com"}
end),
Name = ?config(name, Config),
rebar_test_utils:run_and_check(
Config, [],
@ -40,7 +44,7 @@ app(Config) ->
),
validate_files(
Config, Name,
[{"LICENSE", ["some_name", "anonymous@example.org"]},
[{"LICENSE", ["some_name", "git@email.com"]},
{"README.md", [Name]},
{".gitignore", []},
{"rebar.config", []},
@ -49,6 +53,26 @@ app(Config) ->
{filename:join(["src", Name++"_app.erl"]), [Name]}
]).
app_with_fallbacks(Config) ->
meck:expect(rebar_utils, sh, fun(_, _) -> {error, fallback} end),
Name = ?config(name, Config),
rebar_test_utils:run_and_check(
Config, [],
["new", "test_app", Name],
{ok, []}
),
validate_files(
Config, Name,
[{"LICENSE", ["Anonymous", "anonymous@example.org"]},
{"README.md", [Name]},
{".gitignore", []},
{"rebar.config", []},
{filename:join(["src", Name++".app.src"]), [Name]},
{filename:join(["src", Name++"_sup.erl"]), [Name]},
{filename:join(["src", Name++"_app.erl"]), [Name]}
]).
validate_files(_Config, Name, Checks) ->
[begin
Path = filename:join([Name, File]),

Notiek ielāde…
Atcelt
Saglabāt