@@ -9,51 +9,68 @@ def _module_name(f):
99 return "'{}'" .format (f .basename .replace (".beam" , "" , 1 ))
1010
1111def _impl (ctx ):
12- app_file = ctx .actions .declare_file (ctx .attr .out .name )
13-
1412 if len (ctx .files .app_src ) > 1 :
1513 fail ("Multiple .app.src files ({}) are not supported" .format (
1614 ctx .files .app_src ,
1715 ))
18- elif len (ctx .files .app_src ) == 1 :
19- src = ctx .files .app_src [0 ].path
20- else :
21- src = ""
2216
23- app_module = ctx .attr .app_module if ctx .attr .app_module != "" else ctx .attr .app_name + "_app"
24- if len ([m for m in ctx .files .modules if m .basename == app_module + ".beam" ]) > 0 :
25- registered_list = "[" + "," .join ([ctx .attr .app_name + "_sup" ] + ctx .attr .app_registered ) + "]"
26- else :
27- app_module = ""
28- if len (ctx .attr .app_registered ) > 0 :
29- fail (app_module , "is not present, but app_registered was provided." )
30- registered_list = ""
17+ app_file = ctx .actions .declare_file (ctx .attr .out .name )
18+
19+ (erlang_home , _ , runfiles ) = erlang_dirs (ctx )
3120
3221 modules = "[" + "," .join ([_module_name (m ) for m in ctx .files .modules ]) + "]"
3322
34- applications_list = ""
35- if src == "" or len (ctx .attr .extra_apps ) > 0 or len (ctx .attr .deps ) > 0 :
23+ app_file_tool_path = ctx .attr .app_file_tool [DefaultInfo ].files_to_run .executable .path
24+
25+ if len (ctx .files .app_src ) == 1 :
26+ script = """set -euo pipefail
27+
28+ {maybe_install_erlang}
29+
30+ cat << 'EOF' | "{erlang_home}"/bin/escript {app_file_tool} modules {src} > {out}
31+ {modules}.
32+ EOF
33+
34+ """ .format (
35+ maybe_install_erlang = maybe_install_erlang (ctx ),
36+ erlang_home = erlang_home ,
37+ app_file_tool = app_file_tool_path ,
38+ modules = modules ,
39+ src = ctx .files .app_src [0 ].path ,
40+ out = app_file .path ,
41+ )
42+
43+ runfiles = runfiles .merge (
44+ ctx .attr .app_file_tool [DefaultInfo ].default_runfiles ,
45+ )
46+
47+ inputs = depset (
48+ direct = ctx .files .app_src ,
49+ transitive = [runfiles .files ],
50+ )
51+ else :
52+ app_module = ctx .attr .app_module if ctx .attr .app_module != "" else ctx .attr .app_name + "_app"
53+ if len ([m for m in ctx .files .modules if m .basename == app_module + ".beam" ]) > 0 :
54+ registered_list = "[" + "," .join ([ctx .attr .app_name + "_sup" ] + ctx .attr .app_registered ) + "]"
55+ else :
56+ app_module = ""
57+ if len (ctx .attr .app_registered ) > 0 :
58+ fail (app_module , "is not present, but app_registered was provided." )
59+ registered_list = ""
60+
3661 applications = ["kernel" , "stdlib" ] + ctx .attr .extra_apps
3762 for dep in ctx .attr .deps :
3863 applications .append (dep [ErlangAppInfo ].app_name )
3964 applications_list = "[" + "," .join (applications ) + "]"
4065
41- stamp = ctx .attr .stamp == 1 or (ctx .attr .stamp == - 1 and
42- ctx .attr .private_stamp_detect )
43-
44- (erlang_home , _ , runfiles ) = erlang_dirs (ctx )
45-
46- app_file_tool_path = ctx .attr .app_file_tool [DefaultInfo ].files_to_run .executable .path
66+ stamp = ctx .attr .stamp == 1 or (ctx .attr .stamp == - 1 and
67+ ctx .attr .private_stamp_detect )
4768
48- script = """set -euo pipefail
69+ script = """set -euo pipefail
4970
5071{maybe_install_erlang}
5172
52- if [ -n "{src}" ]; then
53- cp {src} {out}
54- else
55- echo "{{application,'{name}',[{{registered, []}},{{env, []}}]}}." > {out}
56- fi
73+ echo "{{application,'{name}',[{{registered, []}},{{env, []}}]}}." > {out}
5774
5875if [ -n '{description}' ]; then
5976 cat << 'EOF' | "{erlang_home}"/bin/escript {app_file_tool} description {out} > {out}.tmp && mv {out}.tmp {out}
@@ -120,32 +137,31 @@ if [ -n '{extra_keys}' ]; then
120137EOF
121138fi
122139""" .format (
123- maybe_install_erlang = maybe_install_erlang (ctx ),
124- erlang_home = erlang_home ,
125- app_file_tool = app_file_tool_path ,
126- info_file = ctx .info_file .path ,
127- name = ctx .attr .app_name ,
128- description = ctx .attr .app_description ,
129- stamp_version_key = ctx .attr .stamp_version_key if stamp else "" ,
130- version = ctx .attr .app_version ,
131- modules = modules ,
132- registered = registered_list ,
133- applications = applications_list ,
134- app_module = app_module ,
135- env = ctx .attr .app_env ,
136- extra_keys = ctx .attr .app_extra_keys ,
137- src = src ,
138- out = app_file .path ,
139- )
140-
141- runfiles = runfiles .merge (
142- ctx .attr .app_file_tool [DefaultInfo ].default_runfiles ,
143- )
144-
145- inputs = depset (
146- direct = ctx .files .app_src + ([ctx .info_file ] if stamp else []),
147- transitive = [runfiles .files ],
148- )
140+ maybe_install_erlang = maybe_install_erlang (ctx ),
141+ erlang_home = erlang_home ,
142+ app_file_tool = app_file_tool_path ,
143+ info_file = ctx .info_file .path ,
144+ name = ctx .attr .app_name ,
145+ description = ctx .attr .app_description ,
146+ stamp_version_key = ctx .attr .stamp_version_key if stamp else "" ,
147+ version = ctx .attr .app_version ,
148+ modules = modules ,
149+ registered = registered_list ,
150+ applications = applications_list ,
151+ app_module = app_module ,
152+ env = ctx .attr .app_env ,
153+ extra_keys = ctx .attr .app_extra_keys ,
154+ out = app_file .path ,
155+ )
156+
157+ runfiles = runfiles .merge (
158+ ctx .attr .app_file_tool [DefaultInfo ].default_runfiles ,
159+ )
160+
161+ inputs = depset (
162+ direct = [ctx .info_file ] if stamp else [],
163+ transitive = [runfiles .files ],
164+ )
149165
150166 ctx .actions .run_shell (
151167 inputs = inputs ,
0 commit comments