DEV Community

Query Filter
Query Filter

Posted on

bridge-19

#!/usr/bin/env bash

# Exit immediately if a command exits with a non-zero status
set -e

# 1. Validate Input Arguments
TARGETS_FILE="$1"
OUTPUT_DIR="src/main/java/com/citigroup/qffcometbridge/server"
OUTPUT_FILE="${OUTPUT_DIR}/ProfilerAdditionalTargets.java"

if [ -z "$TARGETS_FILE" ]; then
    echo "Error: Please provide the targets file as the first argument."
    echo "Usage: $0 <path_to_profiler_targets.txt>"
    exit 1
fi

if [ ! -f "$TARGETS_FILE" ]; then
    echo "Error: File '$TARGETS_FILE' not found."
    exit 1
fi

# 2. Ensure the package directory structure exists
mkdir -p "$OUTPUT_DIR"

# 3. Read and clean the class names from the input file
# This filters out empty lines, comments (#), and carriage returns (\r)
CLASSES=()
while IFS= read -r line || [ -n "$line" ]; do
    # Strip carriage returns and leading/trailing whitespace
    clean_line=$(echo "$line" | tr -d '\r' | xargs)

    # Skip empty lines or lines starting with '#'
    if [[ -n "$clean_line" && ! "$clean_line" =~ ^# ]]; then
        CLASSES+=("$clean_line")
    fi
done < "$TARGETS_FILE"

# 4. Generate the Java Class Content
echo "Generating ${OUTPUT_FILE}..."

cat << EOF > "$OUTPUT_FILE"
package com.citigroup.qffcometbridge.server;

import java.util.Arrays;
import java.util.List;

/**
 * Automatically generated by Profiler Target Generator script.
 * Do not modify this file manually.
 */
public class ProfilerAdditionalTargets {

    public static List<Class<?>> getEmbeddedTargets() {
        return Arrays.asList(
EOF

# 5. Append the class literals dynamically
total_classes=${#CLASSES[@]}
for i in "${!CLASSES[@]}"; do
    class_name="${CLASSES[$i]}"
    # If it's the last element, don't append a comma
    if [ $((i + 1)) -eq "$total_classes" ]; then
        echo "            ${class_name}.class" >> "$OUTPUT_FILE"
    else
        echo "            ${class_name}.class," >> "$OUTPUT_FILE"
    fi
done

# 6. Append the Java class footer
cat << EOF >> "$OUTPUT_FILE"
        );
    }
}
EOF

echo "Success! Generated class with ${total_classes} embedded targets."
Enter fullscreen mode Exit fullscreen mode

Top comments (0)