This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap
executable file
·57 lines (52 loc) · 2.15 KB
/
bootstrap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env escript
main(_Args) ->
% Compile the source files.
case make:files(filelib:wildcard("src/*.erl"),
[{outdir, "ebin"}, {i, "include"}, debug_info]) of
up_to_date -> ok;
error ->
io:format("Compile failed.~n"),
erlang:halt(1)
end,
% Zip up the beam files.
Files = read_files("*.beam", "ebin") ++
read_files("*.app", "ebin"),
case zip:create("mem", Files, [memory]) of
{ok, {"mem", ZipBin}} ->
Sections = [shebang,
{emu_args, "-noshell -noinput -sname topcat@localhost"},
{archive, ZipBin}],
case escript:create('binary', Sections) of
{ok, Bin} ->
case file:write_file("topcat", Bin) of
ok ->
ok;
{error, WriteError} ->
io:format("Failed to write script file: ~p~n", [WriteError])
end;
{error, ScriptError} ->
io:format("Failed to create script archive: ~p~n", [ScriptError]),
erlang:halt(1)
end;
{error, ZipError} ->
io:format("Failed to create script archive: ~p~n", [ZipError]),
erlang:halt(1)
end.
%% @doc We want to read files from the "ebin" directory, but put them in the
%% root of the ZIP file. zip:create/3 expects Files :: [Name] or Files ::
%% [{Name, Bin}]. We can't use the first, because it includes the "ebin" part
%% of the name, which breaks escript. To use the second, we have to read the
%% files ourselves.
-spec read_files(Wildcard, Dir) -> [{Name, Bin}]
when Wildcard :: string(), Dir :: string(),
Name :: string(), Bin :: binary().
read_files(Wildcard, Dir) ->
[read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
-spec read_file(Filename, Dir) -> {Filename, Bin}
when Filename :: string(), Dir :: string(),
Bin :: binary().
read_file(Filename, Dir) ->
{ok, Bin} = file:read_file(filename:join(Dir, Filename)),
{Filename, Bin}.
%% -*- mode: erlang -*-
%% vi: ft=erlang