Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Add release scripts (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored Apr 16, 2021
1 parent 24d8322 commit 57b77a9
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 0 deletions.
55 changes: 55 additions & 0 deletions scripts/cargo-for-all-lock-files.sh
Original file line number Diff line number Diff line change
@@ -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
155 changes: 155 additions & 0 deletions scripts/increment-cargo-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env bash
set -e

usage() {
cat <<EOF
usage: $0 [major|minor|patch|-preXYZ]
Increments the Cargo.toml version.
Default:
* Removes the prerelease tag if present, otherwise the minor version is incremented.
EOF
exit 0
}

here="$(dirname "$0")"
cd "$here"/..
source scripts/semver.sh
source scripts/read-cargo-variable.sh

ignores=(
.cache
.cargo
target
web3.js/examples
node_modules
)

not_paths=()
for ignore in "${ignores[@]}"; do
not_paths+=(-not -path "*/$ignore/*")
done

# shellcheck disable=2207
Cargo_tomls=($(find . -mindepth 2 -name Cargo.toml "${not_paths[@]}"))
# shellcheck disable=2207
markdownFiles=($(find . -name "*.md" "${not_paths[@]}"))

# Collect the name of all the internal crates
crates=()
for Cargo_toml in "${Cargo_tomls[@]}"; do
crates+=("$(readCargoVariable name "$Cargo_toml")")
done

# Read the current version
MAJOR=0
MINOR=0
PATCH=0
SPECIAL=""

semverParseInto "$(readCargoVariable version "${Cargo_tomls[0]}")" MAJOR MINOR PATCH SPECIAL
[[ -n $MAJOR ]] || usage

currentVersion="$MAJOR\.$MINOR\.$PATCH$SPECIAL"

bump=$1
if [[ -z $bump ]]; then
if [[ -n $SPECIAL ]]; then
bump=dropspecial # Remove prerelease tag
else
bump=minor
fi
fi
SPECIAL=""

# Figure out what to increment
case $bump in
patch)
PATCH=$((PATCH + 1))
;;
major)
MAJOR=$((MAJOR+ 1))
MINOR=0
PATCH=0
;;
minor)
MINOR=$((MINOR+ 1))
PATCH=0
;;
dropspecial)
;;
check)
badTomls=()
for Cargo_toml in "${Cargo_tomls[@]}"; do
if ! grep "^version *= *\"$currentVersion\"$" "$Cargo_toml" &>/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
14 changes: 14 additions & 0 deletions scripts/read-cargo-variable.sh
Original file line number Diff line number Diff line change
@@ -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
}
130 changes: 130 additions & 0 deletions scripts/semver.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 57b77a9

Please sign in to comment.