001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.build;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: import org.eclipse.ant.internal.ui.IAntUIConstants;
018: import org.eclipse.ant.internal.ui.launchConfigurations.AntLaunchShortcut;
019: import org.eclipse.ant.internal.ui.launchConfigurations.IAntLaunchConfigurationConstants;
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IMarker;
022: import org.eclipse.core.resources.IProject;
023: import org.eclipse.core.resources.IResource;
024: import org.eclipse.core.resources.IWorkspaceRunnable;
025: import org.eclipse.core.resources.IncrementalProjectBuilder;
026: import org.eclipse.core.runtime.CoreException;
027: import org.eclipse.core.runtime.IProgressMonitor;
028: import org.eclipse.core.runtime.Preferences;
029: import org.eclipse.debug.core.ILaunchConfiguration;
030: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
031: import org.eclipse.jdt.core.IJavaProject;
032: import org.eclipse.jdt.core.JavaCore;
033: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
034: import org.eclipse.jdt.launching.JavaRuntime;
035: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
036: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
037: import org.eclipse.jface.action.IAction;
038: import org.eclipse.jface.dialogs.MessageDialog;
039: import org.eclipse.jface.operation.IRunnableWithProgress;
040: import org.eclipse.jface.viewers.ISelection;
041: import org.eclipse.jface.viewers.IStructuredSelection;
042: import org.eclipse.pde.core.plugin.TargetPlatform;
043: import org.eclipse.pde.internal.build.IXMLConstants;
044: import org.eclipse.pde.internal.core.exports.BuildUtilities;
045: import org.eclipse.pde.internal.core.natures.PDE;
046: import org.eclipse.pde.internal.ui.PDEPlugin;
047: import org.eclipse.pde.internal.ui.PDEUIMessages;
048: import org.eclipse.ui.IObjectActionDelegate;
049: import org.eclipse.ui.IWorkbenchPart;
050: import org.eclipse.ui.PlatformUI;
051:
052: public abstract class BaseBuildAction implements IObjectActionDelegate {
053:
054: protected IFile fManifestFile;
055:
056: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
057: }
058:
059: public void run(IAction action) {
060: if (!fManifestFile.exists())
061: return;
062:
063: IRunnableWithProgress op = new IRunnableWithProgress() {
064: public void run(IProgressMonitor monitor) {
065: IWorkspaceRunnable wop = new IWorkspaceRunnable() {
066: public void run(IProgressMonitor monitor)
067: throws CoreException {
068: try {
069: doBuild(monitor);
070: } catch (InvocationTargetException e) {
071: PDEPlugin.logException(e);
072: }
073: }
074: };
075: try {
076: PDEPlugin.getWorkspace().run(wop, monitor);
077: } catch (CoreException e) {
078: PDEPlugin.logException(e);
079: }
080: }
081: };
082: try {
083: PlatformUI.getWorkbench().getProgressService().runInUI(
084: PDEPlugin.getActiveWorkbenchWindow(), op,
085: PDEPlugin.getWorkspace().getRoot());
086: } catch (InterruptedException e) {
087: } catch (InvocationTargetException e) {
088: PDEPlugin.logException(e);
089: }
090:
091: }
092:
093: public void selectionChanged(IAction action, ISelection selection) {
094: if (selection instanceof IStructuredSelection) {
095: Object obj = ((IStructuredSelection) selection)
096: .getFirstElement();
097: if (obj != null && obj instanceof IFile) {
098: this .fManifestFile = (IFile) obj;
099: }
100: }
101:
102: }
103:
104: private void doBuild(IProgressMonitor monitor)
105: throws CoreException, InvocationTargetException {
106: monitor.beginTask(PDEUIMessages.BuildAction_Validate, 4);
107: if (!ensureValid(fManifestFile, monitor)) {
108: monitor.done();
109: return;
110: }
111: monitor.worked(1);
112: monitor.setTaskName(PDEUIMessages.BuildAction_Generate);
113: makeScripts(monitor);
114: monitor.worked(1);
115: monitor.setTaskName(PDEUIMessages.BuildAction_Update);
116: refreshLocal(monitor);
117: monitor.worked(1);
118: IProject project = fManifestFile.getProject();
119: IFile generatedFile = (IFile) project.findMember("build.xml"); //$NON-NLS-1$
120: if (generatedFile != null)
121: setDefaultValues(generatedFile);
122: monitor.worked(1);
123:
124: }
125:
126: protected abstract void makeScripts(IProgressMonitor monitor)
127: throws InvocationTargetException, CoreException;
128:
129: public static boolean ensureValid(IFile file,
130: IProgressMonitor monitor) throws CoreException {
131: // Force the build if autobuild is off
132: IProject project = file.getProject();
133: if (!project.getWorkspace().isAutoBuilding()) {
134: String builderID = "feature.xml".equals(file.getName()) ? PDE.FEATURE_BUILDER_ID : PDE.MANIFEST_BUILDER_ID; //$NON-NLS-1$
135: project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD,
136: builderID, null, monitor);
137: }
138:
139: if (hasErrors(file)) {
140: // There are errors against this file - abort
141: MessageDialog.openError(null,
142: PDEUIMessages.BuildAction_ErrorDialog_Title,
143: PDEUIMessages.BuildAction_ErrorDialog_Message);
144: return false;
145: }
146: return true;
147: }
148:
149: public static boolean hasErrors(IFile file) throws CoreException {
150: IMarker[] markers = file.findMarkers(IMarker.PROBLEM, true,
151: IResource.DEPTH_ZERO);
152: for (int i = 0; i < markers.length; i++) {
153: Object att = markers[i].getAttribute(IMarker.SEVERITY);
154: if (att != null && att instanceof Integer) {
155: if (((Integer) att).intValue() == IMarker.SEVERITY_ERROR)
156: return true;
157: }
158: }
159: return false;
160: }
161:
162: protected void refreshLocal(IProgressMonitor monitor)
163: throws CoreException {
164: IProject project = fManifestFile.getProject();
165: project.refreshLocal(IResource.DEPTH_ONE, monitor);
166: IFile file = project.getFile("dev.properties"); //$NON-NLS-1$
167: if (file.exists())
168: file.delete(true, false, monitor);
169: project.refreshLocal(IResource.DEPTH_ONE, monitor);
170: }
171:
172: public static void setDefaultValues(IFile generatedFile) {
173: try {
174: List configs = AntLaunchShortcut
175: .findExistingLaunchConfigurations(generatedFile);
176: ILaunchConfigurationWorkingCopy launchCopy;
177: if (configs.size() == 0) {
178: ILaunchConfiguration config = AntLaunchShortcut
179: .createDefaultLaunchConfiguration(generatedFile);
180: launchCopy = config.getWorkingCopy();
181: } else {
182: launchCopy = ((ILaunchConfiguration) configs.get(0))
183: .getWorkingCopy();
184: }
185: if (launchCopy == null)
186: return;
187:
188: Map properties = new HashMap();
189: properties = launchCopy
190: .getAttribute(
191: IAntLaunchConfigurationConstants.ATTR_ANT_PROPERTIES,
192: properties);
193: properties.put(IXMLConstants.PROPERTY_BASE_WS,
194: TargetPlatform.getWS());
195: properties.put(IXMLConstants.PROPERTY_BASE_OS,
196: TargetPlatform.getOS());
197: properties.put(IXMLConstants.PROPERTY_BASE_ARCH,
198: TargetPlatform.getOSArch());
199: properties.put(IXMLConstants.PROPERTY_BASE_NL,
200: TargetPlatform.getNL());
201: properties.put("eclipse.running", "true"); //$NON-NLS-1$ //$NON-NLS-2$
202:
203: properties.put(IXMLConstants.PROPERTY_JAVAC_FAIL_ON_ERROR,
204: "false"); //$NON-NLS-1$
205: properties.put(IXMLConstants.PROPERTY_JAVAC_DEBUG_INFO,
206: "on"); //$NON-NLS-1$
207: properties.put(IXMLConstants.PROPERTY_JAVAC_VERBOSE,
208: "false"); //$NON-NLS-1$
209:
210: IProject project = generatedFile.getProject();
211: if (!project.hasNature(JavaCore.NATURE_ID)) {
212: Preferences pref = JavaCore.getPlugin()
213: .getPluginPreferences();
214: properties.put(IXMLConstants.PROPERTY_JAVAC_SOURCE,
215: pref.getString(JavaCore.COMPILER_SOURCE));
216: properties
217: .put(
218: IXMLConstants.PROPERTY_JAVAC_TARGET,
219: pref
220: .getString(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM));
221: } else {
222: IJavaProject jProject = JavaCore.create(project);
223: properties.put(IXMLConstants.PROPERTY_JAVAC_SOURCE,
224: jProject.getOption(JavaCore.COMPILER_SOURCE,
225: true));
226: properties
227: .put(
228: IXMLConstants.PROPERTY_JAVAC_TARGET,
229: jProject
230: .getOption(
231: JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
232: true));
233: }
234: properties.put(IXMLConstants.PROPERTY_BOOTCLASSPATH,
235: BuildUtilities.getBootClasspath());
236: IExecutionEnvironmentsManager manager = JavaRuntime
237: .getExecutionEnvironmentsManager();
238: IExecutionEnvironment[] envs = manager
239: .getExecutionEnvironments();
240: for (int i = 0; i < envs.length; i++) {
241: String id = envs[i].getId();
242: if (id != null)
243: properties.put(id, BuildUtilities
244: .getBootClasspath(id));
245: }
246:
247: launchCopy
248: .setAttribute(
249: IAntLaunchConfigurationConstants.ATTR_ANT_PROPERTIES,
250: properties);
251: launchCopy
252: .setAttribute(
253: IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME,
254: (String) null);
255: launchCopy
256: .setAttribute(
257: IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE,
258: (String) null);
259: launchCopy
260: .setAttribute(
261: IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
262: (String) null);
263: launchCopy
264: .setAttribute(
265: IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
266: (String) null);
267: launchCopy.setAttribute(
268: IAntUIConstants.ATTR_DEFAULT_VM_INSTALL,
269: (String) null);
270: launchCopy.doSave();
271: } catch (CoreException e) {
272: }
273: }
274:
275: }
|