|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | # |
3 | | -# |
4 | | - |
5 | | -#set -e |
6 | | - |
7 | | -# import common functions |
8 | | -source "$(dirname $0)/common-func.sh" |
9 | | - |
10 | | -function show_help() { |
11 | | - binName="bash $(basename $0)" |
12 | | - |
13 | | - cat <<EOF |
14 | | -Release all sub-repo to new tag version and push to remote repo |
15 | | -
|
16 | | -Usage: |
17 | | - ${binName} [-t VERSION] [NAME ...] |
| 3 | +# TODO with release message |
18 | 4 |
|
19 | | -Options: |
20 | | - -a Release all components |
21 | | - -t <version> Specifies the version number to be published, |
22 | | - without specifying the automatic calculation of the next version. |
23 | | - eg: v2.0.4 |
24 | | - -y No confirmation required |
25 | | - -h, --help Display the help information |
| 5 | +set -e |
26 | 6 |
|
27 | | -Example: |
28 | | - ${binName} -a Release all components, will auto calc next version |
29 | | - ${binName} -t v2.0.4 -a Release all components, use user input version |
30 | | - ${binName} -t v2.0.4 event Release one component |
31 | | - ${binName} -t v2.0.4 event stdlib Release multi components |
| 7 | +binName="bash $(basename $0)" |
32 | 8 |
|
33 | | -EOF |
| 9 | +if [[ -z "$1" ]] |
| 10 | +then |
| 11 | + echo "Release all sub-repo to new tag version and push to remote repo" |
| 12 | + echo -e "Usage:\n $binName VERSION" |
| 13 | + echo "Example:" |
| 14 | + echo " $binName v1.0.0 Tag for all sub-repos and push to remote repo" |
| 15 | + echo " $binName v1.0.0 http-server Tag for one sub-repo and push to remote repo" |
34 | 16 | exit 0 |
35 | | -} |
36 | | - |
37 | | -# 显示帮助 |
38 | | -[[ "$1" = "" || "$1" = "-h" || "$1" = "--help" ]] && show_help |
39 | | - |
40 | | -RELEASE_TAG=AUTO |
41 | | -NEED_CONFIRM=Y |
42 | | - |
43 | | -# parse input options |
44 | | -# ref https://www.cnblogs.com/yxzfscg/p/5338775.html |
45 | | -while getopts "t:ahy" arg; do #选项后面的冒号表示该选项需要参数 |
46 | | - case ${arg} in |
47 | | - a) |
48 | | - COMPONENTS=$(ls src/) ;; |
49 | | - h) |
50 | | - show_help ;; |
51 | | - y) |
52 | | - NEED_CONFIRM=N ;; |
53 | | - t) |
54 | | - RELEASE_TAG=$OPTARG ;; |
55 | | - ?) #当有不认识的选项的时候arg为? |
56 | | - echo "Missing argument" |
57 | | - exit 1 |
58 | | - ;; |
59 | | - esac |
60 | | -done |
61 | | - |
62 | | -shift $(($OPTIND - 1)) |
| 17 | +fi |
63 | 18 |
|
| 19 | +RELEASE_TAG=$1 |
64 | 20 | TARGET_BRANCH=master |
65 | 21 | CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD` |
66 | | -#CURRENT_BRANCH=master |
67 | 22 |
|
68 | | -[[ -n "$@" ]] && COMPONENTS=$@ |
| 23 | +SUB_REPOS=$2 |
69 | 24 |
|
70 | | -if [[ -z "${COMPONENTS}" ]]; then |
71 | | - colored_text "Please input want released component names or use option: -a" red |
72 | | - exit 1 |
| 25 | +if [[ -z "$2" ]]; then |
| 26 | + SUB_REPOS=$(ls src/) |
73 | 27 | fi |
74 | 28 |
|
75 | 29 | echo "Will released version: ${RELEASE_TAG}" |
76 | 30 | echo "Will released projects:" |
77 | | -echo " " ${COMPONENTS} |
78 | | -#colored_text "${COMPONENTS}" |
79 | | - |
80 | | -if [[ "$NEED_CONFIRM" = "Y" ]]; then |
81 | | - if ! user_confirm "Continue"; then |
82 | | - colored_text "Good Bye" |
83 | | - exit 0 |
84 | | - fi |
85 | | -fi |
86 | | - |
87 | | -TMP_DIR="/tmp/release-components-git" |
| 31 | +echo ${SUB_REPOS} |
88 | 32 |
|
89 | | -yellow_text "> rm -rf ${TMP_DIR} && mkdir ${TMP_DIR}" |
90 | | -rm -rf ${TMP_DIR} && mkdir ${TMP_DIR}; |
91 | | -#pwd |
92 | | -yellow_text "> cp -R $(pwd)/. ${TMP_DIR}" |
93 | | -cp -R $(pwd)/. ${TMP_DIR} |
94 | | -#cd ${TMP_DIR} && git checkout . && pwd; |
95 | | -cd ${TMP_DIR} && git checkout . && pwd; |
| 33 | +TMP_DIR="/tmp/swoft-repos" |
96 | 34 |
|
97 | | -for LIB_NAME in ${COMPONENTS} ; do |
98 | | - colored_text "\n====== Releasing the component:【${LIB_NAME}】" cyan |
| 35 | +for LIB_NAME in ${SUB_REPOS} ; do |
| 36 | + echo "" |
| 37 | + echo "====== Releasing the component:【${LIB_NAME}】" |
99 | 38 |
|
100 | 39 | # REMOTE_URL=`git remote get-url ${LIB_NAME}` |
101 | | -# REMOTE_URL="git@github.com:swoft-cloud/swoft-${LIB_NAME}.git" |
| 40 | + REMOTE_URL="git@github.com:swoft-cloud/swoft-${LIB_NAME}.git" |
102 | 41 |
|
103 | | - colored_text "Check sub-component remote" |
104 | | - yellow_text "> git remote -v | grep ${LIB_NAME}" |
105 | | - REMOTE_INFO=`git remote -v | grep ${LIB_NAME}` |
| 42 | + echo "> rm -rf ${TMP_DIR} && mkdir ${TMP_DIR}"; |
| 43 | + rm -rf ${TMP_DIR} && mkdir ${TMP_DIR}; |
106 | 44 |
|
107 | | - if [[ -z "$REMOTE_INFO" ]]; then |
108 | | - red_text "Not found remote for the component: ${LIB_NAME}" |
109 | | - continue |
110 | | - fi |
| 45 | + ( |
| 46 | + cd ${TMP_DIR}; |
| 47 | + echo "Begin clone ${REMOTE_URL} to ${TMP_DIR}" |
| 48 | + git clone ${REMOTE_URL} . --depth=200 |
| 49 | + git checkout ${CURRENT_BRANCH}; |
111 | 50 |
|
112 | | - yellow_text "> git pull ${LIB_NAME}" |
113 | | - git pull ${LIB_NAME}; |
| 51 | + # like: v2.0.0 |
| 52 | + LAST_RELEASE=$(git describe --tags $(git rev-list --tags --max-count=1)) |
114 | 53 |
|
115 | | - NEW_BRANCH=${LIB_NAME}-master |
| 54 | + if [[ -z "$LAST_RELEASE" ]]; then |
| 55 | + echo "There has not been any releases. Releasing $1"; |
116 | 56 |
|
117 | | - yellow_text "> git checkout -b ${NEW_BRANCH} ${LIB_NAME}/master" |
118 | | - git checkout -b ${NEW_BRANCH} ${LIB_NAME}/master; |
119 | | - |
120 | | - yellow_text "> git pull ${LIB_NAME} ${TARGET_BRANCH}" |
121 | | - git pull ${LIB_NAME} ${TARGET_BRANCH}; |
122 | | - |
123 | | - # like: v2.0.0 |
124 | | - LAST_RELEASE=$(git describe --tags $(git rev-list --tags --max-count=1)) |
125 | | - |
126 | | - # this is first release |
127 | | - if [[ -z "$LAST_RELEASE" ]]; then |
128 | | - if [[ "$RELEASE_TAG" = "AUTO" ]]; then |
129 | | - read -p "Please input release tag version: " RELEASE_TAG |
130 | | - fi |
131 | | - |
132 | | - colored_text "There has not been any releases. Releasing $RELEASE_TAG"; |
133 | | - else |
134 | | - # auto find next version tag |
135 | | - if [[ "$RELEASE_TAG" = "AUTO" ]]; then |
136 | | - RELEASE_TAG=$(php dtool.php git:tag --only-tag --next-tag ${LAST_RELEASE}) |
137 | | - fi |
| 57 | + # git tag $1 -s -m "Release $1" |
| 58 | + git tag -a $1 -m "Release $1" |
| 59 | + git push origin --tags |
| 60 | + else |
| 61 | + echo "Last release $LAST_RELEASE"; |
138 | 62 |
|
139 | | - echo "Last release $LAST_RELEASE"; |
| 63 | + CHANGES_SINCE_LAST_RELEASE=$(git log --oneline --decorate "$LAST_RELEASE"...master) |
140 | 64 |
|
141 | | - CHANGES_SINCE_LAST_RELEASE=$(git log --oneline --decorate "$LAST_RELEASE"...master) |
| 65 | + if [[ ! -z "$CHANGES_SINCE_LAST_RELEASE" ]]; then |
| 66 | + echo "There are changes since last release. Releasing $1"; |
142 | 67 |
|
143 | | - if [[ ! -z "$CHANGES_SINCE_LAST_RELEASE" ]]; then |
144 | | - colored_text "There are changes since last release. Releasing $RELEASE_TAG"; |
145 | | - else |
146 | | - blue_text "No any change since last release. Skip release"; |
147 | | - continue |
| 68 | + # git tag $1 -s -m "Release $1" |
| 69 | + git tag -a $1 -m "Release $1" |
| 70 | + git push origin --tags |
| 71 | + else |
| 72 | + echo "No change since last release."; |
| 73 | + fi |
148 | 74 | fi |
149 | | - fi |
150 | | - |
151 | | - # git tag $1 -s -m "Release $RELEASE_TAG" |
152 | | - yellow_text "> git tag -a $1 -m \"Release $RELEASE_TAG\"" |
153 | | - git tag -a ${RELEASE_TAG} -m "Release $RELEASE_TAG"; |
154 | | - |
155 | | - yellow_text "> git push $LIB_NAME origin $RELEASE_TAG" |
156 | | - git push ${LIB_NAME} ${RELEASE_TAG}; |
| 75 | + ) |
157 | 76 | done |
158 | 77 |
|
159 | | -yellow_text "> git checkout ${CURRENT_BRANCH}" |
160 | | -git checkout ${CURRENT_BRANCH} |
161 | | - |
162 | | -colored_text "\nRelease Completed!" |
| 78 | +echo "" |
| 79 | +echo "Completed!" |
| 80 | +exit |
0 commit comments