diff --git a/scripts/cargo-for-all-lock-files.sh b/scripts/cargo-for-all-lock-files.sh new file mode 100755 index 00000000..6aefae12 --- /dev/null +++ b/scripts/cargo-for-all-lock-files.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +here="$(dirname "$0")" +cargo="$(readlink -f "${here}/../cargo")" + +set -e + +shifted_args=() +while [[ -n $1 ]]; do + if [[ $1 = -- ]]; then + escape_marker=found + shift + break + elif [[ $1 = "--ignore-exit-code" ]]; then + ignore=1 + shift + else + shifted_args+=("$1") + shift + fi +done + +# When "--" appear at the first and shifted_args is empty, consume it here +# to unambiguously pass and use any other "--" for cargo +if [[ -n $escape_marker && ${#shifted_args[@]} -gt 0 ]]; then + files="${shifted_args[*]}" + for file in $files; do + if [[ $file = "${file%Cargo.lock}" ]]; then + echo "$0: unrecognizable as Cargo.lock path (prepend \"--\"?): $file" >&2 + exit 1 + fi + done + shifted_args=() +else + files="$(git ls-files :**Cargo.lock)" +fi + +for lock_file in $files; do + if [[ -n $CI ]]; then + echo "--- [$lock_file]: cargo " "${shifted_args[@]}" "$@" + fi + + if (set -x && cd "$(dirname "$lock_file")" && cargo "${shifted_args[@]}" "$@"); then + # noop + true + else + failed_exit_code=$? + if [[ -n $ignore ]]; then + echo "$0: WARN: ignoring last cargo command failed exit code as requested:" $failed_exit_code + true + else + exit $failed_exit_code + fi + fi +done diff --git a/scripts/increment-cargo-version.sh b/scripts/increment-cargo-version.sh new file mode 100755 index 00000000..92889baf --- /dev/null +++ b/scripts/increment-cargo-version.sh @@ -0,0 +1,155 @@ +#!/usr/bin/env bash +set -e + +usage() { + cat </dev/null; then + badTomls+=("$Cargo_toml") + fi + done + if [[ ${#badTomls[@]} -ne 0 ]]; then + echo "Error: Incorrect crate version specified in: ${badTomls[*]}" + exit 1 + fi + exit 0 + ;; +-*) + if [[ $1 =~ ^-[A-Za-z0-9]*$ ]]; then + SPECIAL="$1" + else + echo "Error: Unsupported characters found in $1" + exit 1 + fi + ;; +*) + echo "Error: unknown argument: $1" + usage + ;; +esac + +# Version bumps should occur in their own commit. Disallow bumping version +# in dirty working trees. Gate after arg parsing to prevent breaking the +# `check` subcommand. +( + set +e + if ! git diff --exit-code; then + echo -e "\nError: Working tree is dirty. Commit or discard changes before bumping version." 1>&2 + exit 1 + fi +) + +newVersion="$MAJOR.$MINOR.$PATCH$SPECIAL" + +# Update all the Cargo.toml files +for Cargo_toml in "${Cargo_tomls[@]}"; do + # Set new crate version + ( + set -x + sed -i "$Cargo_toml" -e "0,/^version =/{s/^version = \"[^\"]*\"$/version = \"$newVersion\"/}" + ) + + # Fix up the version references to other internal crates + for crate in "${crates[@]}"; do + ( + set -x + sed -i "$Cargo_toml" -e " + s/^$crate = { *path *= *\"\([^\"]*\)\" *, *version *= *\"[^\"]*\"\(.*\)} *\$/$crate = \{ path = \"\1\", version = \"=$newVersion\"\2\}/ + " + ) + done +done + +# Update all the documentation references +for file in "${markdownFiles[@]}"; do + # Set new crate version + ( + set -x + sed -i "$file" -e "s/$currentVersion/$newVersion/g" + ) +done + +# Update cargo lock files +scripts/cargo-for-all-lock-files.sh tree + +echo "$currentVersion -> $newVersion" + +exit 0 diff --git a/scripts/read-cargo-variable.sh b/scripts/read-cargo-variable.sh new file mode 100644 index 00000000..7f6c9518 --- /dev/null +++ b/scripts/read-cargo-variable.sh @@ -0,0 +1,14 @@ +# source this file + +readCargoVariable() { + declare variable="$1" + declare Cargo_toml="$2" + + while read -r name equals value _; do + if [[ $name = "$variable" && $equals = = ]]; then + echo "${value//\"/}" + return + fi + done < <(cat "$Cargo_toml") + echo "Unable to locate $variable in $Cargo_toml" 1>&2 +} diff --git a/scripts/semver.sh b/scripts/semver.sh new file mode 100755 index 00000000..e5237a4e --- /dev/null +++ b/scripts/semver.sh @@ -0,0 +1,130 @@ +#!/usr/bin/env sh + +function semverParseInto() { + local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' + #MAJOR + eval $2=`echo $1 | sed -e "s#$RE#\1#"` + #MINOR + eval $3=`echo $1 | sed -e "s#$RE#\2#"` + #MINOR + eval $4=`echo $1 | sed -e "s#$RE#\3#"` + #SPECIAL + eval $5=`echo $1 | sed -e "s#$RE#\4#"` +} + +function semverEQ() { + local MAJOR_A=0 + local MINOR_A=0 + local PATCH_A=0 + local SPECIAL_A=0 + + local MAJOR_B=0 + local MINOR_B=0 + local PATCH_B=0 + local SPECIAL_B=0 + + semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A + semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B + + if [ $MAJOR_A -ne $MAJOR_B ]; then + return 1 + fi + + if [ $MINOR_A -ne $MINOR_B ]; then + return 1 + fi + + if [ $PATCH_A -ne $PATCH_B ]; then + return 1 + fi + + if [[ "_$SPECIAL_A" != "_$SPECIAL_B" ]]; then + return 1 + fi + + + return 0 + +} + +function semverLT() { + local MAJOR_A=0 + local MINOR_A=0 + local PATCH_A=0 + local SPECIAL_A=0 + + local MAJOR_B=0 + local MINOR_B=0 + local PATCH_B=0 + local SPECIAL_B=0 + + semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A + semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B + + if [ $MAJOR_A -lt $MAJOR_B ]; then + return 0 + fi + + if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -lt $MINOR_B ]]; then + return 0 + fi + + if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -le $MINOR_B && $PATCH_A -lt $PATCH_B ]]; then + return 0 + fi + + if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then + return 1 + fi + if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" != "_" ]] ; then + return 1 + fi + if [[ "_$SPECIAL_A" != "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then + return 0 + fi + + if [[ "_$SPECIAL_A" < "_$SPECIAL_B" ]]; then + return 0 + fi + + return 1 + +} + +function semverGT() { + semverEQ $1 $2 + local EQ=$? + + semverLT $1 $2 + local LT=$? + + if [ $EQ -ne 0 ] && [ $LT -ne 0 ]; then + return 0 + else + return 1 + fi +} + +if [ "___semver.sh" == "___`basename $0`" ]; then + +MAJOR=0 +MINOR=0 +PATCH=0 +SPECIAL="" + +semverParseInto $1 MAJOR MINOR PATCH SPECIAL +echo "$1 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + +semverParseInto $2 MAJOR MINOR PATCH SPECIAL +echo "$2 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + +semverEQ $1 $2 +echo "$1 == $2 -> $?." + +semverLT $1 $2 +echo "$1 < $2 -> $?." + +semverGT $1 $2 +echo "$1 > $2 -> $?." + +fi