Skip to content

Commit 06e3761

Browse files
committed
Add cargo config / scripts
1 parent 241ff09 commit 06e3761

File tree

10 files changed

+186
-2
lines changed

10 files changed

+186
-2
lines changed

.cargo/config.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[alias]
2+
unit-test = "test --lib --features full-validation"
3+
integration = "run-script integration"
4+
gen-schema = "run-script gen-schema"
5+
optimize = "run-script optimize"
6+
precommit = "run-script precommit"
7+
lint = "run-script lint"
8+
fix-lint = "run-script fix-lint"

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ authors = ["Babylon Labs Ltd. <[email protected]>"]
1111
publish = false
1212

1313
[workspace.metadata.scripts]
14-
gen-proto = "./scripts/protocgen.sh"
15-
gen-data = "go run datagen/*.go"
1614
gen-schema = "./scripts/schema.sh"
1715
optimize = "./scripts/optimizer.sh"
1816
precommit = "./scripts/precommit.sh"

scripts/fix-lint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
cargo fmt --all
4+
cargo clippy --all-targets --fix --allow-dirty --allow-staged

scripts/integration_test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
./scripts/optimizer.sh
4+
5+
cargo test --test integration

scripts/lint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
cargo fmt --all -- --check
4+
cargo check
5+
cargo clippy --all-targets -- -D warnings

scripts/optimizer.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
DOCKER=$(which docker)
4+
CUR_DIR=$(pwd)
5+
CUR_BASENAME=$(basename $CUR_DIR)
6+
7+
OPTIMIZER_IMAGE_NAME="babylonlabs/optimizer"
8+
9+
$DOCKER run --rm -v "$CUR_DIR":/code \
10+
--mount type=volume,source="${CUR_BASENAME}_cache",target=/target \
11+
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
12+
$OPTIMIZER_IMAGE_NAME

scripts/precommit.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
cargo fix-lint
4+
cargo gen-schema
5+
# TODO: add cargo gen-proto

scripts/schema.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
4+
for CONTRACT in ./contracts/*/
5+
do
6+
(cd $CONTRACT && cargo schema)
7+
done

scripts/set_version.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
set -o errexit -o nounset -o pipefail
3+
command -v shellcheck >/dev/null && shellcheck "$0"
4+
5+
function print_usage() {
6+
echo "Usage: $0 [-h|--help] <new_version>"
7+
echo "e.g.: $0 0.8.0"
8+
}
9+
10+
if [ "$#" -ne 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]
11+
then
12+
print_usage
13+
exit 1
14+
fi
15+
16+
# Check repo
17+
SCRIPT_DIR="$(realpath "$(dirname "$0")")"
18+
if [[ "$(realpath "$SCRIPT_DIR/..")" != "$(pwd)" ]]; then
19+
echo "Script must be called from the repo root"
20+
exit 2
21+
fi
22+
23+
# Ensure repo is not dirty
24+
CHANGES_IN_REPO=$(git status --porcelain --untracked-files=no)
25+
if [[ -n "$CHANGES_IN_REPO" ]]; then
26+
echo "Repository is dirty. Showing 'git status' and 'git --no-pager diff' for debugging now:"
27+
git status && git --no-pager diff
28+
exit 3
29+
fi
30+
31+
CARGO_TOML="./Cargo.toml"
32+
33+
NEW="$1"
34+
OLD=$(sed -n -e 's/^version[[:space:]]*=[[:space:]]*"\(.*\)"/\1/p' "$CARGO_TOML")
35+
echo "Updating old version $OLD to new version $NEW ..."
36+
37+
FILES_MODIFIED=()
38+
39+
sed -i -e "s/^version\([[:space:]]*\)=[[:space:]]*\"$OLD\"/version\1= \"$NEW\"/" "$CARGO_TOML"
40+
FILES_MODIFIED+=("$CARGO_TOML")
41+
42+
cargo build
43+
FILES_MODIFIED+=("Cargo.lock")
44+
45+
for CONTRACT in ./contracts/*/
46+
do
47+
(cd "$CONTRACT" && cargo schema)
48+
FILES_MODIFIED+=("$CONTRACT"/schema/)
49+
done
50+
51+
echo "Staging ${FILES_MODIFIED[*]} ..."
52+
git add "${FILES_MODIFIED[@]}"
53+
git commit -m "Set version: $NEW"

scripts/update_changelog.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
# This is a wrapper around `github_changelog_generator` (https://github.com/github-changelog-generator)
3+
# to simplify / automate updating of the CHANGELOG.md file.
4+
#
5+
# Originally developed for CosmWasm cw_plus (https://github.com/CosmWasm/cw-plus) repository.
6+
set -o errexit -o pipefail
7+
8+
ORIGINAL_OPTS=$*
9+
# Requires getopt from util-linux 2.37.4 (brew install gnu-getopt on Mac)
10+
OPTS=$(getopt -l "help,since-tag:,upcoming-tag:,full,token:" -o "hu:ft" -- "$@") || exit 1
11+
12+
function print_usage() {
13+
echo -e "Usage: $0 [-h|--help] [-f|--full] [--since-tag <tag>] [-u|--upcoming-tag] <tag> [-t|--token <token>]
14+
-h, --help Display help
15+
-f, --full Process changes since the beginning (by default: since latest git version tag)
16+
--since-tag <tag> Process changes since git version tag <tag> (by default: since latest git version tag)
17+
-u, --upcoming-tag <tag> Add a <tag> title in CHANGELOG for the new changes
18+
--token <token> Pass changelog github token <token>"
19+
}
20+
21+
function remove_opt() {
22+
ORIGINAL_OPTS=$(echo "$ORIGINAL_OPTS" | sed "s/\\B$1\\b//")
23+
}
24+
25+
eval set -- "$OPTS"
26+
while true
27+
do
28+
case $1 in
29+
-h|--help)
30+
print_usage
31+
exit 0
32+
;;
33+
--since-tag)
34+
shift
35+
TAG="$1"
36+
;;
37+
-f|--full)
38+
TAG="<FULL>"
39+
remove_opt $1
40+
;;
41+
-u|--upcoming-tag)
42+
remove_opt $1
43+
shift
44+
UPCOMING_TAG="$1"
45+
remove_opt $1
46+
;;
47+
--)
48+
shift
49+
break
50+
;;
51+
esac
52+
shift
53+
done
54+
55+
# Get user and repo from ./.git/config
56+
ORIGIN_URL=$(git config --local remote.origin.url)
57+
GITHUB_USER=$(echo $ORIGIN_URL | sed -n 's#.*:\([^\/]*\)\/.*#\1#p')
58+
echo "Github user: $GITHUB_USER"
59+
GITHUB_REPO=$(echo $ORIGIN_URL | sed -n 's#.*/\(.*\)\.git#\1#p')
60+
echo "Github repo: $GITHUB_REPO"
61+
62+
if [ -z "$TAG" ]
63+
then
64+
# Use latest git version tag
65+
TAG=$(git tag --sort=creatordate | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | tail -1)
66+
ORIGINAL_OPTS="$ORIGINAL_OPTS --since-tag $TAG"
67+
fi
68+
69+
echo "Git version tag: $TAG"
70+
71+
cp CHANGELOG.md /tmp/CHANGELOG.md.$$
72+
# Consolidate tag for matching changelog entries
73+
TAG=$(echo "$TAG" | sed -e 's/-[^-]*$//')
74+
echo "Consolidated tag: $TAG"
75+
sed -i -n "/^## \\[${TAG}[^]]*\\]/,\$p" CHANGELOG.md
76+
77+
echo github_changelog_generator -u $GITHUB_USER -p $GITHUB_REPO --base CHANGELOG.md $ORIGINAL_OPTS
78+
github_changelog_generator -u $GITHUB_USER -p $GITHUB_REPO --base CHANGELOG.md $ORIGINAL_OPTS || cp /tmp/CHANGELOG.md.$$ CHANGELOG.md
79+
80+
if [ -n "$UPCOMING_TAG" ]
81+
then
82+
# Add "upcoming" version tag
83+
TODAY=$(date "+%Y-%m-%d")
84+
sed -i "s+\[Full Changelog\](https://github.com/$GITHUB_USER/$GITHUB_REPO/compare/\(.*\)\.\.\.HEAD)+[Full Changelog](https://github.com/$GITHUB_USER/$GITHUB_REPO/compare/$UPCOMING_TAG...HEAD)\n\n## [$UPCOMING_TAG](https://github.com/$GITHUB_USER/$GITHUB_REPO/tree/$UPCOMING_TAG) ($TODAY)\n\n[Full Changelog](https://github.com/$GITHUB_USER/$GITHUB_REPO/compare/\1...$UPCOMING_TAG)+" CHANGELOG.md
85+
fi
86+
87+
rm -f /tmp/CHANGELOG.md.$$

0 commit comments

Comments
 (0)