use of com.google.idea.blaze.base.scope.BlazeContext in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method getIdeInfo.
private static IdeInfoResult getIdeInfo(Project project, BlazeContext parentContext, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet, ImmutableSet<LanguageClass> activeLanguages, ShardedTargetList shardedTargets, AspectStrategy aspectStrategy) {
return Scope.push(parentContext, context -> {
context.push(new TimingScope(String.format("Execute%sCommand", Blaze.buildSystemName(project)), EventType.BlazeInvocation));
Set<File> ideInfoFiles = new LinkedHashSet<>();
Function<Integer, String> progressMessage = count -> String.format("Building IDE info files for shard %s of %s...", count, shardedTargets.shardedTargets.size());
Function<List<TargetExpression>, BuildResult> invocation = targets -> {
IdeInfoResult result = getIdeInfoForTargets(project, context, workspaceRoot, projectViewSet, activeLanguages, targets, aspectStrategy);
ideInfoFiles.addAll(result.files);
return result.buildResult;
};
BuildResult result = shardedTargets.runShardedCommand(project, context, progressMessage, invocation);
return new IdeInfoResult(ideInfoFiles, result);
});
}
use of com.google.idea.blaze.base.scope.BlazeContext in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method updateState.
@Nullable
static State updateState(Project project, BlazeContext parentContext, @Nullable State prevState, ImmutableMap<File, Long> fileState, BlazeConfigurationHandler configHandler, WorkspaceLanguageSettings workspaceLanguageSettings, ImportRoots importRoots, AspectStrategy aspectStrategy, List<File> newFiles, List<File> removedFiles, boolean mergeWithOldState) {
Result<State> result = Scope.push(parentContext, (ScopedFunction<Result<State>>) context -> {
context.push(new TimingScope("UpdateTargetMap", EventType.Other));
ImmutableMap<File, Long> nextFileState = fileState;
if (mergeWithOldState && prevState != null) {
ImmutableMap.Builder<File, Long> fileStateBuilder = ImmutableMap.<File, Long>builder().putAll(fileState);
for (Map.Entry<File, Long> entry : prevState.fileState.entrySet()) {
if (!fileState.containsKey(entry.getKey())) {
fileStateBuilder.put(entry);
}
}
nextFileState = fileStateBuilder.build();
}
State state = new State();
state.fileState = nextFileState;
state.workspaceLanguageSettings = workspaceLanguageSettings;
state.aspectStrategyName = aspectStrategy.getName();
Map<TargetKey, TargetIdeInfo> targetMap = Maps.newHashMap();
if (prevState != null) {
targetMap.putAll(prevState.targetMap.map());
state.fileToTargetMapKey.putAll(prevState.fileToTargetMapKey);
}
if (!mergeWithOldState) {
for (File removedFile : removedFiles) {
TargetKey key = state.fileToTargetMapKey.remove(removedFile);
if (key != null) {
targetMap.remove(key);
}
}
}
AtomicLong totalSizeLoaded = new AtomicLong(0);
Set<LanguageClass> ignoredLanguages = Sets.newConcurrentHashSet();
ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();
List<ListenableFuture<TargetFilePair>> futures = Lists.newArrayList();
for (File file : newFiles) {
futures.add(executor.submit(() -> {
totalSizeLoaded.addAndGet(file.length());
IntellijIdeInfo.TargetIdeInfo message = aspectStrategy.readAspectFile(file);
TargetIdeInfo target = protoToTarget(workspaceLanguageSettings, importRoots, message, ignoredLanguages);
return new TargetFilePair(file, target);
}));
}
Set<TargetKey> newTargets = new HashSet<>();
Set<String> configurations = new LinkedHashSet<>();
configurations.add(configHandler.defaultConfigurationPathComponent);
int duplicateTargetLabels = 0;
try {
for (TargetFilePair targetFilePair : Futures.allAsList(futures).get()) {
if (targetFilePair.target != null) {
File file = targetFilePair.file;
String config = configHandler.getConfigurationPathComponent(file);
configurations.add(config);
TargetKey key = targetFilePair.target.key;
if (targetMap.putIfAbsent(key, targetFilePair.target) == null) {
state.fileToTargetMapKey.forcePut(file, key);
} else {
if (!newTargets.add(key)) {
duplicateTargetLabels++;
}
if (Objects.equals(config, configHandler.defaultConfigurationPathComponent)) {
targetMap.put(key, targetFilePair.target);
state.fileToTargetMapKey.forcePut(file, key);
}
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Result.error(null);
} catch (ExecutionException e) {
return Result.error(e);
}
context.output(PrintOutput.log(String.format("Loaded %d aspect files, total size %dkB", newFiles.size(), totalSizeLoaded.get() / 1024)));
if (duplicateTargetLabels > 0) {
context.output(new PerformanceWarning(String.format("There were %d duplicate rules, built with the following " + "configurations: %s.\nYour IDE sync is slowed down by ~%d%%.", duplicateTargetLabels, configurations, (100 * duplicateTargetLabels / targetMap.size()))));
}
ignoredLanguages.retainAll(LanguageSupport.availableAdditionalLanguages(workspaceLanguageSettings.getWorkspaceType()));
warnIgnoredLanguages(project, context, ignoredLanguages);
state.targetMap = new TargetMap(ImmutableMap.copyOf(targetMap));
return Result.of(state);
});
if (result.error != null) {
logger.error(result.error);
return null;
}
return result.result;
}
use of com.google.idea.blaze.base.scope.BlazeContext in project intellij by bazelbuild.
the class LibraryEditor method updateProjectLibraries.
public static void updateProjectLibraries(Project project, BlazeContext context, ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData, Collection<BlazeLibrary> libraries) {
Set<LibraryKey> intelliJLibraryState = Sets.newHashSet();
for (Library library : ProjectLibraryTable.getInstance(project).getLibraries()) {
String name = library.getName();
if (name != null) {
intelliJLibraryState.add(LibraryKey.fromIntelliJLibraryName(name));
}
}
context.output(PrintOutput.log(String.format("Workspace has %d libraries", libraries.size())));
LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
LibraryTable.ModifiableModel libraryTableModel = libraryTable.getModifiableModel();
try {
for (BlazeLibrary library : libraries) {
updateLibrary(project, blazeProjectData.artifactLocationDecoder, libraryTable, libraryTableModel, library);
}
// Garbage collect unused libraries
List<LibrarySource> librarySources = Lists.newArrayList();
for (BlazeSyncPlugin syncPlugin : BlazeSyncPlugin.EP_NAME.getExtensions()) {
LibrarySource librarySource = syncPlugin.getLibrarySource(projectViewSet, blazeProjectData);
if (librarySource != null) {
librarySources.add(librarySource);
}
}
Predicate<Library> gcRetentionFilter = librarySources.stream().map(LibrarySource::getGcRetentionFilter).filter(Objects::nonNull).reduce(Predicate::or).orElse(o -> false);
Set<LibraryKey> newLibraryKeys = libraries.stream().map((blazeLibrary) -> blazeLibrary.key).collect(Collectors.toSet());
for (LibraryKey libraryKey : intelliJLibraryState) {
String libraryIntellijName = libraryKey.getIntelliJLibraryName();
if (!newLibraryKeys.contains(libraryKey)) {
Library library = libraryTable.getLibraryByName(libraryIntellijName);
if (!gcRetentionFilter.test(library)) {
if (library != null) {
libraryTableModel.removeLibrary(library);
}
}
}
}
} finally {
libraryTableModel.commit();
}
}
use of com.google.idea.blaze.base.scope.BlazeContext in project intellij by bazelbuild.
the class BlazeSyncIntegrationTestCase method doSetup.
@Before
public void doSetup() throws Exception {
thisClassDisposable = Disposer.newDisposable();
projectViewManager = new MockProjectViewManager(getProject(), thisClassDisposable);
new MockBlazeVcsHandler(thisClassDisposable);
blazeInfoData = new MockBlazeInfoRunner();
blazeIdeInterface = new MockBlazeIdeInterface();
eventLogger = new MockEventLoggingService(thisClassDisposable);
moduleMocker = new ProjectModuleMocker(getProject(), thisClassDisposable);
registerApplicationService(BlazeInfoRunner.class, blazeInfoData);
registerApplicationService(BlazeIdeInterface.class, blazeIdeInterface);
errorCollector = new ErrorCollector();
context = new BlazeContext();
context.addOutputSink(IssueOutput.class, errorCollector);
fileSystem.createDirectory(projectDataDirectory.getPath() + "/.blaze/modules");
blazeInfoData.setResults(ImmutableMap.<String, String>builder().put(BlazeInfo.blazeBinKey(Blaze.getBuildSystem(getProject())), BLAZE_BIN).put(BlazeInfo.blazeGenfilesKey(Blaze.getBuildSystem(getProject())), BLAZE_GENFILES).put(BlazeInfo.EXECUTION_ROOT_KEY, EXECUTION_ROOT).put(BlazeInfo.OUTPUT_BASE_KEY, OUTPUT_BASE).put(BlazeInfo.OUTPUT_PATH_KEY, OUTPUT_PATH).put(BlazeInfo.PACKAGE_PATH_KEY, workspaceRoot.toString()).build());
}
use of com.google.idea.blaze.base.scope.BlazeContext in project intellij by bazelbuild.
the class BlazeCidrLauncher method createProcess.
@Override
public ProcessHandler createProcess(CommandLineState state) throws ExecutionException {
ImmutableList<String> testHandlerFlags = ImmutableList.of();
BlazeTestUiSession testUiSession = useTestUi() ? TestUiSessionProvider.getInstance(project).getTestUiSession(configuration.getTarget()) : null;
if (testUiSession != null) {
testHandlerFlags = testUiSession.getBlazeFlags();
}
ProjectViewSet projectViewSet = Preconditions.checkNotNull(ProjectViewManager.getInstance(project).getProjectViewSet());
List<String> fixedBlazeFlags = getFixedBlazeFlags();
BlazeCommand.Builder command = BlazeCommand.builder(Blaze.getBuildSystemProvider(project).getBinaryPath(), handlerState.getCommandState().getCommand()).addTargets(configuration.getTarget()).addBlazeFlags(BlazeFlags.blazeFlags(project, projectViewSet, handlerState.getCommandState().getCommand(), BlazeInvocationContext.NonSync)).addBlazeFlags(testHandlerFlags).addBlazeFlags(fixedBlazeFlags).addExeFlags(handlerState.getExeFlagsState().getExpandedFlags());
state.setConsoleBuilder(createConsoleBuilder(testUiSession));
state.addConsoleFilters(getConsoleFilters().toArray(new Filter[0]));
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
return new ScopedBlazeProcessHandler(project, command.build(), workspaceRoot, new ScopedBlazeProcessHandler.ScopedProcessHandlerDelegate() {
@Override
public void onBlazeContextStart(BlazeContext context) {
context.push(new IssuesScope(project, BlazeUserSettings.getInstance().getShowProblemsViewForRunAction()));
}
@Override
public ImmutableList<ProcessListener> createProcessListeners(BlazeContext context) {
LineProcessingOutputStream outputStream = LineProcessingOutputStream.of(BlazeConsoleLineProcessorProvider.getAllStderrLineProcessors(context));
return ImmutableList.of(new LineProcessingProcessAdapter(outputStream));
}
});
}
Aggregations