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) -> 'generate-appups'(_Config, ReltoolFile) ->
%% Get the old release path %% 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 %% 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]), NewVerPath = filename:join([".", Name]),
{NewName, NewVer} = rebar_utils:get_rel_release_info(Name, NewVerPath), {NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NewVerPath),
{OldName, OldVer} = rebar_utils:get_rel_release_info(Name, OldVerPath), {OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath),
%% Run some simple checks %% Run some simple checks
true = rebar_utils:prop_check(NewVer =/= OldVer, true = rebar_utils:prop_check(NewVer =/= OldVer,
@ -86,8 +86,10 @@
%% =================================================================== %% ===================================================================
get_upgraded_apps(OldAppFiles, NewAppFiles) -> get_upgraded_apps(OldAppFiles, NewAppFiles) ->
OldAppsVer = [get_app_version(AppFile) || AppFile <- OldAppFiles], OldAppsVer = [{rebar_app_utils:app_name(AppFile),
NewAppsVer = [get_app_version(AppFile) || AppFile <- NewAppFiles], 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), UpgradedApps = lists:subtract(NewAppsVer, OldAppsVer),
lists:map( lists:map(
fun({App, NewVer}) -> fun({App, NewVer}) ->
@ -96,14 +98,6 @@ get_upgraded_apps(OldAppFiles, NewAppFiles) ->
end, end,
UpgradedApps). 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) -> file_to_name(File) ->
filename:rootname(filename:basename(File)). filename:rootname(filename:basename(File)).

View file

@ -26,7 +26,14 @@
%% ------------------------------------------------------------------- %% -------------------------------------------------------------------
-module(rebar_rel_utils). -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() ->
is_rel_dir(rebar_utils:get_cwd()). is_rel_dir(rebar_utils:get_cwd()).
@ -39,3 +46,42 @@ is_rel_dir(Dir) ->
false -> false ->
false false
end. 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 %% 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 %% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal %% of this software and associated documentation files (the "Software"), to deal
@ -32,91 +32,69 @@
-export(['generate-upgrade'/2]). -export(['generate-upgrade'/2]).
%% public api %% ====================================================================
%% Public API
%% ====================================================================
'generate-upgrade'(_Config, ReltoolFile) -> 'generate-upgrade'(_Config, ReltoolFile) ->
case rebar_config:get_global(previous_release, false) of %% Get the old release path
false -> OldVerPath = rebar_rel_utils:get_previous_release_path(),
?ABORT("previous_release=PATH is required to "
"create upgrade package~n", []);
OldVerPath ->
%% Run checks to make sure that building a package is possible
{NewName, NewVer} = run_checks(OldVerPath, ReltoolFile),
NameVer = NewName ++ "_" ++ NewVer,
%% Save the code path prior to doing anything %% Run checks to make sure that building a package is possible
OrigPath = code:get_path(), {NewName, NewVer} = run_checks(OldVerPath, ReltoolFile),
NameVer = NewName ++ "_" ++ NewVer,
%% Prepare the environment for building the package %% Save the code path prior to doing anything
ok = setup(OldVerPath, NewName, NewVer, NameVer), OrigPath = code:get_path(),
%% Build the package %% Prepare the environment for building the package
run_systools(NameVer, NewName), ok = setup(OldVerPath, NewName, NewVer, NameVer),
%% Boot file changes %% Build the package
{ok, _} = boot_files(NewVer, NewName), run_systools(NameVer, NewName),
%% Extract upgrade and tar it back up with changes %% Boot file changes
make_tar(NameVer), {ok, _} = boot_files(NewVer, NewName),
%% Clean up files that systools created %% Extract upgrade and tar it back up with changes
ok = cleanup(NameVer, NewName, NewVer), make_tar(NameVer),
%% Restore original path %% Clean up files that systools created
true = code:set_path(OrigPath), ok = cleanup(NameVer, NewName, NewVer),
ok %% Restore original path
end. true = code:set_path(OrigPath),
%% internal api ok.
%% ===================================================================
%% Internal functions
%% ==================================================================
run_checks(OldVerPath, ReltoolFile) -> 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]), "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]), 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]), "Release directory doesn't exist (~p)~n", [NamePath]),
{NewName, NewVer} = get_release_version(Name, NamePath), {NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NamePath),
{OldName, OldVer} = get_release_version(Name, OldVerPath), {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", []), "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", []), "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", []), "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", []), "Reltool and .rel versions do not match~n", []),
{NewName, NewVer}. {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) -> setup(OldVerPath, NewName, NewVer, NameVer) ->
NewRelPath = filename:join([".", NewName]), NewRelPath = filename:join([".", NewName]),
Src = filename:join([NewRelPath, "releases", Src = filename:join([NewRelPath, "releases",

View file

@ -38,9 +38,6 @@
abort/2, abort/2,
escript_foldl/3, escript_foldl/3,
find_executable/1, find_executable/1,
get_reltool_release_info/1,
get_rel_release_info/2,
get_previous_release_path/0,
prop_check/3]). prop_check/3]).
-include("rebar.hrl"). -include("rebar.hrl").
@ -155,38 +152,6 @@ find_executable(Name) ->
"\"" ++ filename:nativename(Path) ++ "\"" "\"" ++ filename:nativename(Path) ++ "\""
end. 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 %% Helper function for checking values and aborting when needed
prop_check(true, _, _) -> true; prop_check(true, _, _) -> true;
prop_check(false, Msg, Args) -> ?ABORT(Msg, Args). prop_check(false, Msg, Args) -> ?ABORT(Msg, Args).