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
|
|
|
|
%% ===================================================================
|
|
|
|
|
2010-08-29 20:33:17 +00:00
|
|
|
escriptize(Config, 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
|
2010-04-28 14:44:06 +00:00
|
|
|
AppName = rebar_app_utils:app_name(AppFile),
|
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-05-10 16:09:23 +00:00
|
|
|
AppNameString = atom_to_list(AppName),
|
|
|
|
TempDir = make_temp_dir(AppNameString),
|
|
|
|
ok = copy_files(Config, AppNameString, TempDir),
|
|
|
|
{ok, Dirs} = file:list_dir(TempDir),
|
2010-08-29 20:33:17 +00:00
|
|
|
|
2012-05-10 16:09:23 +00:00
|
|
|
case zip:create("mem", Dirs, [memory, {cwd, TempDir}]) of
|
2009-12-22 13:45:01 +00:00
|
|
|
{ok, {"mem", ZipBin}} ->
|
2012-05-10 16:09:23 +00:00
|
|
|
ok = rebar_file_utils:rm_rf(TempDir),
|
2009-12-22 13:45:01 +00:00
|
|
|
%% 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"),
|
|
|
|
EmuArgs = rebar_config:get(Config, escript_emu_args, "%%!\n"),
|
|
|
|
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-06-04 16:36:28 +00:00
|
|
|
?ABORT
|
2009-12-22 13:45:01 +00:00
|
|
|
end;
|
|
|
|
{error, ZipError} ->
|
2012-05-10 16:09:23 +00:00
|
|
|
ok = rebar_file_utils:rm_rf(TempDir),
|
2011-01-28 15:08:27 +00:00
|
|
|
?ERROR("Failed to construct ~p escript: ~p\n",
|
|
|
|
[AppName, ZipError]),
|
2012-06-04 16:36:28 +00:00
|
|
|
?ABORT
|
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),
|
|
|
|
ok = file:change_mode(Filename, Mode bor 8#00100),
|
2009-12-22 13:45:01 +00:00
|
|
|
ok.
|
|
|
|
|
2010-10-19 20:21:41 +00:00
|
|
|
clean(Config, AppFile) ->
|
|
|
|
%% Extract the application name from the archive -- this is the default
|
|
|
|
%% name of the generated script
|
|
|
|
AppName = rebar_app_utils:app_name(AppFile),
|
|
|
|
|
|
|
|
%% Get the output filename for the escript -- this may include dirs
|
|
|
|
Filename = rebar_config:get_local(Config, escript_name, AppName),
|
|
|
|
rebar_file_utils:delete_each([Filename]).
|
2010-08-29 20:33:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2012-05-10 16:09:23 +00:00
|
|
|
make_temp_dir(AppName) ->
|
2012-06-14 21:59:36 +00:00
|
|
|
TempDir = temp_name(AppName ++ "."),
|
|
|
|
case file:make_dir(TempDir) of
|
|
|
|
ok ->
|
|
|
|
TempDir;
|
2012-05-10 16:09:23 +00:00
|
|
|
Error ->
|
2012-05-18 20:16:38 +00:00
|
|
|
?ABORT("Failed to create temporary directory: ~p~n",
|
|
|
|
[Error])
|
2012-05-10 16:09:23 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
temp_name(Prefix) ->
|
|
|
|
Hash = erlang:phash2(make_ref()),
|
2012-06-14 21:59:36 +00:00
|
|
|
Prefix ++ integer_to_list(Hash).
|
2012-05-10 16:09:23 +00:00
|
|
|
|
|
|
|
copy_files(Config, AppName, Temp) ->
|
|
|
|
BaseEbinDir = filename:join(AppName, "ebin"),
|
|
|
|
EbinDir = filename:join(Temp, BaseEbinDir),
|
|
|
|
|
|
|
|
%% Look for a list of other applications (dependencies) to include
|
2012-05-18 20:16:38 +00:00
|
|
|
%% in the output file. We then use the .app files for each of
|
|
|
|
%% these to pull in all the .beam files.
|
2012-05-10 16:09:23 +00:00
|
|
|
InclApps = rebar_config:get_local(Config, escript_incl_apps, []),
|
|
|
|
InclEbinDirs = get_app_ebin_dirs(InclApps, []),
|
|
|
|
%% copy incl_apps files
|
|
|
|
lists:foreach(fun(Src) -> ok = copy_files(Src, EbinDir) end, InclEbinDirs),
|
|
|
|
|
2012-05-18 20:16:38 +00:00
|
|
|
%% Look for a list of extra files to copy
|
|
|
|
InclExtr = rebar_config:get_local(Config, escript_incl_extra, []),
|
|
|
|
lists:foreach(fun({Src, Dst}) ->
|
|
|
|
copy_files(Src, filename:join(Temp, Dst))
|
|
|
|
end, InclExtr),
|
|
|
|
|
|
|
|
%% copy script's beam files and app file
|
2012-05-10 16:09:23 +00:00
|
|
|
EbinSrc = filename:join(["ebin", "*"]),
|
|
|
|
ok = copy_files(EbinSrc, EbinDir).
|
|
|
|
|
|
|
|
copy_files(Src, Dst) ->
|
|
|
|
ok = filelib:ensure_dir(filename:join(Dst, "dummy")),
|
2012-05-18 20:16:38 +00:00
|
|
|
rebar_file_utils:cp_r([Src], Dst).
|
2012-05-10 16:09:23 +00:00
|
|
|
|
|
|
|
get_app_ebin_dirs([], Acc) ->
|
2010-08-29 20:33:17 +00:00
|
|
|
Acc;
|
2012-05-10 16:09:23 +00:00
|
|
|
get_app_ebin_dirs([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-05-10 16:09:23 +00:00
|
|
|
%% TODO: shouldn't we also include .app files? escript
|
2012-06-14 21:59:36 +00:00
|
|
|
%% should support multiple app files in one ebin/
|
2012-05-10 16:09:23 +00:00
|
|
|
Acc2 = filename:join(Path, "*.beam"),
|
|
|
|
get_app_ebin_dirs(Rest, [Acc2|Acc])
|
2010-08-29 20:33:17 +00:00
|
|
|
end.
|