microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
.github/skills/installer/hve-core-installer/scripts/agent-copy.sh
85lines · modecode
| 1 | #!/usr/bin/env bash |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | # Copies selected HVE-Core agents to the target repository. |
| 5 | # Creates .github/agents/, copies agent files, computes SHA256 hashes, |
| 6 | # and writes .hve-tracking.json manifest for upgrade tracking. |
| 7 | # Usage: agent-copy.sh <hve_core_base_path> <collection_id> <file1> [file2...] |
| 8 | # Files are paths relative to the agents/ directory. |
| 9 | set -euo pipefail |
| 10 | |
| 11 | hve_core_base_path="${1:?Usage: $0 <hve_core_base_path> <collection_id> <file1> [file2...]}" |
| 12 | collection_id="${2:?Usage: $0 <hve_core_base_path> <collection_id> <file1> [file2...]}" |
| 13 | shift 2 |
| 14 | |
| 15 | source_base="$hve_core_base_path/.github/agents" |
| 16 | target_dir=".github/agents" |
| 17 | manifest_path=".hve-tracking.json" |
| 18 | keep_existing="${KEEP_EXISTING:-false}" |
| 19 | collisions_file="${COLLISIONS_FILE:-}" |
| 20 | has_jq=false |
| 21 | command -v jq >/dev/null 2>&1 && has_jq=true |
| 22 | |
| 23 | # Load collision list when keep_existing is enabled |
| 24 | declare -A collision_set |
| 25 | if [ "$keep_existing" = "true" ] && [ -n "$collisions_file" ] && [ -f "$collisions_file" ]; then |
| 26 | while IFS= read -r line; do |
| 27 | [ -n "$line" ] && collision_set["$line"]=1 |
| 28 | done < "$collisions_file" |
| 29 | fi |
| 30 | |
| 31 | # Create target directory |
| 32 | mkdir -p "$target_dir" |
| 33 | |
| 34 | # Get version from package.json |
| 35 | if [ "$has_jq" = true ]; then |
| 36 | version=$(jq -r '.version' "$hve_core_base_path/package.json") |
| 37 | else |
| 38 | version=$(grep -o '"version": *"[^"]*"' "$hve_core_base_path/package.json" | head -1 | sed 's/.*"\([^"]*\)"/\1/') |
| 39 | fi |
| 40 | |
| 41 | # Initialize manifest JSON |
| 42 | installed=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 43 | files_json="{}" |
| 44 | |
| 45 | # Copy files (source paths are relative to agents/, target is flat) |
| 46 | for file in "$@"; do |
| 47 | filename=$(basename "$file") |
| 48 | source_path="$source_base/$file" |
| 49 | target_path="$target_dir/$filename" |
| 50 | rel_path=".github/agents/$filename" |
| 51 | |
| 52 | # Skip files the user chose to keep during collision resolution |
| 53 | if [ "$keep_existing" = "true" ] && [ -n "${collision_set[$target_path]+x}" ]; then |
| 54 | echo "⏭️ Kept existing: $filename" |
| 55 | continue |
| 56 | fi |
| 57 | |
| 58 | cp "$source_path" "$target_path" |
| 59 | hash=$(sha256sum "$target_path" | cut -d' ' -f1) |
| 60 | if [ "$has_jq" = true ]; then |
| 61 | files_json=$(echo "$files_json" | jq --arg path "$rel_path" --arg ver "$version" --arg sha "$hash" \ |
| 62 | '. + {($path): {"version": $ver, "sha256": $sha, "status": "managed"}}') |
| 63 | else |
| 64 | # Build JSON entries without jq |
| 65 | if [ "$files_json" = "{}" ]; then |
| 66 | files_json="{\"$rel_path\": {\"version\": \"$version\", \"sha256\": \"$hash\", \"status\": \"managed\"}}" |
| 67 | else |
| 68 | files_json="${files_json%\}}, \"$rel_path\": {\"version\": \"$version\", \"sha256\": \"$hash\", \"status\": \"managed\"}}" |
| 69 | fi |
| 70 | fi |
| 71 | echo "✅ Copied $filename" |
| 72 | done |
| 73 | |
| 74 | # Write manifest |
| 75 | if [ "$has_jq" = true ]; then |
| 76 | jq -n --arg src "microsoft/hve-core" --arg ver "$version" --arg inst "$installed" \ |
| 77 | --arg col "$collection_id" --argjson files "$files_json" \ |
| 78 | '{source: $src, version: $ver, installed: $inst, collection: $col, files: $files}' \ |
| 79 | > "$manifest_path" |
| 80 | else |
| 81 | cat > "$manifest_path" <<EOF |
| 82 | {"source": "microsoft/hve-core", "version": "$version", "installed": "$installed", "collection": "$collection_id", "files": $files_json} |
| 83 | EOF |
| 84 | fi |
| 85 | echo "✅ Created $manifest_path" |
| 86 | |