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-12-02 12:15:35 +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_reltool).
|
|
|
|
|
|
|
|
-export([generate/2,
|
|
|
|
clean/2]).
|
|
|
|
|
|
|
|
-include("rebar.hrl").
|
2009-12-03 23:06:06 +00:00
|
|
|
-include_lib("reltool/src/reltool.hrl").
|
2009-12-15 00:30:51 +00:00
|
|
|
-include_lib("kernel/include/file.hrl").
|
2009-12-02 12:15:35 +00:00
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
|
|
|
generate(Config, ReltoolFile) ->
|
2010-06-22 20:13:39 +00:00
|
|
|
%% Make sure we have decent version of reltool available
|
|
|
|
check_vsn(),
|
|
|
|
|
2009-12-02 12:15:35 +00:00
|
|
|
%% Load the reltool configuration from the file
|
2009-12-03 23:06:06 +00:00
|
|
|
ReltoolConfig = load_config(ReltoolFile),
|
|
|
|
|
|
|
|
%% Spin up reltool server and load our config into it
|
2009-12-11 14:20:59 +00:00
|
|
|
{ok, Server} = reltool:start_server([sys_tuple(ReltoolConfig)]),
|
2009-12-03 23:06:06 +00:00
|
|
|
|
|
|
|
%% Do some validation of the reltool configuration; error messages out of
|
|
|
|
%% reltool are still pretty cryptic
|
2009-12-11 14:20:59 +00:00
|
|
|
validate_rel_apps(Server, sys_tuple(ReltoolConfig)),
|
2009-12-02 12:15:35 +00:00
|
|
|
|
2009-12-03 23:06:06 +00:00
|
|
|
%% Finally, run reltool
|
2010-06-15 00:16:26 +00:00
|
|
|
case catch(run_reltool(Server, Config, ReltoolConfig)) of
|
2009-12-02 12:15:35 +00:00
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, failed} ->
|
|
|
|
?FAIL;
|
|
|
|
Other2 ->
|
|
|
|
?ERROR("Unexpected error: ~p\n", [Other2]),
|
|
|
|
?FAIL
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
clean(_Config, ReltoolFile) ->
|
2009-12-03 23:06:06 +00:00
|
|
|
ReltoolConfig = load_config(ReltoolFile),
|
2010-06-15 00:16:26 +00:00
|
|
|
TargetDir = target_dir(ReltoolConfig),
|
2009-12-03 23:06:06 +00:00
|
|
|
rebar_file_utils:rm_rf(TargetDir),
|
|
|
|
rebar_file_utils:delete_each(["reltool.spec"]).
|
2009-12-02 12:15:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2010-06-22 20:13:39 +00:00
|
|
|
check_vsn() ->
|
|
|
|
case code:lib_dir(reltool) of
|
|
|
|
{error, bad_name} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Reltool support requires the reltool application "
|
|
|
|
"to be installed!", []);
|
2010-06-22 20:13:39 +00:00
|
|
|
Path ->
|
|
|
|
ReltoolVsn = filename:basename(Path),
|
|
|
|
case ReltoolVsn < "reltool-0.5.2" of
|
|
|
|
true ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Reltool support requires at least reltool-0.5.2; "
|
|
|
|
"this VM is using ~s\n", [ReltoolVsn]);
|
2010-06-22 20:13:39 +00:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
%%
|
|
|
|
%% Load terms from reltool.config
|
|
|
|
%%
|
|
|
|
load_config(ReltoolFile) ->
|
|
|
|
case file:consult(ReltoolFile) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
Terms;
|
|
|
|
Other ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Failed to load expected config from ~s: ~p\n",
|
|
|
|
[ReltoolFile, Other])
|
2010-06-15 00:16:26 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%
|
2011-01-28 15:08:27 +00:00
|
|
|
%% Look for the {sys, [...]} tuple in the reltool.config file.
|
|
|
|
%% Without this present, we can't run reltool.
|
2010-06-15 00:16:26 +00:00
|
|
|
%%
|
2009-12-11 14:20:59 +00:00
|
|
|
sys_tuple(ReltoolConfig) ->
|
2010-10-25 22:38:51 +00:00
|
|
|
case lists:keyfind(sys, 1, ReltoolConfig) of
|
|
|
|
{sys, _} = SysTuple ->
|
|
|
|
SysTuple;
|
2009-12-11 14:20:59 +00:00
|
|
|
false ->
|
2010-06-15 00:16:26 +00:00
|
|
|
?ABORT("Failed to find {sys, [...]} tuple in reltool.config.", [])
|
2009-12-11 14:20:59 +00:00
|
|
|
end.
|
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
%%
|
|
|
|
%% 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.
|
|
|
|
%%
|
|
|
|
target_dir(ReltoolConfig) ->
|
2010-06-22 16:25:42 +00:00
|
|
|
case rebar_config:get_global(target_dir, undefined) of
|
|
|
|
undefined ->
|
2010-10-25 22:38:51 +00:00
|
|
|
case lists:keyfind(target_dir, 1, ReltoolConfig) of
|
|
|
|
{target_dir, TargetDir} ->
|
2010-06-22 16:25:42 +00:00
|
|
|
filename:absname(TargetDir);
|
2010-06-15 00:16:26 +00:00
|
|
|
false ->
|
2010-06-22 16:25:42 +00:00
|
|
|
{sys, SysInfo} = sys_tuple(ReltoolConfig),
|
2010-10-25 22:38:51 +00:00
|
|
|
case lists:keyfind(rel, 1, SysInfo) of
|
|
|
|
{rel, Name, _Vsn, _Apps} ->
|
2010-06-22 16:25:42 +00:00
|
|
|
filename:absname(Name);
|
|
|
|
false ->
|
|
|
|
filename:absname("target")
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
TargetDir ->
|
|
|
|
filename:absname(TargetDir)
|
2009-12-03 23:06:06 +00:00
|
|
|
end.
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2009-12-02 12:15:35 +00:00
|
|
|
%%
|
2010-06-15 00:16:26 +00:00
|
|
|
%% Look for overlay_vars file reference. The user can override this from the
|
|
|
|
%% command line (i.e. globals), so we check there first and then fall back to
|
|
|
|
%% what is present in the reltool.config file
|
2009-12-02 12:15:35 +00:00
|
|
|
%%
|
2010-06-15 00:16:26 +00:00
|
|
|
overlay_vars(ReltoolConfig) ->
|
|
|
|
case rebar_config:get_global(overlay_vars, undefined) of
|
|
|
|
undefined ->
|
2010-10-25 22:38:51 +00:00
|
|
|
case lists:keyfind(overlay_vars, 1, ReltoolConfig) of
|
|
|
|
{overlay_vars, File} ->
|
2010-06-15 00:16:26 +00:00
|
|
|
File;
|
|
|
|
false ->
|
|
|
|
undefined
|
|
|
|
end;
|
|
|
|
File ->
|
|
|
|
File
|
|
|
|
end.
|
|
|
|
|
2009-12-02 12:15:35 +00:00
|
|
|
|
2009-12-11 14:20:59 +00:00
|
|
|
validate_rel_apps(ReltoolServer, {sys, ReltoolConfig}) ->
|
2010-10-25 22:38:51 +00:00
|
|
|
case lists:keyfind(rel, 1, ReltoolConfig) of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
{rel, _Name, _Vsn, Apps} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
%% Identify all the apps that do NOT exist, based on
|
|
|
|
%% what's available from the reltool server
|
|
|
|
Missing = lists:sort(
|
|
|
|
[App || App <- Apps,
|
|
|
|
app_exists(App, ReltoolServer) == false]),
|
2009-12-03 23:06:06 +00:00
|
|
|
case Missing of
|
|
|
|
[] ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Apps in {rel, ...} section not found by "
|
|
|
|
"reltool: ~p\n", [Missing])
|
2009-12-03 23:06:06 +00:00
|
|
|
end;
|
2010-10-25 22:38:51 +00:00
|
|
|
Rel ->
|
2009-12-03 23:06:06 +00:00
|
|
|
%% Invalid release format!
|
2010-10-25 22:38:51 +00:00
|
|
|
?ABORT("Invalid {rel, ...} section in reltools.config: ~p\n", [Rel])
|
2009-12-03 23:06:06 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
app_exists(App, Server) when is_atom(App) ->
|
|
|
|
case reltool_server:get_app(Server, App) of
|
|
|
|
{ok, _} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
app_exists(AppTuple, Server) when is_tuple(AppTuple) ->
|
|
|
|
app_exists(element(1, AppTuple), Server).
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2009-12-03 23:06:06 +00:00
|
|
|
|
2010-06-19 13:46:57 +00:00
|
|
|
run_reltool(Server, _Config, ReltoolConfig) ->
|
2009-12-03 23:06:06 +00:00
|
|
|
case reltool:get_target_spec(Server) of
|
|
|
|
{ok, Spec} ->
|
2010-06-15 00:16:26 +00:00
|
|
|
%% Pull the target dir and make sure it exists
|
|
|
|
TargetDir = target_dir(ReltoolConfig),
|
2009-12-11 14:20:59 +00:00
|
|
|
mk_target_dir(TargetDir),
|
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
%% Dump the spec, if necessary
|
|
|
|
dump_spec(Spec),
|
2009-12-11 14:20:59 +00:00
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
%% Have reltool actually run
|
|
|
|
case reltool:eval_target_spec(Spec, code:root_dir(), TargetDir) of
|
2009-12-03 23:06:06 +00:00
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, Reason} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Failed to generate target from spec: ~p\n",
|
|
|
|
[Reason])
|
2010-06-15 00:16:26 +00:00
|
|
|
end,
|
|
|
|
|
2011-01-28 15:08:27 +00:00
|
|
|
%% Initialize overlay vars with some basics
|
|
|
|
%% (that can get overwritten)
|
2010-06-15 00:16:26 +00:00
|
|
|
OverlayVars0 = [{erts_vsn, "erts-" ++ erlang:system_info(version)}],
|
|
|
|
|
|
|
|
%% Load up any variables specified by overlay_vars
|
|
|
|
OverlayVars = case overlay_vars(ReltoolConfig) of
|
|
|
|
undefined ->
|
2010-06-17 02:57:01 +00:00
|
|
|
dict:from_list(OverlayVars0);
|
2010-06-15 00:16:26 +00:00
|
|
|
File ->
|
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
|
|
|
dict:from_list(OverlayVars0 ++ Terms);
|
|
|
|
{error, Reason2} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Unable to load overlay_vars "
|
|
|
|
"from ~s: ~p\n",
|
2010-06-15 00:16:26 +00:00
|
|
|
[File, Reason2])
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% Finally, overlay the files specified by the overlay section
|
2011-01-08 18:09:24 +00:00
|
|
|
case lists:keyfind(overlay, 1, ReltoolConfig) of
|
|
|
|
{overlay, Overlay} when is_list(Overlay) ->
|
|
|
|
execute_overlay(Overlay, OverlayVars, rebar_utils:get_cwd(),
|
|
|
|
TargetDir);
|
2010-06-15 00:16:26 +00:00
|
|
|
false ->
|
2011-01-08 18:09:24 +00:00
|
|
|
?INFO("No {overlay, [...]} found in reltool.config.\n", []);
|
|
|
|
_ ->
|
|
|
|
?ABORT("{overlay, [...]} entry in reltool.config "
|
|
|
|
"must be a list.\n", [])
|
2009-12-03 23:06:06 +00:00
|
|
|
end;
|
2010-06-15 00:16:26 +00:00
|
|
|
|
2009-12-02 12:15:35 +00:00
|
|
|
{error, Reason} ->
|
2010-06-15 00:16:26 +00:00
|
|
|
?ABORT("Unable to generate spec: ~s\n", [Reason])
|
2009-12-02 12:15:35 +00:00
|
|
|
end.
|
2009-12-11 14:20:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
mk_target_dir(TargetDir) ->
|
|
|
|
case file:make_dir(TargetDir) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, eexist} ->
|
|
|
|
%% Output directory already exists; if force=1, wipe it out
|
2009-12-31 23:08:00 +00:00
|
|
|
case rebar_config:get_global(force, "0") of
|
|
|
|
"1" ->
|
2009-12-11 14:20:59 +00:00
|
|
|
rebar_file_utils:rm_rf(TargetDir),
|
|
|
|
ok = file:make_dir(TargetDir);
|
|
|
|
_ ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ERROR("Release target directory ~p already exists!\n",
|
|
|
|
[TargetDir]),
|
2009-12-11 14:20:59 +00:00
|
|
|
?FAIL
|
|
|
|
end
|
2009-12-30 12:13:39 +00:00
|
|
|
end.
|
|
|
|
|
2009-12-03 23:06:06 +00:00
|
|
|
|
|
|
|
dump_spec(Spec) ->
|
|
|
|
case rebar_config:get_global(dump_spec, "0") of
|
|
|
|
"1" ->
|
|
|
|
SpecBin = list_to_binary(io_lib:print(Spec, 1, 120, -1)),
|
|
|
|
ok = file:write_file("reltool.spec", SpecBin);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
|
|
|
|
execute_overlay([], _Vars, _BaseDir, _TargetDir) ->
|
|
|
|
ok;
|
|
|
|
execute_overlay([{mkdir, Out} | Rest], Vars, BaseDir, TargetDir) ->
|
|
|
|
OutFile = render(filename:join([TargetDir, Out, "dummy"]), Vars),
|
2010-10-10 20:11:13 +00:00
|
|
|
ok = filelib:ensure_dir(OutFile),
|
2010-06-15 00:16:26 +00:00
|
|
|
?DEBUG("Created dir ~s\n", [filename:dirname(OutFile)]),
|
|
|
|
execute_overlay(Rest, Vars, BaseDir, TargetDir);
|
|
|
|
execute_overlay([{copy, In} | Rest], _Vars, BaseDir, TargetDir) ->
|
|
|
|
execute_overlay([{copy, In, ""} | Rest], _Vars, BaseDir, TargetDir);
|
|
|
|
execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
|
|
|
|
InFile = render(filename:join(BaseDir, In), Vars),
|
|
|
|
OutFile = render(filename:join(TargetDir, Out), Vars),
|
|
|
|
case filelib:is_dir(InFile) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
ok = filelib:ensure_dir(OutFile)
|
|
|
|
end,
|
2010-10-26 04:19:30 +00:00
|
|
|
rebar_file_utils:cp_r([InFile], OutFile),
|
2010-06-15 00:16:26 +00:00
|
|
|
execute_overlay(Rest, Vars, BaseDir, TargetDir);
|
|
|
|
execute_overlay([{template, In, Out} | Rest], Vars, BaseDir, TargetDir) ->
|
2010-06-21 21:08:30 +00:00
|
|
|
InFile = render(filename:join(BaseDir, In), Vars),
|
|
|
|
{ok, InFileData} = file:read_file(InFile),
|
2010-06-15 00:16:26 +00:00
|
|
|
OutFile = render(filename:join(TargetDir, Out), Vars),
|
|
|
|
ok = filelib:ensure_dir(OutFile),
|
|
|
|
case file:write_file(OutFile, render(InFileData, Vars)) of
|
|
|
|
ok ->
|
2010-06-21 21:08:30 +00:00
|
|
|
ok = apply_file_info(InFile, OutFile),
|
2010-06-15 00:16:26 +00:00
|
|
|
?DEBUG("Templated ~p\n", [OutFile]),
|
|
|
|
execute_overlay(Rest, Vars, BaseDir, TargetDir);
|
2009-12-15 00:30:51 +00:00
|
|
|
{error, Reason} ->
|
2010-06-15 00:16:26 +00:00
|
|
|
?ABORT("Failed to template ~p: ~p\n", [OutFile, Reason])
|
2009-12-15 00:30:51 +00:00
|
|
|
end;
|
2010-06-15 00:16:26 +00:00
|
|
|
execute_overlay([{create, Out, Contents} | Rest], Vars, BaseDir, TargetDir) ->
|
|
|
|
OutFile = render(filename:join(TargetDir, Out), Vars),
|
|
|
|
ok = filelib:ensure_dir(OutFile),
|
|
|
|
case file:write_file(OutFile, Contents) of
|
|
|
|
ok ->
|
|
|
|
?DEBUG("Created ~p\n", [OutFile]),
|
|
|
|
execute_overlay(Rest, Vars, BaseDir, TargetDir);
|
|
|
|
{error, Reason} ->
|
|
|
|
?ABORT("Failed to create ~p: ~p\n", [OutFile, Reason])
|
|
|
|
end;
|
|
|
|
execute_overlay([{replace, Out, Regex, Replacement} | Rest],
|
|
|
|
Vars, BaseDir, TargetDir) ->
|
2011-01-28 15:08:27 +00:00
|
|
|
execute_overlay([{replace, Out, Regex, Replacement, []} | Rest],
|
|
|
|
Vars, BaseDir, TargetDir);
|
2010-06-15 00:16:26 +00:00
|
|
|
execute_overlay([{replace, Out, Regex, Replacement, Opts} | Rest],
|
|
|
|
Vars, BaseDir, TargetDir) ->
|
|
|
|
Filename = render(filename:join(TargetDir, Out), Vars),
|
|
|
|
{ok, OrigData} = file:read_file(Filename),
|
2011-01-28 15:08:27 +00:00
|
|
|
Data = re:replace(OrigData, Regex, Replacement,
|
|
|
|
[global, {return, binary}] ++ Opts),
|
2010-06-15 00:16:26 +00:00
|
|
|
case file:write_file(Filename, Data) of
|
|
|
|
ok ->
|
|
|
|
?DEBUG("Edited ~s: s/~s/~s/\n", [Filename, Regex, Replacement]),
|
|
|
|
execute_overlay(Rest, Vars, BaseDir, TargetDir);
|
|
|
|
{error, Reason} ->
|
|
|
|
?ABORT("Failed to edit ~p: ~p\n", [Filename, Reason])
|
|
|
|
end;
|
|
|
|
execute_overlay([Other | _Rest], _Vars, _BaseDir, _TargetDir) ->
|
|
|
|
{error, {unsupported_operation, Other}}.
|
2009-12-11 14:20:59 +00:00
|
|
|
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2010-06-15 00:16:26 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
%% Render a binary to a string, using mustache and the specified context
|
|
|
|
%%
|
|
|
|
render(Bin, Context) ->
|
2010-10-25 22:38:51 +00:00
|
|
|
ReOpts = [global, {return, list}],
|
2010-06-15 00:16:26 +00:00
|
|
|
%% Be sure to escape any double-quotes before rendering...
|
2010-10-25 22:38:51 +00:00
|
|
|
Str0 = re:replace(Bin, "\\\\", "\\\\\\", ReOpts),
|
|
|
|
Str1 = re:replace(Str0, "\"", "\\\\\"", ReOpts),
|
2010-06-21 20:48:37 +00:00
|
|
|
mustache:render(Str1, Context).
|
2010-06-21 21:08:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
apply_file_info(InFile, OutFile) ->
|
|
|
|
{ok, FileInfo} = file:read_file_info(InFile),
|
|
|
|
ok = file:write_file_info(OutFile, FileInfo).
|