Adapt gerrit_plugin.bzl to version in Gerrit core This change adapts the gerrit_plugin.bzl file to the version in Gerrit core and makes it usable for in-tree builds of plugins. Change-Id: I22cdeb2c82bed60faa9687a76742d3a625576a68
diff --git a/MODULE.bazel b/MODULE.bazel index 0d0806a..3c2f98d 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -1,6 +1,18 @@ +GERRIT_API_VERSION = "3.12.0" module( name = "com_googlesource_gerrit_bazlets", + version = GERRIT_API_VERSION, ) bazel_dep(name = "rules_java", version = "7.6.1") bazel_dep(name = "rules_python", version = "1.7.0") + +gerrit_api_version = use_repo_rule( + "//:gerrit_api_version.bzl", + "gerrit_api_version", +) +gerrit_api_version( + name = "gerrit_api_version", + version = GERRIT_API_VERSION, + visibility = ["//visibility:public"], +)
diff --git a/gerrit_api_version.bzl b/gerrit_api_version.bzl index 58fa01a..bba1de4 100644 --- a/gerrit_api_version.bzl +++ b/gerrit_api_version.bzl
@@ -13,8 +13,9 @@ # limitations under the License. def _gerrit_api_version_impl(ctx): - ctx.file("version.txt", "Gerrit-ApiVersion: " + ctx.attr.version, False) - ctx.file("BUILD.bazel", 'exports_files(["version.txt"])', False) + ctx.file("version.bzl", "GERRIT_API_VERSION = \"" + ctx.attr.version + "\"", False) + ctx.file("version.txt", ctx.attr.version, False) + ctx.file("BUILD.bazel", "exports_files([\"version.txt\"])", False) gerrit_api_version = repository_rule( implementation = _gerrit_api_version_impl,
diff --git a/gerrit_plugin.bzl b/gerrit_plugin.bzl index 258829c..9828043 100644 --- a/gerrit_plugin.bzl +++ b/gerrit_plugin.bzl
@@ -1,3 +1,7 @@ +""" +Build rules for plugins. +""" + load("@rules_java//java:defs.bzl", "java_binary", "java_library") load( "//tools:commons.bzl", @@ -6,6 +10,7 @@ _plugin_test_deps = "PLUGIN_TEST_DEPS", ) load("//tools:genrule2.bzl", "genrule2") +load("//tools:junit.bzl", "junit_tests") """Bazel rule for building [Gerrit Code Review](https://www.gerritcodereview.com/) gerrit_plugin is rule for building Gerrit plugins using Bazel. @@ -22,40 +27,74 @@ srcs = [], resources = [], resource_jars = [], + runtime_deps = [], manifest_entries = [], dir_name = None, target_suffix = "", + deploy_env = [], **kwargs): - if not dir_name: - dir_name = name + """Builds a Gerrit plugin. - if native.module_name(): + Args: + name: The name of the plugin. + deps: List of additional dependencies for the plugin. + provided_deps: List of dependencies that are provided by Gerrit and should not be bundled. + srcs: List of Java source files for the plugin. + resources: List of resource files to be included in the plugin JAR. + resource_jars: List of JARs containing resources. + runtime_deps: List of runtime dependencies. + manifest_entries: List of additional lines to add to the plugin's manifest file. + dir_name: The directory name for the plugin, used in stamping. Defaults to `name`. + target_suffix: Suffix to append to the final plugin JAR name. + deploy_env: Environment variables for the deploy JAR. + **kwargs: Additional arguments passed to the underlying `java_library` and `java_binary` rules. + + This rule creates a deployable .jar file for a Gerrit plugin.""" + + # Determine where to get Gerrit API dependencies from + if not native.module_name(): + # Gerrit and/or plugin does not use bazel modules yet; use Gerrit API from + # maven repository as defined in gerrit_api.bzl + # TODO(thomas): Remove after migration to Bazel modules is complete + gerrit_api_neverlink = PLUGIN_DEPS_NEVERLINK + elif (native.module_name() == "gerrit"): + # In-tree build, i.e. Gerrit is the main module; use Gerrit API from Gerrit + # source tree + gerrit_api_neverlink = ["//plugins:plugin-lib-neverlink"] + else: + # Standalone build; use Gerrit API from maven repository as defined in + # plugin's module java_library( - name = "gerrit-api-neverlink", + name = name + "-gerrit-api-neverlink", neverlink = 1, + visibility = ["//visibility:public"], exports = ["@external_plugin_deps//:com_google_gerrit_gerrit_plugin_api"], ) - plugin_deps_neverlink = [":gerrit-api-neverlink"] - else: - plugin_deps_neverlink = PLUGIN_DEPS_NEVERLINK + gerrit_api_neverlink = [":" + name + "-gerrit-api-neverlink"] java_library( name = name + "__plugin", srcs = srcs, resources = resources, - deps = provided_deps + deps + plugin_deps_neverlink, + deps = provided_deps + deps + gerrit_api_neverlink, + runtime_deps = runtime_deps, visibility = ["//visibility:public"], **kwargs ) + if not dir_name: + dir_name = name + java_binary( name = "%s__non_stamped" % name, deploy_manifest_lines = manifest_entries + ["Gerrit-ApiType: plugin"], main_class = "Dummy", runtime_deps = [ ":%s__plugin" % name, - ] + resource_jars, + ] + runtime_deps + resource_jars, + deploy_env = deploy_env, visibility = ["//visibility:public"], + **kwargs ) native.genrule( @@ -80,7 +119,7 @@ "API_VERSION=$$(cat $(location @gerrit_api_version//:version.txt))", "cd $$TMP", "unzip -q $$ROOT/$<", - "echo \"Implementation-Version: $$GEN_VERSION\n$$API_VERSION\n$$(cat META-INF/MANIFEST.MF)\" > META-INF/MANIFEST.MF", + "echo \"Implementation-Version: $$GEN_VERSION\nGerrit-ApiVersion: $$API_VERSION\n$$(cat META-INF/MANIFEST.MF)\" > META-INF/MANIFEST.MF", "find . -exec touch '{}' ';'", "zip -Xqr $$ROOT/$@ .", ]), @@ -91,3 +130,45 @@ outs = ["%s%s.jar" % (name, target_suffix)], visibility = ["//visibility:public"], ) + +def gerrit_plugin_tests( + name, + srcs = [], + deps = [], + **kwargs): + """Runs junit tests for a Gerrit plugin. + + Args: + name: The name of the plugin. + deps: List of additional dependencies for the plugin. + srcs: List of Java source files for the plugin. + **kwargs: Additional arguments passed to the underlying `junit_tests` rule. + """ + + # Determine where to get Gerrit API dependencies from + if not native.module_name(): + # Gerrit and/or plugin does not use bazel modules yet; use Gerrit API from + # maven repository as defined in gerrit_api.bzl + # TODO(thomas): Remove after migration to Bazel modules is complete + gerrit_api_deps = PLUGIN_DEPS + PLUGIN_TEST_DEPS + elif (native.module_name() == "gerrit"): + # In-tree build, i.e. Gerrit is the main module; use Gerrit API from Gerrit + # source tree + gerrit_api_deps = [ + "//plugins:plugin-lib", + "//java/com/google/gerrit/acceptance:lib", + ] + else: + # Standalone build; use Gerrit API from maven repository as defined in + # plugin's module + gerrit_api_deps = [ + "@external_plugin_deps//:com_google_gerrit_gerrit_acceptance_framework", + "@external_plugin_deps//:com_google_gerrit_gerrit_plugin_api", + ] + + junit_tests( + name = name, + srcs = srcs, + deps = deps + gerrit_api_deps, + **kwargs + )