Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul shell scripts #47

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ git clone git@github.com:apcountryman/build-avr-gcc.git

## Usage

See the `build-avr-gcc` and `install-device-family-pack` scripts' help text for usage
details.
See the `build-avr-gcc.sh` and `install-device-family-pack.sh` scripts' help text for
usage details.
```shell
./build-avr-gcc --help
./install-device-family-packs --help
./build-avr-gcc.sh --help
./install-device-family-packs.sh --help
```

## Versioning
Expand All @@ -37,11 +37,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.
```shell
$ ./git/hooks/install --help
$ ./git/hooks/install.sh --help
```

## Code of Conduct
Expand Down
77 changes: 45 additions & 32 deletions build-avr-gcc → build-avr-gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,49 @@ function abort()
exit 1
}

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

function display_help_text()
{
echo "NAME"
echo " $mnemonic - Build avr-gcc."
echo "SYNOPSIS"
echo " $mnemonic --help"
echo " $mnemonic --version"
echo " $mnemonic --avr-gcc-version <avr-gcc-version>"
echo " --install-prefix <install-prefix> [--jobs <jobs>]"
echo "OPTIONS"
echo " --avr-gcc-version <avr-gcc-version>"
echo " Specify the version of avr-gcc to build. The following avr-gcc versions"
echo " are supported:"
echo " 8.3.0"
echo " 9.3.0"
echo " --help"
echo " Display this help text."
echo " --install-prefix <install-prefix>"
echo " Specify the install prefix. '<install-prefix>/bin' must be in '\$PATH'"
echo " prior to the execution of this script."
echo " --jobs <jobs>"
echo " Specify the number of build jobs to use when building. If the number of"
echo " jobs is not specified, 'nproc - 1' jobs will be used."
echo " --version"
echo " Display the version of this script."
echo "EXAMPLES"
echo " $mnemonic --help"
echo " $mnemonic --version"
echo " $mnemonic --avr-gcc-version 8.3.0 --install-prefix ~/bin/avr-gcc/8.3.0"
echo " $mnemonic --avr-gcc-version 8.3.0 --install-prefix ~/bin/avr-gcc/8.3.0"
echo " --jobs 1"
echo " $mnemonic --avr-gcc-version 9.3.0 --install-prefix ~/bin/avr-gcc/9.3.0"
echo " $mnemonic --avr-gcc-version 9.3.0 --install-prefix ~/bin/avr-gcc/9.3.0"
echo " --jobs 1"
printf "%b" \
"NAME\n" \
" $mnemonic - Build avr-gcc.\n" \
"SYNOPSIS\n" \
" $mnemonic --help\n" \
" $mnemonic --version\n" \
" $mnemonic --avr-gcc-version <avr-gcc-version>\n" \
" --install-prefix <install-prefix> [--jobs <jobs>]\n" \
"OPTIONS\n" \
" --avr-gcc-version <avr-gcc-version>\n" \
" Specify the version of avr-gcc to build. The following avr-gcc versions\n" \
" are supported:\n" \
" 8.3.0\n" \
" 9.3.0\n" \
" --help\n" \
" Display this help text.\n" \
" --install-prefix <install-prefix>\n" \
" Specify the install prefix. '<install-prefix>/bin' must be in '\$PATH'\n" \
" prior to the execution of this script.\n" \
" --jobs <jobs>\n" \
" Specify the number of build jobs to use when building. If the number of\n" \
" jobs is not specified, 'nproc - 1' jobs will be used.\n" \
" --version\n" \
" Display the version of this script.\n" \
"EXAMPLES\n" \
" $mnemonic --help\n" \
" $mnemonic --version\n" \
" $mnemonic --avr-gcc-version 8.3.0 --install-prefix ~/bin/avr-gcc/8.3.0\n" \
" $mnemonic --avr-gcc-version 8.3.0 --install-prefix ~/bin/avr-gcc/8.3.0\n" \
" --jobs 1\n" \
" $mnemonic --avr-gcc-version 9.3.0 --install-prefix ~/bin/avr-gcc/9.3.0\n" \
" $mnemonic --avr-gcc-version 9.3.0 --install-prefix ~/bin/avr-gcc/9.3.0\n" \
" --jobs 1\n" \
""
}

function display_version()
Expand Down Expand Up @@ -295,6 +304,7 @@ function build_avr_gcc()
"avr-libc"
)

local component
for component in "${components[@]}"; do
local component_build_directory="$build_directory/$component/build"

Expand All @@ -312,6 +322,9 @@ function main()
{
local -r script=$( readlink -f "$0" )
local -r mnemonic=$( basename "$script" )

validate_script

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

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 build-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 the build-avr-gcc repository. The"
echo " following 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 | 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
Loading