-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to accept named arguments and handle tag prefix change for r…
…enamed packages
- Loading branch information
Showing
2 changed files
with
192 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,68 @@ | ||
#!/usr/bin/env bash | ||
|
||
package_name="$1" | ||
remote=${2:-'origin'} | ||
release_commits_regex=${3:-'^\d\{1,3\}\.\d\{1,3\}\.\d\{1,3\}'} | ||
source "$PWD/scripts/semver.sh" | ||
|
||
remote='origin' | ||
release_commits_regex='^\d\{1,3\}\.\d\{1,3\}\.\d\{1,3\}' | ||
version_before_package_rename='99999.0.0' | ||
tag_prefix_before_package_rename="$1" | ||
|
||
while [[ $# -gt 0 ]]; do | ||
key="$1" | ||
|
||
case $key in | ||
-r | --remote) | ||
remote="$2" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
-p | --regex-pattern) | ||
release_commits_regex="$2" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
-v | --version-before-package-rename) | ||
version_before_package_rename="$2" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
-t | --tag-prefix-before-package-rename) | ||
tag_prefix_before_package_rename="$2" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
*) # package name | ||
package_name="$1" | ||
shift # past argument | ||
;; | ||
esac | ||
done | ||
|
||
get-version-commit-pairs() { | ||
for log in "$(git log --oneline --grep $release_commits_regex merged-packages/$package_name)"; do | ||
echo "$log" | cut -d' ' -f1,2 | sed 's/\([^ ]*\)\([ ]*\) \([^ ]*\)\([ ]*\)/\3\4 \1\2/' | ||
done | ||
while read -r log; do | ||
commit="$(echo "$log" | cut -d' ' -f1)" | ||
version="$(echo "$log" | cut -d' ' -f2)" | ||
echo "$version $commit" | ||
done <<<"$(git log --oneline --grep $release_commits_regex merged-packages/$package_name)" | ||
} | ||
|
||
prepend-package-name() { | ||
for pair in "$(get-version-commit-pairs)"; do | ||
echo "$pair" | sed "s/\([^ ]*\)\([ ]*\) \([^ ]*\)\([ ]*\)/@metamask\/$package_name@\1\2 \3\4/" | ||
done | ||
prepend-tag-name() { | ||
while read -r pair; do | ||
version="$(echo "$pair" | cut -d' ' -f1)" | ||
commit="$(echo "$pair" | cut -d' ' -f2)" | ||
if semverLT "$version" "$version_before_package_rename" || semverEQ "$version" "$version_before_package_rename"; then | ||
tag_name="$tag_prefix_before_package_rename@$version" | ||
else | ||
tag_name="@metamask/$package_name@$version" | ||
fi | ||
echo "$tag_name $commit" | ||
done <<<"$(get-version-commit-pairs)" | ||
} | ||
|
||
tag_commit_pairs=$(prepend-package-name) | ||
|
||
for pair in "$tag_commit_pairs"; do | ||
echo "$pair" | xargs -n 2 bash -c 'git tag "$0" "$1"' | ||
done | ||
|
||
for pair in "$tag_commit_pairs"; do | ||
echo "$pair" | cut -d' ' -f1 | xargs -n 1 bash -c 'git push '"$remote"' "$0"' | ||
done | ||
while read -r pair; do | ||
tag="$(echo "$pair" | cut -d' ' -f1)" | ||
commit="$(echo "$pair" | cut -d' ' -f2)" | ||
# echo "$tag $commit" | ||
git tag "$tag" "$commit" | ||
git push "$remote" "$tag" | ||
done <<<"$(prepend-tag-name)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/env sh | ||
|
||
# https://github.com/cloudflare/semver_bash/blob/master/semver.sh | ||
|
||
function semverParseInto() { | ||
local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' | ||
#MAJOR | ||
eval $2=`echo $1 | sed -e "s#$RE#\1#"` | ||
#MINOR | ||
eval $3=`echo $1 | sed -e "s#$RE#\2#"` | ||
#MINOR | ||
eval $4=`echo $1 | sed -e "s#$RE#\3#"` | ||
#SPECIAL | ||
eval $5=`echo $1 | sed -e "s#$RE#\4#"` | ||
} | ||
|
||
function semverEQ() { | ||
local MAJOR_A=0 | ||
local MINOR_A=0 | ||
local PATCH_A=0 | ||
local SPECIAL_A=0 | ||
|
||
local MAJOR_B=0 | ||
local MINOR_B=0 | ||
local PATCH_B=0 | ||
local SPECIAL_B=0 | ||
|
||
semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A | ||
semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B | ||
|
||
if [ $MAJOR_A -ne $MAJOR_B ]; then | ||
return 1 | ||
fi | ||
|
||
if [ $MINOR_A -ne $MINOR_B ]; then | ||
return 1 | ||
fi | ||
|
||
if [ $PATCH_A -ne $PATCH_B ]; then | ||
return 1 | ||
fi | ||
|
||
if [[ "_$SPECIAL_A" != "_$SPECIAL_B" ]]; then | ||
return 1 | ||
fi | ||
|
||
|
||
return 0 | ||
|
||
} | ||
|
||
function semverLT() { | ||
local MAJOR_A=0 | ||
local MINOR_A=0 | ||
local PATCH_A=0 | ||
local SPECIAL_A=0 | ||
|
||
local MAJOR_B=0 | ||
local MINOR_B=0 | ||
local PATCH_B=0 | ||
local SPECIAL_B=0 | ||
|
||
semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A | ||
semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B | ||
|
||
if [ $MAJOR_A -lt $MAJOR_B ]; then | ||
return 0 | ||
fi | ||
|
||
if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -lt $MINOR_B ]]; then | ||
return 0 | ||
fi | ||
|
||
if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -le $MINOR_B && $PATCH_A -lt $PATCH_B ]]; then | ||
return 0 | ||
fi | ||
|
||
if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then | ||
return 1 | ||
fi | ||
if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" != "_" ]] ; then | ||
return 1 | ||
fi | ||
if [[ "_$SPECIAL_A" != "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then | ||
return 0 | ||
fi | ||
|
||
if [[ "_$SPECIAL_A" < "_$SPECIAL_B" ]]; then | ||
return 0 | ||
fi | ||
|
||
return 1 | ||
|
||
} | ||
|
||
function semverGT() { | ||
semverEQ $1 $2 | ||
local EQ=$? | ||
|
||
semverLT $1 $2 | ||
local LT=$? | ||
|
||
if [ $EQ -ne 0 ] && [ $LT -ne 0 ]; then | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
} | ||
|
||
if [ "___semver.sh" == "___`basename $0`" ]; then | ||
|
||
MAJOR=0 | ||
MINOR=0 | ||
PATCH=0 | ||
SPECIAL="" | ||
|
||
semverParseInto $1 MAJOR MINOR PATCH SPECIAL | ||
echo "$1 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" | ||
|
||
semverParseInto $2 MAJOR MINOR PATCH SPECIAL | ||
echo "$2 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" | ||
|
||
semverEQ $1 $2 | ||
echo "$1 == $2 -> $?." | ||
|
||
semverLT $1 $2 | ||
echo "$1 < $2 -> $?." | ||
|
||
semverGT $1 $2 | ||
echo "$1 > $2 -> $?." | ||
|
||
fi |