Repository navigation
Expand file tree
/
Copy pathchangeset.sh
More file actions
executable file
·276 lines (232 loc) · 7.43 KB
/
Copy pathchangeset.sh
File metadata and controls
executable file
·276 lines (232 loc) · 7.43 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CHANGESET_DIR="$SCRIPT_DIR/.changeset"
VERSION_FILE="$SCRIPT_DIR/VERSION"
VERSION_SWIFT="$SCRIPT_DIR/Sources/TurnkeyHttp/Internal/Version.swift"
CHANGELOG_FILE="$SCRIPT_DIR/CHANGELOG.md"
# --- Word lists for filename generation ---
ADJECTIVES=(
brave bright calm clever cool crisp
daring deep eager fair fast fierce
fluffy gentle giant golden grand green
happy humble icy jolly keen kind
light lively lucky magic mighty misty
noble proud quick rapid red regal
sharp silent silver sleek slim smooth
soft solid spicy spring steady still
strong super sweet swift tall thick
tiny tough vivid warm wet wild
)
NOUNS=(
badger bear bird breeze brook canyon
cloud coral crane creek dawn deer
dolphin dove dragon dusk eagle ember
falcon flame forest fox frost galaxy
garden glacier grove harbor hawk hill
island jade lake leaf lion maple
meadow moon oak ocean orchid otter
panda pearl pine planet pond puma
quartz rain raven ridge river rose
sage seal shadow shore sky sparrow
stone storm summit sun tiger trail
valley wave willow wind wolf yarn
)
# --- Helpers ---
generate_filename() {
local adj="${ADJECTIVES[$((RANDOM % ${#ADJECTIVES[@]}))]}"
local noun="${NOUNS[$((RANDOM % ${#NOUNS[@]}))]}"
local name="$adj-$noun"
if [[ -f "$CHANGESET_DIR/$name.md" ]]; then
name="$name-$((RANDOM % 1000))"
fi
echo "$name"
}
parse_bump() {
local file="$1"
awk '/^---$/{n++; next} n==1 && /^bump:/{print $2; exit}' "$file"
}
parse_description() {
local file="$1"
# Capture everything after the second '---' delimiter, trimming leading/trailing blank lines
awk 'BEGIN{n=0} /^---$/{n++; next} n>=2{print}' "$file" | \
sed -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;ba' -e '}'
}
collect_changesets() {
local changesets=()
if [[ -d "$CHANGESET_DIR" ]]; then
while IFS= read -r -d '' f; do
changesets+=("$f")
done < <(find "$CHANGESET_DIR" -name '*.md' -print0 2>/dev/null)
fi
printf '%s\0' "${changesets[@]}"
}
# --- Commands ---
cmd_add() {
mkdir -p "$CHANGESET_DIR"
echo ""
echo "What kind of change is this?"
echo ""
PS3="Select bump type (1-3): "
select BUMP_TYPE in patch minor major; do
[[ -n "$BUMP_TYPE" ]] && break
echo "Invalid selection. Please choose 1, 2, or 3."
done
echo ""
read -r -p "Describe the change: " DESCRIPTION
if [[ -z "$DESCRIPTION" ]]; then
echo "Error: description cannot be empty."
exit 1
fi
local filename
filename=$(generate_filename)
local filepath="$CHANGESET_DIR/$filename.md"
cat > "$filepath" <<EOF
---
bump: $BUMP_TYPE
---
$DESCRIPTION
EOF
echo ""
echo "Created changeset: .changeset/$filename.md"
}
cmd_version() {
if [[ ! -f "$VERSION_FILE" ]]; then
echo "Error: VERSION file not found at $VERSION_FILE"
exit 1
fi
if [[ ! -f "$VERSION_SWIFT" ]]; then
echo "Error: Version.swift not found at $VERSION_SWIFT"
exit 1
fi
local changesets=()
if [[ -d "$CHANGESET_DIR" ]]; then
while IFS= read -r -d '' f; do
changesets+=("$f")
done < <(find "$CHANGESET_DIR" -name '*.md' -print0 2>/dev/null)
fi
if [[ ${#changesets[@]} -eq 0 ]]; then
echo "No pending changesets found. Nothing to bump."
exit 0
fi
local highest="patch"
for cs in "${changesets[@]}"; do
local bump
bump=$(parse_bump "$cs")
case "$bump" in
major) highest="major" ;;
minor) [[ "$highest" != "major" ]] && highest="minor" ;;
patch) ;;
*)
echo "Warning: invalid bump type '$bump' in $(basename "$cs"), skipping."
;;
esac
done
local current_version
current_version=$(tr -d '[:space:]' < "$VERSION_FILE")
local major minor patch
IFS='.' read -r major minor patch <<< "$current_version"
case "$highest" in
major) major=$((major + 1)); minor=0; patch=0 ;;
minor) minor=$((minor + 1)); patch=0 ;;
patch) patch=$((patch + 1)) ;;
esac
local new_version="$major.$minor.$patch"
echo "$new_version" > "$VERSION_FILE"
sed -i '' "s/let sdkVersion = \".*\"/let sdkVersion = \"$new_version\"/" "$VERSION_SWIFT"
echo "Bumped version: $current_version -> $new_version ($highest)"
}
cmd_changelog() {
local changesets=()
if [[ -d "$CHANGESET_DIR" ]]; then
while IFS= read -r -d '' f; do
changesets+=("$f")
done < <(find "$CHANGESET_DIR" -name '*.md' -print0 2>/dev/null)
fi
if [[ ${#changesets[@]} -eq 0 ]]; then
echo "No pending changesets found. Nothing to generate."
exit 0
fi
local version
version=$(tr -d '[:space:]' < "$VERSION_FILE")
local date
date=$(date +%Y-%m-%d)
local major_changes=() minor_changes=() patch_changes=()
for cs in "${changesets[@]}"; do
local bump description
bump=$(parse_bump "$cs")
description=$(parse_description "$cs")
case "$bump" in
major) major_changes+=("$description") ;;
minor) minor_changes+=("$description") ;;
*) patch_changes+=("$description") ;;
esac
done
local section=""
section+="## $version - $date"$'\n\n'
if [[ ${#major_changes[@]} -gt 0 ]]; then
section+="### Major Changes"$'\n\n'
for entry in "${major_changes[@]}"; do
# Multi-line entries are rendered as-is; single-line as bullet
if [[ "$entry" == *$'\n'* ]]; then
section+="$entry"$'\n\n'
else
section+="- $entry"$'\n'
fi
done
section+=$'\n'
fi
if [[ ${#minor_changes[@]} -gt 0 ]]; then
section+="### Minor Changes"$'\n\n'
for entry in "${minor_changes[@]}"; do
if [[ "$entry" == *$'\n'* ]]; then
section+="$entry"$'\n\n'
else
section+="- $entry"$'\n'
fi
done
section+=$'\n'
fi
if [[ ${#patch_changes[@]} -gt 0 ]]; then
section+="### Patch Changes"$'\n\n'
for entry in "${patch_changes[@]}"; do
if [[ "$entry" == *$'\n'* ]]; then
section+="$entry"$'\n\n'
else
section+="- $entry"$'\n'
fi
done
section+=$'\n'
fi
if [[ -f "$CHANGELOG_FILE" ]]; then
local header body
# Split on the first blank line after the header
header=$(head -1 "$CHANGELOG_FILE")
body=$(tail -n +3 "$CHANGELOG_FILE")
printf '%s\n\n%s%s\n' "$header" "$section" "$body" > "$CHANGELOG_FILE"
else
printf '# Changelog\n\n%s' "$section" > "$CHANGELOG_FILE"
fi
for cs in "${changesets[@]}"; do
rm "$cs"
done
echo "Generated changelog for version $version"
echo "Deleted ${#changesets[@]} changeset file(s)"
}
usage() {
echo "Usage: changeset.sh <command>"
echo ""
echo "Commands:"
echo " add Create a new changeset"
echo " version Bump package version based on pending changesets"
echo " changelog Generate changelog from pending changesets"
echo ""
exit 1
}
# --- Main ---
case "${1:-}" in
add) cmd_add ;;
version) cmd_version ;;
changelog) cmd_changelog ;;
*) usage ;;
esac