Skip to content

Commit 008d16f

Browse files
authored
Merge pull request #2137 from input-output-hk/ensemble/add-js-packages-to-upgrade-version-scripts
Also bump versions of js packages in upgrade version script
2 parents ad40680 + 2fab021 commit 008d16f

File tree

4 files changed

+228
-123
lines changed

4 files changed

+228
-123
lines changed

docs/devbook/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This page gathers the available guides and tools for Mithril nodes developers.
44

55
# Guides
66

7-
| Operation | Location | Description |
8-
| --------------------------------------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
9-
| **Upgrade crates and openapi versions** | [upgrade-crates-and-openapi-versions](./upgrade-crates-and-openapi-versions/README.md) | Script to easily upgrade crates and openapi versions before merging a pull request |
10-
| **Upgrade the repository dependencies** | [upgrade-repository-dependencies](./upgrade-repository-dependencies/README.md) | Upgrade the project's dependencies in the repository |
7+
| Operation | Location | Description |
8+
| --------------------------------------- | ------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------- |
9+
| **Bump versions** | [bump-versions](bump-versions/README.md) | Script to easily bump crates, js packages, and openapi versions before merging a pull request |
10+
| **Upgrade the repository dependencies** | [upgrade-repository-dependencies](./upgrade-repository-dependencies/README.md) | Upgrade the project's dependencies in the repository |

docs/devbook/upgrade-crates-and-openapi-versions/README.md renamed to docs/devbook/bump-versions/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Upgrade the crates and openapi versions before merging a pull request
1+
# Bump the crates, js packages, and openapi versions before merging a pull request
22

33
## Introduction
44

5-
This devbook provides a script that allows to automatically upgrade the crates and Open API versions in the project.
5+
This devbook provides a script that allows to automatically bump the crates, js packages, and Open API versions in the project.
66

7-
Only the crates and Open API specifications with changes on the branch compared to `origin/main` are upgraded.
7+
Only the crates, js packages, and Open API specifications with changes on the branch compared to `origin/main` are bumped.
88

99
## Prerequisites
1010

@@ -24,7 +24,7 @@ cargo install cargo-get
2424
Just run the script without argument, by default no changes are made to the project.
2525

2626
```shell
27-
. ./docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh
27+
. ./docs/devbook/bump-versions/bump_versions.sh
2828
```
2929

3030
### Run
@@ -34,18 +34,18 @@ Just run the script without argument, by default no changes are made to the proj
3434
>
3535
> This means that running the script more than once will bump the versions again.
3636
37-
Run the script with the `--run` argument to bump the crates and openapi versions.
37+
Run the script with the `--run` argument to bump the crates, js packages, and openapi versions.
3838

3939
The script will output a preformatted commit message that can be used to create a commit when it completes.
4040

4141
```shell
42-
. ./docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh --run
42+
. ./docs/devbook/bump-versions/bump_versions.sh --run
4343
```
4444

4545
If you want the script to do the commit for you, add the `--commit` argument.
4646

4747
```shell
48-
. ./docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh --run --commit
48+
. ./docs/devbook/bump-versions/bump_versions.sh --run --commit
4949
```
5050

5151
> [!NOTE]
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#!/usr/bin/env bash
2+
set +a -u -o pipefail
3+
4+
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
5+
6+
display_help() {
7+
echo "Check crates, js packages, and openapi changes against 'origin/main' and update their versions"
8+
echo
9+
echo "By default, no changes are made (dry-run mode), use '--run' to apply the changes."
10+
echo "At the end of the script, the commit message to used is displayed, use '--commit' to commit the changes."
11+
echo
12+
echo "Usage: $0 [OPTIONS]"
13+
echo
14+
echo "Options:"
15+
echo " --run: to apply the changes (default is dry-run)"
16+
echo " --commit: to commit the changes"
17+
echo
18+
echo "Prerequisites:"
19+
echo " 'cargo-get' needs to be installed ('cargo install cargo-get')."
20+
echo
21+
exit 0;
22+
}
23+
24+
check_requirements() {
25+
cargo get --version 2> /dev/null 1> /dev/null ||
26+
error "It seems 'cargo-get' is not installed or not in the path (to install run: 'cargo install cargo-get').";
27+
}
28+
29+
error() {
30+
echo "ERROR: $1";
31+
exit 1;
32+
}
33+
34+
readonly ORANGE="\e[1;33m"
35+
readonly GREEN="\e[1;32m"
36+
readonly RESET="\e[0m"
37+
38+
readonly OPEN_API_FILE=openapi.yaml
39+
declare OPEN_API_UPDATE=""
40+
declare OPEN_API_UPDATE_MESSAGE=""
41+
42+
update_crate_versions() {
43+
# NOTE
44+
# `cargo get workspace.members` display the list of path to crates in the workspace.
45+
# for the `cargo set-version` command, we need the name of the module (last element of the path).
46+
#
47+
local -r dry_run=$1
48+
local -r -n files_modify=$2
49+
local -r -a members="$(cargo get workspace.members --delimiter " ")"
50+
local -i nb_files_modify=0
51+
local package_name
52+
53+
local cargo_options=""
54+
if [ true = "$dry_run" ]
55+
then
56+
cargo_options=--dry-run
57+
fi
58+
59+
for member in $members
60+
do
61+
nb_files_modify=$(echo "$files_modify" | grep -c "^$member/")
62+
if [[ $nb_files_modify -gt 0 ]]
63+
then
64+
package_name=${member##*/}
65+
cargo set-version $cargo_options --package "${package_name##*/}" --bump patch
66+
fi
67+
68+
done
69+
}
70+
71+
update_package_json_versions() {
72+
local -r dry_run=$1
73+
local -r -n files_modify=$2
74+
local -r -n package_json_files=$3
75+
local -r -a members=$(echo "$package_json_files" | xargs dirname)
76+
local -i nb_files_modify=0
77+
78+
local package_name version_line patch_number next_patch_number new_version must_update_package_locks
79+
must_update_package_locks=false
80+
81+
for member in $members
82+
do
83+
nb_files_modify=$(echo "$files_modify" | grep -c "^$member/")
84+
if [[ $nb_files_modify -gt 0 ]]
85+
then
86+
if [[ $member == "mithril-client-wasm" ]]
87+
then
88+
must_update_package_locks=true
89+
fi
90+
91+
version_line="$(grep -E "\"version\": \"[0-9]+\.[0-9]+\.[0-9]+\"" "$member/package.json" | sed "s/[\",]//g")"
92+
patch_number=$(echo "$version_line" | cut -d . -f 3)
93+
next_patch_number=$((patch_number + 1))
94+
new_version=$(echo "$version_line" | cut -d . -f 1-2).$next_patch_number
95+
echo -e " ${GREEN}Upgrading${RESET} [js] $member from ${version_line##*version: } to ${new_version##*version: }"
96+
97+
if [ true = "$dry_run" ]
98+
then
99+
echo -e "${ORANGE}warning${RESET}: aborting $member update due to dry run"
100+
else
101+
package_name=${member##*/}
102+
pushd "$member" > /dev/null || exit
103+
npm --no-git-tag-version version patch 1>/dev/null
104+
popd > /dev/null || exit
105+
fi
106+
fi
107+
done
108+
109+
if [ true = "$must_update_package_locks" ]
110+
then
111+
echo -e "${ORANGE}mithril-client-wasm version changed${RESET}: updating package-lock.json files to reflect the new version"
112+
113+
for member in $members
114+
do
115+
if [[ false = "$dry_run" && -e "$member/package-lock.json" ]]
116+
then
117+
package_name=${member##*/}
118+
pushd "$member" > /dev/null || exit
119+
npm install 1>/dev/null
120+
popd > /dev/null || exit
121+
fi
122+
done
123+
fi
124+
}
125+
126+
update_openapi_version() {
127+
local -r dry_run=$1
128+
local -r version_line=$(grep -E "version: [0-9]+\.[0-9]+\.[0-9]+" $OPEN_API_FILE)
129+
local -r patch_number=$(echo "$version_line" | cut -d . -f 3)
130+
local -r next_patch_number=$((patch_number + 1))
131+
local -r new_version=$(echo "$version_line" | cut -d . -f 1-2).$next_patch_number
132+
133+
echo -e " ${GREEN}Upgrading${RESET} $OPEN_API_FILE from ${version_line##*version: } to ${new_version##*version: }"
134+
if [ true = "$dry_run" ]
135+
then
136+
echo -e "${ORANGE}warning${RESET}: aborting $OPEN_API_FILE update due to dry run"
137+
else
138+
sed -i "s/$version_line/$new_version/g" $OPEN_API_FILE
139+
fi
140+
OPEN_API_UPDATE="\n* $OPEN_API_FILE from \`${version_line##*version: }\` to \`${new_version##*version: }\`"
141+
OPEN_API_UPDATE_MESSAGE=" and \`$OPEN_API_FILE\` version"
142+
}
143+
144+
################
145+
check_requirements
146+
147+
declare DRY_RUN=true
148+
declare COMMIT=false
149+
readonly COMMIT_REF="HEAD"
150+
while [[ "$#" -gt 0 ]]; do
151+
case $1 in
152+
-h|--help) display_help ;;
153+
--run) DRY_RUN=false ;;
154+
--commit) COMMIT=true ;;
155+
esac
156+
shift
157+
done
158+
159+
FILES_MODIFY="$(git diff "$COMMIT_REF" --name-only origin/main)"
160+
readonly -a FILES_MODIFY
161+
PACKAGE_JSON_FILES="$(find -- * -name package.json | grep -v -e "/node_modules/" -e "/pkg/" -e "/dist/" -e "/.next/" -e "docs/website/")"
162+
readonly -a PACKAGE_JSON_FILES
163+
164+
update_crate_versions $DRY_RUN FILES_MODIFY
165+
update_package_json_versions $DRY_RUN FILES_MODIFY PACKAGE_JSON_FILES
166+
167+
if [ "$(echo "${FILES_MODIFY[@]}" | grep -xc "$OPEN_API_FILE")" -gt 0 ]
168+
then
169+
update_openapi_version $DRY_RUN
170+
fi
171+
172+
if [ true = $DRY_RUN ]
173+
then
174+
echo -e "${ORANGE}warning${RESET}: script is run in dry mode. To apply the changes, run ${GREEN}$0 --run${RESET}"
175+
else
176+
# NOTE
177+
# The goal is to transform individual `git diff` formatted output to a list item with the crate or package name and the version change.
178+
# ie, transform this:
179+
# ```
180+
# diff --git a/mithril-explorer/package.json b/mithril-explorer/package.json
181+
# index 20f2e0030..e0e680d52 100644
182+
# --- a/mithril-explorer/package.json
183+
# +++ b/mithril-explorer/package.json
184+
# @@ -1,6 +1,6 @@
185+
# {
186+
# "name": "mithril-explorer",
187+
# - "version": "0.7.19",
188+
# + "version": "0.7.20",
189+
# "private": true,
190+
# "scripts": {
191+
# "dev": "next dev",
192+
# ```
193+
# to this:
194+
# `* [js] mithril-explorer from `0.7.18` to `0.7.19``
195+
#
196+
UPDATED_CRATES="$(find . -name Cargo.toml -exec git diff "$COMMIT_REF" {} + | grep -E "^[\+\-]version = \"[0-9\.]+\"|name = " | tr '\n' ' ' | sed -r "s/ name = \"([a-z\-]+)\" -version = \"([0-9\.]+)\" \+version = \"([0-9\.]+)\" ?/* \1 from \`\2\` to \`\3\`\n/g")"
197+
if [[ -n $UPDATED_CRATES ]]
198+
then
199+
UPDATED_CRATES="\n${UPDATED_CRATES}"
200+
fi
201+
UPDATED_PACKAGE_JSONS="$(echo "$PACKAGE_JSON_FILES" | xargs git diff "$COMMIT_REF" | grep -E "^[\+\-] *\"version\": \"[0-9\.]+\"|name\": " | tr '\n' ' ' | sed -r "s/ *\"name\": \"([a-z@\-]*\/)?([a-z\-]+)\", - \"version\": \"([0-9\.]+)\", \+ \"version\": \"([0-9\.]+)\", ?/* [js] \2 from \`\3\` to \`\4\`\n/g")"
202+
if [[ -n $UPDATED_PACKAGE_JSONS ]]
203+
then
204+
UPDATED_PACKAGE_JSONS="\n${UPDATED_PACKAGE_JSONS}"
205+
fi
206+
207+
COMMIT_MESSAGE=$(echo -e "chore: upgrade crate versions${OPEN_API_UPDATE_MESSAGE}\n${UPDATED_CRATES}${UPDATED_PACKAGE_JSONS}${OPEN_API_UPDATE}")
208+
209+
echo -e "$COMMIT_MESSAGE"
210+
211+
if [ true = $COMMIT ]
212+
then
213+
git add --update $OPEN_API_FILE Cargo.lock ./*/Cargo.toml ./internal/*/Cargo.toml ./mithril-test-lab/*/Cargo.toml
214+
git add --update ./*/package.json ./*/package-lock.json examples/*/package.json examples/*/package-lock.json
215+
git commit -m "$COMMIT_MESSAGE"
216+
fi
217+
fi

docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)