Search in sources :

Example 26 with ImportRoots

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);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) ProjectViewEdit(com.google.idea.blaze.base.projectview.ProjectViewEdit)

Example 27 with ImportRoots

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;
}
Also used : ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) TargetExpression(com.google.idea.blaze.base.model.primitives.TargetExpression) BuildSystem(com.google.idea.blaze.base.settings.Blaze.BuildSystem) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) BuildTargetFinder(com.google.idea.blaze.base.sync.BuildTargetFinder)

Example 28 with ImportRoots

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);
}
Also used : ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) SyncState(com.google.idea.blaze.base.model.SyncState) AtomicLong(java.util.concurrent.atomic.AtomicLong) AspectStrategy(com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategy) File(java.io.File)

Example 29 with ImportRoots

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));
    }
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) SourceFolder(com.intellij.openapi.roots.SourceFolder) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) ContentEntry(com.intellij.openapi.roots.ContentEntry) SourceTestConfig(com.google.idea.blaze.base.sync.projectview.SourceTestConfig) SourceFolderProvider(com.google.idea.blaze.base.sync.SourceFolderProvider) File(java.io.File)

Aggregations

ImportRoots (com.google.idea.blaze.base.sync.projectview.ImportRoots)29 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)25 Test (org.junit.Test)16 DirectoryEntry (com.google.idea.blaze.base.projectview.section.sections.DirectoryEntry)11 File (java.io.File)11 WorkspaceRoot (com.google.idea.blaze.base.model.primitives.WorkspaceRoot)7 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)7 ImmutableList (com.google.common.collect.ImmutableList)6 BlazeContext (com.google.idea.blaze.base.scope.BlazeContext)6 Project (com.intellij.openapi.project.Project)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 Lists (com.google.common.collect.Lists)5 Kind (com.google.idea.blaze.base.model.primitives.Kind)5 List (java.util.List)5 Nullable (javax.annotation.Nullable)5 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)4 LanguageClass (com.google.idea.blaze.base.model.primitives.LanguageClass)4 IssueOutput (com.google.idea.blaze.base.scope.output.IssueOutput)4 ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)4 Set (java.util.Set)4