From 59f10f27e80ac41d4b31fa0f4a2f1353846aeacd Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 4 Aug 2023 15:24:22 -0700 Subject: [PATCH] Support blazerc section in project view PiperOrigin-RevId: 553927607 --- .../section/sections/BuildConfigSection.java | 79 +++++++++++++++++++ .../section/sections/Sections.java | 3 +- .../base/projectview/ProjectViewSetTest.java | 4 + 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 base/src/com/google/idea/blaze/base/projectview/section/sections/BuildConfigSection.java diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/BuildConfigSection.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/BuildConfigSection.java new file mode 100644 index 00000000000..1abd6e59d37 --- /dev/null +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/BuildConfigSection.java @@ -0,0 +1,79 @@ +/* + * Copyright 2023 The Bazel Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.idea.blaze.base.projectview.section.sections; + +import com.google.idea.blaze.base.model.primitives.WorkspacePath; +import com.google.idea.blaze.base.projectview.parser.ParseContext; +import com.google.idea.blaze.base.projectview.parser.ProjectViewParser; +import com.google.idea.blaze.base.projectview.section.ScalarSection; +import com.google.idea.blaze.base.projectview.section.ScalarSectionParser; +import com.google.idea.blaze.base.projectview.section.SectionKey; +import com.google.idea.blaze.base.projectview.section.SectionParser; +import java.io.File; +import javax.annotation.Nullable; + +/** A section to specify build-system's configuration file. */ +public final class BuildConfigSection { + public static final SectionKey> KEY = + SectionKey.of("build_config_file"); + public static final SectionParser PARSER = new BuildConfigSectionParser(); + + private static class BuildConfigSectionParser extends ScalarSectionParser { + public BuildConfigSectionParser() { + super(KEY, ':'); + } + + @Nullable + @Override + protected WorkspacePath parseItem( + ProjectViewParser parser, ParseContext parseContext, String text) { + String error = WorkspacePath.validate(text); + if (error != null) { + parseContext.addError(error); + return null; + } + + WorkspacePath workspacePath = new WorkspacePath(text); + if (parser.isRecursive()) { + File rcFile = parseContext.getWorkspacePathResolver().resolveToFile(workspacePath); + if (rcFile == null || !rcFile.exists()) { + parseContext.addError( + String.format( + "Could not resolve the build configuration file: %s", + workspacePath.relativePath())); + } + } + return workspacePath; + } + + @Override + protected void printItem(StringBuilder sb, WorkspacePath section) { + sb.append(section); + } + + @Override + public ItemType getItemType() { + return ItemType.FileItem; + } + + @Override + public String quickDocs() { + return "Specifies build configuration file for the remote build services"; + } + } + + private BuildConfigSection() {} +} diff --git a/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java b/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java index 10e9eccd94f..54df9d80387 100644 --- a/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java +++ b/base/src/com/google/idea/blaze/base/projectview/section/sections/Sections.java @@ -43,7 +43,8 @@ public class Sections { RunConfigurationsSection.PARSER, ShardBlazeBuildsSection.PARSER, TargetShardSizeSection.PARSER, - BazelBinarySection.PARSER); + BazelBinarySection.PARSER, + BuildConfigSection.PARSER); public static List getParsers() { List parsers = Lists.newArrayList(PARSERS); diff --git a/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java b/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java index d53e05fba5e..4224f3530d8 100644 --- a/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java +++ b/base/tests/unittests/com/google/idea/blaze/base/projectview/ProjectViewSetTest.java @@ -33,6 +33,7 @@ import com.google.idea.blaze.base.projectview.section.sections.AdditionalLanguagesSection; import com.google.idea.blaze.base.projectview.section.sections.AutomaticallyDeriveTargetsSection; import com.google.idea.blaze.base.projectview.section.sections.BazelBinarySection; +import com.google.idea.blaze.base.projectview.section.sections.BuildConfigSection; import com.google.idea.blaze.base.projectview.section.sections.BuildFlagsSection; import com.google.idea.blaze.base.projectview.section.sections.DirectoryEntry; import com.google.idea.blaze.base.projectview.section.sections.DirectorySection; @@ -107,6 +108,9 @@ public void testProjectViewSetSerializable() { .add( ScalarSection.builder(BazelBinarySection.KEY) .set(new File("/bazel/path/override"))) + .add( + ScalarSection.builder(BuildConfigSection.KEY) + .set(new WorkspacePath("test"))) .build()) .build();