/
please
executable file
·374 lines (316 loc) · 12.5 KB
/
please
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env bash
#
# Simple github release for PHP &/or NodeJS projects.
#
# Author:
# Jitendra Adhikari <jiten.adhikary@gmail.com>
#
# Be sure to check readme doc at https://github.com/adhocore/please
#
set -e
[[ "$(uname)" == "Darwin" ]] && declare -a EVENTS || declare -A EVENTS
FULL=$(readlink -n $0 || echo $0) # fullpath
PLIZ=$(basename $0) # name
line() { echo -e "\e[${3:-0};$2m$1\e[0m"; if [[ "$4" != "" ]]; then exit $4; fi }
error() { line "$1" 31 0 $2; }
ok() { line "${1:- Done}" 32 0 $2; }
info() { line "$1" 33 0 $2; }
comment() { line "$1" 30 1 $2; }
qgit() { git "$@" >> $TMP_LOG; }
# github api
github()
{
RESP=$(bash -c "curl --silent -H 'Authorization: token $GH_AUTH_TOKEN' $@")
echo "$RESP" >> ${TMP_LOG}
ERROR=$(echo $RESP | jq -r '.message?')
[[ $ERROR ]] && [[ "null" != "$ERROR" ]] && error "Github: $ERROR" 1
echo "$RESP"
}
# show usage
usage()
{
cat <<- USAGE
$PLIZ $(info $(version)) | $(ok "(c) Jitendra Adhikari") | $PLIZ is semver release made easy.
$(ok Usage:) $PLIZ [command|scope] [--options]
$(ok Commands:)
$(info version) $(comment "Print current version of itself.")
$(info help) $(comment "Show help information and usage examples.")
$(ok Scope:)
$(info major) $(comment "Bumps the <major> part of semver.")
$(info minor) $(comment "Bumps the <minor> part of semver.")
$(ok Options:)
$(info "-c --chlog") $(comment "Forces creation of CHANGELOG.md file.")
$(info "-h --help") $(comment "Show help information and usage.")
$(info "-H --chash") $(comment "Prepends commit hash into log.")
$(info "-o --organize") $(comment "Commit types as CSV for changelog or release notes.")
$(comment "(Default: feat,fix,refactor,perf,docs,infra,chore)")
$(info "-L | --label") $(comment "Set custom release label.")
$(info "-N | --no-sync") $(comment "Do not sync the DEPLOY_BRANCH with remote (faster but unsafe).")
$(info "-p --public") $(comment "Set scoped npm package for public access.")
$(info "--prev-version") $(comment "Fix the previous version (skips API call).")
$(info "-u --update") $(comment "Update $PLIZ to latest version.")
$(info "-v --vfile") $(comment "Forces creation of VERSION file.")
$(info "-V --version") $(comment "Forces the exact version to be released.")
$(info "-y --yes") $(comment "Assume yes for any confirmation.")
$(ok Events:)
$(info "--before-all") $(comment "Run the command before anything (very start).")
$(info "--before-npm") $(comment "Run the command before releasing to npm.")
$(info "--before-push") $(comment "Run the command before pushing code to remote.")
$(info "--before-vcs") $(comment "Run the command before releasing to VCS.")
$(info "--before-phar") $(comment "Run the command before uploading phar binary.")
$(info "--after-all") $(comment "Run the command after everything finishes.")
(See https://github.com/adhocore/please#events for more info)
USAGE
if [ "$1" == "--help" ]; then
cat <<- EXAMPLE
$(ok Examples:)
$PLIZ
$PLIZ version $(comment "# prints current version of itself")
$PLIZ --update $(comment "# updates $PLIZ if new version available")
$PLIZ --organize feat,fix,docs $(comment "# includes only features, fixes and docs")
$PLIZ --vfile --chash $(comment "# creates VERSION file, adds commit hash")
$PLIZ minor --public --yes $(comment "# releases minor version without asking")
$PLIZ major --vfile --chlog $(comment "# releases next major version with VERSION and CHANGELOG files")
$PLIZ --vfile --chlog --version 1.5.0 $(comment "# releases version 1.5.0 with VERSION and CHANGELOG files")
$PLIZ --before-all 'echo {REPO}' $(comment "# before release, runs 'echo {REPO}' with REPO interpolated")
EXAMPLE
fi
exit 0
}
# organize commits
organize-commits() # $1 = diff_range, $2 = commit_types_csv
{
declare -A LABELS=(
[feat]=Features [fix]="Bug Fixes" [refactor]="Internal Refactors"
[perf]="Performance Improvements" [test]=Tests [docs]=Documentations
[chore]=Miscellaneous [style]="Code Standards" [infra]="Infrastructure Changes"
[rebase]=Rebasing [revert]=Reversal [build]=Builds [unfeat]="Removed Features"
)
declare -A CHANGELOG
while read -r COMMIT; do
if [[ $COMMIT =~ ^([a-z]+)\(?([^\)]+)?\)?:\ (.+) ]]; then
type="${BASH_REMATCH[1]}" scope="${BASH_REMATCH[2]}" subject="${BASH_REMATCH[3]}"
if [[ "$scope" != "" ]]; then scope="**${scope^}**: "; fi
CHANGELOG[$type]="${CHANGELOG[$type]}- ${scope}${subject^}\n"
fi
done <<< $(git log $1 --no-merges --oneline --pretty="$GCFMT" --grep "\[skip" --grep "skip\]" --grep "StyleCI" --invert-grep)
ORGANIZED="" TYPES=($(echo ${2:-feat,fix,refactor,revert,perf,chore,infra,docs,build} | tr ',' '\n'))
for TYPE in ${TYPES[@]}; do
if [[ "${CHANGELOG[$TYPE]}" != "" ]]; then
ORGANIZED="$ORGANIZED### ${LABELS[$TYPE]}\n${CHANGELOG[$TYPE]}\n"
fi
done
echo $ORGANIZED
}
# update please
update()
{
NEWVER=$(curl -sSL https://raw.githubusercontent.com/adhocore/please/main/VERSION)
[[ "v$NEWVER" == "$(version)" ]] && ok "Already latest version" 0
info "Updating $PLIZ ..."
curl -sSLo ${FULL} https://raw.githubusercontent.com/adhocore/please/main/please
ok " Done [${NEWVER}]" 0
}
version() { echo v0.12.3; }
# get box.phar path
get-box()
{
case $(php -r "echo substr(phpversion(), 0, 3);") in
7.2 | 7.3 | 7.4) BOX_VER=3.8.0 ;;
7.1) BOX_VER=3.7.0 ;;
7.0 | 5.6 | 5.5 | 5.4 | 5.3) BOX_VER=2.7.5 ;;
*) echo "Can't determine box.phar version"; exit 1 ;;
esac
if [ ! -e ~/box.$BOX_VER.phar ]; then
if [ "$BOX_VER" == "2.7.5" ]; then
curl -sSLo ~/box.$BOX_VER.phar "https://github.com/box-project/box2/releases/download/2.7.5/box-2.7.5.phar" >> $TMP_LOG
else
curl -sSLo ~/box.$BOX_VER.phar "https://github.com/humbug/box/releases/download/$BOX_VER/box.phar" >> $TMP_LOG
fi
fi
echo $(realpath ~/box.$BOX_VER.phar)
}
# publish phar
publish-phar()
{
PBIN=$(cat composer.json | jq -r '.bin[0]?')
if [ "null" == "$PBIN" ]; then
info "Check ${TMP_LOG} for logs" && error "Check bin value in composer.json" 1
fi
trigger before-phar
info "Compiling phar ..."
php $(get-box) compile >> ${TMP_LOG}
ok
PHAR_FILE=$(realpath $PBIN.phar) NAME=$(basename $PHAR_FILE)
info "Publishing phar ..."
SIZE=$(github \
"-H 'Content-Type: application/octet-stream' \
--data-binary @$PHAR_FILE \
https://uploads.$GH_REPO/releases/$LAST_RELEASE_ID/assets?name=$NAME" \
| jq -r '.size')
ok
info "[OK] $NAME $SIZE bytes"
}
# publish npm
publish-npm()
{
PKG=$(cat ./package.json) PVT=$(echo $PKG | jq -r '.private?')
echo $PKG | jq ".version = \"$NEXT_VERSION\"" > ./package.json
qgit add ./package.json && GPUSH=1
info "Publishing npm ..."
if [ "true" == "$PVT" ]; then
info " package marked private"
return 0
elif [ ! -f ~/.npmrc ]; then
npm adduser
elif [[ "" = "$(cat ~/.npmrc | grep _authToken || echo '')" ]]; then
npm adduser
fi
trigger before-npm
npm publish $PUBLIC >> $TMP_LOG
ok
}
# trigger event
trigger() # $1 = event
{
local CMD="${EVENTS[$1]}"
if [[ "$CMD" != "" ]]; then
echo -e "$(info Running) $(ok "$1")\n$(comment "> $CMD")"
eval "CMD=\"${CMD//{/\${}\"" && eval "$CMD"
fi
}
VCS=${VCS:-github.com}
GCFMT="%s (%an)" # git commit log format
REPO=$(git config --get remote.origin.url | sed -e "s/git@$VCS://;s/https:\/\/$VCS\///;s/\.git//")
[[ "$VCS" == *"bitbucket"* ]] && CMT=commits || CMT=commit
# load events
[[ -f ./please.json ]] && eval $(cat ./please.json | jq -r 'to_entries|map("EVENTS[\(.key)]=\"\(.value)\"")|.[]')
# argv parsing
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help | help) usage $1 ;;
-u | --update) update ;;
-v | --vfile) VFILE=1; shift 1 ;;
-p | --public) PUBLIC="--access=public"; shift 1 ;;
-c | --chlog) CHLOG=1; shift 1 ;;
-H | --chash) GCFMT="$GCFMT [_%h_](https://$VCS/$REPO/$CMT/%h)"; shift 1 ;;
-y | --yes) YES=1; shift 1 ;;
-V | --version) NEXT_VERSION=$2; shift 2 ;;
-L | --label) LABEL=$2; shift 2 ;;
-P | --prev-version) PREV_VERSION=$2; shift 2 ;;
-o | --organize) TYPES=$2; shift 2 ;;
-N | --no-sync) NOSYNC=1; shift 1 ;;
--before-* | --after-*) EVENTS[${1/--}]="$2"; shift 2 ;;
version) version; exit 0 ;;
minor) MINOR=1; shift 1 ;;
major) MAJOR=1; shift 1 ;;
*) error "Invalid arg $1. (Run $PLIZ --help)" 1 ;;
esac
done
[[ $REPO ]] || error "$(pwd) is not git repo" 1 # exit
[[ "$VCS" == *"github"* ]] && [[ -z "$GH_AUTH_TOKEN" ]] && error "GH_AUTH_TOKEN missing" 1 # exit
GPUSH=0 # git push pending
MAJOR=${MAJOR:-0} # bump major
MINOR=${MINOR:-0} # bump minor
VFILE=${VFILE:-0} # create version file
CHLOG=${CHLOG:-0} # create changelog file
YES=${YES:-0} # assume yes
NOSYNC=${NOSYNC:-0} # do no sync deploy branch (faster for retry)
GH_REPO="github.com/repos/$REPO" TMP_LOG=$(mktemp -t PLZ.XXXXXXXXXX)
ok "Repository $REPO" && info "Log file $TMP_LOG"
DEPLOY_BRANCH="${DEPLOY_BRANCH:-master}"
trigger before-all
if [ -z "$TRAVIS" ] || [ "$DEPLOY_BRANCH" == "$TRAVIS_BRANCH" ]; then
info "Updating $DEPLOY_BRANCH ..."
[[ $NOSYNC -eq 0 ]] && qgit stash && qgit checkout $DEPLOY_BRANCH && qgit fetch --tags && qgit pull origin $DEPLOY_BRANCH --rebase
qgit checkout $DEPLOY_BRANCH # for NOSYNC=1
ok
else
info "Skip release"
exit 0
fi
info "Getting latest releases ..."
[[ $PREV_VERSION ]] || PREV_VERSION=$(github "https://api.$GH_REPO/releases?per_page=1" | jq -r '.[0]?.tag_name?')
VERSION="$PREV_VERSION"
RANGE="$VERSION..HEAD"
[[ "null" == "$VERSION" ]] && { VERSION="0.0.0" RANGE=""; }
ok " Done [$VERSION]"
info "Collecting commits ..."
COMMITS=$(organize-commits "$RANGE" "$TYPES")
ok
[[ $COMMITS ]] || error "Nothing to release" 0 # exit
echo $COMMITS >> $TMP_LOG
# determine next version
info "Bumping version ..."
if [[ "" = "$NEXT_VERSION" ]]; then
FEAT=$(git log $RANGE --oneline --pretty="%s" | grep -P '^feat' || echo "")
if [[ $MAJOR -eq 1 ]]; then
V=""
if [[ $VERSION == v* ]]; then
V="v" VERSION="${VERSION//v/}"
fi
NEXT_VERSION=$(echo $VERSION | (IFS=".$IFS"; read a b c && echo $V$((a + 1)).0.0))
elif [[ $MINOR -eq 1 ]]; then
NEXT_VERSION=$(echo $VERSION | (IFS=".$IFS"; read a b c && echo $a.$((b + 1)).0))
elif [[ "" != "$FEAT" ]]; then
NEXT_VERSION=$(echo $VERSION | (IFS=".$IFS"; read a b c && echo $a.$((b + 1)).0))
else
NEXT_VERSION=$(echo $VERSION | (IFS=".$IFS"; read a b c && echo $a.$b.$((c + 1))))
fi
trigger after-version
fi
ok " Done [$NEXT_VERSION]"
if [[ $YES -eq 0 ]]; then
read -p "Are you sure to release [$NEXT_VERSION]? [y/N] " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]] || error "Skipping release for $NEXT_VERSION" 1 # exit
fi
# changelog backup
if [ -f ./CHANGELOG.md ]; then
OLD="\n$(cat ./CHANGELOG.md)" CHLOG=0
[[ $OLD != *"[$NEXT_VERSION]"* ]] && CHLOG=1
fi
# changelog file
if [[ $CHLOG -eq 1 ]]; then
info "Updating changelog ..."
echo -e "## [$NEXT_VERSION](https://github.com/$REPO/releases/tag/$NEXT_VERSION) ($(date -u '+%Y-%m-%d'))\n\n$COMMITS$OLD" > ./CHANGELOG.md
qgit add ./CHANGELOG.md && GPUSH=1
ok
fi
# version file
if [ -f ./VERSION ] || [[ $VFILE -eq 1 ]]; then
echo "$NEXT_VERSION" > ./VERSION
qgit add ./VERSION && GPUSH=1
fi
# do we need to publish npm?
[[ -f ./package.json ]] && publish-npm
# push changes
if [[ $GPUSH -eq 1 ]]; then
trigger before-push
qgit commit --allow-empty -m "chore: new version $NEXT_VERSION [skip ci] [ci skip]"
info "Pushing pending commits ..."
qgit push origin $DEPLOY_BRANCH
ok
fi
[[ "$VCS" == *"bitbucket"* ]] && trigger after-all && exit 0
# github release
trigger before-vcs
info "Releasing $NEXT_VERSION ..."
COMMITS=$(printf %s "$COMMITS")
[[ $LABEL ]] && LABEL=" ($LABEL)"
VERSION_INFO="Version $NEXT_VERSION${LABEL}"
LAST_RELEASE_ID=$(curl --silent -H "Authorization: token $GH_AUTH_TOKEN" \
-H 'Content-Type: application/json' \
-d "{\"tag_name\":\"$NEXT_VERSION\",\"name\":\"$VERSION_INFO\",\"body\":\"$COMMITS\",\"target_commitish\":\"$DEPLOY_BRANCH\"}" \
https://api.$GH_REPO/releases \
| jq -r '.id?')
if [ "null" == "${LAST_RELEASE_ID:-null}" ]; then
error "Something went wrong" && info "Check $TMP_LOG for logs" 1 # exit
fi
ok " Done (https://github.com/$REPO/releases/tag/$NEXT_VERSION)"
qgit tag -a "$NEXT_VERSION" HEAD -m "$VERSION_INFO" && echo "Created tag $NEXT_VERSION" || true
trigger after-release
# do we need to publish phar?
[[ -f ./box.json ]] && [[ -f ./composer.json ]] && publish-phar
trigger after-all
info "Check $TMP_LOG for logs"