-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpublish.sh
More file actions
executable file
·114 lines (93 loc) · 2.95 KB
/
Copy pathpublish.sh
File metadata and controls
executable file
·114 lines (93 loc) · 2.95 KB
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
#!/usr/bin/env bash
set -euo pipefail
# Usage:
# ./publish.sh patch
# ./publish.sh minor
# ./publish.sh major
# ./publish.sh 1.2.3
BUMP="${1:-patch}"
PYPROJECT="pyproject.toml"
if [[ ! -f "$PYPROJECT" ]]; then
echo "ERROR: $PYPROJECT not found in current directory."
exit 1
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "ERROR: Not inside a git repository."
exit 1
fi
if [[ -n "$(git status --porcelain)" ]]; then
echo "WARNING: Working tree is not clean (you have uncommitted changes)."
echo " This script will 'git add .' and commit them."
echo
fi
# Extract current version from [project] section in a macOS-awk compatible way
CURRENT_VERSION="$(
awk '
BEGIN { in_project=0 }
/^[[:space:]]*\[project\][[:space:]]*$/ { in_project=1; next }
in_project && /^[[:space:]]*\[/ { in_project=0 } # next table starts -> leave [project]
in_project && /^[[:space:]]*version[[:space:]]*=[[:space:]]*"[0-9]+\.[0-9]+\.[0-9]+"/ {
match($0, /"[0-9]+\.[0-9]+\.[0-9]+"/)
v = substr($0, RSTART + 1, RLENGTH - 2)
print v
exit
}
' "$PYPROJECT"
)"
if [[ -z "${CURRENT_VERSION:-}" ]]; then
echo "ERROR: Could not find [project] version like: version = \"1.2.3\" in $PYPROJECT"
exit 1
fi
# Compute new version
NEW_VERSION=""
if [[ "$BUMP" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
NEW_VERSION="$BUMP"
else
IFS='.' read -r MA MI PA <<< "$CURRENT_VERSION"
case "$BUMP" in
patch) ((PA+=1)) ;;
minor) ((MI+=1)); PA=0 ;;
major) ((MA+=1)); MI=0; PA=0 ;;
*)
echo "ERROR: bump must be patch|minor|major|X.Y.Z (got: $BUMP)"
exit 1
;;
esac
NEW_VERSION="${MA}.${MI}.${PA}"
fi
# Ensure tag doesn't already exist
if git rev-parse "v${NEW_VERSION}" >/dev/null 2>&1; then
echo "ERROR: git tag v${NEW_VERSION} already exists."
exit 1
fi
echo "Bumping version: ${CURRENT_VERSION} -> ${NEW_VERSION}"
# Edit pyproject.toml in-place: only update version inside [project]
NEW_VERSION="$NEW_VERSION" perl -0777 -i -pe '
my $new = $ENV{NEW_VERSION} // die "NEW_VERSION not set\n";
# Replace version inside the [project] table only.
# This is a conservative multiline match: [project] ... version = "x.y.z"
if (s/(\[project\][\s\S]*?\n\s*version\s*=\s*")(\d+\.\d+\.\d+)(")/$1$new$3/m) {
# ok
} else {
die "version line not found under [project]\n";
}
' "$PYPROJECT"
# rm -rf dist/
# build/ and *.egg-info must go too: a stale egg-info makes setuptools ignore
# the packages.find exclude and silently ship smrpgpatchbuilder.management.
rm -rf dist/ build/ src/*.egg-info
# python -m build
python3 -m build
# git add .
git add .
# git commit -m "<version number>"
git commit -m "${NEW_VERSION}"
# git push
git push
# git tag v<version number>
git tag "v${NEW_VERSION}"
# git push origin v<version number>
git push origin "v${NEW_VERSION}"
# Twine upload (won't prompt if TWINE_USERNAME/TWINE_PASSWORD are set)
python3 -m twine upload dist/*
echo "Done: released ${NEW_VERSION}"