From 00775cedff16adb459e5cf56e1d41282f11f6182 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Mon, 17 Jun 2024 21:24:16 -0700 Subject: [PATCH 01/12] Added support for checkstle2junit.xslt --- java/BUILD.bazel | 1 + java/checkstyle2junit.xslt | 44 +++++++++++++++++++++++++++++++++++++ java/private/checkstyle.bzl | 20 ++++++++++++++--- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 java/checkstyle2junit.xslt diff --git a/java/BUILD.bazel b/java/BUILD.bazel index 71889967..5f60161b 100644 --- a/java/BUILD.bazel +++ b/java/BUILD.bazel @@ -17,6 +17,7 @@ alias( exports_files([ "checkstyle-strict.xml", "google-checks.xml", + "checkstyle2junit.xslt" ]) checkstyle_config( diff --git a/java/checkstyle2junit.xslt b/java/checkstyle2junit.xslt new file mode 100644 index 00000000..1c474ae1 --- /dev/null +++ b/java/checkstyle2junit.xslt @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Line + + : + + + + + diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index bedec371..6fa073b6 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -17,15 +17,24 @@ def _checkstyle_impl(ctx): script = "\n".join([ "#!/usr/bin/env bash", "set -o pipefail", - "set -e", + "set +e", "OLDPWD=$PWD", ] + maybe_cd_config_dir + [ - "$OLDPWD/{lib} -f {output_format} -c {config} {srcs} |sed s:$OLDPWD/::g".format( + "$OLDPWD/{lib} -o checkstyle.xml -f xml -c {config} {srcs}".format( lib = info.checkstyle.short_path, output_format = output_format, config = config.basename, srcs = " ".join(["$OLDPWD/" + f.short_path for f in ctx.files.srcs]), ), + "checkstyle_exit_code=$?", + "cat checkstyle.xml", + # Apply sed to the file in place + "sed s:$OLDPWD/::g checkstyle.xml > checkstyle-stripped.xml", + # Run xsltproc to convert the XML + "xsltproc {xslt} checkstyle-stripped.xml > $XML_OUTPUT_FILE".format( + xslt = ctx.file.xslt.basename, + ), + "exit $checkstyle_exit_code", ]) out = ctx.actions.declare_file(ctx.label.name + "exec") @@ -35,7 +44,7 @@ def _checkstyle_impl(ctx): ) runfiles = ctx.runfiles( - files = ctx.files.srcs + [info.checkstyle], + files = ctx.files.srcs + [info.checkstyle] + [ctx.file.xslt], ) return [ @@ -69,6 +78,11 @@ _checkstyle_test = rule( values = ["plain", "xml"], default = "plain", ), + "xslt": attr.label( + doc = "Path to the checkstyle2junit.xslt file", + allow_single_file = True, + default = "@contrib_rules_jvm//java:checkstyle2junit.xslt", + ), }, executable = True, test = True, From cf45fbf598cbf1d11d8a963f3b6c77cc20912ba7 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Mon, 17 Jun 2024 21:36:14 -0700 Subject: [PATCH 02/12] Remove cat --- java/private/checkstyle.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index 6fa073b6..8b1e40bc 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -27,7 +27,6 @@ def _checkstyle_impl(ctx): srcs = " ".join(["$OLDPWD/" + f.short_path for f in ctx.files.srcs]), ), "checkstyle_exit_code=$?", - "cat checkstyle.xml", # Apply sed to the file in place "sed s:$OLDPWD/::g checkstyle.xml > checkstyle-stripped.xml", # Run xsltproc to convert the XML From f5f8534f06d8e55256d3e7f59e1ce0886aa4ae70 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Mon, 17 Jun 2024 21:44:14 -0700 Subject: [PATCH 03/12] Changed to error --- java/checkstyle2junit.xslt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/checkstyle2junit.xslt b/java/checkstyle2junit.xslt index 1c474ae1..a403d373 100644 --- a/java/checkstyle2junit.xslt +++ b/java/checkstyle2junit.xslt @@ -29,7 +29,7 @@ - + @@ -39,6 +39,6 @@ : - + From 030e4ab009e174e9b2987d35efd6b3ba7556904b Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 18 Jun 2024 09:55:50 -0700 Subject: [PATCH 04/12] Changes to show multiple test cases --- java/checkstyle2junit.xslt | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/java/checkstyle2junit.xslt b/java/checkstyle2junit.xslt index a403d373..b15281f1 100644 --- a/java/checkstyle2junit.xslt +++ b/java/checkstyle2junit.xslt @@ -8,7 +8,7 @@ - + @@ -16,29 +16,23 @@ - + - - - - + - + + + + + + Line + + : + + + - - - - - - - - Line - - : - - - - + From b7748eb1079cfdcc34e9a06863c1e71021c41180 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 18 Jun 2024 10:08:26 -0700 Subject: [PATCH 05/12] add number of tests --- java/checkstyle2junit.xslt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/java/checkstyle2junit.xslt b/java/checkstyle2junit.xslt index b15281f1..ae25c26c 100644 --- a/java/checkstyle2junit.xslt +++ b/java/checkstyle2junit.xslt @@ -5,9 +5,9 @@ - + - + @@ -18,10 +18,14 @@ + + + + - + @@ -31,7 +35,7 @@ : - + From 95753b21925eb9d15e9c2db60811b1b2691ba88e Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 18 Jun 2024 10:40:18 -0700 Subject: [PATCH 06/12] more changers to xlst --- java/checkstyle2junit.xslt | 6 ++++-- java/private/checkstyle.bzl | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/java/checkstyle2junit.xslt b/java/checkstyle2junit.xslt index ae25c26c..71a3ad42 100644 --- a/java/checkstyle2junit.xslt +++ b/java/checkstyle2junit.xslt @@ -4,6 +4,9 @@ + + + @@ -19,8 +22,7 @@ - - + diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index 8b1e40bc..6fa073b6 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -27,6 +27,7 @@ def _checkstyle_impl(ctx): srcs = " ".join(["$OLDPWD/" + f.short_path for f in ctx.files.srcs]), ), "checkstyle_exit_code=$?", + "cat checkstyle.xml", # Apply sed to the file in place "sed s:$OLDPWD/::g checkstyle.xml > checkstyle-stripped.xml", # Run xsltproc to convert the XML From b72ef827cd62c66422a37001b91e77d28dbaa87d Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 18 Jun 2024 11:34:30 -0700 Subject: [PATCH 07/12] Add package --- java/checkstyle2junit.xslt | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/java/checkstyle2junit.xslt b/java/checkstyle2junit.xslt index 71a3ad42..4bbdf88e 100644 --- a/java/checkstyle2junit.xslt +++ b/java/checkstyle2junit.xslt @@ -3,20 +3,22 @@ - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + From 9fab46292f833edce33affc0cebe10fc9b4c45ce Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 18 Jun 2024 14:36:43 -0700 Subject: [PATCH 08/12] Added XLST natively --- java/private/checkstyle.bzl | 17 +++++---- .../contrib_rules_jvm/xml/BUILD.bazel | 6 ++++ .../xml/XSLTTransformer.java | 35 +++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel create mode 100644 java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index 6fa073b6..a72191f1 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -27,12 +27,12 @@ def _checkstyle_impl(ctx): srcs = " ".join(["$OLDPWD/" + f.short_path for f in ctx.files.srcs]), ), "checkstyle_exit_code=$?", - "cat checkstyle.xml", # Apply sed to the file in place "sed s:$OLDPWD/::g checkstyle.xml > checkstyle-stripped.xml", - # Run xsltproc to convert the XML - "xsltproc {xslt} checkstyle-stripped.xml > $XML_OUTPUT_FILE".format( - xslt = ctx.file.xslt.basename, + # Run the Java XSLT transformation tool + "$OLDPWD/{xslt_transformer} checkstyle-stripped.xml $OLDPWD/{xslt} > $XML_OUTPUT_FILE".format( + xslt_transformer = ctx.executable._xslt_transformer.short_path, + xslt = ctx.file.xslt.short_path, ), "exit $checkstyle_exit_code", ]) @@ -44,8 +44,8 @@ def _checkstyle_impl(ctx): ) runfiles = ctx.runfiles( - files = ctx.files.srcs + [info.checkstyle] + [ctx.file.xslt], - ) + files = ctx.files.srcs + [info.checkstyle, ctx.file.xslt], + ).merge(ctx.attr._xslt_transformer[DefaultInfo].default_runfiles) return [ DefaultInfo( @@ -83,6 +83,11 @@ _checkstyle_test = rule( allow_single_file = True, default = "@contrib_rules_jvm//java:checkstyle2junit.xslt", ), + "_xslt_transformer": attr.label( + default = "@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/xml:xslt_transformer", + executable=True, + cfg = "host" + ), }, executable = True, test = True, diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel new file mode 100644 index 00000000..ff639f45 --- /dev/null +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel @@ -0,0 +1,6 @@ +java_binary( + name = "xslt_transformer", + srcs = ["XSLTTransformer.java"], + main_class = "XSLTTransformer", + visibility = ["//visibility:public"], +) \ No newline at end of file diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java new file mode 100644 index 00000000..5396c9f9 --- /dev/null +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java @@ -0,0 +1,35 @@ +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.File; + +public class XSLTTransformer { + public static void main(String[] args) throws Exception { + if (args.length != 2) { + System.err.println("Usage: java XSLTTransformer "); + System.exit(1); + } + + String inputXML = args[0]; + String xsltFile = args[1]; + + // Create transformer factory + TransformerFactory factory = TransformerFactory.newInstance(); + + // Load the XSLT file + StreamSource xslt = new StreamSource(new File(xsltFile)); + + // Create a transformer + Transformer transformer = factory.newTransformer(xslt); + + // Load the input XML file + StreamSource xmlInput = new StreamSource(new File(inputXML)); + + // Set the output file + StreamResult xmlOutput = new StreamResult(System.out); + + // Perform the transformation + transformer.transform(xmlInput, xmlOutput); + } +} From c2d612da3a6c3805e408936137ff31d1ee2f8e6d Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 18 Jun 2024 14:43:06 -0700 Subject: [PATCH 09/12] Remove format type --- java/private/checkstyle.bzl | 7 ------- 1 file changed, 7 deletions(-) diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index a72191f1..2c4b0db6 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -9,7 +9,6 @@ Checkstyle rule implementation def _checkstyle_impl(ctx): info = ctx.attr.config[CheckStyleInfo] config = info.config_file - output_format = info.output_format config_dir = paths.dirname(config.short_path) maybe_cd_config_dir = ["cd {}".format(config_dir)] if config_dir else [] @@ -22,7 +21,6 @@ def _checkstyle_impl(ctx): ] + maybe_cd_config_dir + [ "$OLDPWD/{lib} -o checkstyle.xml -f xml -c {config} {srcs}".format( lib = info.checkstyle.short_path, - output_format = output_format, config = config.basename, srcs = " ".join(["$OLDPWD/" + f.short_path for f in ctx.files.srcs]), ), @@ -73,11 +71,6 @@ _checkstyle_test = rule( [CheckStyleInfo], ], ), - "output_format": attr.string( - doc = "Output Format can be plain or xml. Defaults to plain", - values = ["plain", "xml"], - default = "plain", - ), "xslt": attr.label( doc = "Path to the checkstyle2junit.xslt file", allow_single_file = True, From e6097f876533adaff9ab94e20c2baff34d6c79f1 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 25 Jun 2024 14:01:31 -0700 Subject: [PATCH 10/12] Java formatted XSLTTransformer.java --- .../xml/XSLTTransformer.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java index 5396c9f9..87094d29 100644 --- a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/XSLTTransformer.java @@ -1,35 +1,35 @@ +import java.io.File; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; -import java.io.File; public class XSLTTransformer { - public static void main(String[] args) throws Exception { - if (args.length != 2) { - System.err.println("Usage: java XSLTTransformer "); - System.exit(1); - } + public static void main(String[] args) throws Exception { + if (args.length != 2) { + System.err.println("Usage: java XSLTTransformer "); + System.exit(1); + } - String inputXML = args[0]; - String xsltFile = args[1]; + String inputXML = args[0]; + String xsltFile = args[1]; - // Create transformer factory - TransformerFactory factory = TransformerFactory.newInstance(); + // Create transformer factory + TransformerFactory factory = TransformerFactory.newInstance(); - // Load the XSLT file - StreamSource xslt = new StreamSource(new File(xsltFile)); + // Load the XSLT file + StreamSource xslt = new StreamSource(new File(xsltFile)); - // Create a transformer - Transformer transformer = factory.newTransformer(xslt); + // Create a transformer + Transformer transformer = factory.newTransformer(xslt); - // Load the input XML file - StreamSource xmlInput = new StreamSource(new File(inputXML)); + // Load the input XML file + StreamSource xmlInput = new StreamSource(new File(inputXML)); - // Set the output file - StreamResult xmlOutput = new StreamResult(System.out); + // Set the output file + StreamResult xmlOutput = new StreamResult(System.out); - // Perform the transformation - transformer.transform(xmlInput, xmlOutput); - } + // Perform the transformation + transformer.transform(xmlInput, xmlOutput); + } } From 2e78ba59d21d5ef853dac40a858548bf339946b1 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 25 Jun 2024 14:03:54 -0700 Subject: [PATCH 11/12] Buildifier fixes --- java/BUILD.bazel | 2 +- java/private/checkstyle.bzl | 4 ++-- .../github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/java/BUILD.bazel b/java/BUILD.bazel index 5f60161b..e446576e 100644 --- a/java/BUILD.bazel +++ b/java/BUILD.bazel @@ -17,7 +17,7 @@ alias( exports_files([ "checkstyle-strict.xml", "google-checks.xml", - "checkstyle2junit.xslt" + "checkstyle2junit.xslt", ]) checkstyle_config( diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index 2c4b0db6..e8b490cc 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -78,8 +78,8 @@ _checkstyle_test = rule( ), "_xslt_transformer": attr.label( default = "@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/xml:xslt_transformer", - executable=True, - cfg = "host" + executable = True, + cfg = "host", ), }, executable = True, diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel index ff639f45..bd574d59 100644 --- a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel @@ -3,4 +3,4 @@ java_binary( srcs = ["XSLTTransformer.java"], main_class = "XSLTTransformer", visibility = ["//visibility:public"], -) \ No newline at end of file +) From 519a79549b168dfe825a2f44808215582ed29956 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Sun, 14 Jul 2024 21:41:45 -0400 Subject: [PATCH 12/12] Formatted again. --- java/private/checkstyle.bzl | 4 ++-- .../bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/java/private/checkstyle.bzl b/java/private/checkstyle.bzl index e8b490cc..b43326b0 100644 --- a/java/private/checkstyle.bzl +++ b/java/private/checkstyle.bzl @@ -77,9 +77,9 @@ _checkstyle_test = rule( default = "@contrib_rules_jvm//java:checkstyle2junit.xslt", ), "_xslt_transformer": attr.label( - default = "@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/xml:xslt_transformer", + default = "@contrib_rules_jvm//java/src/com/github/bazel_contrib/contrib_rules_jvm/xml:XSLTTransformer", executable = True, - cfg = "host", + cfg = "exec", ), }, executable = True, diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel index bd574d59..7bc79db5 100644 --- a/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/xml/BUILD.bazel @@ -1,6 +1,14 @@ -java_binary( - name = "xslt_transformer", +load("@rules_java//java:defs.bzl", "java_binary", "java_library") + +java_library( + name = "xml", srcs = ["XSLTTransformer.java"], + visibility = ["//:__subpackages__"], +) + +java_binary( + name = "XSLTTransformer", main_class = "XSLTTransformer", visibility = ["//visibility:public"], + runtime_deps = [":xml"], )