@@ -31,29 +31,22 @@ def _impl(ctx):
3131
3232 entries = {}
3333 for f in ctx .files .srcs + ctx .files .hdrs + ctx .files .beam :
34+ if f .basename in entries :
35+ fail ("Duplicate entry for %s: '%s'" % (f , f .basename ))
3436 entries [f .basename ] = f
3537 for dep in flat_deps ([ctx .attr .app ]):
3638 lib_info = dep [ErlangAppInfo ]
3739 for src in lib_info .beam :
3840 if src .is_directory :
39- if ctx .attr .flat :
40- archive_path = ""
41- else :
42- archive_path = path_join (lib_info .app_name , "ebin" )
41+ archive_path = path_join (lib_info .app_name , "ebin" )
4342 else :
44- if ctx .attr .flat :
45- archive_path = src .basename
46- else :
47- archive_path = path_join (lib_info .app_name , "ebin" , src .basename )
48- if archive_path in entries :
49- fail ("Duplicate entry for %s: '%s'" % (src , archive_path ))
43+ archive_path = path_join (lib_info .app_name , "ebin" , src .basename )
44+ if archive_path in entries :
45+ fail ("Duplicate entry for %s: '%s'" % (src , archive_path ))
5046 entries [archive_path ] = src
5147 for src in ([] if ctx .attr .drop_hrl else lib_info .include ) + lib_info .priv :
52- if ctx .attr .flat :
53- archive_path = src .basename
54- else :
55- rp = additional_file_dest_relative_path (dep .label , src )
56- archive_path = path_join (lib_info .app_name , rp )
48+ rp = additional_file_dest_relative_path (dep .label , src )
49+ archive_path = path_join (lib_info .app_name , rp )
5750 if archive_path in entries :
5851 fail ("Duplicate entry for %s: '%s'" % (src , archive_path ))
5952 entries [archive_path ] = src
@@ -89,13 +82,30 @@ def _impl(ctx):
8982 -noshell \\
9083 -eval 'io:format("Assembling {name} escript...~n", []),
9184ContentsDir = "{contents_dir}",
92- ArchiveEntries = filelib:fold_files(
85+ Entries = filelib:fold_files(
9386 ContentsDir, "", true,
9487 fun(Path, Entries) ->
95- Rel = string:prefix(Path, ContentsDir ++ "/"),
9688 {{ok, Bin}} = file:read_file(Path),
97- [{{Rel, Bin}} | Entries]
89+ Rel = string:prefix(Path, ContentsDir ++ "/"),
90+ Dest = case {flat} of
91+ true ->
92+ filename:basename(Path);
93+ false ->
94+ Rel
95+ end,
96+ [{{Dest, Rel, Bin}} | Entries]
9897 end, []),
98+ UniqueEntries = lists:foldr(
99+ fun ({{Dest, Rel, Bin}}, Acc) ->
100+ case Acc of
101+ #{{Dest := _}} ->
102+ io:format(" dropping ~s (conflicting entry at ~s)~n", [Rel, Dest]),
103+ halt(1);
104+ _ ->
105+ Acc#{{Dest => Bin}}
106+ end
107+ end, #{{}}, Entries),
108+ ArchiveEntries = maps:to_list(UniqueEntries),
99109ok = escript:create("{output}",
100110 [{headers}
101111 {{archive, ArchiveEntries, []}}]),
@@ -107,6 +117,7 @@ halt().
107117 erlang_home = erlang_home ,
108118 contents_dir = contents_dir .path ,
109119 name = name ,
120+ flat = str (ctx .attr .flat ).lower (),
110121 headers = "" .join (["{}, " .format (h ) for h in ctx .attr .headers ]),
111122 output = output .path ,
112123 )
0 commit comments