Skip to content

Commit

Permalink
chore(hack): add release_mod script
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Nov 17, 2024
1 parent 3459602 commit dd186e3
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
44 changes: 44 additions & 0 deletions hack/lib.sh
Original file line number Diff line number Diff line change
@@ -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}"
}
83 changes: 83 additions & 0 deletions hack/release_mod.sh
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit dd186e3

Please sign in to comment.