Skip to content

Commit

Permalink
Improve check.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann committed Sep 11, 2024
1 parent fd20bb9 commit 8a8a4df
Showing 1 changed file with 127 additions and 15 deletions.
142 changes: 127 additions & 15 deletions scripts/check.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
shopt -s inherit_errexit

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "${SCRIPT_DIR}/../"
Expand Down Expand Up @@ -52,11 +53,20 @@ done
# Check for 'docker' and abort if it is not running.
docker info > /dev/null 2>&1 || { echo >&2 "ERROR - requires 'docker', please start docker and try again."; exit 1; }

# Check for 'cargo' and abort if it is not available.
cargo -V > /dev/null 2>&1 || { echo >&2 "ERROR - requires 'cargo' for compile the binaries"; exit 1; }
# Check for 'cargo deny' and abort if it is not available.
cargo deny -V > /dev/null 2>&1 || { echo >&2 "ERROR - 'cargo deny' not found, install using 'cargo install --locked cargo-deny'"; exit 1; }

# Check for 'awk' command and abort if it is not available.
command -v awk > /dev/null 2>&1 || { echo >&2 "ERROR - requires 'awk' for verify semantic versioning"; exit 1; }

# Check for 'grep' command and abort if it is not available.
command -v grep > /dev/null 2>&1 || { echo >&2 "ERROR - this script requires 'grep' internally"; exit 1; }

# Check for 'head' command and abort if it is not available.
command -v head > /dev/null 2>&1 || { echo >&2 "ERROR - this script requires 'head' internally"; exit 1; }

# Check for 'solc' and abort if it is not available.
solc --version > /dev/null 2>&1 || { echo >&2 "ERROR - requires 'solc >= 0.8.20' for compile the contracts"; exit 1; }
solc --version > /dev/null 2>&1 || { echo >&2 "ERROR - requires 'solc >= 0.8.25' for compile the contracts"; exit 1; }

# Check for 'cargo deny' and abort if it is not available.
cargo deny -V > /dev/null 2>&1 || { echo >&2 "ERROR - 'cargo deny' not found, install using 'cargo install --locked cargo-deny'"; exit 1; }
Expand All @@ -82,6 +92,119 @@ if test -t 1 && command -v tput >/dev/null 2>&1; then
fi
: "${ncols:=72}"

# shellcheck disable=SC2001
function semver2int() {
local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
local major;
local minor;
local patch;
major="$(echo "$1" | sed -e "s#${RE}#\1#")"
minor="$(echo "$1" | sed -e "s#${RE}#\2#")"
patch="$(echo "$1" | sed -e "s#${RE}#\3#")"
# Convert semver to decimal
major=$((major * 1000000000))
minor=$((minor * 1000000))
patch=$((patch * 1))
echo $((major + minor + patch))
}

# shellcheck disable=SC2001
function parse_semver() {
local version="$1"
if [[ "${version}" =~ ^[0-9]+\.[0-9]+$ ]]; then
version="${version}.0"
fi
local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
local major;
local minor;
local patch;
major="$(echo "${version}" | sed -e "s#${RE}#\1#")"
minor="$(echo "${version}" | sed -e "s#${RE}#\2#")"
patch="$(echo "${version}" | sed -e "s#${RE}#\3#")"
echo "${major}.${minor}.${patch}"
}

# Check sematic version
function checkSemver() {
local name=''
local actual
local expect
local lhs
local rhs
if [[ "$#" -gt 2 ]]; then
name="${bold_color}${1}${reset_color}${bold_color}:${reset_color} "
shift 1
fi
actual="$(parse_semver "${1}")"
expect="$(parse_semver "${2}")"
lhs="$(semver2int "${actual}")"
rhs="$(semver2int "${expect}")"
if [[ "${lhs}" -lt "${rhs}" ]]; then
printf "%s${error_color}%s < %s ${reset_color}\n" "${name}" "${actual}" "${expect}"
# print suggestion if provided
if [[ -n "${3}" ]]; then
printf "%s\n" "${3}"
fi
exit 1
fi
}

# Check `solc` version
solc_version="$(solc --version)"
solc_version="$(tr '\n' ' ' <<< "${solc_version}")"
solc_version="$(awk -FVersion: '{ print $NF }' <<< "${solc_version}")"
solc_version="$(awk -F. '{print $1 "." $2 "." $3 }' <<< "${solc_version}")"
# shellcheck disable=SC2001
solc_version="$(sed 's/[^0-9\.]*//g' <<< "${solc_version}")"
checkSemver 'solc' \
"${solc_version}" \
'0.8.25' \
"install svm following this instructions: ${bold_color}https://github.com/alloy-rs/svm-rs${reset_color}"

# Check `rustc` version
rustc_version="$(rustc --version)"
rustc_version="$(awk '{print $2}' <<< "${rustc_version}")"
checkSemver 'rustc' \
"${rustc_version}" \
'1.79.0' \
"upgrade rustc with command: ${warn_color}rustup update stable${reset_color}"

# Check `cargo-deny` version
cargo_deny_version="$(cargo deny --version)"
cargo_deny_version="$(awk '{print $2}' <<< "${cargo_deny_version}")"
checkSemver 'cargo deny' \
"${cargo_deny_version}" \
'0.14.24' \
"upgrade ${bold_color}cargo-deny${reset_color} with command: ${warn_color}cargo install cargo-deny${reset_color}"

# Check `dprint` version
dprint_version="$(dprint --version)"
dprint_version="$(awk '{print $2}' <<< "${dprint_version}")"
checkSemver 'dprint' \
"${dprint_version}" \
'0.47.1' \
"upgrade ${bold_color}dprint${reset_color} with command: ${warn_color}cargo install dprint${reset_color}"

# Check `shellcheck` version
shellcheck_version="$(shellcheck --version)"
shellcheck_version="$(grep 'version:' <<< "${shellcheck_version}")"
shellcheck_version="$(awk '{print $2}' <<< "${shellcheck_version}")"
# "$(shellcheck --version | grep 'version:' | awk '{print $2}')"
checkSemver 'shellcheck' \
"${shellcheck_version}" \
'0.9.0' \
"upgrade ${bold_color}shellcheck${reset_color} following the instructions at: ${warn_color}https://github.com/koalaman/shellcheck${reset_color}"

# Check `docker api` version
docker_version="$(docker version)"
docker_version="$(grep 'API version:' <<< "${docker_version}")"
docker_version="$(head -1 <<< "${docker_version}")"
docker_version="$(awk '{ print $3 }' <<< "${docker_version}")"
checkSemver 'docker api' \
"${docker_version}" \
'1.42.0' \
"the minimal docker api version supported by ${bold_color}rosetta-docker${reset_color} is ${warn_color}1.42${reset_color}"

# Print the command in yellow, and the output in red
print_failed_command() {
printf >&2 "${bold_color}${warn_color}%s${reset_color}\n${error_color}%s${reset_color}\n" "$1" "$2"
Expand Down Expand Up @@ -109,7 +232,6 @@ if [[ "${RUN_FIX}" == "1" ]]; then
fi

if [[ "${CHECK_FORMAT}" == "1" ]]; then
exec_cmd 'update cargo deny' 'cargo install --locked cargo-deny --force'
exec_cmd 'shellcheck' 'shellcheck --enable=all --severity=style ./scripts/*.sh'
exec_cmd 'cargo fmt' 'cargo +nightly fmt --all -- --check'
exec_cmd 'dprint check' 'dprint check'
Expand Down Expand Up @@ -183,16 +305,6 @@ if [[ "${RUN_TESTS}" == "1" ]]; then
--exclude rosetta-client \
--exclude rosetta-testing-arbitrum \
--exclude rosetta-testing-binance
# cargo test --locked --all-features --workspace
#exec_cmd 'cargo test' 'cargo test --locked --all-features --workspace'
exec_cmd 'cleanup docker' "${SCRIPT_DIR}/reset_docker.sh"
fi
#exec_cmd 'reset docker' "${SCRIPT_DIR}/reset_docker.sh"
#cargo test --locked --all-features --workspace


#exec_cmd 'cargo test' 'cargo test --locked --all-features --workspace'

#echo "Running ${SCRIPT_DIR}/build_connectors.sh"
#"${SCRIPT_DIR}/build_connectors.sh"
#
#cargo test --locked --all-features --workspace

0 comments on commit 8a8a4df

Please sign in to comment.