From 9b467e6799d0ad02366e13bb2f2236178dc463a6 Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Sat, 18 May 2024 12:09:08 +0800 Subject: [PATCH] Add shell linting and tidying and update all shell scripts --- determine-cross-version.sh | 14 ++++++++------ dev/bin/install-dev-tools.sh | 31 ++++++++++++++++++------------- git/hooks/pre-commit.sh | 4 ++-- install-cross-nix.sh | 5 +++-- precious.toml | 16 ++++++++++++++++ set-build-command.sh | 7 +++++-- set-cross-compile.sh | 9 ++++++--- set-cross-dir.sh | 5 ++++- strip-binary.sh | 11 +++++++---- 9 files changed, 69 insertions(+), 33 deletions(-) diff --git a/determine-cross-version.sh b/determine-cross-version.sh index 6efde06..eca48c5 100755 --- a/determine-cross-version.sh +++ b/determine-cross-version.sh @@ -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" diff --git a/dev/bin/install-dev-tools.sh b/dev/bin/install-dev-tools.sh index fedc51c..9234136 100755 --- a/dev/bin/install-dev-tools.sh +++ b/dev/bin/install-dev-tools.sh @@ -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 diff --git a/git/hooks/pre-commit.sh b/git/hooks/pre-commit.sh index 63595ad..9256ae2 100755 --- a/git/hooks/pre-commit.sh +++ b/git/hooks/pre-commit.sh @@ -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 diff --git a/install-cross-nix.sh b/install-cross-nix.sh index 2f73c01..0c0dc0e 100755 --- a/install-cross-nix.sh +++ b/install-cross-nix.sh @@ -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 diff --git a/precious.toml b/precious.toml index 2ca71f9..1397105 100644 --- a/precious.toml +++ b/precious.toml @@ -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 diff --git a/set-build-command.sh b/set-build-command.sh index 953cb6c..2eb3d73 100755 --- a/set-build-command.sh +++ b/set-build-command.sh @@ -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 diff --git a/set-cross-compile.sh b/set-cross-compile.sh index a8119b6..a5bc987 100755 --- a/set-cross-compile.sh +++ b/set-cross-compile.sh @@ -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" diff --git a/set-cross-dir.sh b/set-cross-dir.sh index f67a0af..33f1a20 100755 --- a/set-cross-dir.sh +++ b/set-cross-dir.sh @@ -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" diff --git a/strip-binary.sh b/strip-binary.sh index 63d1d19..7c33d74 100755 --- a/strip-binary.sh +++ b/strip-binary.sh @@ -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