From ff16ea611e1690d7c86c4949693e3d80fd169c4c Mon Sep 17 00:00:00 2001 From: Roman Salvador Date: Mon, 11 Sep 2023 10:33:43 +0200 Subject: [PATCH 1/2] Optimize java_stub_template.txt classpath pre-processing --- .../bazel/rules/java/java_stub_template.txt | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt index b3f5070751dfd1..8e813062dbfa50 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt @@ -309,18 +309,20 @@ function create_and_run_classpath_jar() { for path in ${CLASSPATH}; do # Loop through the characters of the path and convert characters that are # not alphanumeric nor -_.~/ to their 2-digit hexadecimal representation - local i c buff - local converted_path="" - - for ((i=0; i<${#path}; i++)); do - c=${path:$i:1} - case ${c} in - [-_.~/a-zA-Z0-9] ) buff=${c} ;; - * ) printf -v buff '%%%02x' "'$c'" - esac - converted_path+="${buff}" - done - path=${converted_path} + if [[ ! $path =~ ^[-_.~/a-zA-Z0-9]*$ ]]; then + local i c buff + local converted_path="" + + for ((i=0; i<${#path}; i++)); do + c=${path:$i:1} + case ${c} in + [-_.~/a-zA-Z0-9] ) buff=${c} ;; + * ) printf -v buff '%%%02x' "'$c'" + esac + converted_path+="${buff}" + done + path=${converted_path} + fi if is_windows; then path="file:/${path}" # e.g. "file:/C:/temp/foo.jar" From cac724da66ed35a424af461fdf68af64a187f54a Mon Sep 17 00:00:00 2001 From: Roman Salvador Date: Mon, 11 Sep 2023 13:51:16 +0200 Subject: [PATCH 2/2] Move 'pwd' call outside the loop and reuse its value --- .../devtools/build/lib/bazel/rules/java/java_stub_template.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt index 8e813062dbfa50..16129cc14dc1b2 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt @@ -306,6 +306,7 @@ function create_and_run_classpath_jar() { OLDIFS="$IFS" IFS="${CLASSPATH_SEPARATOR}" # Use a custom separator for the loop. + current_dir=$(pwd) for path in ${CLASSPATH}; do # Loop through the characters of the path and convert characters that are # not alphanumeric nor -_.~/ to their 2-digit hexadecimal representation @@ -330,7 +331,7 @@ function create_and_run_classpath_jar() { # If not absolute, qualify the path case "${path}" in /*) ;; # Already an absolute path - *) path="$(pwd)/${path}";; # Now qualified + *) path="${current_dir}/${path}";; # Now qualified esac path="file:${path}" # e.g. "file:/usr/local/foo.jar" fi