Skip to content

Commit

Permalink
Flush aspect cache when updating cp container and when rming project #…
Browse files Browse the repository at this point in the history
  • Loading branch information
simontoens authored Dec 10, 2020
1 parent 1b8fba9 commit 88f3022
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ public synchronized void flushAspectInfoCache(Set<String> targets) {
* contains the package name.
*/
public synchronized Set<String> flushAspectInfoCacheForPackage(String packageName) {
Set<BazelLabel> flushedPackages = this.aspectHelper.flushAspectInfoCacheForPackage(new BazelLabel(packageName));
LOG.info("Flushed aspect cache for packages: " + flushedPackages);
BazelLabel packageLabel = new BazelLabel(packageName);
Set<BazelLabel> flushedPackages = this.aspectHelper.flushAspectInfoCacheForPackage(packageLabel);
LOG.info("Flushed aspect cache for package: " + packageLabel);
return flushedPackages.stream().map(BazelLabel::getPackagePath).collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -144,14 +145,13 @@ public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject pro
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject javaProject, IClasspathContainer containerSuggestion) throws CoreException {
final boolean isRootProject = false; // this is wrong if the root project has a BUILD file ...
IProject project = javaProject.getProject();
String projectName = project.getName();
LOG.info("Updating the Classpath Container of project " + projectName);
flushProjectCaches(projectName);
flushProjectCaches(project);
Job.create(CLASSPATH_CONTAINER_UPDATE_JOB_NAME, monitor -> {
try {
// let the ClasspathContainer recompute its entries
IClasspathContainer container = getClasspathContainer(project, isRootProject);
setClasspathContainerForProject(containerPath, javaProject, container, monitor);
LOG.info("Updated classpath container of " + project.getName());
} catch (IOException | InterruptedException | BackingStoreException e) {
BazelPluginActivator.error("Error while updating Bazel classpath container.", e);
} catch (BazelCommandLineToolConfigurationException e) {
Expand All @@ -160,11 +160,20 @@ public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject ja
}).schedule();
}

private static void flushProjectCaches(String projectName) {
private static void flushProjectCaches(IProject project) {
// get downstream projects of the given project
JavaCoreHelper javaCoreHelper = BazelPluginActivator.getJavaCoreHelper();
IJavaProject[] allImportedProjects = javaCoreHelper.getAllBazelJavaProjects(false);
Set<IProject> downstreams = EclipseClasspathUtil.getDownstreamProjectsOf(project, allImportedProjects);

// flush caches
BazelWorkspace bzlWs = BazelPluginActivator.getBazelWorkspace();
BazelCommandManager bzlCmdMgr = BazelPluginActivator.getBazelCommandManager();
BazelWorkspaceCommandRunner bzlWsCmdRunner = bzlCmdMgr.getWorkspaceCommandRunner(bzlWs);
BazelPluginActivator.getBazelProjectManager().flushCaches(projectName, bzlWsCmdRunner);
BazelPluginActivator.getBazelProjectManager().flushCaches(project.getName(), bzlWsCmdRunner);
for (IProject downstreamProject : downstreams) {
BazelPluginActivator.getBazelProjectManager().flushCaches(downstreamProject.getName(), bzlWsCmdRunner);
}
}

// Remove projects imported successfully
Expand All @@ -184,6 +193,7 @@ private void undo() throws CoreException {
isCorrupt.set(true);

Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), "Error", generateImportErrorMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.salesforce.bazel.eclipse.BazelPluginActivator;
import com.salesforce.bazel.eclipse.builder.BazelProblemMarkerManager;
import com.salesforce.bazel.eclipse.config.BazelEclipseProjectSupport;
import com.salesforce.bazel.eclipse.runtime.api.ResourceHelper;
import com.salesforce.bazel.eclipse.wizard.BazelProjectImporter;
import com.salesforce.bazel.sdk.logging.LogHelper;
import com.salesforce.bazel.sdk.model.BazelLabel;
Expand All @@ -52,13 +53,15 @@ public class ProjectViewEditor extends AbstractDecoratedTextEditor {
private final ProjectViewPackageLocation rootPackage;
private final BazelProblemMarkerManager markerManager;
private final BazelProjectManager projectManager;
private final ResourceHelper resourceHelper;

public ProjectViewEditor() {
this.rootProject = getBazelRootProject();
this.rootDirectory = BazelPluginActivator.getBazelWorkspace().getBazelWorkspaceRootDirectory();
this.rootPackage = new ProjectViewPackageLocation(this.rootDirectory, "");
this.markerManager = new BazelProblemMarkerManager(getClass().getName());
this.projectManager = BazelPluginActivator.getBazelProjectManager();
this.resourceHelper = BazelPluginActivator.getResourceHelper();
setDocumentProvider(new TextFileDocumentProvider());
super.setSourceViewerConfiguration(new SourceViewerConfiguration() {
@Override
Expand Down Expand Up @@ -148,9 +151,7 @@ private void deleteProjects(IJavaProject[] projects) {
@Override
protected void execute(IProgressMonitor monitor) throws CoreException {
for (IJavaProject project : projects) {
final boolean deleteContent = true; // delete metadata also, under the Eclipse Workspace directory
final boolean force = true;
project.getProject().delete(deleteContent, force, monitor);
resourceHelper.deleteProject(project.getProject(), monitor);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.IProcessFactory;
Expand Down Expand Up @@ -74,7 +75,7 @@ public interface ResourceHelper {
* Returns the IProjects for the Bazel Workspace project.
*/
IProject[] getProjectsForBazelWorkspace(BazelWorkspace bazelWorkspace);

/**
* Creates a new project resource in the workspace using the given project description. Upon successful completion,
* the project will exist but be closed.
Expand Down Expand Up @@ -104,6 +105,29 @@ public interface ResourceHelper {
IProject createProject(IProject newProject, IProjectDescription description, IProgressMonitor monitor)
throws CoreException;

/**
* Deletes this project from the workspace AND flushes relevant caches.
* No action is taken if this project does not exist.
*
* This method is long-running; progress and cancellation are provided
* by the given progress monitor.
* </p>
*
* @param project the project to delete
* @param monitor a progress monitor, or <code>null</code> if progress
* reporting is not desired
* @exception CoreException if this method fails. Reasons include:
* <ul>
* <li> This project could not be deleted.</li>
* <li> This project's contents could not be deleted.</li>
* <li> Resource changes are disallowed during certain types of resource change
* event notification. See <code>IResourceChangeEvent</code> for more details.</li>
* </ul>
* @exception OperationCanceledException if the operation is canceled.
* Cancelation can occur even if no progress monitor is provided.
*/
void deleteProject(IProject project, IProgressMonitor monitor) throws CoreException;

/**
* Opens this project. No action is taken if the project is already open.
* <p>
Expand Down Expand Up @@ -342,7 +366,7 @@ IProject createProject(IProject newProject, IProjectDescription description, IPr
* If the launch configuration associated with the given launch specifies a process factory, it will be used to
* instantiate the new process.
* </p>
*
*
* @param launch
* the launch the process is contained in
* @param process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
import com.salesforce.bazel.eclipse.BazelPluginActivator;
import com.salesforce.bazel.eclipse.config.BazelProjectConstants;
import com.salesforce.bazel.eclipse.runtime.api.ResourceHelper;
import com.salesforce.bazel.sdk.command.BazelCommandManager;
import com.salesforce.bazel.sdk.command.BazelWorkspaceCommandRunner;
import com.salesforce.bazel.sdk.model.BazelWorkspace;

/**
* Resource helper implementation used when running in a live Eclipse runtime.
*
*
* @author plaird
*
*/
Expand All @@ -79,16 +81,16 @@ public IProject getBazelWorkspaceProject(BazelWorkspace bazelWorkspace) {
}
return null;
}

/**
* Returns the IProjects for the Bazel Workspace project.
*/
@Override
public IProject[] getProjectsForBazelWorkspace(BazelWorkspace bazelWorkspace) {
// todo once we support multiple Bazel workspaces, this will need to figure that out
return getEclipseWorkspaceRoot().getProjects();
return getEclipseWorkspaceRoot().getProjects();
}

/**
* Creates the project described by newProject, with the passed description.
*/
Expand All @@ -102,6 +104,22 @@ public IProject createProject(IProject newProject, IProjectDescription descripti
return newProject;
}

/**
* Deletes the specified project.
*/
public void deleteProject(IProject project, IProgressMonitor monitor) throws CoreException {
if (!isBazelRootProject(project)) {
// clear the cached data for this project
BazelWorkspace bzlWs = BazelPluginActivator.getBazelWorkspace();
BazelCommandManager bzlCmdMgr = BazelPluginActivator.getBazelCommandManager();
BazelWorkspaceCommandRunner bzlWsCmdRunner = bzlCmdMgr.getWorkspaceCommandRunner(bzlWs);
BazelPluginActivator.getBazelProjectManager().flushCaches(project.getName(), bzlWsCmdRunner);
}
boolean deleteContent = true; // delete metadata also, under the Eclipse Workspace directory
boolean force = true;
project.getProject().delete(deleteContent, force, monitor);
}

/**
* Opens the project, or no-op if already open
*/
Expand Down Expand Up @@ -147,7 +165,7 @@ public IWorkspace getEclipseWorkspace() {
public IWorkspaceRoot getEclipseWorkspaceRoot() {
return ResourcesPlugin.getWorkspace().getRoot();
}

@Override
public String getResourceAbsolutePath(IResource resource) {
if (resource == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public IProject getProjectByName(String projectName) {

return project;
}

@Override
public IProject getBazelWorkspaceProject(BazelWorkspace bazelWorkspace) {
for (IProject candidate : getEclipseWorkspaceRoot().getProjects()) {
Expand All @@ -113,7 +113,7 @@ public IProject getBazelWorkspaceProject(BazelWorkspace bazelWorkspace) {
public IProject[] getProjectsForBazelWorkspace(BazelWorkspace bazelWorkspace) {
return mockProjects.values().toArray(new IProject[] {});
}


@Override
public boolean isBazelRootProject(IProject project) {
Expand All @@ -130,6 +130,11 @@ public IProject createProject(IProject newProject, IProjectDescription descripti
return newProject;
}

@Override
public void deleteProject(IProject project, IProgressMonitor monitor) {

}

@Override
public void openProject(IProject project, IProgressMonitor monitor) throws CoreException {
Mockito.when(project.isOpen()).thenReturn(true);
Expand Down

0 comments on commit 88f3022

Please sign in to comment.