|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +CHART_ARG="$1" |
| 6 | + |
| 7 | +if [ -z "$CHART_ARG" ]; then |
| 8 | + echo "π Auto-detecting changed Helm charts from git..." |
| 9 | + CHART_ARG=$(git diff --cached --name-only | grep -E '(^|/)Chart\.yaml$' | xargs -n1 dirname | paste -sd, -) |
| 10 | + if [ -z "$CHART_ARG" ]; then |
| 11 | + echo "β
No changed charts detected, skipping" |
| 12 | + exit 0 |
| 13 | + fi |
| 14 | + echo "π¦ Charts to update: $CHART_ARG" |
| 15 | +fi |
| 16 | + |
| 17 | +# Temporary files |
| 18 | +TMP_GRAPH="/tmp/chart_graph.$$" |
| 19 | +TMP_PATHMAP="/tmp/chart_pathmap.$$" |
| 20 | +TMP_VISITED="/tmp/chart_visited.$$" |
| 21 | +TMP_FILTERED="/tmp/chart_filtered.$$" |
| 22 | +TMP_SORTED="/tmp/chart_sorted.$$" |
| 23 | + |
| 24 | +echo "" > "$TMP_GRAPH" |
| 25 | +echo "" > "$TMP_PATHMAP" |
| 26 | +echo "" > "$TMP_VISITED" |
| 27 | +echo "" > "$TMP_FILTERED" |
| 28 | +echo "" > "$TMP_SORTED" |
| 29 | + |
| 30 | +realpath_compat() { |
| 31 | + if command -v realpath >/dev/null 2>&1; then |
| 32 | + realpath "$1" |
| 33 | + else |
| 34 | + python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "$1" |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +# Chart discovery (root and tests/) |
| 39 | +echo "π Discovering all charts..." |
| 40 | +# shellcheck disable=SC2044 |
| 41 | +for chart_file in $(find . -type f -name "Chart.yaml" -path "./*/Chart.yaml" -o -path "./tests/*/Chart.yaml"); do |
| 42 | + chart_dir=$(dirname "$chart_file") |
| 43 | + chart_abs=$(realpath_compat "$chart_dir") |
| 44 | + echo "$chart_abs:$chart_dir" >> "$TMP_PATHMAP" |
| 45 | + |
| 46 | + # Parse internal deps |
| 47 | + awk ' |
| 48 | + /^dependencies:/ { in_deps = 1; next } |
| 49 | + in_deps && /^[[:space:]]*-/ { dep_block = 1; next } |
| 50 | + dep_block && /repository:[[:space:]]*file:\/\// { |
| 51 | + match($0, /file:\/\/[^[:space:]#\"]+/) |
| 52 | + if (RSTART > 0) { |
| 53 | + dep = substr($0, RSTART+7, RLENGTH-7) |
| 54 | + print dep |
| 55 | + } |
| 56 | + } |
| 57 | + in_deps && /^[^[:space:]]/ { in_deps = 0; dep_block = 0 } |
| 58 | + ' "$chart_file" | while read -r dep_rel; do |
| 59 | + dep_abs=$(realpath_compat "$chart_dir/$dep_rel") |
| 60 | + echo "$chart_abs $dep_abs" >> "$TMP_GRAPH" |
| 61 | + done |
| 62 | +done |
| 63 | + |
| 64 | +echo "π¦ Charts discovered:" |
| 65 | +cut -d':' -f2- "$TMP_PATHMAP" |
| 66 | + |
| 67 | +# Graph traversal |
| 68 | +visit_downward() { |
| 69 | + local node="$1" |
| 70 | + grep -q "^$node\$" "$TMP_VISITED" && return |
| 71 | + echo "$node" >> "$TMP_VISITED" |
| 72 | + grep "^$node " "$TMP_GRAPH" | cut -d' ' -f2 | while read -r child; do |
| 73 | + visit_downward "$child" |
| 74 | + done |
| 75 | +} |
| 76 | + |
| 77 | +if [ -z "$CHART_ARG" ]; then |
| 78 | + echo "π‘ No chart specified β processing all charts" |
| 79 | + # shellcheck disable=SC2013 |
| 80 | + for chart_abs in $(cut -d':' -f1 "$TMP_PATHMAP"); do |
| 81 | + visit_downward "$chart_abs" |
| 82 | + done |
| 83 | +else |
| 84 | + echo "π’ Processing chart(s): $CHART_ARG" |
| 85 | + IFS=',' read -r -a TARGET_CHARTS <<< "$CHART_ARG" |
| 86 | + for chart_path in "${TARGET_CHARTS[@]}"; do |
| 87 | + if [ ! -d "$chart_path" ]; then |
| 88 | + echo "β Chart folder '$chart_path' not found." |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + TARGET_PATH=$(realpath_compat "$chart_path") |
| 92 | + echo "π§ͺ Visiting chart: $TARGET_PATH" |
| 93 | + grep "^$TARGET_PATH " "$TMP_GRAPH" || echo " βͺοΈ No dependencies" |
| 94 | + visit_downward "$TARGET_PATH" |
| 95 | + done |
| 96 | +fi |
| 97 | + |
| 98 | +echo "β
Visited charts:" |
| 99 | +cat "$TMP_VISITED" |
| 100 | + |
| 101 | +# Filter graph |
| 102 | +while read -r from to; do |
| 103 | + grep -q "^$from\$" "$TMP_VISITED" && grep -q "^$to\$" "$TMP_VISITED" && echo "$from $to" >> "$TMP_FILTERED" |
| 104 | +done < "$TMP_GRAPH" |
| 105 | + |
| 106 | +echo "π Filtered dependency edges:" |
| 107 | +cat "$TMP_FILTERED" |
| 108 | + |
| 109 | +# Topological sort |
| 110 | +tsort "$TMP_FILTERED" > "$TMP_SORTED" |
| 111 | + |
| 112 | +# Include any isolated charts not in sorted output |
| 113 | +# shellcheck disable=SC2013 |
| 114 | +for node in $(cat "$TMP_VISITED"); do |
| 115 | + grep -q "^$node\$" "$TMP_SORTED" || echo "$node" >> "$TMP_SORTED" |
| 116 | +done |
| 117 | + |
| 118 | +echo "π Topological update order (leaf β root):" |
| 119 | +tac "$TMP_SORTED" |
| 120 | + |
| 121 | +# Run helm updates |
| 122 | +echo "π Running helm dependency updates..." |
| 123 | +while read -r abs_path; do |
| 124 | + rel_path=$(grep "^$abs_path:" "$TMP_PATHMAP" | cut -d':' -f2-) |
| 125 | + if [ -n "$rel_path" ]; then |
| 126 | + echo "π helm dependency update $rel_path" |
| 127 | + helm dependency update "$rel_path" || echo "β Failed in $rel_path" |
| 128 | + fi |
| 129 | +done < <(tac "$TMP_SORTED") |
| 130 | + |
| 131 | +# Cleanup |
| 132 | +rm -f "$TMP_GRAPH" "$TMP_PATHMAP" "$TMP_VISITED" "$TMP_FILTERED" "$TMP_SORTED" |
0 commit comments