Skip to content

Commit

Permalink
format shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
expipiplus1 committed Oct 29, 2024
1 parent 3b5bf5d commit 77b69a2
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 98 deletions.
11 changes: 10 additions & 1 deletion .github/actions/format-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ runs:
- name: install prettier
shell: bash
run: |
run: |
npm install [email protected]
echo "$(npm bin -g)" >> $GITHUB_PATH
- name: install shfmt
shell: bash
run: |
tmpdir=$(mktemp -d)
curl -L -H "Authorization: token ${{github.token}}" \
-o "$tmpdir/shfmt" \
https://github.com/mvdan/sh/releases/download/v3.10.0/shfmt_v3.10.0_linux_amd64
chmod +x "$tmpdir/shfmt"
echo "$tmpdir" >> $GITHUB_PATH
127 changes: 58 additions & 69 deletions docs/scripts/release-note.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,23 @@ verbose=true
$verbose && echo "Reminder: PLEASE make sure your local repo is up-to-date before running the script." >&2

gh=""
for candidate in "$(which gh.exe)" "/mnt/c/Program Files/GitHub CLI/gh.exe" "/c/Program Files/GitHub CLI/gh.exe" "/cygdrive/c/Program Files/GitHub CLI/gh.exe"
do
if [ -x "$candidate" ]
then
gh="$candidate"
break
fi
for candidate in "$(which gh.exe)" "/mnt/c/Program Files/GitHub CLI/gh.exe" "/c/Program Files/GitHub CLI/gh.exe" "/cygdrive/c/Program Files/GitHub CLI/gh.exe"; do
if [ -x "$candidate" ]; then
gh="$candidate"
break
fi
done
if [ "x$gh" = "x" ] || ! [ -x "$gh" ]
then
echo "File not found: gh.exe"
echo "gh.exe can be downloaded from https://cli.github.com"
exit 1
if [ "x$gh" = "x" ] || ! [ -x "$gh" ]; then
echo "File not found: gh.exe"
echo "gh.exe can be downloaded from https://cli.github.com"
exit 1
fi
$verbose && echo "gh.exe is found from: $gh" >&2

if [ "x$1" = "x" ]
then
echo "This script requires 'since' information for git-log command."
echo "Usage: $0 2024-07-30"
exit 1
if [ "x$1" = "x" ]; then
echo "This script requires 'since' information for git-log command."
echo "Usage: $0 2024-07-30"
exit 1
fi
since="$1"

Expand All @@ -45,62 +41,55 @@ commitsCount="$(echo "$commits" | wc -l)"

echo "=== Breaking changes ==="
breakingChanges=""
for i in $(seq $commitsCount)
do
line="$(echo "$commits" | head -$i | tail -1)"

# Get PR number from the git commit title
pr="$(echo "$line" | grep '#[1-9][0-9][0-9][0-9][0-9]*' | sed 's|.* (\#\([1-9][0-9][0-9][0-9][0-9]*\))|\1|')"
[ "x$pr" = "x" ] && continue

# Check if the PR is marked as a breaking change
if "$gh" issue view $pr --json labels | grep -q 'pr: breaking change'
then
breakingChanges+="$line"
fi
for i in $(seq $commitsCount); do
line="$(echo "$commits" | head -$i | tail -1)"

# Get PR number from the git commit title
pr="$(echo "$line" | grep '#[1-9][0-9][0-9][0-9][0-9]*' | sed 's|.* (\#\([1-9][0-9][0-9][0-9][0-9]*\))|\1|')"
[ "x$pr" = "x" ] && continue

# Check if the PR is marked as a breaking change
if "$gh" issue view $pr --json labels | grep -q 'pr: breaking change'; then
breakingChanges+="$line"
fi
done
if [ "x$breakingChanges" = "x" ]
then
echo "No breaking changes"
if [ "x$breakingChanges" = "x" ]; then
echo "No breaking changes"
else
echo "$breakingChanges"
echo "$breakingChanges"
fi
echo ""

echo "=== All changes for this release ==="
for i in $(seq $commitsCount)
do
line="$(echo "$commits" | head -$i | tail -1)"

result="$line"
for dummy in 1
do
# Get PR number from the git commit title
pr="$(echo "$line" | grep '#[1-9][0-9][0-9][0-9][0-9]*' | sed 's|.* (\#\([1-9][0-9][0-9][0-9][0-9]*\))|\1|')"
[ "x$pr" = "x" ] && break

# Mark breaking changes with "[BREAKING]"
if "$gh" issue view $pr --json labels | grep -q 'pr: breaking change'
then
result="[BREAKING] $line"
fi

# Get the issue number for the PR
body="$("$gh" issue view $pr --json body)"
[ "x$body" = "x" ] && break
issue="$(echo "$body" | grep '#[1-9][0-9][0-9][0-9][0-9]*' | sed 's|.*\#\([1-9][0-9][0-9][0-9][0-9]*\).*|\1|')"
[ "x$issue" = "x" ] && break

# Get the labels of the issue
label="$("$gh" issue view $issue --json labels)"
[ "x$label" = "x" ] && break

# Get the goal type from the labels
goal="$(echo "$label" | grep '"goal:' | sed 's|.*"goal:\([^"]*\)".*|\1|')"
[ "x$goal" = "x" ] && break

result+=" (#$issue:$goal)"
done
echo "$result"
for i in $(seq $commitsCount); do
line="$(echo "$commits" | head -$i | tail -1)"

result="$line"
for dummy in 1; do
# Get PR number from the git commit title
pr="$(echo "$line" | grep '#[1-9][0-9][0-9][0-9][0-9]*' | sed 's|.* (\#\([1-9][0-9][0-9][0-9][0-9]*\))|\1|')"
[ "x$pr" = "x" ] && break

# Mark breaking changes with "[BREAKING]"
if "$gh" issue view $pr --json labels | grep -q 'pr: breaking change'; then
result="[BREAKING] $line"
fi

# Get the issue number for the PR
body="$("$gh" issue view $pr --json body)"
[ "x$body" = "x" ] && break
issue="$(echo "$body" | grep '#[1-9][0-9][0-9][0-9][0-9]*' | sed 's|.*\#\([1-9][0-9][0-9][0-9][0-9]*\).*|\1|')"
[ "x$issue" = "x" ] && break

# Get the labels of the issue
label="$("$gh" issue view $issue --json labels)"
[ "x$label" = "x" ] && break

# Get the goal type from the labels
goal="$(echo "$label" | grep '"goal:' | sed 's|.*"goal:\([^"]*\)".*|\1|')"
[ "x$goal" = "x" ] && break

result+=" (#$issue:$goal)"
done
echo "$result"
done

38 changes: 30 additions & 8 deletions external/build-llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EOF
#
# Some helper functions
#
msg(){
msg() {
printf "%s\n" "$1" >&2
}

Expand Down Expand Up @@ -62,13 +62,35 @@ extra_arguments=()

while [[ "$#" -gt 0 ]]; do
case $1 in
-h | --help) help; exit ;;
--repo) repo=$2; shift;;
--branch) branch=$2; shift;;
--source-dir) source_dir=$2; shift;;
--config) config=$2; shift;;
--install-prefix) install_prefix=$2; shift;;
--) shift; extra_arguments+=("$@"); break;;
-h | --help)
help
exit
;;
--repo)
repo=$2
shift
;;
--branch)
branch=$2
shift
;;
--source-dir)
source_dir=$2
shift
;;
--config)
config=$2
shift
;;
--install-prefix)
install_prefix=$2
shift
;;
--)
shift
extra_arguments+=("$@")
break
;;
*)
msg "Unknown parameter passed: $1"
help >&2
Expand Down
30 changes: 14 additions & 16 deletions external/bump-glslang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ while [[ "$#" -gt 0 ]]; do
shift
done

if [ $help ]; then
if [ "$help" ]; then
me=$(basename "$0")
cat <<EOF
$me: Update external/glslang and dependencies
Expand All @@ -62,27 +62,24 @@ EOF
exit
fi

big_msg()
{
big_msg() {
echo
echo "################################################################"
echo "$1"
echo "################################################################"
}

require_bin()
{
if ! command -v "$1" &> /dev/null
then
echo "This script needs $1, but it isn't in \$PATH"
missing_bin=1
require_bin() {
if ! command -v "$1" &>/dev/null; then
echo "This script needs $1, but it isn't in \$PATH"
missing_bin=1
fi
}
require_bin "jq"
require_bin "git"
require_bin "python"
require_bin "cmake"
if [ $missing_bin ]; then
if [ "$missing_bin" ]; then
exit 1
fi

Expand All @@ -100,13 +97,13 @@ if ! test -f "$glslang/.git"; then
exit 1
fi

known_good_commit(){
known_good_commit() {
jq <"$glslang/known_good.json" \
".commits | .[] | select(.name == \"$1\") | .commit" \
--raw-output
}

bump_dep(){
bump_dep() {
commit=$(known_good_commit "$2")
big_msg "Fetching $commit from origin in $1"
git -C "$1" fetch origin "$commit"
Expand All @@ -116,7 +113,7 @@ bump_dep(){

declare -A old_ref
declare -A new_ref
merge_dep(){
merge_dep() {
name=$1
dir=$2
up=$3
Expand Down Expand Up @@ -180,14 +177,15 @@ rm -f "$spirv_tools_generated/*.{inc,h}"
cp --target-directory "$spirv_tools_generated" "$build"/*.{inc,h}
set +x

if [ $do_commit ]; then
if [ "$do_commit" ]; then
big_msg "Committing changes"
msg=$(cat <<EOF
msg=$(
cat <<EOF
external/glslang: ${old_ref["glslang"]} -> ${new_ref["glslang"]}
external/spirv-tools: ${old_ref["spirv-tools"]} -> ${new_ref["spirv-tools"]}"
EOF
)
)

git commit \
--message "$msg" \
Expand Down
33 changes: 29 additions & 4 deletions extras/formatting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ require_bin "xargs" "3"
require_bin "diff" "2"
require_bin "clang-format" "17" "18"
require_bin "prettier" "3"
require_bin "shfmt" "3"

if [ "$missing_bin" ]; then
exit 1
Expand All @@ -83,6 +84,8 @@ fi
exit_code=0

cmake_formatting() {
echo "Formatting CMake files..."

readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')

common_args=(
Expand All @@ -100,7 +103,9 @@ cmake_formatting() {
}

cpp_formatting() {
readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h')
echo "Formatting cpp files..."

readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')

if [ "$check_only" -eq 1 ]; then
local tmpdir
Expand All @@ -125,17 +130,37 @@ cpp_formatting() {
}

yaml_json_formatting() {
echo "Formatting yaml and json files..."

readarray -t files < <(git ls-files "*.yaml" "*.yml" "*.json" ':!external/**')

if [ "$check_only" -eq 1 ]; then
prettier --check "${files[@]}" || exit_code=1
else
prettier --write "${files[@]}" | grep -v '(unchanged)'
prettier --write "${files[@]}" | grep -v '(unchanged)' || :
fi
}

sh_formatting() {
echo "Formatting sh files..."

readarray -t files < <(git ls-files "*.sh")

common_args=(
# default 8 is way too wide
--indent 2
)

if [ "$check_only" -eq 1 ]; then
shfmt "${common_args[@]}" --diff "${files[@]}" || exit_code=1
else
shfmt "${common_args[@]}" --write "${files[@]}"
fi
}

# cmake_formatting
# cpp_formatting
cmake_formatting
cpp_formatting
yaml_json_formatting
sh_formatting

exit $exit_code

0 comments on commit 77b69a2

Please sign in to comment.