Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94ab6051cb36bfb770a5a3aae27b390a7b383beb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************************
 * Copyright (c) 2000, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.ui.actions;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Vector;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.make.core.IMakeBuilderInfo;
import org.eclipse.cdt.make.core.IMakeCommonBuildInfo;
import org.eclipse.cdt.make.core.IMakeTarget;
import org.eclipse.cdt.make.core.IMakeTargetManager;
import org.eclipse.cdt.make.core.MakeBuilder;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.MakeProjectNature;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.ui.wizards.UpdateMakeProjectWizard;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class UpdateMakeProjectAction implements IWorkbenchWindowActionDelegate {

	private ISelection fSelection;

	@Override
	public void dispose() {
	}

	@Override
	public void init(IWorkbenchWindow window) {
	}

	@Override
	public void run(IAction action) {
		if (fSelection instanceof IStructuredSelection) {
			Object[] elems = ((IStructuredSelection) fSelection).toArray();
			ArrayList<IProject> projects = new ArrayList<IProject>(elems.length);

			for (int i = 0; i < elems.length; i++) {
				Object elem = elems[i];
				IProject project = null;

				if (elem instanceof IFile) {
					IFile file = (IFile) elem;
					project = file.getProject();
				} else if (elem instanceof IProject) {
					project = (IProject) elem;
				} else if (elem instanceof ICProject) {
					project = ((ICProject) elem).getProject();
				}
				if (project != null) {
					projects.add(project);
				}
			}

			final IProject[] projectArray = projects.toArray(new IProject[projects.size()]);

			UpdateMakeProjectWizard wizard = new UpdateMakeProjectWizard(projectArray);
			WizardDialog dialog = new WizardDialog(MakeUIPlugin.getActiveWorkbenchShell(), wizard);
			dialog.open();
		}

	}

	public static IProject[] getOldProjects() {
		IProject[] project = MakeUIPlugin.getWorkspace().getRoot().getProjects();
		Vector<IProject> result = new Vector<IProject>();
		try {
			for (int i = 0; i < project.length; i++) {
				if (isOldProject(project[i])) {
					result.add(project[i]);
				}
			}
		} catch (CoreException e) {
			MakeUIPlugin.logException(e);
		}

		return result.toArray(new IProject[result.size()]);
	}

	protected static boolean isOldProject(IProject project) throws CoreException {
		if (project.isAccessible()) {
			IProjectDescription desc = project.getDescription();
			ICommand builder[] = desc.getBuildSpec();
			for (int j = 0; j < builder.length; j++) {
				if (builder[j].getBuilderName().equals(MakeCorePlugin.OLD_BUILDER_ID)) {
					return true;
				}
			}
		}
		return false;
	}

	static public void run(boolean fork, IRunnableContext context, final IProject[] projects) {
		try {
			context.run(fork, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					try {
						IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
							@Override
							public void run(IProgressMonitor monitor) throws CoreException {
								doProjectUpdate(monitor, projects);
							}
						};
						MakeUIPlugin.getWorkspace().run(runnable, monitor);
					} catch (CoreException e) {
						throw new InvocationTargetException(e);
					} catch (OperationCanceledException e) {
						throw new InterruptedException(e.getMessage());
					}
				}
			});
		} catch (InterruptedException e) {
			return;
		} catch (InvocationTargetException e) {
			MakeUIPlugin.logException(e, MakeUIPlugin.getResourceString("UpdateMakeProjectAction.exception.error"), //$NON-NLS-1$
					MakeUIPlugin.getResourceString("UpdateMakeProjectAction.eception.message")); //$NON-NLS-1$
		}
	}

	public static class TargetConvertVisitor implements IResourceProxyVisitor {
		private final int TOTAL_WORK = 100;
		private int halfWay = TOTAL_WORK / 2;
		private int currentIncrement = 4;
		private int nextProgress = currentIncrement;
		private int worked = 0;
		IProgressMonitor monitor;

		public TargetConvertVisitor(IProgressMonitor monitor) {
			this.monitor = monitor;
			monitor.beginTask(MakeUIPlugin.getResourceString("UpdateMakeProjectAction.monitor.convert"), TOTAL_WORK); //$NON-NLS-1$
		}

		@Override
		public boolean visit(IResourceProxy proxy) throws CoreException {
			try {
				if (proxy.getType() != IResource.FOLDER && proxy.getType() != IResource.PROJECT) {
					return false;
				}
				IContainer container = (IContainer) proxy.requestResource();
				monitor.subTask(container.getProjectRelativePath().toString());
				QualifiedName qName = new QualifiedName("org.eclipse.cdt.make", "goals"); //$NON-NLS-1$ //$NON-NLS-2$
				String goal = container.getPersistentProperty(qName);
				if (goal != null) {
					goal = goal.trim();
					IMakeTargetManager manager = MakeCorePlugin.getDefault().getTargetManager();
					String[] builder = manager.getTargetBuilders(container.getProject());
					IMakeTarget target = manager.createTarget(container.getProject(), goal, builder[0]);
					target.setBuildAttribute(IMakeTarget.BUILD_TARGET, goal);
					manager.addTarget(container, target);
					container.setPersistentProperty(qName, null);
				}
				return true;
			} finally {
				if (--nextProgress <= 0) {
					//we have exhausted the current increment, so report progress
					monitor.worked(1);
					worked++;
					if (worked >= halfWay) {
						//we have passed the current halfway point, so double the
						//increment and reset the halfway point.
						currentIncrement *= 2;
						halfWay += (TOTAL_WORK - halfWay) / 2;
					}
					//reset the progress counter to another full increment
					nextProgress = currentIncrement;
				}

			}
		}
	}

	protected static void doProjectUpdate(IProgressMonitor monitor, IProject[] project) throws CoreException {
		monitor.beginTask(MakeUIPlugin.getResourceString("UpdateMakeProjectAction.monitor.update"), project.length * 4); //$NON-NLS-1$
		try {
			for (int i = 0; i < project.length; i++) {
				// remove old builder
				project[i].refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(monitor, 1));
				MakeProjectNature.removeFromBuildSpec(project[i], MakeCorePlugin.OLD_BUILDER_ID,
						new SubProgressMonitor(monitor, 1));

				// convert .cdtproject
				CCorePlugin.getDefault().mapCProjectOwner(project[i], MakeCorePlugin.MAKE_PROJECT_ID, true);
				// add new nature
				MakeProjectNature.addNature(project[i], new SubProgressMonitor(monitor, 1));

				// move existing build properties to new
				IMakeBuilderInfo newInfo = MakeCorePlugin.createBuildInfo(project[i], MakeBuilder.BUILDER_ID);
				final int LOCATION = 0, FULL_ARGS = 1, INC_ARGS = 2, STOP_ERORR = 3, USE_DEFAULT = 4;
				QualifiedName[] qName = new QualifiedName[USE_DEFAULT + 1];
				qName[LOCATION] = new QualifiedName(CCorePlugin.PLUGIN_ID, "buildLocation"); //$NON-NLS-1$
				qName[FULL_ARGS] = new QualifiedName(CCorePlugin.PLUGIN_ID, "buildFullArguments"); //$NON-NLS-1$
				qName[INC_ARGS] = new QualifiedName(CCorePlugin.PLUGIN_ID, "buildIncrementalArguments"); //$NON-NLS-1$
				qName[STOP_ERORR] = new QualifiedName(CCorePlugin.PLUGIN_ID, "stopOnError"); //$NON-NLS-1$
				qName[USE_DEFAULT] = new QualifiedName(CCorePlugin.PLUGIN_ID, "useDefaultBuildCmd"); //$NON-NLS-1$

				String property = project[i].getPersistentProperty(qName[LOCATION]);
				if (property != null) {
					newInfo.setBuildAttribute(IMakeCommonBuildInfo.BUILD_COMMAND, property);
				}
				property = project[i].getPersistentProperty(qName[FULL_ARGS]);
				if (property != null) {
					newInfo.setBuildAttribute(IMakeCommonBuildInfo.BUILD_ARGUMENTS, property);
				}
				property = project[i].getPersistentProperty(qName[STOP_ERORR]);
				if (property != null) {
					newInfo.setStopOnError(Boolean.valueOf(property).booleanValue());
				}
				property = project[i].getPersistentProperty(qName[USE_DEFAULT]);
				if (property != null) {
					newInfo.setUseDefaultBuildCmd(Boolean.valueOf(property).booleanValue());
				}
				for (int j = 0; j < qName.length; j++) {
					project[i].setPersistentProperty(qName[j], null);
				}

				IProgressMonitor subMon = new SubProgressMonitor(monitor, 1,
						SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
				project[i].accept(new TargetConvertVisitor(subMon), 0);
				monitor.worked(1);
			}
		} finally {
			monitor.done();
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		boolean enabled = false;
		fSelection = selection;
		if (fSelection instanceof IStructuredSelection) {
			Object sel = ((IStructuredSelection) fSelection).getFirstElement();
			if (sel instanceof IAdaptable) {
				IResource res = ((IAdaptable) sel).getAdapter(IResource.class);
				try {
					if (res instanceof IProject && isOldProject((IProject) res)) {
						enabled = true;
					}
				} catch (CoreException e) {
				}
			}
		}
		action.setEnabled(enabled);
	}
}

Back to the top