Skip to content

Commit c46af90

Browse files
committed
feat: script to generate java configs
Signed-off-by: jogerj <[email protected]>
1 parent 39031ac commit c46af90

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

minify-html-java/src/main/java/in/wilsonl/minifyhtml/Configuration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
@Value
1818
public class Configuration {
19+
/* BEGIN FIELD NAMES */
1920
boolean allowNoncompliantUnquotedAttributeValues;
2021
boolean allowOptimalEntities;
2122
boolean allowRemovingSpacesBetweenAttributes;
@@ -31,4 +32,5 @@ public class Configuration {
3132
boolean preserveChevronPercentTemplateSyntax;
3233
boolean removeBangs;
3334
boolean removeProcessingInstructions;
35+
/* END FIELD NAMES */
3436
}

minify-html-java/src/main/rust/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn build_cfg(env: &JNIEnv, obj: &JObject) -> Cfg {
1111
#[rustfmt::skip]
1212
// This is a statement because "attributes on expressions are experimental".
1313
let cfg = Cfg {
14+
// BEGIN CONFIGURATION FIELDS
1415
allow_noncompliant_unquoted_attribute_values: env.call_method(*obj, "getAllowNoncompliantUnquotedAttributeValues", "()Z", &[]).unwrap().z().unwrap(),
1516
allow_optimal_entities: env.call_method(*obj, "getAllowOptimalEntities", "()Z", &[]).unwrap().z().unwrap(),
1617
allow_removing_spaces_between_attributes: env.call_method(*obj, "getAllowRemovingSpacesBetweenAttributes", "()Z", &[]).unwrap().z().unwrap(),
@@ -26,6 +27,7 @@ fn build_cfg(env: &JNIEnv, obj: &JObject) -> Cfg {
2627
preserve_chevron_percent_template_syntax: env.call_method(*obj, "getPreserveChevronPercentTemplateSyntax", "()Z", &[]).unwrap().z().unwrap(),
2728
remove_bangs: env.call_method(*obj, "getRemoveBangs", "()Z", &[]).unwrap().z().unwrap(),
2829
remove_processing_instructions: env.call_method(*obj, "getRemoveProcessingInstructions", "()Z", &[]).unwrap().z().unwrap(),
30+
// END CONFIGURATION FIELDS
2931
};
3032
cfg
3133
}

minify-html-java/update-java.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)