Skip to content

Commit 40db110

Browse files
Support arbitrary launch process commands (#67)
1 parent 8b121ba commit 40db110

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

lib/cnb_process_utils.sh

+32-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ parse_launch_processes() {
77
local launch_toml_file="${2}"
88

99
local type=""
10-
local command=""
1110
local line
1211

1312
while IFS= read -r line; do
@@ -16,12 +15,39 @@ parse_launch_processes() {
1615
type="${BASH_REMATCH[1]}"
1716
fi
1817

19-
# Extract the command from lines like `command = ["bash", "-c", "run_command"]`
20-
if [[ ${line} =~ ^command[[:space:]]*=[[:space:]]*\[\"bash\",[[:space:]]*\"-c\",[[:space:]]*\"(.*)\"[[:space:]]*\] ]]; then
21-
command="${BASH_REMATCH[1]}"
22-
_processes["${type}"]="${command}"
18+
# Extract command array from lines like `command = ["bash", "-c", "run_command"]`,
19+
# `command = ["dotnet", "test", "foo.csproj"]`.
20+
# `command = ["dotnet", "test", "foo bar.csproj", "--verbosity", "normal"]`.
21+
if [[ ${line} =~ ^command[[:space:]]*=[[:space:]]*\[(.*)\] ]]; then
22+
local command=()
23+
local raw_command="${BASH_REMATCH[1]}"
24+
local quoted_command=()
25+
26+
# Extract quoted values
27+
while [[ ${raw_command} =~ \"([^\"]+)\" ]]; do
28+
local arg="${BASH_REMATCH[1]}"
29+
command+=("${arg}")
30+
31+
# Quote arg if it differs from it's own `printf '%q'` formatting. This
32+
# is a hack to check for special characters that'd need escaping (while
33+
# preferring to quote the arg for readability) without using regex, sed etc.
34+
if [[ "$(printf '%q' "${arg}")" != "${arg}" ]]; then
35+
quoted_command+=("\"${arg}\"")
36+
else
37+
quoted_command+=("${arg}")
38+
fi
39+
40+
raw_command=${raw_command#*\""${arg}"\"}
41+
done
42+
43+
# Store the command, handling `bash -c` case where we only want the (unquoted) third argument
44+
if [[ ${#command[@]} -ge 3 && "${command[0]}" == "bash" && "${command[1]}" == "-c" ]]; then
45+
_processes["${type}"]="${command[2]}"
46+
else
47+
_processes["${type}"]="${quoted_command[*]}"
48+
fi
49+
2350
type=""
24-
command=""
2551
fi
2652
done <"${launch_toml_file}"
2753
}

0 commit comments

Comments
 (0)