Skip to content

Commit

Permalink
Overhaul shell scripts (#144)
Browse files Browse the repository at this point in the history
Resolves #143 (Overhaul shell scripts).
  • Loading branch information
apcountryman authored Mar 28, 2024
1 parent bc5c248 commit f5cb74d
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- uses: actions/checkout@v4
- name: Analyze
shell: bash
run: ./ci/analyze --analyzer shellcheck
run: ./ci/analyze.sh --analyzer shellcheck
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ If the toolchain fails to locate tools, consult the documentation for CMake's

## Usage (Development)

This repository's Git `pre-commit` hook script is the simplest way to configure, build,
This repository's Git `pre-commit.sh` hook script is the simplest way to configure, build,
and test this project during development.
See the `pre-commit` script's help text for usage details.
See the `pre-commit.sh` script's help text for usage details.
```shell
./git/hooks/pre-commit --help
./git/hooks/pre-commit.sh --help
```

Additional checks, such as static analysis, are performed by this project's GitHub Actions
Expand All @@ -67,11 +67,11 @@ workflow.

## Git Hooks

To install this repository's Git hooks, run the `install` script located in the
To install this repository's Git hooks, run the `install.sh` script located in the
`git/hooks` directory.
See the `install` script's help text for usage details.
See the `install.sh` script's help text for usage details.
```
$ ./git/hooks/install --help
$ ./git/hooks/install.sh --help
```

## Code of Conduct
Expand Down
87 changes: 66 additions & 21 deletions ci/analyze → ci/analyze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,75 @@ function abort()
exit 1
}

function validate_script()
{
if ! shellcheck "$script"; then
abort
fi
}

function display_help_text()
{
echo "NAME"
echo " $mnemonic - Run a static analyzer against toolchain-avr-gcc."
echo "SYNOPSIS"
echo " $mnemonic --help"
echo " $mnemonic --version"
echo " $mnemonic --analyzer <analyzer>"
echo "OPTIONS"
echo " --analyzer <analyzer>"
echo " Specify the analyzer to run against toolchain-avr-gcc. The following"
echo " analyzers are supported:"
echo " shellcheck"
echo " --help"
echo " Display this help text."
echo " --version"
echo " Display the version of this script."
echo "EXAMPLES"
echo " $mnemonic --help"
echo " $mnemonic --version"
echo " $mnemonic --analyzer shellcheck"
local analyzer

printf "%b" \
"NAME\n" \
" $mnemonic - Ensure no static analysis errors are present.\n" \
"SYNOPSIS\n" \
" $mnemonic --help\n" \
" $mnemonic --version\n" \
" $mnemonic --analyzer <analyzer>\n" \
"OPTIONS\n" \
" --analyzer <analyzer>\n" \
" Specify the analyzer to run. The following analyzers are supported:\n" \
""

for analyzer in "${analyzers[@]}"; do
printf "%b" \
" $analyzer\n" \
""
done

printf "%b" \
" --help\n" \
" Display this help text.\n" \
" --version\n" \
" Display the version of this script.\n" \
"EXAMPLES\n" \
" $mnemonic --help\n" \
" $mnemonic --version\n" \
""

for analyzer in "${analyzers[@]}"; do
printf "%b" \
" $mnemonic --analyzer $analyzer\n" \
""
done
}

function display_version()
{
echo "$mnemonic, version $version"
}

function value_is_in_array()
{
local -r target_value="$1"; shift
local -r array=( "$@" )

local value
for value in "${array[@]}"; do
if [[ "$target_value" == "$value" ]]; then
return 0;
fi
done

return 1
}

function run_shellcheck()
{
local scripts; mapfile -t scripts < <( git -C "$repository" ls-files ':!:*.py' | xargs -r -d '\n' -I '{}' find "$repository/{}" -executable ); readonly scripts
local scripts; mapfile -t scripts < <( git -C "$repository" ls-files '*.sh' | xargs -r -d '\n' -I '{}' find "$repository/{}" ); readonly scripts

if ! shellcheck "${scripts[@]}"; then
abort
Expand All @@ -81,9 +119,16 @@ function main()
{
local -r script=$( readlink -f "$0" )
local -r mnemonic=$( basename "$script" )

validate_script

local -r repository=$( readlink -f "$( dirname "$script" )/.." )
local -r version=$( git -C "$repository" describe --match=none --always --dirty --broken )

local -r analyzers=(
shellcheck
)

while [[ "$#" -gt 0 ]]; do
local argument="$1"; shift

Expand All @@ -99,7 +144,7 @@ function main()

local -r analyzer="$1"; shift

if [[ "$analyzer" != "shellcheck" ]]; then
if ! value_is_in_array "$analyzer" "${analyzers[@]}"; then
abort "'$analyzer' is not a supported analyzer"
fi
;;
Expand Down
152 changes: 0 additions & 152 deletions git/hooks/install

This file was deleted.

Loading

0 comments on commit f5cb74d

Please sign in to comment.