2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2009-12-31 18:42:53 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
2009-11-25 22:23:42 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% 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_rel_utils).
|
|
|
|
|
2011-02-17 18:12:32 +00:00
|
|
|
-export([is_rel_dir/0,
|
2011-02-16 17:46:40 +00:00
|
|
|
is_rel_dir/1,
|
|
|
|
get_reltool_release_info/1,
|
|
|
|
get_rel_release_info/1,
|
|
|
|
get_rel_release_info/2,
|
2011-03-03 18:39:44 +00:00
|
|
|
get_rel_apps/1,
|
|
|
|
get_rel_apps/2,
|
|
|
|
get_previous_release_path/0,
|
2011-06-30 21:50:56 +00:00
|
|
|
get_rel_file_path/2,
|
|
|
|
load_config/1,
|
|
|
|
get_sys_tuple/1,
|
|
|
|
get_target_dir/1,
|
2011-10-11 16:44:34 +00:00
|
|
|
get_root_dir/1,
|
2011-06-30 21:50:56 +00:00
|
|
|
get_target_parent_dir/1]).
|
2011-02-16 17:46:40 +00:00
|
|
|
|
|
|
|
-include("rebar.hrl").
|
2009-11-25 22:23:42 +00:00
|
|
|
|
|
|
|
is_rel_dir() ->
|
2010-06-19 16:53:54 +00:00
|
|
|
is_rel_dir(rebar_utils:get_cwd()).
|
2009-11-25 22:23:42 +00:00
|
|
|
|
|
|
|
is_rel_dir(Dir) ->
|
2009-12-02 12:15:35 +00:00
|
|
|
Fname = filename:join([Dir, "reltool.config"]),
|
2011-01-07 15:32:36 +00:00
|
|
|
case filelib:is_regular(Fname) of
|
2009-12-02 12:15:35 +00:00
|
|
|
true ->
|
|
|
|
{true, Fname};
|
|
|
|
false ->
|
2009-11-25 22:23:42 +00:00
|
|
|
false
|
|
|
|
end.
|
2011-02-16 17:46:40 +00:00
|
|
|
|
|
|
|
%% Get release name and version from a reltool.config
|
2011-09-20 16:44:41 +00:00
|
|
|
get_reltool_release_info([{sys, Config}| _]) ->
|
2011-06-30 21:50:56 +00:00
|
|
|
{rel, Name, Ver, _} = proplists:lookup(rel, Config),
|
2011-09-20 16:44:41 +00:00
|
|
|
{Name, Ver};
|
|
|
|
get_reltool_release_info(ReltoolFile) when is_list(ReltoolFile) ->
|
|
|
|
case file:consult(ReltoolFile) of
|
|
|
|
{ok, ReltoolConfig} ->
|
|
|
|
get_reltool_release_info(ReltoolConfig);
|
|
|
|
_ ->
|
|
|
|
?ABORT("Failed to parse ~s~n", [ReltoolFile])
|
|
|
|
end.
|
2011-02-16 17:46:40 +00:00
|
|
|
|
|
|
|
%% 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) ->
|
2011-03-03 18:39:44 +00:00
|
|
|
RelPath = get_rel_file_path(Name, Path),
|
|
|
|
get_rel_release_info(RelPath).
|
|
|
|
|
|
|
|
%% Get list of apps included in a release from a rel file
|
|
|
|
get_rel_apps(RelFile) ->
|
|
|
|
case file:consult(RelFile) of
|
|
|
|
{ok, [{release, _, _, Apps}]} ->
|
2011-05-24 01:17:03 +00:00
|
|
|
make_proplist(Apps, []);
|
2011-03-03 18:39:44 +00:00
|
|
|
_ ->
|
|
|
|
?ABORT("Failed to parse ~s~n", [RelFile])
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% Get list of apps included in a release from a name and a path
|
|
|
|
get_rel_apps(Name, Path) ->
|
|
|
|
RelPath = get_rel_file_path(Name, Path),
|
|
|
|
get_rel_apps(RelPath).
|
|
|
|
|
|
|
|
%% Get rel file path from name and path
|
|
|
|
get_rel_file_path(Name, Path) ->
|
2011-02-16 17:46:40 +00:00
|
|
|
[RelFile] = filelib:wildcard(filename:join([Path, "releases", "*",
|
|
|
|
Name ++ ".rel"])),
|
|
|
|
[BinDir|_] = re:replace(RelFile, Name ++ "\\.rel", ""),
|
2011-03-03 18:39:44 +00:00
|
|
|
filename:join([binary_to_list(BinDir), Name ++ ".rel"]).
|
2011-02-16 17:46:40 +00:00
|
|
|
|
|
|
|
%% 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.
|
2011-05-24 01:17:03 +00:00
|
|
|
|
2011-06-30 21:50:56 +00:00
|
|
|
%%
|
|
|
|
%% Load terms from reltool.config
|
|
|
|
%%
|
|
|
|
load_config(ReltoolFile) ->
|
|
|
|
case file:consult(ReltoolFile) of
|
|
|
|
{ok, Terms} ->
|
2011-11-20 23:01:53 +00:00
|
|
|
expand_version(Terms, filename:dirname(ReltoolFile));
|
2011-06-30 21:50:56 +00:00
|
|
|
Other ->
|
|
|
|
?ABORT("Failed to load expected config from ~s: ~p\n",
|
|
|
|
[ReltoolFile, Other])
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Look for the {sys, [...]} tuple in the reltool.config file.
|
|
|
|
%% Without this present, we can't run reltool.
|
|
|
|
%%
|
|
|
|
get_sys_tuple(ReltoolConfig) ->
|
|
|
|
case lists:keyfind(sys, 1, ReltoolConfig) of
|
|
|
|
{sys, _} = SysTuple ->
|
|
|
|
SysTuple;
|
|
|
|
false ->
|
|
|
|
?ABORT("Failed to find {sys, [...]} tuple in reltool.config.", [])
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Look for {target_dir, TargetDir} in the reltool config file; if none is
|
|
|
|
%% found, use the name of the release as the default target directory.
|
|
|
|
%%
|
|
|
|
get_target_dir(ReltoolConfig) ->
|
|
|
|
case rebar_config:get_global(target_dir, undefined) of
|
|
|
|
undefined ->
|
|
|
|
case lists:keyfind(target_dir, 1, ReltoolConfig) of
|
|
|
|
{target_dir, TargetDir} ->
|
|
|
|
filename:absname(TargetDir);
|
|
|
|
false ->
|
|
|
|
{sys, SysInfo} = get_sys_tuple(ReltoolConfig),
|
|
|
|
case lists:keyfind(rel, 1, SysInfo) of
|
|
|
|
{rel, Name, _Vsn, _Apps} ->
|
|
|
|
filename:absname(Name);
|
|
|
|
false ->
|
|
|
|
filename:absname("target")
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
TargetDir ->
|
|
|
|
filename:absname(TargetDir)
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_target_parent_dir(ReltoolConfig) ->
|
2011-11-26 10:04:21 +00:00
|
|
|
TargetDir = get_target_dir(ReltoolConfig),
|
|
|
|
case lists:reverse(tl(lists:reverse(filename:split(TargetDir)))) of
|
2011-06-30 21:50:56 +00:00
|
|
|
[] -> ".";
|
|
|
|
Components -> filename:join(Components)
|
|
|
|
end.
|
|
|
|
|
2011-11-26 11:01:13 +00:00
|
|
|
%%
|
|
|
|
%% Look for root_dir in sys tuple and command line; fall back to
|
|
|
|
%% code:root_dir().
|
|
|
|
%%
|
|
|
|
get_root_dir(ReltoolConfig) ->
|
|
|
|
{sys, SysInfo} = get_sys_tuple(ReltoolConfig),
|
|
|
|
SysRootDirTuple = lists:keyfind(root_dir, 1, SysInfo),
|
|
|
|
CmdRootDir = rebar_config:get_global(root_dir, undefined),
|
|
|
|
case {SysRootDirTuple, CmdRootDir} of
|
|
|
|
%% root_dir in sys typle and no root_dir on cmd-line
|
|
|
|
{{root_dir, SysRootDir}, undefined} ->
|
|
|
|
SysRootDir;
|
|
|
|
%% root_dir in sys typle and also root_dir on cmd-line
|
|
|
|
{{root_dir, SysRootDir}, CmdRootDir} when CmdRootDir =/= undefined ->
|
|
|
|
case string:equal(SysRootDir, CmdRootDir) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
?WARN("overriding reltool.config root_dir with "
|
|
|
|
"different command line root_dir~n", [])
|
|
|
|
end,
|
|
|
|
CmdRootDir;
|
|
|
|
%% no root_dir in sys typle and no root_dir on cmd-line
|
|
|
|
{false, undefined} ->
|
|
|
|
code:root_dir();
|
|
|
|
%% no root_dir in sys tuple but root_dir on cmd-line
|
|
|
|
{false, CmdRootDir} when CmdRootDir =/= undefined ->
|
|
|
|
CmdRootDir
|
|
|
|
end.
|
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
|
|
|
make_proplist([{_,_}=H|T], Acc) ->
|
2012-01-13 18:56:10 +00:00
|
|
|
make_proplist(T, [H|Acc]);
|
2011-05-24 01:17:03 +00:00
|
|
|
make_proplist([H|T], Acc) ->
|
2012-01-13 18:56:10 +00:00
|
|
|
App = element(1, H),
|
|
|
|
Ver = element(2, H),
|
|
|
|
make_proplist(T, [{App,Ver}|Acc]);
|
2011-05-24 01:17:03 +00:00
|
|
|
make_proplist([], Acc) ->
|
2012-01-13 18:56:10 +00:00
|
|
|
Acc.
|
2011-11-20 23:01:53 +00:00
|
|
|
|
|
|
|
expand_version(ReltoolConfig, Dir) ->
|
|
|
|
case lists:keyfind(sys, 1, ReltoolConfig) of
|
|
|
|
{sys, Sys} ->
|
|
|
|
ExpandedSys = {sys, [expand_rel_version(Term, Dir) || Term <- Sys]},
|
|
|
|
lists:keyreplace(sys, 1, ReltoolConfig, ExpandedSys);
|
|
|
|
_ ->
|
|
|
|
ReltoolConfig
|
|
|
|
end.
|
|
|
|
|
|
|
|
expand_rel_version({rel, Name, Version, Apps}, Dir) ->
|
|
|
|
{rel, Name, rebar_utils:vcs_vsn(Version, Dir), Apps};
|
|
|
|
expand_rel_version(Other, _Dir) ->
|
|
|
|
Other.
|