From dd186e37b540a835e418ea50ffffb2498eee092e Mon Sep 17 00:00:00 2001 From: iyear Date: Sun, 17 Nov 2024 19:11:41 +0800 Subject: [PATCH] chore(hack): add release_mod script --- hack/lib.sh | 44 ++++++++++++++++++++++++ hack/release_mod.sh | 83 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 hack/lib.sh create mode 100644 hack/release_mod.sh diff --git a/hack/lib.sh b/hack/lib.sh new file mode 100644 index 0000000..877be3c --- /dev/null +++ b/hack/lib.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +set -euo pipefail + +ROOT_MODULE=$(go list -m) + +function set_root_dir { + # shellcheck disable=SC2034 + ROOT_DIR=$(go list -f '{{.Dir}}' "${ROOT_MODULE}") +} + +set_root_dir + +#### Convenient IO methods ##### +COLOR_RED='\033[0;31m' +COLOR_ORANGE='\033[0;33m' +COLOR_GREEN='\033[0;32m' +COLOR_BLUE='\033[0;94m' +COLOR_BOLD='\033[1m' +COLOR_NONE='\033[0m' # No Color + +function log_error { + >&2 echo -n -e "${COLOR_BOLD}${COLOR_RED}" + >&2 echo "$@" + >&2 echo -n -e "${COLOR_NONE}" +} + +function log_warn { + >&2 echo -n -e "${COLOR_ORANGE}" + >&2 echo "$@" + >&2 echo -n -e "${COLOR_NONE}" +} + +function log_succ { + >&2 echo -n -e "${COLOR_GREEN}" + >&2 echo "$@" + >&2 echo -n -e "${COLOR_NONE}" +} + +function log_info { + >&2 echo -n -e "${COLOR_BLUE}" + >&2 echo "$@" + >&2 echo -n -e "${COLOR_NONE}" +} diff --git a/hack/release_mod.sh b/hack/release_mod.sh new file mode 100644 index 0000000..5835771 --- /dev/null +++ b/hack/release_mod.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +# Examples: + +# Update all dependencies to version vX.Y.Z +# ./hack/release_mod.sh deps vX.Y.Z + +# Add tags to all modules with version vX.Y.Z +# ./hack/release_mod.sh tags v0.1.0 + +set -euo pipefail + +source ./hack/lib.sh + +cmd="${1}" +version="${2}" +if [ -z "${version}" ]; then + log_error "version argument is required" + exit 2 +fi + +go mod tidy + +dirs=$(find . -name "go.mod" -not -path "./docs/*" -exec dirname {} \;) + +function deps() { + log_info "Updating dependencies to version ${version}" + log_info "" + + for dir in ${dirs}; do + ( + log_info "Processing ${dir}" + + cd "${dir}" + + go mod tidy + + modules=$(go list -f '{{if not .Main}}{{if not .Indirect}}{{.Path}}{{end}}{{end}}' -m all) + deps=$(echo "${modules}" | grep -E "${ROOT_MODULE}/.*" || true) + + for dep in ${deps}; do + go mod edit -require "${dep}@${version}" + done + + go mod tidy + + cd "${ROOT_DIR}" + + log_succ " Processed ${dir}" + ) + done + + log_succ "" + log_succ "Dependencies updated, and commit them manually" +} + +function tags(){ + log_info "Adding tags to all modules with version ${version}" + log_info "" + + for dir in ${dirs}; do + ( + log_info "Processing ${dir}" + prefix="${dir#./}" + prefix="${prefix#.}" + # if prefix is not empty, append a slash + if [ -n "${prefix}" ]; then + prefix="${prefix}/" + fi + + tag="${prefix}${version}" + git tag "${tag}" + + log_succ " Tag ${tag}" + ) + done + + log_succ "" + log_succ "Tags added, and push them manually" +} + +# run the function +"${cmd}"