Skip to content

Commit 9648569

Browse files
BencodesCopilot
andauthored
Extract result validation into template file (#231)
* Extract result validation into template file * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 120b874 commit 9648569

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

detekt/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ toolchain(
1515
toolchain_type = "//detekt:toolchain_type",
1616
)
1717

18-
exports_files(["defs.bzl"])
18+
exports_files([
19+
"defs.bzl",
20+
"final_result.sh.tpl",
21+
])

detekt/defs.bzl

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ _ATTRS = {
1010
executable = True,
1111
cfg = "exec",
1212
),
13+
"_final_result_template": attr.label(
14+
default = Label("//detekt:final_result.sh.tpl"),
15+
allow_single_file = True,
16+
),
1317
"srcs": attr.label_list(
1418
mandatory = True,
1519
allow_files = [".kt", ".kts"],
@@ -217,8 +221,9 @@ def _impl(
217221
action_inputs.extend(platform_jar_files + classpath)
218222
detekt_arguments.add("--classpath", ":".join([f.path for f in platform_jar_files] + [f.path for f in classpath]))
219223

220-
action_inputs.extend(ctx.files.plugins)
221-
detekt_arguments.add_joined("--plugins", ctx.files.plugins, join_with = ",")
224+
plugin_jars = [plugin for plugin in ctx.files.plugins if plugin.extension == "jar"]
225+
action_inputs.extend(plugin_jars)
226+
detekt_arguments.add_joined("--plugins", plugin_jars, join_with = ",")
222227

223228
txt_report = ctx.actions.declare_file("{}_detekt_report.txt".format(ctx.label.name))
224229
action_outputs.append(txt_report)
@@ -246,14 +251,13 @@ def _impl(
246251

247252
execution_result = ctx.actions.declare_file("{}_exit_code.txt".format(ctx.label.name))
248253
run_files.append(execution_result)
249-
action_outputs.append(execution_result)
250254
detekt_arguments.add("--execution-result", "{}".format(execution_result.path))
251255

252256
ctx.actions.run(
253257
mnemonic = "Detekt",
254258
progress_message = "Running Detekt for {}".format(str(ctx.label)),
255259
inputs = action_inputs,
256-
outputs = action_outputs,
260+
outputs = action_outputs + [execution_result],
257261
executable = ctx.executable._detekt_wrapper,
258262
execution_requirements = {
259263
"requires-worker-protocol": "proto",
@@ -267,19 +271,14 @@ def _impl(
267271
# Note: this is not compatible with Windows, feel free to submit PR!
268272
# text report-contents are always printed to shell
269273
final_result = ctx.actions.declare_file(ctx.attr.name + ".sh")
270-
ctx.actions.write(
274+
ctx.actions.expand_template(
271275
output = final_result,
272-
content = """
273-
#!/bin/bash
274-
set -euo pipefail
275-
exit_code=$(cat {execution_result})
276-
report=$(cat {text_report})
277-
if [ ! -z "$report" ]; then
278-
echo "$report"
279-
fi
280-
{baseline_script}
281-
exit "$exit_code"
282-
""".format(execution_result = execution_result.short_path, text_report = txt_report.short_path, baseline_script = baseline_script),
276+
template = ctx.file._final_result_template,
277+
substitutions = {
278+
"{baseline_script}": baseline_script,
279+
"{execution_result}": execution_result.short_path,
280+
"{text_report}": txt_report.short_path,
281+
},
283282
is_executable = True,
284283
)
285284

detekt/final_result.sh.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
exit_code="$(<"{execution_result}")"
4+
if [ -s "{text_report}" ]; then
5+
cat "{text_report}"
6+
fi
7+
{baseline_script}
8+
exit "$exit_code"

0 commit comments

Comments
 (0)