1%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
2%% ex: ts=4 sw=4 et
3%% -------------------------------------------------------------------
4%%
5%% rebar: Erlang Build Tools
6%%
7%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
8%%
9%% Permission is hereby granted, free of charge, to any person obtaining a copy
10%% of this software and associated documentation files (the "Software"), to deal
11%% in the Software without restriction, including without limitation the rights
12%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13%% copies of the Software, and to permit persons to whom the Software is
14%% furnished to do so, subject to the following conditions:
15%%
16%% The above copyright notice and this permission notice shall be included in
17%% all copies or substantial portions of the Software.
18%%
19%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25%% THE SOFTWARE.
26%% -------------------------------------------------------------------
27-module(rebar_escripter).
28
29-export([escriptize/2,
30         clean/2]).
31
32%% for internal use only
33-export([info/2]).
34
35-include("rebar.hrl").
36-include_lib("kernel/include/file.hrl").
37
38%% ===================================================================
39%% Public API
40%% ===================================================================
41
42escriptize(Config0, AppFile) ->
43    %% Extract the application name from the archive -- this is the default
44    %% name of the generated script
45    {Config, AppName} = rebar_app_utils:app_name(Config0, AppFile),
46    AppNameStr = atom_to_list(AppName),
47
48    %% Get the output filename for the escript -- this may include dirs
49    Filename = rebar_config:get_local(Config, escript_name, AppName),
50    ok = filelib:ensure_dir(Filename),
51
52    %% Look for a list of other applications (dependencies) to include
53    %% in the output file. We then use the .app files for each of these
54    %% to pull in all the .beam files.
55    InclBeams = get_app_beams(
56                  rebar_config:get_local(Config, escript_incl_apps, []), []),
57
58    %% Look for a list of extra files to include in the output file.
59    %% For internal rebar-private use only. Do not use outside rebar.
60    InclExtra = get_extra(Config),
61
62    %% Construct the archive of everything in ebin/ dir -- put it on the
63    %% top-level of the zip file so that code loading works properly.
64    EbinPrefix = filename:join(AppNameStr, "ebin"),
65    EbinFiles = usort(load_files(EbinPrefix, "*", "ebin")),
66    ExtraFiles = usort(InclBeams ++ InclExtra),
67    Files = EbinFiles ++ ExtraFiles,
68
69    case zip:create("mem", Files, [memory]) of
70        {ok, {"mem", ZipBin}} ->
71            %% Archive was successfully created. Prefix that binary with our
72            %% header and write to our escript file
73            Shebang = rebar_config:get(Config, escript_shebang,
74                                       "#!/usr/bin/env escript\n"),
75            Comment = rebar_config:get(Config, escript_comment, "%%\n"),
76            DefaultEmuArgs = ?FMT("%%! -pa ~s/~s/ebin\n",
77                                  [AppNameStr, AppNameStr]),
78            EmuArgs = rebar_config:get(Config, escript_emu_args,
79                                       DefaultEmuArgs),
80            Script = iolist_to_binary([Shebang, Comment, EmuArgs, ZipBin]),
81            case file:write_file(Filename, Script) of
82                ok ->
83                    ok;
84                {error, WriteError} ->
85                    ?ERROR("Failed to write ~p script: ~p\n",
86                           [AppName, WriteError]),
87                    ?FAIL
88            end;
89        {error, ZipError} ->
90            ?ERROR("Failed to construct ~p escript: ~p\n",
91                   [AppName, ZipError]),
92            ?FAIL
93    end,
94
95    %% Finally, update executable perms for our script
96    {ok, #file_info{mode = Mode}} = file:read_file_info(Filename),
97    ok = file:change_mode(Filename, Mode bor 8#00111),
98    {ok, Config}.
99
100clean(Config0, AppFile) ->
101    %% Extract the application name from the archive -- this is the default
102    %% name of the generated script
103    {Config, AppName} = rebar_app_utils:app_name(Config0, AppFile),
104
105    %% Get the output filename for the escript -- this may include dirs
106    Filename = rebar_config:get_local(Config, escript_name, AppName),
107    rebar_file_utils:delete_each([Filename]),
108    {ok, Config}.
109
110%% ===================================================================
111%% Internal functions
112%% ===================================================================
113
114info(help, escriptize) ->
115    info_help("Generate escript archive");
116info(help, clean) ->
117    info_help("Delete generated escript archive").
118
119info_help(Description) ->
120    ?CONSOLE(
121       "~s.~n"
122       "~n"
123       "Valid rebar.config options:~n"
124       "  ~p~n"
125       "  ~p~n"
126       "  ~p~n"
127       "  ~p~n"
128       "  ~p~n",
129       [
130        Description,
131        {escript_name, "application"},
132        {escript_incl_apps, []},
133        {escript_shebang, "#!/usr/bin/env escript\n"},
134        {escript_comment, "%%\n"},
135        {escript_emu_args, "%%! -pa application/application/ebin\n"}
136       ]).
137
138get_app_beams([], Acc) ->
139    Acc;
140get_app_beams([App | Rest], Acc) ->
141    case code:lib_dir(App, ebin) of
142        {error, bad_name} ->
143            ?ABORT("Failed to get ebin/ directory for "
144                   "~p escript_incl_apps.", [App]);
145        Path ->
146            Prefix = filename:join(atom_to_list(App), "ebin"),
147            Acc2 = load_files(Prefix, "*", Path),
148            get_app_beams(Rest, Acc2 ++ Acc)
149    end.
150
151get_extra(Config) ->
152    Extra = rebar_config:get_local(Config, escript_incl_extra, []),
153    lists:foldl(fun({Wildcard, Dir}, Files) ->
154                        load_files(Wildcard, Dir) ++ Files
155                end, [], Extra).
156
157load_files(Wildcard, Dir) ->
158    load_files("", Wildcard, Dir).
159
160load_files(Prefix, Wildcard, Dir) ->
161    [read_file(Prefix, Filename, Dir)
162     || Filename <- filelib:wildcard(Wildcard, Dir)].
163
164read_file(Prefix, Filename, Dir) ->
165    Filename1 = case Prefix of
166                    "" ->
167                        Filename;
168                    _ ->
169                        filename:join([Prefix, Filename])
170                end,
171    [dir_entries(filename:dirname(Filename1)),
172     {Filename1, file_contents(filename:join(Dir, Filename))}].
173
174file_contents(Filename) ->
175    {ok, Bin} = file:read_file(Filename),
176    Bin.
177
178%% Given a filename, return zip archive dir entries for each sub-dir.
179%% Required to work around issues fixed in OTP-10071.
180dir_entries(File) ->
181    Dirs = dirs(File),
182    [{Dir ++ "/", <<>>} || Dir <- Dirs].
183
184%% Given "foo/bar/baz", return ["foo", "foo/bar", "foo/bar/baz"].
185dirs(Dir) ->
186    dirs1(filename:split(Dir), "", []).
187
188dirs1([], _, Acc) ->
189    lists:reverse(Acc);
190dirs1([H|T], "", []) ->
191    dirs1(T, H, [H]);
192dirs1([H|T], Last, Acc) ->
193    Dir = filename:join(Last, H),
194    dirs1(T, Dir, [Dir|Acc]).
195
196usort(List) ->
197    lists:ukeysort(1, lists:flatten(List)).
198