Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 133 lines (114 sloc) 5.529 kb
c466076 @tuncer Clean up emacs file local variables
tuncer authored
1 %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
9a8015f @tuncer Added vi modeline/emacs local variables to file headers
tuncer authored
2 %% ex: ts=4 sw=4 et
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
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
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
29 -export([escriptize/2,
30 clean/2]).
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
31
32 -include("rebar.hrl").
9441251 @juranki Use file:change_mode/2 instead of invoking chmod
juranki authored
33 -include_lib("kernel/include/file.hrl").
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
34
35 %% ===================================================================
36 %% Public API
37 %% ===================================================================
38
e185e86 @tuncer Remove shared state
tuncer authored
39 escriptize(Config0, AppFile) ->
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
40 %% Extract the application name from the archive -- this is the default
41 %% name of the generated script
e185e86 @tuncer Remove shared state
tuncer authored
42 {Config, AppName} = rebar_app_utils:app_name(Config0, AppFile),
8c85021 @tuncer Deleted trailing whitespace
tuncer authored
43
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
44 %% Get the output filename for the escript -- this may include dirs
45 Filename = rebar_config:get_local(Config, escript_name, AppName),
1690139 @tuncer Match ok result of filelib:ensure_dir/1
tuncer authored
46 ok = filelib:ensure_dir(Filename),
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
47
c91250d @tuncer Use [{File,Bin}, ...] in escriptize zip:create
tuncer authored
48 %% Look for a list of other applications (dependencies) to include
49 %% in the output file. We then use the .app files for each of these
50 %% to pull in all the .beam files.
51 InclBeams = get_app_beams(
52 rebar_config:get_local(Config, escript_incl_apps, []), []),
53
54 %% Look for a list extra files to include in the output file.
55 %% For internal rebar-private use only. Do not use outside rebar.
56 InclExtra = get_extra(Config),
9813126 @dizzyd Adding support for embedding other apps via escript_incl_apps
dizzyd authored
57
c91250d @tuncer Use [{File,Bin}, ...] in escriptize zip:create
tuncer authored
58 %% Construct the archive of everything in ebin/ dir -- put it on the
59 %% top-level of the zip file so that code loading works properly.
60 Files = load_files("*", "ebin") ++ InclBeams ++ InclExtra,
61
62 case zip:create("mem", Files, [memory]) of
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
63 {ok, {"mem", ZipBin}} ->
64 %% Archive was successfully created. Prefix that binary with our
65 %% header and write to our escript file
2e64e91 @roidrage Add options for escript shebang, command, emu_args
roidrage authored
66 Shebang = rebar_config:get(Config, escript_shebang,
67 "#!/usr/bin/env escript\n"),
68 Comment = rebar_config:get(Config, escript_comment, "%%\n"),
69 EmuArgs = rebar_config:get(Config, escript_emu_args, "%%!\n"),
70 Script = iolist_to_binary([Shebang, Comment, EmuArgs, ZipBin]),
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
71 case file:write_file(Filename, Script) of
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
72 ok ->
73 ok;
74 {error, WriteError} ->
63de05d @tuncer Clean up code
tuncer authored
75 ?ERROR("Failed to write ~p script: ~p\n",
76 [AppName, WriteError]),
c86c999 @dizzyd Remove ?FAIL in favor of ?ABORT
dizzyd authored
77 ?ABORT
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
78 end;
79 {error, ZipError} ->
63de05d @tuncer Clean up code
tuncer authored
80 ?ERROR("Failed to construct ~p escript: ~p\n",
81 [AppName, ZipError]),
c86c999 @dizzyd Remove ?FAIL in favor of ?ABORT
dizzyd authored
82 ?ABORT
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
83 end,
84
85 %% Finally, update executable perms for our script
9441251 @juranki Use file:change_mode/2 instead of invoking chmod
juranki authored
86 {ok, #file_info{mode = Mode}} = file:read_file_info(Filename),
57fc7f6 @ferd Change escriptize archive permissions to a+x
ferd authored
87 ok = file:change_mode(Filename, Mode bor 8#00111),
e185e86 @tuncer Remove shared state
tuncer authored
88 {ok, Config}.
b14eaa0 @dizzyd Adding rudimentary escript generator
dizzyd authored
89
e185e86 @tuncer Remove shared state
tuncer authored
90 clean(Config0, AppFile) ->
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
91 %% Extract the application name from the archive -- this is the default
92 %% name of the generated script
e185e86 @tuncer Remove shared state
tuncer authored
93 {Config, AppName} = rebar_app_utils:app_name(Config0, AppFile),
67d4fbf @dizzyd Add support for overriding the default name/location of the escript; …
dizzyd authored
94
95 %% Get the output filename for the escript -- this may include dirs
96 Filename = rebar_config:get_local(Config, escript_name, AppName),
e185e86 @tuncer Remove shared state
tuncer authored
97 rebar_file_utils:delete_each([Filename]),
98 {ok, Config}.
9813126 @dizzyd Adding support for embedding other apps via escript_incl_apps
dizzyd authored
99
100 %% ===================================================================
101 %% Internal functions
102 %% ===================================================================
103
c91250d @tuncer Use [{File,Bin}, ...] in escriptize zip:create
tuncer authored
104 get_app_beams([], Acc) ->
9813126 @dizzyd Adding support for embedding other apps via escript_incl_apps
dizzyd authored
105 Acc;
c91250d @tuncer Use [{File,Bin}, ...] in escriptize zip:create
tuncer authored
106 get_app_beams([App | Rest], Acc) ->
9813126 @dizzyd Adding support for embedding other apps via escript_incl_apps
dizzyd authored
107 case code:lib_dir(App, ebin) of
108 {error, bad_name} ->
63de05d @tuncer Clean up code
tuncer authored
109 ?ABORT("Failed to get ebin/ directory for "
110 "~p escript_incl_apps.", [App]);
9813126 @dizzyd Adding support for embedding other apps via escript_incl_apps
dizzyd authored
111 Path ->
c91250d @tuncer Use [{File,Bin}, ...] in escriptize zip:create
tuncer authored
112 Acc2 = [{filename:join([App, ebin, F]),
113 file_contents(filename:join(Path, F))} ||
114 F <- filelib:wildcard("*", Path)],
115 get_app_beams(Rest, Acc2 ++ Acc)
9813126 @dizzyd Adding support for embedding other apps via escript_incl_apps
dizzyd authored
116 end.
c91250d @tuncer Use [{File,Bin}, ...] in escriptize zip:create
tuncer authored
117
118 get_extra(Config) ->
119 Extra = rebar_config:get_local(Config, escript_incl_extra, []),
120 lists:foldl(fun({Wildcard, Dir}, Files) ->
121 load_files(Wildcard, Dir) ++ Files
122 end, [], Extra).
123
124 load_files(Wildcard, Dir) ->
125 [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
126
127 read_file(Filename, Dir) ->
128 {Filename, file_contents(filename:join(Dir, Filename))}.
129
130 file_contents(Filename) ->
131 {ok, Bin} = file:read_file(Filename),
132 Bin.
Something went wrong with that request. Please try again.