use of com.google.idea.blaze.base.sync.projectview.ImportRoots in project intellij by bazelbuild.
the class AddSourceToProjectHelper method addSourceAndTargetsToProject.
/**
* Adds the parent directory of the specified {@link WorkspacePath}, and the given targets to the
* project view.
*/
static void addSourceAndTargetsToProject(Project project, WorkspacePath workspacePath, List<? extends TargetExpression> targets) {
ImportRoots roots = ImportRoots.forProjectSafe(project);
if (roots == null) {
notifyFailed(project, "Couldn't parse existing project view file. Please sync the project and retry.");
return;
}
WorkspacePath parentPath = Preconditions.checkNotNull(workspacePath.getParent());
boolean addDirectory = !roots.containsWorkspacePath(parentPath);
if (targets.isEmpty() && !addDirectory) {
return;
}
ProjectViewEdit edit = ProjectViewEdit.editLocalProjectView(project, builder -> {
if (addDirectory) {
addDirectory(builder, parentPath);
}
addTargets(builder, targets);
return true;
});
if (edit == null) {
Messages.showErrorDialog("Could not modify project view. Check for errors in your project view and try again", "Error");
return;
}
edit.apply();
BlazeSyncManager.getInstance(project).partialSync(targets);
notifySuccess(project, addDirectory ? parentPath : null, targets);
}
use of com.google.idea.blaze.base.sync.projectview.ImportRoots in project intellij by bazelbuild.
the class PartialSyncAction method getTargets.
private static List<TargetExpression> getTargets(Project project, @Nullable VirtualFile virtualFile) {
if (virtualFile == null) {
return ImmutableList.of();
}
List<TargetExpression> targets = Lists.newArrayList();
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
SourceToTargetMap.getInstance(project);
if (!virtualFile.isDirectory()) {
targets.addAll(SourceToTargetMap.getInstance(project).getTargetsToBuildForSourceFile(new File(virtualFile.getPath())));
}
if (targets.isEmpty()) {
ProjectViewSet projectViewSet = ProjectViewManager.getInstance(project).getProjectViewSet();
if (projectViewSet != null) {
BuildSystem buildSystem = Blaze.getBuildSystem(project);
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, buildSystem).add(projectViewSet).build();
BuildTargetFinder buildTargetFinder = new BuildTargetFinder(project, workspaceRoot, importRoots);
TargetExpression targetExpression = buildTargetFinder.findTargetForFile(new File(virtualFile.getPath()));
if (targetExpression != null) {
targets.add(targetExpression);
}
}
}
return targets;
}
use of com.google.idea.blaze.base.sync.projectview.ImportRoots in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method updateTargetMap.
@Override
public IdeResult updateTargetMap(Project project, BlazeContext context, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet, BlazeVersionData blazeVersionData, BlazeConfigurationHandler configHandler, ShardedTargetList shardedTargets, WorkspaceLanguageSettings workspaceLanguageSettings, ArtifactLocationDecoder artifactLocationDecoder, SyncState.Builder syncStateBuilder, @Nullable SyncState previousSyncState, boolean mergeWithOldState) {
State prevState = previousSyncState != null ? previousSyncState.get(State.class) : null;
// If the language filter has changed, redo everything from scratch
if (prevState != null && !prevState.workspaceLanguageSettings.equals(workspaceLanguageSettings)) {
prevState = null;
}
// If the aspect strategy has changed, redo everything from scratch
final AspectStrategy aspectStrategy = AspectStrategyProvider.findAspectStrategy(blazeVersionData);
if (prevState != null && !Objects.equals(prevState.aspectStrategyName, aspectStrategy.getName())) {
prevState = null;
}
IdeInfoResult ideInfoResult = getIdeInfo(project, context, workspaceRoot, projectViewSet, workspaceLanguageSettings.activeLanguages, shardedTargets, aspectStrategy);
context.output(PrintOutput.log("ide-info result: " + ideInfoResult.buildResult.status));
if (ideInfoResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
return new IdeResult(prevState != null ? prevState.targetMap : null, ideInfoResult.buildResult);
}
// any old state that we have in an attempt not to lose too much code.
if (ideInfoResult.buildResult.status == BuildResult.Status.BUILD_ERROR) {
mergeWithOldState = true;
}
Collection<File> fileList = ideInfoResult.files;
List<File> updatedFiles = Lists.newArrayList();
List<File> removedFiles = Lists.newArrayList();
ImmutableMap<File, Long> fileState = FileDiffer.updateFiles(prevState != null ? prevState.fileState : null, fileList, updatedFiles, removedFiles);
if (fileState == null) {
return new IdeResult(prevState != null ? prevState.targetMap : null, BuildResult.FATAL_ERROR);
}
// if we're merging with the old state, no files are removed
int targetCount = fileList.size() + (mergeWithOldState ? removedFiles.size() : 0);
int removedCount = mergeWithOldState ? 0 : removedFiles.size();
context.output(PrintOutput.log(String.format("Total rules: %d, new/changed: %d, removed: %d", targetCount, updatedFiles.size(), removedCount)));
ListenableFuture<?> prefetchFuture = PrefetchService.getInstance().prefetchFiles(project, updatedFiles, true, false);
if (!FutureUtil.waitForFuture(context, prefetchFuture).timed("FetchAspectOutput", EventType.Prefetching).withProgressMessage("Reading IDE info result...").run().success()) {
return new IdeResult(prevState != null ? prevState.targetMap : null, BuildResult.FATAL_ERROR);
}
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project)).add(projectViewSet).build();
State state = updateState(project, context, prevState, fileState, configHandler, workspaceLanguageSettings, importRoots, aspectStrategy, updatedFiles, removedFiles, mergeWithOldState);
if (state == null) {
return new IdeResult(prevState != null ? prevState.targetMap : null, BuildResult.FATAL_ERROR);
}
syncStateBuilder.put(State.class, state);
return new IdeResult(state.targetMap, ideInfoResult.buildResult);
}
use of com.google.idea.blaze.base.sync.projectview.ImportRoots in project intellij by bazelbuild.
the class ContentEntryEditor method createContentEntries.
public static void createContentEntries(Project project, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData, DirectoryStructure rootDirectoryStructure, ModifiableRootModel modifiableRootModel) {
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project)).add(projectViewSet).build();
Collection<WorkspacePath> rootDirectories = importRoots.rootDirectories();
Collection<WorkspacePath> excludeDirectories = importRoots.excludeDirectories();
Multimap<WorkspacePath, WorkspacePath> excludesByRootDirectory = sortExcludesByRootDirectory(rootDirectories, excludeDirectories);
SourceTestConfig testConfig = new SourceTestConfig(projectViewSet);
SourceFolderProvider provider = SourceFolderProvider.getSourceFolderProvider(blazeProjectData);
for (WorkspacePath rootDirectory : rootDirectories) {
File rootFile = workspaceRoot.fileForPath(rootDirectory);
ContentEntry contentEntry = modifiableRootModel.addContentEntry(UrlUtil.pathToUrl(rootFile.getPath()));
for (WorkspacePath exclude : excludesByRootDirectory.get(rootDirectory)) {
File excludeFolder = workspaceRoot.fileForPath(exclude);
contentEntry.addExcludeFolder(UrlUtil.fileToIdeaUrl(excludeFolder));
}
ImmutableMap<File, SourceFolder> sourceFolders = provider.initializeSourceFolders(contentEntry);
SourceFolder rootSource = sourceFolders.get(rootFile);
walkFileSystem(workspaceRoot, testConfig, excludesByRootDirectory.get(rootDirectory), contentEntry, provider, sourceFolders, rootSource, rootDirectory, rootDirectoryStructure.directories.get(rootDirectory));
}
}
Aggregations