Skip to content

Commit

Permalink
Adds a allow_manual_tags_sync in the .bazelproject to allow manual ta…
Browse files Browse the repository at this point in the history
…gs to be synced in the repo.
  • Loading branch information
rogerhu committed Jul 12, 2023
1 parent f0ea421 commit a0d21b8
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
1 change: 1 addition & 0 deletions base/src/META-INF/blaze-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@
<BlazeTestEventsHandler implementation="com.google.idea.blaze.base.run.smrunner.BlazeGenericTestEventsHandler" order="last"/>
<ProjectViewDefaultValueProvider implementation="com.google.idea.blaze.base.projectview.section.sections.DirectorySection$DirectoriesProjectViewDefaultValueProvider"/>
<ProjectViewDefaultValueProvider implementation="com.google.idea.blaze.base.projectview.section.sections.AutomaticallyDeriveTargetsSection$DefaultValueProvider"/>
<ProjectViewDefaultValueProvider implementation="com.google.idea.blaze.base.projectview.section.sections.SyncManualTargetsSection$DefaultValueProvider"/>
<ProjectViewDefaultValueProvider implementation="com.google.idea.blaze.base.projectview.section.sections.TargetSection$TargetsProjectViewDefaultValueProvider"/>
<ProjectViewDefaultValueProvider implementation="com.google.idea.blaze.base.projectview.section.sections.AdditionalLanguagesSection$AdditionalLanguagesDefaultValueProvider"/>
<PrefetchFileSource implementation="com.google.idea.blaze.base.prefetch.ProtoPrefetchFileSource"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ public class BlazeQueryDirectoryToTargetProvider implements DirectoryToTargetPro
@Override
public List<TargetInfo> doExpandDirectoryTargets(
Project project,
Boolean shouldManualTargetSync,
ImportRoots directories,
WorkspacePathResolver pathResolver,
BlazeContext context) {
return runQuery(project, getQueryString(directories), context);
return runQuery(project, getQueryString(directories, shouldManualTargetSync), context);
}

private static String getQueryString(ImportRoots directories) {
private static String getQueryString(ImportRoots directories, boolean allowManualTargetsSync) {
StringBuilder targets = new StringBuilder();
targets.append(
directories.rootDirectories().stream()
Expand All @@ -67,6 +68,10 @@ private static String getQueryString(ImportRoots directories) {
targets.append(" - " + TargetExpression.allFromPackageRecursive(excluded).toString());
}

if (allowManualTargetsSync) {
return targets.toString();
}

// exclude 'manual' targets, which shouldn't be built when expanding wildcard target patterns
if (SystemInfo.isWindows) {
// TODO(b/201974254): Windows support for Bazel sync (see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ static boolean hasProvider() {
@Nullable
static List<TargetInfo> expandDirectoryTargets(
Project project,
Boolean shouldManualTargetSync,
ImportRoots directories,
WorkspacePathResolver pathResolver,
BlazeContext context) {
return Arrays.stream(EP_NAME.getExtensions())
.map(p -> p.doExpandDirectoryTargets(project, directories, pathResolver, context))
.map(p -> p.doExpandDirectoryTargets(project, shouldManualTargetSync, directories, pathResolver, context))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
Expand All @@ -63,6 +64,7 @@ static List<TargetInfo> expandDirectoryTargets(
@Nullable
List<TargetInfo> doExpandDirectoryTargets(
Project project,
Boolean shouldManualTargetSync,
ImportRoots directories,
WorkspacePathResolver pathResolver,
BlazeContext context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Sections {
ImportSection.PARSER,
DirectorySection.PARSER,
AutomaticallyDeriveTargetsSection.PARSER,
SyncManualTargetsSection.PARSER,
TargetSection.PARSER,
WorkspaceTypeSection.PARSER,
AdditionalLanguagesSection.PARSER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2019 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.projectview.ProjectView;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
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.ProjectViewDefaultValueProvider;
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 com.google.idea.blaze.base.settings.BuildSystemName;
import javax.annotation.Nullable;

/** If set to true, automatically derives targets from the project directories. */
public class SyncManualTargetsSection {
public static final SectionKey<Boolean, ScalarSection<Boolean>> KEY =
SectionKey.of("allow_manual_targets_sync");
public static final SectionParser PARSER = new SyncManualTargetsParser();

private static class SyncManualTargetsParser
extends ScalarSectionParser<Boolean> {
SyncManualTargetsParser() {
super(KEY, ':');
}

@Override
@Nullable
protected Boolean parseItem(ProjectViewParser parser, ParseContext parseContext, String text) {
if (text.equals("true")) {
return true;
}
if (text.equals("false")) {
return false;
}
parseContext.addError(
"'allow_manual_tags_sync' must be set to 'true' or 'false' (e.g."
+ " 'allow_manual_tags_sync: true')");
return null;
}

@Override
protected void printItem(StringBuilder sb, Boolean item) {
sb.append(item);
}

@Override
public ItemType getItemType() {
return ItemType.Other;
}

@Override
public String quickDocs() {
return "If set to true, project targets will be derived from the directories.";
}
}

static class DefaultValueProvider implements ProjectViewDefaultValueProvider {
@Override
public ProjectView addProjectViewDefaultValue(
BuildSystemName buildSystemName,
ProjectViewSet projectViewSet,
ProjectView topLevelProjectView) {
if (!topLevelProjectView.getSectionsOfType(KEY).isEmpty()) {
return topLevelProjectView;
}
return ProjectView.builder(topLevelProjectView)
.add(
TextBlockSection.of(
TextBlock.of(
"# Automatically targets tagged as manual to be synced")))
.add(ScalarSection.builder(KEY).set(false))
.add(TextBlockSection.of(TextBlock.newLine()))
.build();
}

@Override
public SectionKey<?, ?> getSectionKey() {
return KEY;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.projectview.section.sections.AutomaticallyDeriveTargetsSection;
import com.google.idea.blaze.base.projectview.section.sections.SyncManualTargetsSection;
import com.google.idea.blaze.base.projectview.section.sections.TargetSection;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.scope.Scope;
Expand Down Expand Up @@ -88,6 +89,10 @@ private static boolean shouldDeriveSyncTargetsFromDirectories(ProjectViewSet vie
return viewSet.getScalarValue(AutomaticallyDeriveTargetsSection.KEY).orElse(false);
}

private static boolean shouldSyncManualTargets(ProjectViewSet viewSet) {
return viewSet.getScalarValue(SyncManualTargetsSection.KEY).orElse(false);
}

private static ImmutableList<TargetExpression> deriveTargetsFromDirectories(
Project project,
BlazeContext context,
Expand Down Expand Up @@ -120,7 +125,7 @@ private static ImmutableList<TargetExpression> deriveTargetsFromDirectories(
// We don't want blaze build errors to fail the whole sync
childContext.setPropagatesErrors(false);
return DirectoryToTargetProvider.expandDirectoryTargets(
project, importRoots, pathResolver, childContext);
project, shouldSyncManualTargets(projectViewSet), importRoots, pathResolver, childContext);
});
if (context.isCancelled()) {
throw new SyncCanceledException();
Expand Down

0 comments on commit a0d21b8

Please sign in to comment.