DEV Community

Query Filter
Query Filter

Posted on

bridge-20

#!/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, clean, deduplicate, and sort the class names from the input file
# This filters out empty lines, comments (#), and carriage returns (\r)
RAW_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
        RAW_CLASSES+=("$clean_line")
    fi
done < "$TARGETS_FILE"

# Deduplicate and sort using standard utilities if entries exist
CLASSES=()
if [ ${#RAW_CLASSES[@]} -gt 0 ]; then
    # Unique and sort via pipeline
    while IFS= read -r sorted_class; do
        CLASSES+=("$sorted_class")
    done < <(printf "%s\n" "${RAW_CLASSES[@]}" | sort -u)
fi

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

total_classes=${#CLASSES[@]}

# Write the common class header and import statements
cat << EOF > "$OUTPUT_FILE"
package com.citigroup.qffcometbridge.server;

import java.util.Arrays;
import java.util.Collections;
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() {
EOF

# 5. Conditional generation depending on whether any classes were found
if [ "$total_classes" -eq 0 ]; then
    # If empty, safely return an immutable empty list
    cat << EOF >> "$OUTPUT_FILE"
        return Collections.emptyList();
    }
}
EOF
else
    # If populated, generate the Arrays.asList(...) block
    cat << EOF >> "$OUTPUT_FILE"
        return Arrays.asList(
EOF

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

    # Append the closing brace footer for the non-empty block
    cat << EOF >> "$OUTPUT_FILE"
        );
    }
}
EOF
fi

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

Top comments (0)