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

View File

@ -1,32 +1,37 @@
#!/bin/bash #!/bin/bash
set -eo pipefail set -eo pipefail
set -x
function run () { function run() {
echo $1 echo "$1"
eval $1 eval "$1"
} }
function install_tools () { function install_tools() {
curl --silent --location \ 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 sh
run "ubi --project houseabsolute/precious --in ~/bin" run "ubi --project houseabsolute/precious --in $HOME/bin"
run "ubi --project houseabsolute/omegasort --in ~/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 "npm install prettier"
run "curl -L https://cpanmin.us/ -o ~/bin/cpanm" run "curl -L https://cpanmin.us/ -o $HOME/bin/cpanm"
run "chmod 0755 ~/bin/cpanm" run "chmod 0755 $HOME/bin/cpanm"
run "~/bin/cpanm --sudo --notest Perl::Tidy" run "$HOME/bin/cpanm --sudo --notest Perl::Tidy"
} }
if [ "$1" == "-v" ]; then if [ "$1" == "-v" ]; then
set -x set -x
fi fi
mkdir -p $HOME/bin mkdir -p "$HOME"/bin
set +e 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 if [ "$?" -eq "0" ]; then
path_has_home_bin=1 path_has_home_bin=1
fi fi
@ -40,7 +45,7 @@ install_tools
echo "Tools were installed into $HOME/bin." echo "Tools were installed into $HOME/bin."
if [ -z "$path_has_home_bin" ]; then 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 fi
exit 0 exit 0

View File

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

View File

@ -9,7 +9,7 @@ VERSION="$2"
cd "$CROSS_DIR" 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" cargo install cross --git https://github.com/cross-rs/cross --rev "$VERSION"
mv "$HOME/.cargo/bin/cross" . mv "$HOME/.cargo/bin/cross" .
exit 0 exit 0
@ -22,6 +22,7 @@ fi
export TARGET=. export TARGET=.
curl --silent --location \ 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 sh
# shellcheck disable=SC2086
./ubi --project cross-rs/cross --matching musl --in . $VERSION_ARGS ./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 sorted",
"The .+ file is not unique", "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 -e
set -x set -x
set -o pipefail
CROSS_DIR="$1" CROSS_DIR="$1"
if [ -f "$CROSS_DIR/cross" ]; then if [ -f "$CROSS_DIR/cross" ]; then
echo "build-command=$CROSS_DIR/cross" >> $GITHUB_OUTPUT echo "build-command=$CROSS_DIR/cross" >>"$GITHUB_OUTPUT"
else else
echo "build-command=cargo" >> $GITHUB_OUTPUT echo "build-command=cargo" >>"$GITHUB_OUTPUT"
fi fi

View File

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

View File

@ -1,4 +1,7 @@
#!/bin/bash
set -e set -e
set -x 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 -e
set -x set -x
set -o pipefail
TARGET=$1 TARGET=$1
did_strip="" did_strip=""
strip_binary () { strip_binary() {
if [[ $( uname -s ) =~ "Darwin" ]]; then if [[ $(uname -s) =~ "Darwin" ]]; then
stripped=$( 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" strip "$exe"
echo "stripped $exe" echo "stripped $exe"
done done
) )
else else
stripped=$( 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" strip "$exe"
echo "stripped $exe" echo "stripped $exe"
done done