|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Configuration paths |
| 4 | +RUST_CFG_FILE="../minify-html/src/cfg/mod.rs" |
| 5 | +JAVA_CONFIG_FILE="src/main/java/in/wilsonl/minifyhtml/Configuration.java" |
| 6 | +RUST_LIB_FILE="src/main/rust/lib.rs" |
| 7 | + |
| 8 | +# Function to convert snake_case to camelCase |
| 9 | +snake_to_camel() { |
| 10 | + echo "$1" | sed -r 's/_([a-z])/\U\1/g' |
| 11 | +} |
| 12 | + |
| 13 | +# Function to capitalize first letter |
| 14 | +capitalize() { |
| 15 | + echo "$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}" |
| 16 | +} |
| 17 | + |
| 18 | +echo "Extracting fields from Rust configuration..." |
| 19 | + |
| 20 | +# Extract field names using grep and sed |
| 21 | +rust_fields=$(grep -oP '^\s*pub \K[a-zA-Z0-9_]+(?=: bool,?\s*$)' "$RUST_CFG_FILE") |
| 22 | + |
| 23 | +if [ -z "$rust_fields" ]; then |
| 24 | + echo "No boolean fields found in $RUST_CFG_FILE" |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +echo "Found fields:" |
| 29 | +while IFS= read -r field; do |
| 30 | + echo " - $field" |
| 31 | +done <<< "$rust_fields" |
| 32 | + |
| 33 | +# Generate Java fields |
| 34 | +echo "Generating Java fields..." |
| 35 | +java_fields="" |
| 36 | +while IFS= read -r field; do |
| 37 | + camel_field=$(snake_to_camel "$field") |
| 38 | + java_fields="$java_fields boolean $camel_field;\n" |
| 39 | +done <<< "$rust_fields" |
| 40 | + |
| 41 | +# Update Java configuration file between markers |
| 42 | +echo "Updating Java configuration file..." |
| 43 | +temp_java=$(mktemp) |
| 44 | +awk -v fields="$java_fields" ' |
| 45 | + /\/\* BEGIN FIELD NAMES \*\// { |
| 46 | + print $0 |
| 47 | + printf "%s", fields |
| 48 | + skip=1 |
| 49 | + next |
| 50 | + } |
| 51 | + /\/\* END FIELD NAMES \*\// { |
| 52 | + print $0 |
| 53 | + skip=0 |
| 54 | + next |
| 55 | + } |
| 56 | + !skip { |
| 57 | + print |
| 58 | + } |
| 59 | +' "$JAVA_CONFIG_FILE" > "$temp_java" |
| 60 | +mv "$temp_java" "$JAVA_CONFIG_FILE" |
| 61 | + |
| 62 | +# Generate Rust mappings |
| 63 | +echo "Generating Rust JNI mappings..." |
| 64 | +rust_mappings="" |
| 65 | + |
| 66 | +while IFS= read -r field; do |
| 67 | + camel_field=$(snake_to_camel "$field") |
| 68 | + getter_name="get$(capitalize "$camel_field")" |
| 69 | + |
| 70 | + mapping=" $field: env.call_method(*obj, \"$getter_name\", \"()Z\", &[]).unwrap().z().unwrap()," |
| 71 | + |
| 72 | + rust_mappings="$rust_mappings$mapping\n" |
| 73 | +done <<< "$rust_fields" |
| 74 | + |
| 75 | +# Update Rust lib file between markers |
| 76 | +echo "Updating Rust library file..." |
| 77 | +temp_rust=$(mktemp) |
| 78 | +awk -v mappings="$rust_mappings" ' |
| 79 | + /\/\/ BEGIN CONFIGURATION FIELDS/ { |
| 80 | + print $0 |
| 81 | + printf "%s", mappings |
| 82 | + skip=1 |
| 83 | + next |
| 84 | + } |
| 85 | + /\/\/ END CONFIGURATION FIELDS/ { |
| 86 | + print $0 |
| 87 | + skip=0 |
| 88 | + next |
| 89 | + } |
| 90 | + !skip { |
| 91 | + print |
| 92 | + } |
| 93 | +' "$RUST_LIB_FILE" > "$temp_rust" |
| 94 | +mv "$temp_rust" "$RUST_LIB_FILE" |
| 95 | + |
| 96 | +echo "Code generation completed!" |
| 97 | +echo "Updated files:" |
| 98 | +echo " - $JAVA_CONFIG_FILE" |
| 99 | +echo " - $RUST_LIB_FILE" |
0 commit comments