Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build_defs/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
load(
":intellij_plugin.bzl",
_intellij_plugin = "intellij_plugin",
_intellij_plugin_library = "intellij_plugin_library",
_optional_plugin_xml = "optional_plugin_xml",
)
load(":intellij_plugin_library.bzl", _intellij_plugin_library = "intellij_plugin_library")

# Re-export these symbols
intellij_plugin = _intellij_plugin
Expand Down
55 changes: 12 additions & 43 deletions build_defs/intellij_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ intellij_plugin(
"""

load("@rules_java//java:defs.bzl", "JavaInfo", "java_binary", "java_common", "java_import")
load(":intellij_plugin_library.bzl", "OptionalPluginXmlInfo", "IntellijPluginLibraryInfo")
load(
"//build_defs:restrictions.bzl",
"RestrictedInfo",
Expand All @@ -46,8 +47,6 @@ load(
"validate_unchecked_internal",
)

_OptionalPluginXmlInfo = provider(fields = ["optional_plugin_xmls"])

def _optional_plugin_xml_impl(ctx):
attr = ctx.attr
optional_plugin_xmls = []
Expand All @@ -56,7 +55,7 @@ def _optional_plugin_xml_impl(ctx):
plugin_xml = ctx.file.plugin_xml,
module = attr.module,
))
return [_OptionalPluginXmlInfo(optional_plugin_xmls = optional_plugin_xmls)]
return [OptionalPluginXmlInfo(optional_plugin_xmls = optional_plugin_xmls)]

optional_plugin_xml = rule(
implementation = _optional_plugin_xml_impl,
Expand All @@ -66,41 +65,11 @@ optional_plugin_xml = rule(
},
)

_IntellijPluginLibraryInfo = provider(fields = ["plugin_xmls", "optional_plugin_xmls", "java_info"])

def _intellij_plugin_library_impl(ctx):
java_info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])

plugin_xmls = []
for target in ctx.attr.plugin_xmls:
for file in target.files.to_list():
plugin_xmls.append(file)

return [
_IntellijPluginLibraryInfo(
plugin_xmls = depset(plugin_xmls, order = "preorder"),
optional_plugin_xmls = [
dep[_OptionalPluginXmlInfo]
for dep in ctx.attr.optional_plugin_xmls
],
java_info = java_info,
),
]

intellij_plugin_library = rule(
implementation = _intellij_plugin_library_impl,
attrs = {
"deps": attr.label_list(providers = [JavaInfo]),
"plugin_xmls": attr.label_list(allow_files = [".xml"]),
"optional_plugin_xmls": attr.label_list(providers = [_OptionalPluginXmlInfo]),
},
)

def _merge_plugin_xmls(ctx):
dep_plugin_xmls = []
for dep in ctx.attr.deps:
if _IntellijPluginLibraryInfo in dep:
dep_plugin_xmls.append(dep[_IntellijPluginLibraryInfo].plugin_xmls)
if IntellijPluginLibraryInfo in dep:
dep_plugin_xmls.append(dep[IntellijPluginLibraryInfo].plugin_xmls)
plugin_xmls = depset([ctx.file.plugin_xml], transitive = dep_plugin_xmls, order = "preorder")

if len(plugin_xmls.to_list()) == 1:
Expand Down Expand Up @@ -145,7 +114,7 @@ A trick to overcome this limitation is to create a synthetic file, which only pu
by a `<depends>first_plugin</depends>` directive on the and contain another <depends>second_plugin</depends> directive
for the second plugin.

Te purpose of `_create_dependency_file_chain` method is to implement this trick. It receives a `_OptionalPluginXmlInfo`
Te purpose of `_create_dependency_file_chain` method is to implement this trick. It receives a `OptionalPluginXmlInfo`
structure, which contains the xml file and N of its plugin dependencies. Then it creates actions to generate N-1
.xml files that make up the dependency chain.

Expand Down Expand Up @@ -175,12 +144,12 @@ def _merge_optional_plugin_xmls(ctx):
module_to_xmls = {}
optional_plugin_xml_providers = []
for dep in ctx.attr.deps:
if _IntellijPluginLibraryInfo in dep:
if IntellijPluginLibraryInfo in dep:
optional_plugin_xml_providers.extend(
dep[_IntellijPluginLibraryInfo].optional_plugin_xmls,
dep[IntellijPluginLibraryInfo].optional_plugin_xmls.to_list(),
)
optional_plugin_xml_providers.extend(
[target[_OptionalPluginXmlInfo] for target in ctx.attr.optional_plugin_xmls],
[target[OptionalPluginXmlInfo] for target in ctx.attr.optional_plugin_xmls],
)
for provider in optional_plugin_xml_providers:
for xml in provider.optional_plugin_xmls:
Expand Down Expand Up @@ -256,15 +225,15 @@ def _package_meta_inf_files(ctx, final_plugin_xml_file, module_to_merged_xmls):
return jar_file

def _intellij_plugin_java_deps_impl(ctx):
java_infos = [dep[_IntellijPluginLibraryInfo].java_info for dep in ctx.attr.deps]
java_infos = [dep[IntellijPluginLibraryInfo].java_info for dep in ctx.attr.deps]
return [java_common.merge(java_infos)]

_intellij_plugin_java_deps = rule(
implementation = _intellij_plugin_java_deps_impl,
attrs = {
"deps": attr.label_list(
mandatory = True,
providers = [[_IntellijPluginLibraryInfo]],
providers = [[IntellijPluginLibraryInfo]],
),
},
)
Expand Down Expand Up @@ -301,9 +270,9 @@ _intellij_plugin_jar = rule(
attrs = {
"deploy_jar": attr.label(mandatory = True, allow_single_file = [".jar"]),
"plugin_xml": attr.label(mandatory = True, allow_single_file = [".xml"]),
"optional_plugin_xmls": attr.label_list(providers = [_OptionalPluginXmlInfo]),
"optional_plugin_xmls": attr.label_list(providers = [OptionalPluginXmlInfo]),
"jar_name": attr.string(mandatory = True),
"deps": attr.label_list(providers = [[_IntellijPluginLibraryInfo]]),
"deps": attr.label_list(providers = [[IntellijPluginLibraryInfo]]),
"restrict_deps": attr.bool(),
"restricted_deps": attr.label_list(aspects = [restricted_deps_aspect]),
"plugin_icons": attr.label_list(allow_files = True),
Expand Down
136 changes: 136 additions & 0 deletions build_defs/intellij_plugin_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

IntellijPluginLibraryInfo = provider(
doc = "Flat intellij plugin library; contains the merged infomation from this library and all its dependencies.",
fields = {
"plugin_xmls": "Depset of files",
"optional_plugin_xmls": "Depset of OptionalPluginXmlInfo providers",
"java_info": "Single JavaInfo provider (depreacated rules should get JavaInfo directly form the target)",
},
)

OptionalPluginXmlInfo = provider(
doc = "Contains a list of structs that descibe the optional plugin xmls.",
fields = ["optional_plugin_xmls"],
)

def _resource_file(target):
"""Makes sore that every target in the resource mapping privdes a single file."""
files = target[DefaultInfo].files.to_list()

if len(files) != 1:
fail("only mappings to single files are supported for resources")

return files[0]

def _import_resources(ctx):
"""Builds a jar (aka. zip) from the resource mapping."""
output = ctx.actions.declare_file(ctx.label.name + "_resource.jar")

mapping = [
"%s=%s" % (paths.join("resources", path), _resource_file(target).path)
for path, target in ctx.attr.resources.items()
]

ctx.actions.run(
inputs = [_resource_file(target) for target in ctx.attr.resources.values()],
outputs = [output],
mnemonic = "IntellijPluginResource",
progress_message = "Creating intellij plugin resource jar for %{label}",
executable = ctx.executable._zipper,
arguments = ["c", output.path] + mapping,
)

return JavaInfo(output_jar = output, compile_jar = output)

def _merge_plugin_xmls(ctx):
"""Merges all dependent plugin_xmls and the current one."""
return depset(
direct = ctx.files.plugin_xmls,
transitive = [
dep[IntellijPluginLibraryInfo].plugin_xmls
for dep in ctx.attr.deps
if IntellijPluginLibraryInfo in dep
],
order = "preorder",
)

def _merge_optional_plugin_xmls(ctx):
"""Marges all depedent optional plugin_xmls and the current ones."""
return depset(
direct = [dep[OptionalPluginXmlInfo] for dep in ctx.attr.optional_plugin_xmls],
transitive = [
dep[IntellijPluginLibraryInfo].optional_plugin_xmls
for dep in ctx.attr.deps
if IntellijPluginLibraryInfo in dep
],
order = "preorder",
)

def _intellij_plugin_library_rule_impl(ctx):
java_info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])

if ctx.attr.resources:
java_info = java_common.merge([java_info, _import_resources(ctx)])

plugin_info = IntellijPluginLibraryInfo(
plugin_xmls = _merge_plugin_xmls(ctx),
optional_plugin_xmls = _merge_optional_plugin_xmls(ctx),
java_info = java_info,
)

return [plugin_info, java_info]

_intellij_plugin_library = rule(
implementation = _intellij_plugin_library_rule_impl,
attrs = {
"resources": attr.string_keyed_label_dict(
doc = "Maps a file to a specific location inside the resource directory of the plugin.",
allow_files = True,
),
"deps": attr.label_list(
doc = "List of java dependencies and plugin dependencies for this library",
providers = [[JavaInfo], [JavaInfo, IntellijPluginLibraryInfo]],
),
"plugin_xmls": attr.label_list(
doc = "List of unconditional plugin xmls.",
allow_files = [".xml"],
),
"optional_plugin_xmls": attr.label_list(
providers = [OptionalPluginXmlInfo],
),
"_zipper": attr.label(
default = Label("@bazel_tools//tools/zip:zipper"),
executable = True,
cfg = "exec",
),
},
)

def intellij_plugin_library(name, srcs = None, deps = None, **kwargs):
"""
Backwards compatible implementation of the intellij_plugin_library rule.

If sources are not None compiles them into a kotlin library. Also supports
dependencies between plugin libraries.
"""

deps = deps or []

if srcs != None:
kt_jvm_library(
name = name + "_ktlib",
srcs = srcs,
deps = deps + ["//intellij_platform_sdk:plugin_api",],
visibility = ["//visibility:private"],
)

deps += [name + "_ktlib"]

_intellij_plugin_library(
name = name,
deps = deps,
**kwargs
)
7 changes: 2 additions & 5 deletions clwb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ licenses(["notice"])

intellij_plugin_library(
name = "plugin_library",
optional_plugin_xmls = [
"optional_clwb_oclang",
"//clwb/src/com/google/idea/blaze/clwb/radler:plugin_xml",
],
optional_plugin_xmls = ["optional_clwb_oclang"],
plugin_xmls = ["src/META-INF/clwb.xml"],
visibility = PLUGIN_PACKAGES_VISIBILITY,
deps = [
":clwb_lib",
"//clwb/src/com/google/idea/blaze/clwb/radler:lib",
"//clwb/src/com/google/idea/blaze/clwb/radler",
],
)

Expand Down
14 changes: 5 additions & 9 deletions clwb/src/com/google/idea/blaze/clwb/radler/BUILD
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("//build_defs:build_defs.bzl", "optional_plugin_xml")

licenses(["notice"])

package(default_visibility = ["//clwb:__subpackages__"])
load("//build_defs:build_defs.bzl", "intellij_plugin_library", "optional_plugin_xml")

optional_plugin_xml(
name = "plugin_xml",
module = ["org.jetbrains.plugins.clion.radler"],
plugin_xml = "optional-plugin.xml",
)

kt_jvm_library(
name = "lib",
intellij_plugin_library(
name = "radler",
srcs = glob(["*.kt"]),
optional_plugin_xmls = [":plugin_xml"],
visibility = ["//clwb:__subpackages__"],
deps = [
"//clwb:clwb_lib",
"//intellij_platform_sdk:plugin_api",
"//sdkcompat:sdkcompat_radler",
],
)