Files
sealos/scripts/generate-dependencies.sh
T
ce8c0bffdb refactor(ci): implement selective module and concurrency (#6384)
* ci(frontend): implement selective module builds with path filters

* update

* fix

* feat(ci): controller/service ci detech (#113)

* feat: controller/service ci detech

* fix: remove needs golangci

* fix: remove name

* fix: remove zombiedetector

* fix: rmeove imagemonitor

* fix: add detect-build-modules.sh change detech

* feat: cross dep detech

* fix: controller and service build

* fix: service cross

* fix: base ref

* chore: reusable detech changes

* fix: force all default values

* feat: generate dependencies

* Update scripts/detect-build-modules.sh

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update scripts/detect-build-modules.sh

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update scripts/generate-dependencies.sh

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update scripts/detect-build-modules.sh

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: permission

* feat: add concurrency

* chore: workflow name

---------

Co-authored-by: zijiren <84728412+zijiren233@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: zijiren233 <pyh1670605849@gmail.com>
2025-12-19 23:23:43 +08:00

109 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Script to generate module dependencies cache file
# This script is sourced by detect-build-modules.sh
# It uses CONTROLLER_MODULES, SERVICE_MODULES, and other functions from the parent script
# Function to parse go.mod and extract dependencies
parse_go_mod_dependencies() {
local type="$1"
local module="$2"
local module_path
module_path=$(get_module_path "$type" "$module")
[[ -z "$module_path" ]] && return
local module_dir="${REPO_ROOT}/${type}/${module_path}"
if [[ ! -f "$module_dir/go.mod" ]]; then
return
fi
local deps=""
# Use go mod edit -json to get direct dependencies
local go_mod_json
# Ensure Go is installed and available
if ! command -v go >/dev/null 2>&1; then
echo "Warning: Go is not installed or not in PATH; cannot extract dependencies for module ${type}/${module} at ${module_dir}" >&2
return
fi
if ! go_mod_json=$(cd "$module_dir" && go mod edit -json 2>/dev/null); then
echo "Warning: Failed to run 'go mod edit -json' for module ${type}/${module} in ${module_dir}; dependencies may be incomplete." >&2
return
fi
# Get current module path to exclude self-reference
local self_path="github.com/labring/sealos/${type}/${module_path}"
# Extract sealos internal dependencies
while IFS= read -r dep_path; do
[[ -z "$dep_path" ]] && continue
[[ "$dep_path" == "$self_path" ]] && continue
if [[ "$dep_path" =~ github\.com/labring/sealos/(controllers|service)/([a-zA-Z0-9_/-]+)$ ]]; then
local dep_type="${BASH_REMATCH[1]}"
local dep_module="${BASH_REMATCH[2]}"
# Find the module name from path
local found_module=""
if [[ "$dep_type" == "controllers" ]]; then
for mod_name in "${!CONTROLLER_MODULES[@]}"; do
if [[ "${CONTROLLER_MODULES[$mod_name]}" == "$dep_module" ]]; then
found_module="$mod_name"
break
fi
done
else
for mod_name in "${!SERVICE_MODULES[@]}"; do
if [[ "${SERVICE_MODULES[$mod_name]}" == "$dep_module" ]]; then
found_module="$mod_name"
break
fi
done
fi
if [[ -n "$found_module" ]]; then
if [[ -n "$deps" ]]; then
deps="${deps},${dep_type}/${found_module}"
else
deps="${dep_type}/${found_module}"
fi
fi
fi
done < <(echo "$go_mod_json" | grep -oE 'github\.com/labring/sealos/(controllers|service)/[a-zA-Z0-9_/-]+' | sort -u)
echo "$deps"
}
# Function to generate all dependencies and save to file
generate_dependencies() {
echo "Generating dependencies..." >&2
local output=""
for type in "controllers" "service"; do
local modules
modules=$(get_all_modules "$type")
while IFS= read -r module; do
[[ -z "$module" ]] && continue
local key="${type}/${module}"
local deps
deps=$(parse_go_mod_dependencies "$type" "$module")
if [[ -n "$deps" ]]; then
output+="${key}=${deps}"$'\n'
fi
done <<<"$modules"
done
# Write to file with header comment
{
echo "# Auto-generated by scripts/generate-dependencies.sh"
echo "# Do not edit manually"
echo ""
echo -n "$output"
} >"$DEPS_CACHE_FILE"
}