Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arbitrary launch process commands #67

Merged
merged 4 commits into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions lib/cnb_process_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ parse_launch_processes() {
local launch_toml_file="${2}"

local type=""
local command=""
local line

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

# Extract the command from lines like `command = ["bash", "-c", "run_command"]`
if [[ ${line} =~ ^command[[:space:]]*=[[:space:]]*\[\"bash\",[[:space:]]*\"-c\",[[:space:]]*\"(.*)\"[[:space:]]*\] ]]; then
command="${BASH_REMATCH[1]}"
_processes["${type}"]="${command}"
# Extract command array from lines like `command = ["bash", "-c", "run_command"]`,
# `command = ["dotnet", "test", "foo.csproj"]`.
# `command = ["dotnet", "test", "foo bar.csproj", "--verbosity", "normal"]`.
if [[ ${line} =~ ^command[[:space:]]*=[[:space:]]*\[(.*)\] ]]; then
local command=()
local raw_command="${BASH_REMATCH[1]}"
local quoted_command=()

# Extract quoted values
while [[ ${raw_command} =~ \"([^\"]+)\" ]]; do
local arg="${BASH_REMATCH[1]}"
command+=("${arg}")

# Quote arg if it differs from it's own `printf '%q'` formatting. This
# is a hack to check for special characters that'd need escaping (while
# preferring to quote the arg for readability) without using regex, sed etc.
if [[ "$(printf '%q' "${arg}")" != "${arg}" ]]; then
quoted_command+=("\"${arg}\"")
else
quoted_command+=("${arg}")
fi

raw_command=${raw_command#*\""${arg}"\"}
done

# Store the command, handling `bash -c` case where we only want the (unquoted) third argument
if [[ ${#command[@]} -ge 3 && "${command[0]}" == "bash" && "${command[1]}" == "-c" ]]; then
_processes["${type}"]="${command[2]}"
else
_processes["${type}"]="${quoted_command[*]}"
fi

type=""
command=""
fi
done <"${launch_toml_file}"
}