Support whitespace separated list

This commit is contained in:
Taiki Endo
2025-12-17 19:46:38 +09:00
parent 72b24c709c
commit 5d018ee3d2
4 changed files with 48 additions and 9 deletions

View File

@@ -156,7 +156,12 @@ jobs:
- uses: ./
with:
# NB: Update alias list in tools/publish.rs and case for aliases in main.sh.
tool: nextest,taplo-cli,typos-cli,wasm-bindgen-cli,wasmtime-cli
tool: |
nextest
taplo-cli
typos-cli
wasm-bindgen-cli
wasmtime-cli
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Test bash

View File

@@ -22,7 +22,7 @@ GitHub Action for installing development tools (mainly from GitHub Releases).
| Name | Required | Description | Type | Default |
| ---- | :------: | ----------- | ---- | ------- |
| tool | **✓** | Tools to install (comma-separated list) | String | |
| tool | **✓** | Tools to install (whitespace or comma separated list) | String | |
| checksum | | Whether to enable checksums | Boolean | `true` |
### Example workflow

18
main.sh
View File

@@ -29,6 +29,19 @@ warn() {
info() {
printf >&2 'info: %s\n' "$*"
}
normalize_comma_or_space_separated() {
# Normalize whitespace characters into space because it's hard to handle single input contains lines with POSIX sed alone.
local list="${1//[$'\r\n\t']/ }"
if [[ "${list}" == *","* ]]; then
# If a comma is contained, consider it is a comma-separated list.
# Drop leading and trailing whitespaces in each element.
sed -E 's/ *, */,/g; s/^.//' <<<",${list},"
else
# Otherwise, consider it is a whitespace-separated list.
# Convert whitespace characters into comma.
sed -E 's/ +/,/g; s/^.//' <<<" ${list} "
fi
}
_sudo() {
if type -P sudo >/dev/null; then
sudo "$@"
@@ -440,9 +453,8 @@ tool="${INPUT_TOOL:-}"
tools=()
if [[ -n "${tool}" ]]; then
while read -rd,; do
t="${REPLY# *}"
tools+=("${t%* }")
done <<<"${tool},"
tools+=("${REPLY}")
done < <(normalize_comma_or_space_separated "${tool}")
fi
if [[ ${#tools[@]} -eq 0 ]]; then
warn "no tool specified; this could be caused by a dependabot bug where @<tool_name> tags on this action are replaced by @<version> tags"

View File

@@ -206,7 +206,29 @@ IFS=$'\n'
tools=($(LC_ALL=C sort -u <<<"${tools[*]}"))
IFS=$'\n\t'
# TODO: inject random space before/after of tool name for testing https://github.com/taiki-e/install-action/issues/115.
IFS=','
printf 'tool=%s\n' "${tools[*]}"
IFS=$'\n\t'
comma_sep=$((RANDOM % 2))
list=''
for tool in "${tools[@]}"; do
if [[ -n "${list}" ]]; then
if [[ "${comma_sep}" == "1" ]]; then
list+=','
else
case $((RANDOM % 2)) in
0) list+=' ' ;;
1) list+=$'\t' ;;
esac
fi
fi
case $((RANDOM % 3)) in
0) ;;
1) list+=' ' ;;
2) list+=$' \t ' ;;
esac
list+="${tool}"
case $((RANDOM % 3)) in
0) ;;
1) list+=' ' ;;
2) list+=$' \t ' ;;
esac
done
printf 'tool=%s\n' "${list}"