|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Default values |
| 5 | +INSTALL_PATH="" |
| 6 | +REPO="PowerShell/DSC" |
| 7 | +BRANCH="main" |
| 8 | +RUN_ID="" |
| 9 | + |
| 10 | +# Parse command line arguments |
| 11 | +while [[ $# -gt 0 ]]; do |
| 12 | + case $1 in |
| 13 | + --install-path) |
| 14 | + INSTALL_PATH="$2" |
| 15 | + shift 2 |
| 16 | + ;; |
| 17 | + --repo) |
| 18 | + REPO="$2" |
| 19 | + shift 2 |
| 20 | + ;; |
| 21 | + --branch) |
| 22 | + BRANCH="$2" |
| 23 | + shift 2 |
| 24 | + ;; |
| 25 | + --run-id) |
| 26 | + RUN_ID="$2" |
| 27 | + shift 2 |
| 28 | + ;; |
| 29 | + *) |
| 30 | + echo "Unknown option: $1" |
| 31 | + exit 1 |
| 32 | + ;; |
| 33 | + esac |
| 34 | +done |
| 35 | + |
| 36 | +# Check for GitHub CLI |
| 37 | +if ! command -v gh &> /dev/null; then |
| 38 | + echo "Error: GitHub CLI (gh) is not installed." |
| 39 | + echo "Please install it from: https://cli.github.com/" |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Detect platform |
| 44 | +if [[ "$OSTYPE" == "linux-gnu"* ]]; then |
| 45 | + PLATFORM="linux" |
| 46 | +elif [[ "$OSTYPE" == "darwin"* ]]; then |
| 47 | + PLATFORM="macos" |
| 48 | +else |
| 49 | + echo "Error: Unsupported OS: $OSTYPE" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +# Set default install path if not provided |
| 54 | +if [[ -z "$INSTALL_PATH" ]]; then |
| 55 | + INSTALL_PATH="$HOME/.dsc/bin" |
| 56 | +fi |
| 57 | + |
| 58 | +# Fetch run ID if not provided |
| 59 | +if [[ -z "$RUN_ID" ]]; then |
| 60 | + echo "Fetching latest successful build for branch '$BRANCH'..." |
| 61 | + RUN_ID=$(gh run list -R "$REPO" --branch "$BRANCH" --workflow rust --status success -L 1 --json databaseId -q ".[0].databaseId") |
| 62 | + |
| 63 | + if [[ -z "$RUN_ID" ]]; then |
| 64 | + echo "Error: Failed to find a successful build to install from" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +fi |
| 68 | + |
| 69 | +echo "Downloading artifacts from run $RUN_ID..." |
| 70 | + |
| 71 | +# Create temporary directory |
| 72 | +TMP_DIR=$(mktemp -d) |
| 73 | +trap "rm -rf $TMP_DIR" EXIT |
| 74 | + |
| 75 | +# Download artifact |
| 76 | +gh run download -R "$REPO" "$RUN_ID" -n "${PLATFORM}-bin" --dir "$TMP_DIR" |
| 77 | + |
| 78 | +# Find the tar file |
| 79 | +TAR_FILE=$(find "$TMP_DIR" -name "*.tar.gz" -o -name "*.tgz" | head -n 1) |
| 80 | + |
| 81 | +if [[ -z "$TAR_FILE" ]]; then |
| 82 | + echo "Error: Failed to find downloaded artifact" |
| 83 | + exit 1 |
| 84 | +fi |
| 85 | + |
| 86 | +# Extract |
| 87 | +echo "Extracting archive..." |
| 88 | +tar -xzf "$TAR_FILE" -C "$TMP_DIR" |
| 89 | + |
| 90 | +# Install |
| 91 | +echo "Installing to $INSTALL_PATH..." |
| 92 | +mkdir -p "$INSTALL_PATH" |
| 93 | +mv "$TMP_DIR/dsc" "$INSTALL_PATH/dsc" |
| 94 | +chmod +x "$INSTALL_PATH/dsc" |
| 95 | + |
| 96 | +# Get version |
| 97 | +VERSION=$("$INSTALL_PATH/dsc" --version 2>&1 || echo "unknown") |
| 98 | + |
| 99 | +echo "" |
| 100 | +echo "Successfully installed DSC to $INSTALL_PATH/dsc" |
| 101 | +echo "Version: $VERSION" |
| 102 | +echo "From: https://github.com/$REPO/actions/runs/$RUN_ID" |
| 103 | +echo "" |
| 104 | +echo "To use DSC, add the following to your PATH:" |
| 105 | +echo " export PATH=\"\$PATH:$INSTALL_PATH\"" |
0 commit comments