DEV Community

Query Filter
Query Filter

Posted on

bridge-18

#!/bin/bash

# --- 1. Input Validation ---
GRADLE_FILE="${1:-build.gradle}"

if [ ! -f "$GRADLE_FILE" ]; then
    echo "Error: Target file does not exist at: $GRADLE_FILE" >&2
    exit 1
fi

# Clean up Windows carriage returns if running in Git Bash/MINGW64
if grep -q $'\r' "$GRADLE_FILE"; then
    echo "[PLUGIN] Normalizing line endings for parsing..."
    sed -i 's/\r//g' "$GRADLE_FILE"
fi

# --- 2. Check If ByteBuddy Already Exists ---
if grep -q "net.bytebuddy:byte-buddy" "$GRADLE_FILE"; then
    echo "[PLUGIN] Skip: ByteBuddy dependencies are already present in $GRADLE_FILE."
    exit 0
fi

# --- 3. Identify Insertion Point and Inject ---
# Find the line number of the VERY FIRST "dependencies {" match
TARGET_LINE=$(grep -n -m 1 "dependencies[[:space:]]*{" "$GRADLE_FILE" | cut -d: -f1)

if [ -z "$TARGET_LINE" ]; then
    echo "Error: Could not locate a 'dependencies {' block in $GRADLE_FILE" >&2
    exit 1
fi

echo "[PLUGIN] Found initial target configuration at line $TARGET_LINE"

# Define the precise dependencies payload block to insert
# Note: Indented with 4 spaces to match standard Gradle style
PAYLOAD="    implementation 'net.bytebuddy:byte-buddy:1.11.13'\n    implementation 'net.bytebuddy:byte-buddy-agent:1.11.13'"

# Use sed to append the payload exactly 1 line below the matched block header
# This works natively on both Linux and Git Bash (Windows)
sed -i "${TARGET_LINE}a ${PAYLOAD}" "$GRADLE_FILE"

echo "[PLUGIN] Success: Injected ByteBuddy configurations right under 'dependencies {'."
Enter fullscreen mode Exit fullscreen mode

Top comments (0)