-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.sh
More file actions
executable file
·138 lines (109 loc) · 4.07 KB
/
Copy pathaction.sh
File metadata and controls
executable file
·138 lines (109 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# action.sh: download a pinned rubyfmt release and run it in --check mode.
set -eu
REPO="fables-tales/rubyfmt"
dbg() {
echo "::debug::${*}"
}
err() {
echo "::error::${*}"
}
die() {
err "${*}"
exit 1
}
installed() {
command -v "${1}" >/dev/null 2>&1
}
output() {
echo "${1}=${2}" >> "${GITHUB_OUTPUT}"
}
sha256_of() {
if installed sha256sum; then
sha256sum "${1}" | awk '{print $1}'
else
shasum -a 256 "${1}" | awk '{print $1}'
fi
}
installed curl || die "Cannot run this action without curl"
installed tar || die "Cannot run this action without tar"
# Map the runner's OS/arch to one of rubyfmt's published release platforms.
# See: https://github.com/fables-tales/rubyfmt/releases
case "${RUNNER_OS}-${RUNNER_ARCH}" in
Linux-X64)
platform="Linux-x86_64"
;;
Linux-ARM64)
platform="Linux-aarch64"
;;
macOS-ARM64)
platform="Darwin-arm64"
;;
*)
die "Unsupported runner: ${RUNNER_OS}/${RUNNER_ARCH}. rubyfmt-action supports Linux (x86_64/aarch64) and Apple Silicon macOS runners only."
;;
esac
case "${GHA_RUBYFMT_HEADER_MODE}" in
none) ;;
opt-in) header_flag="--header-opt-in" ;;
opt-out) header_flag="--header-opt-out" ;;
*) die "'header-mode' must be one of 'none', 'opt-in', or 'opt-out' (got '${GHA_RUBYFMT_HEADER_MODE}')" ;;
esac
version_regex='^v?[0-9]+\.[0-9]+\.[0-9]+-[0-9]+$'
requested_version="${GHA_RUBYFMT_VERSION}"
if [[ "${requested_version}" == "latest" ]]; then
version="$(<"${GITHUB_ACTION_PATH}/support/latest")"
elif [[ "${requested_version}" =~ ${version_regex} ]]; then
version="${requested_version#v}"
else
die "'version' must be 'latest' or an exact X.Y.Z-N version (got '${requested_version}')"
fi
# Look up the pinned checksum for this (version, platform) pair from
# ./support/versions. Deliberately avoids bash associative arrays (`declare
# -A`), since macOS runners' default `/bin/bash` is bash 3.2 and lacks them.
sha256="$(awk -v v="${version}" -v p="${platform}" '$1 == v && $2 == p { print $3 }' "${GITHUB_ACTION_PATH}/support/versions")"
[[ -n "${sha256}" ]] || die "Unknown rubyfmt version/platform combination: ${version} / ${platform}. Run support/sync-versions.sh to pin a new version."
asset="rubyfmt-v${version}-${platform}.tar.gz"
url="https://github.com/${REPO}/releases/download/v${version}/${asset}"
workdir="${RUNNER_TEMP}/rubyfmt-action"
mkdir -p "${workdir}"
archive="${workdir}/${asset}"
echo "::group::Downloading rubyfmt v${version} (${platform})"
curl --fail --silent --show-error --location --output "${archive}" "${url}"
echo "::endgroup::"
actual_sha256="$(sha256_of "${archive}")"
[[ "${actual_sha256}" == "${sha256}" ]] \
|| die "Checksum mismatch for ${asset}: expected ${sha256}, got ${actual_sha256}"
tar --extract --gzip --file "${archive}" --directory "${workdir}"
rubyfmt_bin="$(find "${workdir}" -type f -name rubyfmt | head -n1)"
[[ -n "${rubyfmt_bin}" ]] || die "Could not locate the rubyfmt binary after extracting ${asset}"
chmod +x "${rubyfmt_bin}"
arguments=("--check")
[[ "${GHA_RUBYFMT_FAIL_FAST}" == "true" ]] && arguments+=("--fail-fast")
[[ "${GHA_RUBYFMT_INCLUDE_GITIGNORED}" == "true" ]] && arguments+=("--include-gitignored")
[[ -n "${header_flag:-}" ]] && arguments+=("${header_flag}")
diff_output="${workdir}/diff.txt"
echo "::group::Running rubyfmt --check"
set +e
# shellcheck disable=SC2086
"${rubyfmt_bin}" "${arguments[@]}" -- ${GHA_RUBYFMT_PATHS} | tee "${diff_output}"
exitcode="${PIPESTATUS[0]}"
set -e
echo "::endgroup::"
dbg "rubyfmt exited with code ${exitcode}"
if [[ "${exitcode}" -eq 0 ]]; then
output "outcome" "success"
exit 0
fi
output "outcome" "failure"
if [[ "${GHA_RUBYFMT_SUMMARY}" == "true" && -s "${diff_output}" && -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
{
echo "### rubyfmt found unformatted files"
echo
echo '```diff'
cat "${diff_output}"
echo '```'
} >> "${GITHUB_STEP_SUMMARY}"
fi
err "rubyfmt found formatting issues. Run 'rubyfmt -i ${GHA_RUBYFMT_PATHS}' locally to fix them."
exit "${exitcode}"