Clean up rebar_appups and rebar_upgrade

This commit is contained in:
joewilliams 2011-02-16 09:46:40 -08:00 committed by Tuncer Ayaz
parent 377c9e86d5
commit 4e8dcfbfad
4 changed files with 92 additions and 109 deletions

View file

@ -40,13 +40,13 @@
'generate-appups'(_Config, ReltoolFile) ->
%% Get the old release path
OldVerPath = rebar_utils:get_previous_release_path(),
OldVerPath = rebar_rel_utils:get_previous_release_path(),
%% Get the new and old release name and versions
{Name, _Ver} = rebar_utils:get_reltool_release_info(ReltoolFile),
{Name, _Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolFile),
NewVerPath = filename:join([".", Name]),
{NewName, NewVer} = rebar_utils:get_rel_release_info(Name, NewVerPath),
{OldName, OldVer} = rebar_utils:get_rel_release_info(Name, OldVerPath),
{NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NewVerPath),
{OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath),
%% Run some simple checks
true = rebar_utils:prop_check(NewVer =/= OldVer,
@ -86,8 +86,10 @@
%% ===================================================================
get_upgraded_apps(OldAppFiles, NewAppFiles) ->
OldAppsVer = [get_app_version(AppFile) || AppFile <- OldAppFiles],
NewAppsVer = [get_app_version(AppFile) || AppFile <- NewAppFiles],
OldAppsVer = [{rebar_app_utils:app_name(AppFile),
rebar_app_utils:app_vsn(AppFile)} || AppFile <- OldAppFiles],
NewAppsVer = [{rebar_app_utils:app_name(AppFile),
rebar_app_utils:app_vsn(AppFile)} || AppFile <- NewAppFiles],
UpgradedApps = lists:subtract(NewAppsVer, OldAppsVer),
lists:map(
fun({App, NewVer}) ->
@ -96,14 +98,6 @@ get_upgraded_apps(OldAppFiles, NewAppFiles) ->
end,
UpgradedApps).
get_app_version(File) ->
case file:consult(File) of
{ok,[{application, Name,[_,{vsn,Ver}|_]}]} ->
{Name, Ver};
_ ->
?ABORT("Failed to parse ~s~n", [File])
end.
file_to_name(File) ->
filename:rootname(filename:basename(File)).

View file

@ -26,7 +26,14 @@
%% -------------------------------------------------------------------
-module(rebar_rel_utils).
-export([is_rel_dir/0, is_rel_dir/1]).
-export([is_rel_dir/0,
is_rel_dir/1,
get_reltool_release_info/1,
get_rel_release_info/1,
get_rel_release_info/2,
get_previous_release_path/0]).
-include("rebar.hrl").
is_rel_dir() ->
is_rel_dir(rebar_utils:get_cwd()).
@ -39,3 +46,42 @@ is_rel_dir(Dir) ->
false ->
false
end.
%% Get release name and version from a reltool.config
get_reltool_release_info(ReltoolFile) ->
%% expect sys to be the first proplist in reltool.config
case file:consult(ReltoolFile) of
{ok, [{sys, Config}| _]} ->
%% expect the first rel in the proplist to be the one you want
{rel, Name, Ver, _} = proplists:lookup(rel, Config),
{Name, Ver};
_ ->
?ABORT("Failed to parse ~s~n", [ReltoolFile])
end.
%% Get release name and version from a rel file
get_rel_release_info(RelFile) ->
case file:consult(RelFile) of
{ok, [{release, {Name, Ver}, _, _}]} ->
{Name, Ver};
_ ->
?ABORT("Failed to parse ~s~n", [RelFile])
end.
%% Get release name and version from a name and a path
get_rel_release_info(Name, Path) ->
[RelFile] = filelib:wildcard(filename:join([Path, "releases", "*",
Name ++ ".rel"])),
[BinDir|_] = re:replace(RelFile, Name ++ "\\.rel", ""),
get_rel_release_info(filename:join([binary_to_list(BinDir),
Name ++ ".rel"])).
%% Get the previous release path from a global variable
get_previous_release_path() ->
case rebar_config:get_global(previous_release, false) of
false ->
?ABORT("previous_release=PATH is required to "
"create upgrade package~n", []);
OldVerPath ->
OldVerPath
end.

View file

@ -4,7 +4,7 @@
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2011 Joe Williams <joe@joetify.com>
%% Copyright (c) 2011 Joe Williams (joe@joetify.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
@ -32,14 +32,14 @@
-export(['generate-upgrade'/2]).
%% public api
%% ====================================================================
%% Public API
%% ====================================================================
'generate-upgrade'(_Config, ReltoolFile) ->
case rebar_config:get_global(previous_release, false) of
false ->
?ABORT("previous_release=PATH is required to "
"create upgrade package~n", []);
OldVerPath ->
%% Get the old release path
OldVerPath = rebar_rel_utils:get_previous_release_path(),
%% Run checks to make sure that building a package is possible
{NewName, NewVer} = run_checks(OldVerPath, ReltoolFile),
NameVer = NewName ++ "_" ++ NewVer,
@ -65,58 +65,36 @@
%% Restore original path
true = code:set_path(OrigPath),
ok
end.
ok.
%% internal api
%% ===================================================================
%% Internal functions
%% ==================================================================
run_checks(OldVerPath, ReltoolFile) ->
true = prop_check(filelib:is_dir(OldVerPath),
true = rebar_utils:prop_check(filelib:is_dir(OldVerPath),
"Release directory doesn't exist (~p)~n", [OldVerPath]),
{Name, Ver} = get_release_name(ReltoolFile),
{Name, Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolFile),
NamePath = filename:join([".", Name]),
true = prop_check(filelib:is_dir(NamePath),
true = rebar_utils:prop_check(filelib:is_dir(NamePath),
"Release directory doesn't exist (~p)~n", [NamePath]),
{NewName, NewVer} = get_release_version(Name, NamePath),
{OldName, OldVer} = get_release_version(Name, OldVerPath),
{NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NamePath),
{OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath),
true = prop_check(NewName == OldName,
true = rebar_utils:prop_check(NewName == OldName,
"New and old .rel release names do not match~n", []),
true = prop_check(Name == NewName,
true = rebar_utils:prop_check(Name == NewName,
"Reltool and .rel release names do not match~n", []),
true = prop_check(NewVer =/= OldVer,
true = rebar_utils:prop_check(NewVer =/= OldVer,
"New and old .rel contain the same version~n", []),
true = prop_check(Ver == NewVer,
true = rebar_utils:prop_check(Ver == NewVer,
"Reltool and .rel versions do not match~n", []),
{NewName, NewVer}.
get_release_name(ReltoolFile) ->
%% expect sys to be the first proplist in reltool.config
case file:consult(ReltoolFile) of
{ok, [{sys, Config}| _]} ->
%% expect the first rel in the proplist to be the one you want
{rel, Name, Ver, _} = proplists:lookup(rel, Config),
{Name, Ver};
_ ->
?ABORT("Failed to parse ~s~n", [ReltoolFile])
end.
get_release_version(Name, Path) ->
[RelFile] = filelib:wildcard(filename:join([Path, "releases", "*",
Name ++ ".rel"])),
[BinDir|_] = re:replace(RelFile, Name ++ "\\.rel", ""),
{ok, [{release, {Name1, Ver}, _, _}]} =
file:consult(filename:join([binary_to_list(BinDir),
Name ++ ".rel"])),
{Name1, Ver}.
prop_check(true, _, _) -> true;
prop_check(false, Msg, Args) -> ?ABORT(Msg, Args).
setup(OldVerPath, NewName, NewVer, NameVer) ->
NewRelPath = filename:join([".", NewName]),
Src = filename:join([NewRelPath, "releases",

View file

@ -38,9 +38,6 @@
abort/2,
escript_foldl/3,
find_executable/1,
get_reltool_release_info/1,
get_rel_release_info/2,
get_previous_release_path/0,
prop_check/3]).
-include("rebar.hrl").
@ -155,38 +152,6 @@ find_executable(Name) ->
"\"" ++ filename:nativename(Path) ++ "\""
end.
%% Get release name and version from a reltool.config
get_reltool_release_info(ReltoolFile) ->
%% expect sys to be the first proplist in reltool.config
case file:consult(ReltoolFile) of
{ok, [{sys, Config}| _]} ->
%% expect the first rel in the proplist to be the one you want
{rel, Name, Ver, _} = proplists:lookup(rel, Config),
{Name, Ver};
_ ->
?ABORT("Failed to parse ~s~n", [ReltoolFile])
end.
%% Get release name and version from a rel file
get_rel_release_info(Name, Path) ->
[RelFile] = filelib:wildcard(filename:join([Path, "releases", "*",
Name ++ ".rel"])),
[BinDir|_] = re:replace(RelFile, Name ++ "\\.rel", ""),
{ok, [{release, {Name1, Ver}, _, _}]} =
file:consult(filename:join([binary_to_list(BinDir),
Name ++ ".rel"])),
{Name1, Ver}.
%% Get the previous release path from a global variable
get_previous_release_path() ->
case rebar_config:get_global(previous_release, false) of
false ->
?ABORT("previous_release=PATH is required to "
"create upgrade package~n", []);
OldVerPath ->
OldVerPath
end.
%% Helper function for checking values and aborting when needed
prop_check(true, _, _) -> true;
prop_check(false, Msg, Args) -> ?ABORT(Msg, Args).