-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbump_version.sh
executable file
·153 lines (132 loc) · 4.19 KB
/
bump_version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
set -e # Exit on error
TEMPLATE_FILE="CHANGELOG_TEMPLATE.md"
CHANGELOG="CHANGELOG.md"
VERSION_FILE="pyproject.toml"
CURRENT_DATE=$(date +%Y-%m-%d)
echo "Checking if pyproject.toml exists..."
if [[ ! -f "$VERSION_FILE" ]]; then
echo "Error: $VERSION_FILE not found!" >&2
exit 1
fi
echo "Extracting current version from pyproject.toml..."
CURRENT_VERSION=$(awk -F' = "' '/^version =/ {print $2}' "$VERSION_FILE" | tr -d '"')
# Check if extraction was successful
if [[ -z "$CURRENT_VERSION" ]]; then
echo "Error: Could not extract version from $VERSION_FILE" >&2
exit 1
fi
echo "Current version: $CURRENT_VERSION"
# Check if the version section was found
if ! grep -q "## \[$CURRENT_VERSION\]" $CHANGELOG; then
echo "Error: No section with version $CURRENT_VERSION found in $CHANGELOG"
exit 1
fi
# Escape dots in version number for awk pattern matching
VERSION_ESCAPED=$(echo "$CURRENT_VERSION" | sed 's/\./\\./g')
# Split changelog into unreleased and released parts
awk -v ver="$VERSION_ESCAPED" '
BEGIN { p=0 }
$0 ~ "## \\[" ver "\\] - [0-9]{4}-[0-9]{2}-[0-9]{2}" { p=NR }
p==0 { print > "CHANGELOG_unreleased.md" }
p!=0 { print > "CHANGELOG_old.md" }
' CHANGELOG.md
# Removes template to only get user input
awk 'NR > 7' "CHANGELOG_unreleased.md" >temp_changelog.md \
&& mv temp_changelog.md "CHANGELOG_unreleased.md"
# Process the changelog to remove empty sections
awk '
# Store the current line
{ current = $0 }
# If we see a section header, store it and skip to next line
/^### / {
section = $0
getline
# If next line is empty or another section header, skip printing the section
if ($0 ~ /^$/ || $0 ~ /^### /) {
next
}
# Otherwise print both section and content
else {
print section
print $0
}
next
}
# Print any non-section-header line that we havent handled above
!/^### / { print }
' CHANGELOG_unreleased.md >UNRELEASED.tmp && mv UNRELEASED.tmp CHANGELOG_unreleased.md
# Initialize variables to track if we found each type
FOUND_MAJOR=0
FOUND_MINOR=0
FOUND_PATCH=0
# Check for each type of section
if grep -q "### .*major" CHANGELOG_unreleased.md; then
FOUND_MAJOR=1
VERSION_TYPE="major"
elif grep -q "### .*minor" CHANGELOG_unreleased.md; then
FOUND_MINOR=1
VERSION_TYPE="minor"
elif grep -q "### .*patch" CHANGELOG_unreleased.md; then
FOUND_PATCH=1
VERSION_TYPE="patch"
else
echo "Error: No version-affecting changes found in CHANGELOG"
exit 1
fi
echo "Found changes of type: $VERSION_TYPE"
# Extract major, minor, and patch versions
IFS='.' read -r MAJOR MINOR PATCH <<<"$CURRENT_VERSION"
# Bump the correct version
if [[ $FOUND_MAJOR -eq 1 ]]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif [[ $FOUND_MINOR -eq 1 ]]; then
MAJOR=$((MAJOR))
MINOR=$((MINOR + 1))
PATCH=0
elif [[ $FOUND_PATCH -eq 1 ]]; then
MAJOR=$((MAJOR))
MINOR=$((MINOR))
PATCH=$((PATCH + 1))
fi
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "$NEW_VERSION"
CURRENT_DATE=$(date +%Y-%m-%d)
echo "Updating [Unreleased] to [$NEW_VERSION] - $CURRENT_DATE"
awk -v date="$CURRENT_DATE" \
-v new_version="$NEW_VERSION" '
{
if ($0 ~ /## \[Unreleased\]/) {
print "## [" new_version "] - " date
}
else {
print
}
}' CHANGELOG_unreleased.md >temp_changelog.md && mv temp_changelog.md CHANGELOG_unreleased.md
echo "Updating version in pyproject.toml"
awk -v new_version="$NEW_VERSION" '
/^version = / {
print "version = \"" new_version "\""
next
}
{ print }
' pyproject.toml >temp_pyproject.toml && mv temp_pyproject.toml pyproject.toml
echo 'Copying template to the top of the changelog'
{
cat $TEMPLATE_FILE
echo -e "\n\n"
cat CHANGELOG_unreleased.md
cat CHANGELOG_old.md
} >temp_changelog.md && mv temp_changelog.md $CHANGELOG
# # Cleanup temp files
rm CHANGELOG_old.md
rm $TEMPLATE_FILE
# Output new version to a file that GitHub Actions can read
echo "Saving new version into env var."
# Output the new version for GitHub Actions to capture
echo "New version: $NEW_VERSION"
# Make sure to set the output value for the next steps to use
echo "new_version=$NEW_VERSION" >> $GITHUB_ENV
echo 'Changelog and version update completed!'