From bed01751016215d724f984b9c72dc5771ed35018 Mon Sep 17 00:00:00 2001 From: Chris Recoskie Date: Tue, 3 Jun 2008 23:24:47 +0000 Subject: [PATCH] RESOLVED - bug 230159: "Build selected files" does not save changed files https://bugs.eclipse.org/bugs/show_bug.cgi?id=230159 --- .../ui/actions/BuildFilesAction.java | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/actions/BuildFilesAction.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/actions/BuildFilesAction.java index cddc1854b26..df15b5c123d 100644 --- a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/actions/BuildFilesAction.java +++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/actions/BuildFilesAction.java @@ -14,7 +14,9 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jface.action.IAction; @@ -28,6 +30,7 @@ import org.eclipse.cdt.managedbuilder.internal.core.GeneratedMakefileBuilder; import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages; import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator; import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; @@ -37,6 +40,7 @@ import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.ISelectionService; import org.eclipse.ui.actions.ActionDelegate; +import org.eclipse.ui.internal.ide.actions.BuildUtilities; /** * @author crecoskie @@ -209,7 +213,7 @@ public class BuildFilesAction extends ActionDelegate implements boolean isFirstFile = true; while (iterator.hasNext()) { - IFile file = (IFile) iterator.next(); + IFile file = iterator.next(); IManagedBuildInfo buildInfo = ManagedBuildManager .getBuildInfo(file.getProject()); @@ -255,6 +259,12 @@ public class BuildFilesAction extends ActionDelegate implements Job buildFilesJob = new BuildFilesJob(selectedFiles); + List projects = getProjectsToBuild(selectedFiles); + if (projects != null && !projects.isEmpty()) { + // Save all resources prior to doing build + BuildUtilities.saveEditors(projects); + } + buildFilesJob.schedule(); } @@ -355,5 +365,51 @@ public class BuildFilesAction extends ActionDelegate implements // update state update(); } + + + /* + * Returns the projects to build. + * This contains the set of projects which have builders, across all selected resources. + */ + private List getProjectsToBuild(List selectedFiles) { + List projectsToBuild = new LinkedList(); + for (Iterator i = selectedFiles.iterator(); i.hasNext();) { + IFile file = i.next(); + IProject project = file.getProject(); + if (project != null) { + if (!projectsToBuild.contains(project)) { + if (hasBuilder(project)) { + projectsToBuild.add(project); + } + } + } + } + + return projectsToBuild; + } + + /* + * Returns whether there are builders configured on the given project. + * + * @return true if it has builders, + * false if not, or if this couldn't be determined + */ + private boolean hasBuilder(IProject project) { + if (!project.isAccessible()) + return false; + try { + ICommand[] commands = project.getDescription().getBuildSpec(); + if (commands.length > 0) { + return true; + } + } catch (CoreException e) { + // this method is called when selection changes, so + // just fall through if it fails. + // this shouldn't happen anyway, since the list of selected resources + // has already been checked for accessibility before this is called + } + return false; + } + }