Add shell linting and tidying and update all shell scripts

This commit is contained in:
Dave Rolsky 2024-05-18 12:09:08 +08:00
parent c464bd9469
commit 9b467e6799
No known key found for this signature in database
9 changed files with 69 additions and 33 deletions

View File

@ -1,3 +1,5 @@
#!/bin/bash
set -e
set -x
set -o pipefail
@ -5,11 +7,11 @@ set -o pipefail
VERSION=$1
if [ -z "$VERSION" ]; then
JSON=$( curl \
--request GET \
--header "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/cross-rs/cross/releases/latest )
VERSION=$( echo "$JSON" | jq -r ".tag_name")
JSON=$(curl \
--request GET \
--header "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/cross-rs/cross/releases/latest)
VERSION=$(echo "$JSON" | jq -r ".tag_name")
fi
echo "cross-version=$VERSION" >> $GITHUB_OUTPUT
echo "cross-version=$VERSION" >>"$GITHUB_OUTPUT"

View File

@ -1,32 +1,37 @@
#!/bin/bash
set -eo pipefail
set -x
function run () {
echo $1
eval $1
function run() {
echo "$1"
eval "$1"
}
function install_tools () {
function install_tools() {
curl --silent --location \
https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
sh
run "ubi --project houseabsolute/precious --in ~/bin"
run "ubi --project houseabsolute/omegasort --in ~/bin"
run "ubi --project houseabsolute/precious --in $HOME/bin"
run "ubi --project houseabsolute/omegasort --in $HOME/bin"
run "ubi --project koalaman/shellcheck --in $HOME/bin"
run "ubi --project mvdan/sh --in $HOME/bin --exe shfmt"
run "ubi --project crate-ci/typos --in $HOME/bin"
run "npm install prettier"
run "curl -L https://cpanmin.us/ -o ~/bin/cpanm"
run "chmod 0755 ~/bin/cpanm"
run "~/bin/cpanm --sudo --notest Perl::Tidy"
run "curl -L https://cpanmin.us/ -o $HOME/bin/cpanm"
run "chmod 0755 $HOME/bin/cpanm"
run "$HOME/bin/cpanm --sudo --notest Perl::Tidy"
}
if [ "$1" == "-v" ]; then
set -x
fi
mkdir -p $HOME/bin
mkdir -p "$HOME"/bin
set +e
echo ":$PATH:" | grep --extended-regexp ":$HOME/bin:" >& /dev/null
echo ":$PATH:" | grep --extended-regexp ":$HOME/bin:" >&/dev/null
# shellcheck disable=SC2181
if [ "$?" -eq "0" ]; then
path_has_home_bin=1
fi
@ -40,7 +45,7 @@ install_tools
echo "Tools were installed into $HOME/bin."
if [ -z "$path_has_home_bin" ]; then
echo "You should add $HOME/bin to your PATH."
echo "You should add $HOME/bin to your PATH."
fi
exit 0

View File

@ -1,5 +1,6 @@
#!/bin/bash
declare -i status
status=0
PRECIOUS=$(which precious)
@ -7,8 +8,7 @@ if [[ -z $PRECIOUS ]]; then
PRECIOUS=./bin/precious
fi
"$PRECIOUS" lint -s
if (( $? != 0 )); then
if ! "$PRECIOUS" lint -s; then
status+=1
fi

View File

@ -9,7 +9,7 @@ VERSION="$2"
cd "$CROSS_DIR"
if [[ -n "$VERSION" ]] && ! [[ "$VERSION" =~ ^v ]]; then
if [[ -n $VERSION ]] && ! [[ $VERSION =~ ^v ]]; then
cargo install cross --git https://github.com/cross-rs/cross --rev "$VERSION"
mv "$HOME/.cargo/bin/cross" .
exit 0
@ -22,6 +22,7 @@ fi
export TARGET=.
curl --silent --location \
https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
sh
# shellcheck disable=SC2086
./ubi --project cross-rs/cross --matching musl --in . $VERSION_ARGS

View File

@ -45,3 +45,19 @@ ignore_stderr = [
"The .+ file is not sorted",
"The .+ file is not unique",
]
[commands.shellcheck]
type = "lint"
include = "**/*.sh"
cmd = "shellcheck"
ok_exit_codes = 0
lint_failure_exit_codes = 1
[commands.shfmt]
type = "both"
include = "**/*.sh"
cmd = ["shfmt", "--simplify", "--indent", "4"]
lint_flags = "--diff"
tidy_flags = "--write"
ok_exit_codes = 0
lint_failure_exit_codes = 1

View File

@ -1,9 +1,12 @@
#!/bin/bash
set -e
set -x
set -o pipefail
CROSS_DIR="$1"
if [ -f "$CROSS_DIR/cross" ]; then
echo "build-command=$CROSS_DIR/cross" >> $GITHUB_OUTPUT
echo "build-command=$CROSS_DIR/cross" >>"$GITHUB_OUTPUT"
else
echo "build-command=cargo" >> $GITHUB_OUTPUT
echo "build-command=cargo" >>"$GITHUB_OUTPUT"
fi

View File

@ -1,12 +1,15 @@
#!/bin/bash
set -e
set -x
set -o pipefail
TARGET="$1"
# On macOS and Windows, we can cross-compile to all possible targets without
# using cross.
if uname -a | grep --quiet --extended-regexp -i "darwin|msys|windows"; then
echo "needs-cross=false" >> $GITHUB_OUTPUT
echo "needs-cross=false" >>"$GITHUB_OUTPUT"
exit 0
fi
@ -15,8 +18,8 @@ fi
# feature. This feature makes it compile openssl itself, which fails without
# cross.
if echo "$TARGET" | grep --quiet --extended-regexp -i 'x86_64.+linux-(gnu|musl)'; then
echo "needs-cross=false" >> $GITHUB_OUTPUT
echo "needs-cross=false" >>"$GITHUB_OUTPUT"
exit 0
fi
echo "needs-cross=true" >> $GITHUB_OUTPUT
echo "needs-cross=true" >>"$GITHUB_OUTPUT"

View File

@ -1,4 +1,7 @@
#!/bin/bash
set -e
set -x
set -o pipefail
echo "cross-dir=$RUNNER_TEMP" >> $GITHUB_OUTPUT
echo "cross-dir=$RUNNER_TEMP" >>"$GITHUB_OUTPUT"

View File

@ -1,20 +1,23 @@
#!/bin/bash
set -e
set -x
set -o pipefail
TARGET=$1
did_strip=""
strip_binary () {
if [[ $( uname -s ) =~ "Darwin" ]]; then
strip_binary() {
if [[ $(uname -s) =~ "Darwin" ]]; then
stripped=$(
find "$1" -maxdepth 1 -type f -perm +111 | while read exe; do
find "$1" -maxdepth 1 -type f -perm +111 | while read -r exe; do
strip "$exe"
echo "stripped $exe"
done
)
else
stripped=$(
find "$1" -maxdepth 1 -type f -executable | while read exe; do
find "$1" -maxdepth 1 -type f -executable | while read -r exe; do
strip "$exe"
echo "stripped $exe"
done