Skip to content

Commit 6a9a091

Browse files
simuonsWixBuildServer
authored andcommitted
Expose ProtoIdeInfo for proto_library during sync
`idea.plugin.protoeditor` needs to be aware of source roots of proto libraries Add proto files to target sources
1 parent c4fa063 commit 6a9a091

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed

aspect/intellij_info_impl.bzl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,24 @@ def _do_starlark_string_expansion(ctx, name, strings, extra_targets = []):
323323
return strings
324324

325325
##### Builders for individual parts of the aspect output
326+
def collect_proto_info(target, ctx, semantics, ide_info, ide_info_file, output_groups):
327+
if not ProtoInfo in target:
328+
return False
329+
330+
proto_info = target[ProtoInfo]
331+
proto_output = depset([proto_info.direct_descriptor_set])
332+
333+
ide_info["proto_ide_info"] = struct_omit_none(
334+
sources = sources_from_target(ctx),
335+
source_root = proto_info.proto_source_root,
336+
strip_import_prefix = ctx.rule.attr.strip_import_prefix,
337+
import_prefix = ctx.rule.attr.import_prefix,
338+
)
339+
340+
update_sync_output_groups(output_groups, "intellij-info-proto", depset([ide_info_file]))
341+
update_sync_output_groups(output_groups, "intellij-compile-proto", proto_output)
342+
update_sync_output_groups(output_groups, "intellij-resolve-proto", proto_output)
343+
return True
326344

327345
def collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_groups):
328346
"""Updates Python-specific output groups, returns false if not a Python target."""
@@ -1118,6 +1136,7 @@ def intellij_info_aspect_impl(target, ctx, semantics):
11181136
ide_info["test_info"] = build_test_info(ctx)
11191137

11201138
handled = False
1139+
handled = collect_proto_info(target, ctx, semantics, ide_info, ide_info_file, output_groups) or handled
11211140
handled = collect_py_info(target, ctx, semantics, ide_info, ide_info_file, output_groups) or handled
11221141
handled = collect_cpp_info(target, ctx, semantics, ide_info, ide_info_file, output_groups) or handled
11231142
handled = collect_c_toolchain_info(target, ctx, semantics, ide_info, ide_info_file, output_groups) or handled
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
licenses(["notice"]) # Apache 2.0
2+
3+
load(
4+
"//aspect/testing/rules:intellij_aspect_test_fixture.bzl",
5+
"intellij_aspect_test_fixture",
6+
)
7+
8+
proto_library(
9+
name = "a_proto",
10+
srcs = ["a.proto"],
11+
import_prefix = "test",
12+
strip_import_prefix = "/" + package_name(),
13+
)
14+
15+
proto_library(
16+
name = "b_proto",
17+
srcs = ["b.proto"],
18+
deps = [":a_proto"],
19+
)
20+
21+
intellij_aspect_test_fixture(
22+
name = "fixture",
23+
deps = [":b_proto"],
24+
)
25+
26+
java_test(
27+
name = "ProtoLibraryTest",
28+
srcs = ["ProtoLibraryTest.java"],
29+
data = [
30+
":fixture",
31+
],
32+
deps = [
33+
"//aspect/testing:BazelIntellijAspectTest",
34+
"//aspect/testing:guava",
35+
"//aspect/testing/rules:IntellijAspectTest",
36+
"//aspect/testing/rules:intellij_aspect_test_fixture_java_proto",
37+
"//intellij_platform_sdk:test_libs",
38+
"//proto:intellij_ide_info_java_proto",
39+
"@junit//jar",
40+
],
41+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017 The Bazel Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.idea.blaze.aspect.proto.pl;
17+
18+
import com.google.devtools.intellij.IntellijAspectTestFixtureOuterClass.IntellijAspectTestFixture;
19+
import com.google.devtools.intellij.ideinfo.IntellijIdeInfo.TargetIdeInfo;
20+
import com.google.idea.blaze.BazelIntellijAspectTest;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
import static com.google.common.truth.Truth.assertThat;
26+
27+
/** Tests for proto_library. */
28+
@RunWith(JUnit4.class)
29+
public class ProtoLibraryTest extends BazelIntellijAspectTest {
30+
31+
@Test
32+
public void testProtoLibrary() throws Exception {
33+
IntellijAspectTestFixture fixture = loadTestFixture(":fixture");
34+
35+
TargetIdeInfo aProto = findTarget(fixture, ":a_proto");
36+
assertThat(aProto).isNotNull();
37+
assertThat(aProto.hasProtoIdeInfo()).isTrue();
38+
assertThat(relativePathsForArtifacts(aProto.getProtoIdeInfo().getSourcesList()))
39+
.containsExactly(testRelative("a.proto"));
40+
assertThat(aProto.getProtoIdeInfo().getSourceRoot())
41+
.isEqualTo(
42+
"bazel-out/darwin-fastbuild/bin/aspect/testing/tests/src/com/google/idea/blaze/aspect/proto/pl/_virtual_imports/a_proto");
43+
assertThat(aProto.getProtoIdeInfo().getImportPrefix()).isEqualTo("test");
44+
assertThat(aProto.getProtoIdeInfo().getStripImportPrefix())
45+
.isEqualTo("/aspect/testing/tests/src/com/google/idea/blaze/aspect/proto/pl");
46+
47+
TargetIdeInfo bProto = findTarget(fixture, ":b_proto");
48+
assertThat(bProto).isNotNull();
49+
assertThat(bProto.hasProtoIdeInfo()).isTrue();
50+
assertThat(relativePathsForArtifacts(bProto.getProtoIdeInfo().getSourcesList()))
51+
.containsExactly(testRelative("b.proto"));
52+
assertThat(bProto.getProtoIdeInfo().getSourceRoot()).isEqualTo(".");
53+
assertThat(bProto.getProtoIdeInfo().getImportPrefix()).isEmpty();
54+
assertThat(bProto.getProtoIdeInfo().getStripImportPrefix()).isEmpty();
55+
assertThat(dependenciesForTarget(bProto)).contains(dep(aProto));
56+
57+
assertThat(getOutputGroupFiles(fixture, "intellij-info-proto"))
58+
.containsExactly(
59+
testRelative(intellijInfoFileName(aProto.getKey())),
60+
testRelative(intellijInfoFileName(bProto.getKey())));
61+
62+
assertThat(getOutputGroupFiles(fixture, "intellij-compile-proto"))
63+
.containsExactly(
64+
testRelative("a_proto-descriptor-set.proto.bin"),
65+
testRelative("b_proto-descriptor-set.proto.bin"));
66+
67+
assertThat(getOutputGroupFiles(fixture, "intellij-resolve-proto"))
68+
.containsExactly(
69+
testRelative("a_proto-descriptor-set.proto.bin"),
70+
testRelative("b_proto-descriptor-set.proto.bin"));
71+
}
72+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
syntax = "proto2";
2+
package a;
3+
message A {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
syntax = "proto3";
2+
import "test/a.proto";
3+
package b;
4+
message B {a.A a = 1;}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2016 The Bazel Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.idea.blaze.base.ideinfo;
17+
18+
import com.google.common.base.Strings;
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.devtools.intellij.ideinfo.IntellijIdeInfo;
21+
22+
import javax.annotation.Nullable;
23+
import java.util.Objects;
24+
25+
/** Ide info specific to proto rules. */
26+
public final class ProtoIdeInfo implements ProtoWrapper<IntellijIdeInfo.ProtoIdeInfo> {
27+
private final ImmutableList<ArtifactLocation> sources;
28+
@Nullable private final String sourceRoot;
29+
@Nullable private final String importPrefix;
30+
@Nullable private final String stripImportPrefix;
31+
32+
private ProtoIdeInfo(
33+
ImmutableList<ArtifactLocation> sources,
34+
@Nullable String sourceRoot,
35+
@Nullable String importPrefix,
36+
@Nullable String stripImportPrefix) {
37+
this.sources = sources;
38+
this.sourceRoot = sourceRoot;
39+
this.importPrefix = importPrefix;
40+
this.stripImportPrefix = stripImportPrefix;
41+
}
42+
43+
static ProtoIdeInfo fromProto(IntellijIdeInfo.ProtoIdeInfo proto) {
44+
return new ProtoIdeInfo(
45+
ProtoWrapper.map(proto.getSourcesList(), ArtifactLocation::fromProto),
46+
Strings.emptyToNull(proto.getSourceRoot()),
47+
Strings.emptyToNull(proto.getImportPrefix()),
48+
Strings.emptyToNull(proto.getStripImportPrefix()));
49+
}
50+
51+
@Override
52+
public IntellijIdeInfo.ProtoIdeInfo toProto() {
53+
IntellijIdeInfo.ProtoIdeInfo.Builder builder =
54+
IntellijIdeInfo.ProtoIdeInfo.newBuilder().addAllSources(ProtoWrapper.mapToProtos(sources));
55+
ProtoWrapper.setIfNotNull(builder::setSourceRoot, sourceRoot);
56+
ProtoWrapper.setIfNotNull(builder::setImportPrefix, importPrefix);
57+
ProtoWrapper.setIfNotNull(builder::setStripImportPrefix, stripImportPrefix);
58+
return builder.build();
59+
}
60+
61+
public ImmutableList<ArtifactLocation> getSources() {
62+
return sources;
63+
}
64+
65+
@Nullable
66+
public String getSourceRoot() {
67+
return sourceRoot;
68+
}
69+
70+
@Nullable
71+
public String getImportPrefix() {
72+
return importPrefix;
73+
}
74+
75+
@Nullable
76+
public String getStripImportPrefix() {
77+
return stripImportPrefix;
78+
}
79+
80+
public static Builder builder() {
81+
return new Builder();
82+
}
83+
84+
/** Builder for proto info */
85+
public static class Builder {
86+
@Nullable String sourceRoot;
87+
@Nullable String importPrefix;
88+
@Nullable String stripImportPrefix;
89+
90+
public Builder setSourceRoot(String sourceRoot) {
91+
this.sourceRoot = sourceRoot;
92+
return this;
93+
}
94+
95+
public Builder setImportPrefix(String importPrefix) {
96+
this.importPrefix = importPrefix;
97+
return this;
98+
}
99+
100+
public Builder setStripImportPrefix(String stripImportPrefix) {
101+
this.stripImportPrefix = stripImportPrefix;
102+
return this;
103+
}
104+
105+
public ProtoIdeInfo build() {
106+
return new ProtoIdeInfo(ImmutableList.of(), sourceRoot, importPrefix, stripImportPrefix);
107+
}
108+
}
109+
110+
@Override
111+
public boolean equals(Object o) {
112+
if (this == o) {
113+
return true;
114+
}
115+
if (o == null || getClass() != o.getClass()) {
116+
return false;
117+
}
118+
ProtoIdeInfo that = (ProtoIdeInfo) o;
119+
return Objects.equals(sources, that.sources)
120+
&& Objects.equals(sourceRoot, that.sourceRoot)
121+
&& Objects.equals(importPrefix, that.importPrefix)
122+
&& Objects.equals(stripImportPrefix, that.stripImportPrefix);
123+
}
124+
125+
@Override
126+
public int hashCode() {
127+
return Objects.hash(sources, sourceRoot, importPrefix, stripImportPrefix);
128+
}
129+
}

0 commit comments

Comments
 (0)