Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
refactor: Format shell scripts
Browse files Browse the repository at this point in the history
Using 'shfmt' to format shell scripts
See: https://github.com/mvdan/sh for more information
  • Loading branch information
howjmay committed Jul 16, 2020
1 parent 7049f38 commit 6582608
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 351 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ The codebase of this repository follows [Google's C++ guidelines](https://google

* buildifier
* clang-format
* [shfmt](https://github.com/mvdan/sh)

### Buildifier

Expand Down Expand Up @@ -171,6 +172,14 @@ clang-format can be installed by command:
* Debian/Ubuntu based systems: `$ sudo apt-get install clang-format`
* macOS: `$ brew install clang-format`

### shfmt

It requires Go 1.13 or above, and install it with following command.

```shell
GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt
```

## Usage

`Tangle-accelerator` currently supports two categories of APIs
Expand Down
32 changes: 14 additions & 18 deletions endpoint/build-legato.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ set -uo pipefail

COMMON_FILE="tests/endpoint/common.sh"

if [ ! -f "$COMMON_FILE" ]
then
echo "$COMMON_FILE is not exists."
echo "Always execute this script in top-level directory."
exit 1
if [ ! -f "$COMMON_FILE" ]; then
echo "$COMMON_FILE is not exists."
echo "Always execute this script in top-level directory."
exit 1
fi
source $COMMON_FILE

if [ ! -d "legato" ]
then
if ! download_legato_repo;
then
echo "Download Legato AF failed. Try to download again..."
rm -rf legato/ .repo/
if ! download_legato_repo;
then
echo "Failed to download the Legato AF. Please check your connection"
exit 1
fi
fi
build_legato_repo
if [ ! -d "legato" ]; then
if ! download_legato_repo; then
echo "Download Legato AF failed. Try to download again..."
rm -rf legato/ .repo/
if ! download_legato_repo; then
echo "Failed to download the Legato AF. Please check your connection"
exit 1
fi
fi
build_legato_repo
fi
67 changes: 32 additions & 35 deletions endpoint/unit-test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,52 @@ GREEN='\033[0;32m'
NC='\033[0m' # No Color
EP_TEST_CONFIG=(asan tsan ubsan)

function cleanup(){
kill -9 "${TA}"
function cleanup() {
kill -9 "${TA}"
}

function success(){
cleanup
echo -e "${GREEN}ALL test passed${NC}"
exit 0
function success() {
cleanup
echo -e "${GREEN}ALL test passed${NC}"
exit 0
}

function failed(){
cleanup
echo -e "${RED}Unit-test: ${TEST_CASE} not passed${NC}"
exit 1
function failed() {
cleanup
echo -e "${RED}Unit-test: ${TEST_CASE} not passed${NC}"
exit 1
}

function run_test_suite(){
bazel test -c dbg --config "$1" //endpoint/unit-test/...
ret=$?
if [[ ret -ne 0 ]]
then
TEST_CASE="$1"
failed
fi
function run_test_suite() {
bazel test -c dbg --config "$1" //endpoint/unit-test/...
ret=$?
if [[ ret -ne 0 ]]; then
TEST_CASE="$1"
failed
fi
}

function start_ta(){
# Create tangle-accelerator for unit-test
bazel run accelerator &
TA=$!
# Wait until tangle-accelerator has been initialized
echo "==============Wait for TA starting=============="
while read -r line
do
if [[ "$line" == "TA-START" ]]
then
echo "$line"
fi
done <<< $(nc -U -l $socket | tr '\0' '\n')
echo "==============TA has successfully started=============="
function start_ta() {
# Create tangle-accelerator for unit-test
bazel run accelerator &
TA=$!
# Wait until tangle-accelerator has been initialized
echo "==============Wait for TA starting=============="
while read -r line; do
if [[ "$line" == "TA-START" ]]; then
echo "$line"
fi
done <<<$(nc -U -l $socket | tr '\0' '\n')
echo "==============TA has successfully started=============="
}

echo "Start unit-test for endpoint"
start_ta

# Run endpoint unit-test
for i in ${EP_TEST_CONFIG[*]}; do
run_test_suite "$i"
for i in ${EP_TEST_CONFIG[*]}; do
run_test_suite "$i"
done

# Finish
# Finish
success
148 changes: 68 additions & 80 deletions hooks/autohook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
# Author: Nik Kantar <http://nkantar.com>
# Version: 2.1.1
# Website: https://github.com/nkantar/Autohook
# License: MIT -
# License: MIT -

# MIT License
#
#
# Copyright (c) 2017 Nikola Kantar
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -29,90 +29,78 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


echo() {
builtin echo "[Autohook] $@";
builtin echo "[Autohook] $@"
}


install() {
hook_types=(
"applypatch-msg"
"commit-msg"
"post-applypatch"
"post-checkout"
"post-commit"
"post-merge"
"post-receive"
"post-rewrite"
"post-update"
"pre-applypatch"
"pre-auto-gc"
"pre-commit"
"pre-push"
"pre-rebase"
"pre-receive"
"prepare-commit-msg"
"update"
)
hook_types=(
"applypatch-msg"
"commit-msg"
"post-applypatch"
"post-checkout"
"post-commit"
"post-merge"
"post-receive"
"post-rewrite"
"post-update"
"pre-applypatch"
"pre-auto-gc"
"pre-commit"
"pre-push"
"pre-rebase"
"pre-receive"
"prepare-commit-msg"
"update"
)

repo_root=$(git rev-parse --show-toplevel)
hooks_dir="$repo_root/.git/hooks"
autohook_filename="$repo_root/hooks/autohook.sh"
autohook_path=$(realpath $autohook_filename)
for hook_type in "${hook_types[@]}"
do
hook_symlink="$hooks_dir/$hook_type"
ln -s $autohook_path $hook_symlink
done
repo_root=$(git rev-parse --show-toplevel)
hooks_dir="$repo_root/.git/hooks"
autohook_filename="$repo_root/hooks/autohook.sh"
autohook_path=$(realpath $autohook_filename)
for hook_type in "${hook_types[@]}"; do
hook_symlink="$hooks_dir/$hook_type"
ln -s $autohook_path $hook_symlink
done
}


main() {
calling_file=$(basename $0)
calling_file=$(basename $0)

if [[ $calling_file == "autohook.sh" ]]
then
command=$1
if [[ $command == "install" ]]
then
install
fi
else
repo_root=$(git rev-parse --show-toplevel)
hook_type=$calling_file
symlinks_dir="$repo_root/hooks/$hook_type"
files=("$symlinks_dir"/*)
number_of_symlinks="${#files[@]}"
tmpfile=$(mktemp)
if [[ $number_of_symlinks == 1 ]]
then
if [[ "$(basename ${files[0]})" == "*" ]]
then
number_of_symlinks=0
fi
fi
echo "Looking for $hook_type scripts to run...found $number_of_symlinks!"
if [[ $number_of_symlinks -gt 0 ]]
then
for file in "${files[@]}"
do
scriptname=$(basename $file)
echo "BEGIN $scriptname"
eval $file > $tmpfile
ret=$?
if [[ $ret -ne 0 ]]
then
cat $tmpfile
rm $tmpfile
exit $ret
fi
echo "FINISH $scriptname"
done
fi
rm $tmpfile
fi
if [[ $calling_file == "autohook.sh" ]]; then
command=$1
if [[ $command == "install" ]]; then
install
fi
else
repo_root=$(git rev-parse --show-toplevel)
hook_type=$calling_file
symlinks_dir="$repo_root/hooks/$hook_type"
files=("$symlinks_dir"/*)
number_of_symlinks="${#files[@]}"
tmpfile=$(mktemp)
if [[ $number_of_symlinks == 1 ]]; then
if [[ "$(basename ${files[0]})" == "*" ]]; then
number_of_symlinks=0
fi
fi
echo "Looking for $hook_type scripts to run...found $number_of_symlinks!"
if [[ $number_of_symlinks -gt 0 ]]; then
for file in "${files[@]}"; do
scriptname=$(basename $file)
echo "BEGIN $scriptname"
eval $file >$tmpfile
ret=$?
if [[ $ret -ne 0 ]]; then
cat $tmpfile
rm $tmpfile
exit $ret
fi
echo "FINISH $scriptname"
done
fi
rm $tmpfile
fi
}


main "$@"
18 changes: 8 additions & 10 deletions hooks/ci_format_check
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

root=$(git rev-parse --show-toplevel)
status=0
for file in $(find "${@}" -type f | grep -E "\.(c|cc|cpp|h|hh|hpp)\$" | grep -Ev "/third_party/")
do
filepath="$root/$file"
output=$(diff <(cat $filepath) <(clang-format -style=file -fallback-style=none $filepath))
if [ $? -ne 0 ]
then
echo -e "\nFile \e[31m\""$file"\"\e[39m is not compliant with the coding style"
echo "$output"
status=1
fi
for file in $(find "${@}" -type f | grep -E "\.(c|cc|cpp|h|hh|hpp)\$" | grep -Ev "/third_party/"); do
filepath="$root/$file"
output=$(diff <(cat $filepath) <(clang-format -style=file -fallback-style=none $filepath))
if [ $? -ne 0 ]; then
echo -e "\nFile \e[31m\""$file"\"\e[39m is not compliant with the coding style"
echo "$output"
status=1
fi
done
exit $status
14 changes: 8 additions & 6 deletions hooks/formatter
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash

for file in $(find $(git rev-parse --show-toplevel) | egrep "\.(c|cc|cpp|h|hh|hpp|m|mm)\$" | egrep -v -f hooks/format-exclude-list)
do
clang-format -style=file -fallback-style=none -i $file
for file in $(find $(git rev-parse --show-toplevel) | egrep "\.(c|cc|cpp|h|hh|hpp|m|mm)\$" | egrep -v "third_party|output_base"); do
clang-format -style=file -fallback-style=none -i $file
done

for file in $(find $(git rev-parse --show-toplevel) | egrep "\BUILD\$" | egrep -v -f hooks/format-exclude-list )
do
buildifier $file
for file in $(find $(git rev-parse --show-toplevel) | egrep "\BUILD\$" | egrep -v "third_party|output_base"); do
buildifier $file
done
buildifier WORKSPACE

for file in $(find $(git rev-parse --show-toplevel) | egrep "\.sh\$" | egrep -v "third_party|output_base"); do
shfmt -w $file
done
Loading

0 comments on commit 6582608

Please sign in to comment.