Skip to content

Commit

Permalink
feat(scripts): improve tag.sh script and related functionality
Browse files Browse the repository at this point in the history
- Update tag.sh to handle stashed changes and branch switching
- Refactor test.sh and upm.sh scripts for better readability and maintainability
- Improve error handling and add more informative output in all scripts
  • Loading branch information
godcong committed Nov 15, 2024
1 parent cd7969a commit 1b5e13e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 79 deletions.
43 changes: 28 additions & 15 deletions scripts/tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ check_for_go_mod() {
local dir="$1"
local go_mod_name="go.mod"
local updated=1 # Assume not updated by default

# Change to the directory
cd "$dir" || return 1

# Check if the go.mod file exists in the directory
if [ -f "$go_mod_name" ]; then
echo "Find dependencies go.mod in directories:"
echo " ->DIR: $dir"
updated=0
fi

# Return to the original working directory for the next iteration
cd "$ORIGINAL_DIR" || return 1

# Return the update status
return $updated
}
Expand All @@ -37,22 +37,22 @@ function_checks() {
echo "Error: get_latest_tag function is not defined"
exit 1
fi

if ! declare -f get_matching_tags >/dev/null; then
echo "Error: get_matching_tags function is not defined"
exit 1
fi

if ! declare -f get_head_version_tag >/dev/null; then
echo "Error: get_head_version_tag function is not defined"
exit 1
fi

if ! declare -f get_next_module_version >/dev/null; then
echo "Error: get_next_module_version function is not defined"
exit 1
fi

if ! declare -f get_latest_module_tag >/dev/null; then
echo "Error: get_latest_module_tag function is not defined"
exit 1
Expand All @@ -65,11 +65,11 @@ handle_go_mod_directory() {
local module="main"
local module_name
module_name="$(echo "$dir" | sed "s/^.\///")" # Drop the beginning './'

if [ "$module_name" != "." ]; then
module="$module_name"
fi

if check_for_go_mod "$dir"; then
local HEAD_TAG
local LATEST_TAG
Expand All @@ -85,31 +85,40 @@ handle_go_mod_directory() {
echo " ->LATEST_TAG: $LATEST_TAG"
NEXT_TAG=$(get_next_module_version "$module_name")
echo " ->NEXT_TAG: $NEXT_TAG"

if [ -z "$HEAD_TAG" ]; then
echo "Creating new tag: $NEXT_TAG"
create_new_tag "$NEXT_TAG"
fi

echo ""
fi

}

# Define a function to traverse directories and apply the handle_go_mod_directory function
add_go_mod_tag() {
echo "Checking for go.mod files..."
function_checks
local module_name="$1"

echo ""
echo "COMMIT_HASH: $(get_current_commit_hash "$module_name")"
echo ""

local current_branch="main"
current_branch=$(git branch --show-current)
echo "Current branch: $current_branch"

# Save all local changes to the Git stash
echo "Saving all local changes and change to the main branch..."
git stash save --include-untracked
git checkout -B "main" "original/main"

# If a module_name is specified, process only that directory
if [ "$module_name" == "." ]; then
handle_go_mod_directory "."
elif [ -n "$module_name" ]; then
elif [ -n "$module_name" ]; then
handle_go_mod_directory "$module_name"
else
# Skip the root directory ('.')
Expand All @@ -120,6 +129,10 @@ add_go_mod_tag() {
handle_go_mod_directory "$dir"
done
fi

# Restore the original branch and pop the stash
git checkout "$current_branch"
git stash pop
}

add_go_mod_tag "$1"
108 changes: 54 additions & 54 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,64 @@ ROOT_DIR=$1

# Define a function to check for the existence of a go.mod file in a directory and perform corresponding actions
check_go_mod_and_test() {
local dir="$1"
local go_mod_name="go.mod"
local need_test=1 # Assume not need_test by default

# Change to the directory
cd "$dir" || return 1

# Check if the go.mod file exists in the directory
if [ -f "$go_mod_name" ]; then
# If it exists, perform specific operations

# go get -u ./...
# go mod tidy

# local test_status=$?
# Only mark as need_test if tests pass
# if [ $test_status -eq 0 ]; then
need_test=0
# fi
fi

# Return to the original working directory
cd "$ORIGINAL_DIR" || return 1
# Return the update status
return $need_test
local dir="$1"
local go_mod_name="go.mod"
local need_test=1 # Assume not need_test by default
# Change to the directory
cd "$dir" || return 1
# Check if the go.mod file exists in the directory
if [ -f "$go_mod_name" ]; then
# If it exists, perform specific operations
# go get -u ./...
# go mod tidy
# local test_status=$?
# Only mark as need_test if tests pass
# if [ $test_status -eq 0 ]; then
need_test=0
# fi
fi
# Return to the original working directory
cd "$ORIGINAL_DIR" || return 1
# Return the update status
return $need_test
}

# Define a function to traverse directories and apply the check_go_mod_and_act function
check_all_go_mod() {
local start_dir="$1"

# If a module_name is specified, process only that directory
if [ "$start_dir" != "." ]; then
# if check_go_mod_and_test "./$module_name"; then
# echo "Processing directories in: ./$module_name"
# local need_test=$?
# if [ $need_test -eq 0 ]; then
# git_commit_changes "./$module_name"
# fi
start_dir="./$start_dir"
else
start_dir="."
fi

# Skip the root directory ('.')
find "$start_dir" -mindepth 1 -type d -not -path "./.*" -print0 | while IFS= read -r -d '' dir; do

cd "$ORIGINAL_DIR" || return 1
if check_go_mod_and_test "$dir"; then
local need_test=$?
if [ $need_test -eq 0 ]; then
echo "Testing packages in: $dir"
cd "$dir" || continue
go mod tidy || return 1
go test -race ./... || return 1
fi
fi
done
local start_dir="$1"
# If a module_name is specified, process only that directory
if [ "$start_dir" != "." ]; then
# if check_go_mod_and_test "./$module_name"; then
# echo "Processing directories in: ./$module_name"
# local need_test=$?
# if [ $need_test -eq 0 ]; then
# git_commit_changes "./$module_name"
# fi
start_dir="./$start_dir"
else
start_dir="."
fi
# Skip the root directory ('.')
find "$start_dir" -mindepth 1 -type d -not -path "./.*" -print0 | while IFS= read -r -d '' dir; do
cd "$ORIGINAL_DIR" || return 1
if check_go_mod_and_test "$dir"; then
local need_test=$?
if [ $need_test -eq 0 ]; then
echo "Testing packages in: $dir"
cd "$dir" || continue
go mod tidy || return 1
go test -race ./... || return 1
fi
fi
done
}

check_all_go_mod "$ROOT_DIR"
20 changes: 10 additions & 10 deletions scripts/upm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ check_go_mod_and_act() {
local dir="$1"
local go_mod_name="go.mod"
local updated=1 # Assume not updated by default

# Change to the directory
cd "$dir" || return 1

# Check if the go.mod file exists in the directory
if [ -f "$go_mod_name" ]; then
# If it exists, perform specific operations
echo "Updating packages in: $dir"
go get -u ./...
go mod tidy

go test -race ./... || true
local test_status=$?
# Only mark as updated if tests pass
if [ $test_status -eq 0 ]; then
updated=0
fi
fi

# Return to the original working directory
cd "$ORIGINAL_DIR" || return 1
# Return the update status
Expand All @@ -39,36 +39,36 @@ git_commit_changes() {
local dir="$1"
local module_name=""
local commit_message=""

# Change to the directory
cd "$dir" || return

# Construct the commit message, including the module name
# the module name must be the directory name without the beginning './'
# otherwise, the commit message will be incorrect
module_name=$(echo "$dir" | sed "s/^.\///") # Drop the beginning './'

echo "Checking update ${module_name}/(go.mod|go.sum)"
if git status --porcelain | grep -q -E "^[ ]?(M)[ ]? ${module_name}/(go\.mod|go\.sum)$"; then
# Add go.mod and go.sum to the Git staging area
git add go.mod go.sum

commit_message="feat($module_name): Update go.mod and go.sum for ${module_name}"
echo "Committing changes in [$module_name] with message: $commit_message"
# Commit the changes
git commit -m "$commit_message"
else
echo "No changes to commit in [$module_name]"
fi

# Return to the original working directory
cd "$ORIGINAL_DIR" || return
}

# Define a function to traverse directories and apply the check_go_mod_and_act function
update_go_mod() {
local module_name="$1"

# If a module_name is specified, process only that directory
if [ -n "$module_name" ]; then
if check_go_mod_and_act "./$module_name"; then
Expand Down

0 comments on commit 1b5e13e

Please sign in to comment.