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-22 13:45:01 +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_escripter).
|
|
|
|
|
2010-10-19 20:21:41 +00:00
|
|
|
-export([escriptize/2,
|
|
|
|
clean/2]).
|
2009-12-22 13:45:01 +00:00
|
|
|
|
|
|
|
-include("rebar.hrl").
|
2010-12-26 12:56:17 +00:00
|
|
|
-include_lib("kernel/include/file.hrl").
|
2009-12-22 13:45:01 +00:00
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2012-04-22 19:53:32 +00:00
|
|
|
escriptize(Config0, AppFile) ->
|
2010-10-19 20:21:41 +00:00
|
|
|
%% Extract the application name from the archive -- this is the default
|
|
|
|
%% name of the generated script
|
2012-04-22 19:53:32 +00:00
|
|
|
{Config, AppName} = rebar_app_utils:app_name(Config0, AppFile),
|
2012-07-10 10:48:43 +00:00
|
|
|
AppNameStr = atom_to_list(AppName),
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2010-10-19 20:21:41 +00:00
|
|
|
%% Get the output filename for the escript -- this may include dirs
|
|
|
|
Filename = rebar_config:get_local(Config, escript_name, AppName),
|
2010-10-22 20:12:25 +00:00
|
|
|
ok = filelib:ensure_dir(Filename),
|
2010-10-19 20:21:41 +00:00
|
|
|
|
2012-06-17 20:38:00 +00:00
|
|
|
%% Look for a list of other applications (dependencies) to include
|
|
|
|
%% in the output file. We then use the .app files for each of these
|
|
|
|
%% to pull in all the .beam files.
|
|
|
|
InclBeams = get_app_beams(
|
|
|
|
rebar_config:get_local(Config, escript_incl_apps, []), []),
|
|
|
|
|
2012-07-26 16:36:59 +00:00
|
|
|
%% Look for a list of extra files to include in the output file.
|
2012-06-17 20:38:00 +00:00
|
|
|
%% For internal rebar-private use only. Do not use outside rebar.
|
|
|
|
InclExtra = get_extra(Config),
|
2010-08-29 20:33:17 +00:00
|
|
|
|
2012-06-17 20:38:00 +00:00
|
|
|
%% Construct the archive of everything in ebin/ dir -- put it on the
|
|
|
|
%% top-level of the zip file so that code loading works properly.
|
2012-07-10 10:48:43 +00:00
|
|
|
EbinPrefix = filename:join(AppNameStr, "ebin"),
|
|
|
|
EbinFiles = usort(load_files(EbinPrefix, "*", "ebin")),
|
|
|
|
ExtraFiles = usort(InclBeams ++ InclExtra),
|
|
|
|
Files = EbinFiles ++ ExtraFiles,
|
2012-06-17 20:38:00 +00:00
|
|
|
|
|
|
|
case zip:create("mem", Files, [memory]) of
|
2009-12-22 13:45:01 +00:00
|
|
|
{ok, {"mem", ZipBin}} ->
|
|
|
|
%% Archive was successfully created. Prefix that binary with our
|
|
|
|
%% header and write to our escript file
|
2011-06-14 10:11:45 +00:00
|
|
|
Shebang = rebar_config:get(Config, escript_shebang,
|
|
|
|
"#!/usr/bin/env escript\n"),
|
|
|
|
Comment = rebar_config:get(Config, escript_comment, "%%\n"),
|
2012-10-03 02:17:09 +00:00
|
|
|
DefaultEmuArgs = ?FMT("%%! -pa ~s/~s/ebin\n",
|
2012-07-10 10:48:43 +00:00
|
|
|
[AppNameStr, AppNameStr]),
|
|
|
|
EmuArgs = rebar_config:get(Config, escript_emu_args,
|
|
|
|
DefaultEmuArgs),
|
2011-06-14 10:11:45 +00:00
|
|
|
Script = iolist_to_binary([Shebang, Comment, EmuArgs, ZipBin]),
|
2010-10-19 20:21:41 +00:00
|
|
|
case file:write_file(Filename, Script) of
|
2009-12-22 13:45:01 +00:00
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, WriteError} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ERROR("Failed to write ~p script: ~p\n",
|
|
|
|
[AppName, WriteError]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL
|
2009-12-22 13:45:01 +00:00
|
|
|
end;
|
|
|
|
{error, ZipError} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ERROR("Failed to construct ~p escript: ~p\n",
|
|
|
|
[AppName, ZipError]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL
|
2009-12-22 13:45:01 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
%% Finally, update executable perms for our script
|
2010-12-26 12:56:17 +00:00
|
|
|
{ok, #file_info{mode = Mode}} = file:read_file_info(Filename),
|
2012-07-10 00:04:55 +00:00
|
|
|
ok = file:change_mode(Filename, Mode bor 8#00111),
|
2012-04-22 19:53:32 +00:00
|
|
|
{ok, Config}.
|
2009-12-22 13:45:01 +00:00
|
|
|
|
2012-04-22 19:53:32 +00:00
|
|
|
clean(Config0, AppFile) ->
|
2010-10-19 20:21:41 +00:00
|
|
|
%% Extract the application name from the archive -- this is the default
|
|
|
|
%% name of the generated script
|
2012-04-22 19:53:32 +00:00
|
|
|
{Config, AppName} = rebar_app_utils:app_name(Config0, AppFile),
|
2010-10-19 20:21:41 +00:00
|
|
|
|
|
|
|
%% Get the output filename for the escript -- this may include dirs
|
|
|
|
Filename = rebar_config:get_local(Config, escript_name, AppName),
|
2012-04-22 19:53:32 +00:00
|
|
|
rebar_file_utils:delete_each([Filename]),
|
|
|
|
{ok, Config}.
|
2010-08-29 20:33:17 +00:00
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2012-06-17 20:38:00 +00:00
|
|
|
get_app_beams([], Acc) ->
|
2010-08-29 20:33:17 +00:00
|
|
|
Acc;
|
2012-06-17 20:38:00 +00:00
|
|
|
get_app_beams([App | Rest], Acc) ->
|
2010-08-29 20:33:17 +00:00
|
|
|
case code:lib_dir(App, ebin) of
|
|
|
|
{error, bad_name} ->
|
2011-01-28 15:08:27 +00:00
|
|
|
?ABORT("Failed to get ebin/ directory for "
|
|
|
|
"~p escript_incl_apps.", [App]);
|
2010-08-29 20:33:17 +00:00
|
|
|
Path ->
|
2012-07-10 10:48:43 +00:00
|
|
|
Prefix = filename:join(atom_to_list(App), "ebin"),
|
|
|
|
Acc2 = load_files(Prefix, "*", Path),
|
2012-06-17 20:38:00 +00:00
|
|
|
get_app_beams(Rest, Acc2 ++ Acc)
|
2010-08-29 20:33:17 +00:00
|
|
|
end.
|
2012-06-17 20:38:00 +00:00
|
|
|
|
|
|
|
get_extra(Config) ->
|
|
|
|
Extra = rebar_config:get_local(Config, escript_incl_extra, []),
|
|
|
|
lists:foldl(fun({Wildcard, Dir}, Files) ->
|
|
|
|
load_files(Wildcard, Dir) ++ Files
|
|
|
|
end, [], Extra).
|
|
|
|
|
|
|
|
load_files(Wildcard, Dir) ->
|
2012-07-10 10:48:43 +00:00
|
|
|
load_files("", Wildcard, Dir).
|
|
|
|
|
|
|
|
load_files(Prefix, Wildcard, Dir) ->
|
|
|
|
[read_file(Prefix, Filename, Dir)
|
|
|
|
|| Filename <- filelib:wildcard(Wildcard, Dir)].
|
|
|
|
|
|
|
|
read_file(Prefix, Filename, Dir) ->
|
|
|
|
Filename1 = case Prefix of
|
|
|
|
"" ->
|
|
|
|
Filename;
|
|
|
|
_ ->
|
|
|
|
filename:join([Prefix, Filename])
|
|
|
|
end,
|
|
|
|
[dir_entries(filename:dirname(Filename1)),
|
|
|
|
{Filename1, file_contents(filename:join(Dir, Filename))}].
|
2012-06-17 20:38:00 +00:00
|
|
|
|
|
|
|
file_contents(Filename) ->
|
|
|
|
{ok, Bin} = file:read_file(Filename),
|
|
|
|
Bin.
|
2012-07-10 10:48:43 +00:00
|
|
|
|
2012-08-12 12:03:44 +00:00
|
|
|
%% Given a filename, return zip archive dir entries for each sub-dir.
|
|
|
|
%% Required to work around issues fixed in OTP-10071.
|
2012-07-10 10:48:43 +00:00
|
|
|
dir_entries(File) ->
|
|
|
|
Dirs = dirs(File),
|
|
|
|
[{Dir ++ "/", <<>>} || Dir <- Dirs].
|
|
|
|
|
2012-08-12 12:03:44 +00:00
|
|
|
%% Given "foo/bar/baz", return ["foo", "foo/bar", "foo/bar/baz"].
|
2012-07-10 10:48:43 +00:00
|
|
|
dirs(Dir) ->
|
|
|
|
dirs1(filename:split(Dir), "", []).
|
|
|
|
|
|
|
|
dirs1([], _, Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
dirs1([H|T], "", []) ->
|
|
|
|
dirs1(T, H, [H]);
|
|
|
|
dirs1([H|T], Last, Acc) ->
|
|
|
|
Dir = filename:join(Last, H),
|
|
|
|
dirs1(T, Dir, [Dir|Acc]).
|
|
|
|
|
|
|
|
usort(List) ->
|
|
|
|
lists:ukeysort(1, lists:flatten(List)).
|