summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java
blob: d8c123219eeec1a747669e64ca1887659002da2b (plain)
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
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.ide.wizard;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.autotools.core.AutotoolsNewProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.cdt.core.templateengine.process.processes.Messages;
import org.eclipse.cdt.internal.autotools.core.configure.AutotoolsConfigurationManager;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager;
import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IDiscoveredPathInfo;
import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IPerProjectDiscoveredPathInfo;
import org.eclipse.cdt.make.internal.core.scannerconfig.util.SymbolEntry;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.templateengine.ProjectCreatedActions;
import org.eclipse.cdt.managedbuilder.ui.wizards.MBSCustomPageManager;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.cdt.ui.wizards.CDTMainWizardPage;
import org.eclipse.cdt.internal.ui.wizards.ICDTCommonProjectWizard;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
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.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.yocto.sdk.ide.YoctoGeneralException;
import org.yocto.sdk.ide.YoctoProfileElement;
import org.yocto.sdk.ide.YoctoSDKChecker;
import org.yocto.sdk.ide.YoctoSDKMessages;
import org.yocto.sdk.ide.YoctoSDKPlugin;
import org.yocto.sdk.ide.YoctoUIElement;
import org.yocto.sdk.ide.natures.YoctoSDKAutotoolsProjectNature;
import org.yocto.sdk.ide.natures.YoctoSDKCMakeProjectNature;
import org.yocto.sdk.ide.natures.YoctoSDKEmptyProjectNature;
import org.yocto.sdk.ide.natures.YoctoSDKNatureUtils;
import org.yocto.sdk.ide.natures.YoctoSDKProjectNature;
import org.yocto.sdk.ide.utils.ProjectPreferenceUtils;
import org.yocto.sdk.ide.utils.YoctoSDKUtils;

@SuppressWarnings("restriction")
public class NewYoctoProjectTemplateProcess extends ProcessRunner {
	protected boolean savedAutoBuildingValue;
	protected ProjectCreatedActions pca;
	protected IManagedBuildInfo info;
	protected List<Character> illegalChars = Arrays.asList('$', '"','#','%','&','\'','(',')','*', '+', ',','.','/',':',';','<','=','>','?','@','[','\\',']','^','`','{','|','}','~');
	private static final String PROJECT_NAME_ERROR = "Wizard.SDK.Error.ProjectName";

	private boolean isCProject;
	private boolean isEmptyProject;
	private boolean isAutotoolsProject;
	private boolean isCMakeProject;

	public NewYoctoProjectTemplateProcess() {
		pca = new ProjectCreatedActions();

		isCProject = false;
		isEmptyProject = false;
		isAutotoolsProject = false;
		isCMakeProject = false;
	}

	private String printIllegalChars(){
		String print = "";
		for (Character ch : illegalChars)
			print += ch + ", ";
		print = print.substring(0, print.length() - 2);
		return print;
	}

	public void process(TemplateCore template, ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {

		String projectName = args[0].getSimpleValue();
		String location = args[1].getSimpleValue();
		String artifactExtension = args[2].getSimpleValue();
		String isCProjectValue = args[3].getSimpleValue();
		String isEmptyProjetValue = args[4].getSimpleValue();
		String isAutotoolsProjectValue = args[5].getSimpleValue();
		String isCMakeProjectValue = args[6].getSimpleValue();

		isCProject = Boolean.valueOf(isCProjectValue).booleanValue();
		isEmptyProject = Boolean.valueOf(isEmptyProjetValue).booleanValue();
		isAutotoolsProject = Boolean.valueOf(isAutotoolsProjectValue).booleanValue();
		isCMakeProject = Boolean.valueOf(isCMakeProjectValue).booleanValue();

		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		try {
			if (!isValidProjectName(projectName)) {

				IWizardPage[] pages = MBSCustomPageManager.getPages();
				if(pages != null && pages.length > 0) {
					CDTMainWizardPage cdtMainPage = (CDTMainWizardPage)pages[0];
					cdtMainPage.setPageComplete(false);
					ICDTCommonProjectWizard wizard = (ICDTCommonProjectWizard) pages[0].getWizard();
					wizard.performCancel();

					project.delete(true, null);
				}
				throw new ProcessFailureException(YoctoSDKMessages.getFormattedString(PROJECT_NAME_ERROR, new Object[]{projectName, printIllegalChars()}));
			}

			if (!project.exists()) {
				IWorkspace workspace = ResourcesPlugin.getWorkspace();
				turnOffAutoBuild(workspace);

				IPath locationPath = null;
				if (location != null && !location.trim().equals("")) { //$NON-NLS-1$
					locationPath = Path.fromPortableString(location);
				}

				List<?> configs = template.getTemplateInfo().getConfigurations();

				if (configs == null || configs.size() == 0) {
					throw new ProcessFailureException(Messages.getString("NewManagedProject.4") + projectName); //$NON-NLS-1$
				}

				pca.setProject(project);
				pca.setProjectLocation(locationPath);
				pca.setConfigs((IConfiguration[]) configs.toArray(new IConfiguration[configs.size()]));
				pca.setArtifactExtension(artifactExtension);
				info = pca.createProject(monitor, CCorePlugin.DEFAULT_INDEXER, isCProject);

				addNatures(project, false, monitor);

				info.setValid(true);
				ManagedBuildManager.saveBuildInfo(project, true);

				restoreAutoBuild(workspace);
			} else {

				IWorkspace workspace = ResourcesPlugin.getWorkspace();
				turnOffAutoBuild(workspace);

				YoctoSDKChecker.checkIfGloballySelectedYoctoProfileIsValid();

				addNatures(project, true, monitor);

				//restoreAutoBuild(workspace);
				IDiscoveredPathManager manager = MakeCorePlugin.getDefault().getDiscoveryManager();
				IDiscoveredPathInfo pathInfo = manager.getDiscoveredInfo(project);

				if (pathInfo instanceof IPerProjectDiscoveredPathInfo) {
					IPerProjectDiscoveredPathInfo projectPathInfo =
							(IPerProjectDiscoveredPathInfo) pathInfo;
					projectPathInfo.setIncludeMap(new LinkedHashMap<String, Boolean>());
					projectPathInfo.setSymbolMap(new LinkedHashMap<String, SymbolEntry>());
					manager.removeDiscoveredInfo(project);
				}
			}
		}
		catch (CoreException e)
		{
			throw new ProcessFailureException(Messages.getString("NewManagedProject.3") + e.getMessage(), e); //$NON-NLS-1$
		}
		catch (BuildException e)
		{
			throw new ProcessFailureException(Messages.getString("NewManagedProject.3") + e.getMessage()); //$NON-NLS-1$
		}
		catch (YoctoGeneralException e)
		{
			try {
				project.delete(true, monitor);
			} catch (CoreException err) {
				throw new ProcessFailureException(Messages.getString("NewManagedProject.3") + e.getMessage() //$NON-NLS-1$
						+ " " + err.getMessage()); //$NON-NLS-1$
			}
			throw new OperationCanceledException(Messages.getString("NewManagedProject.3") + e.getMessage()); //$NON-NLS-1$
		}
	}

	private boolean isValidProjectName(String projectName) {
		Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_\\-]*$"); //$NON-NLS-1$
		Matcher matcher = pattern.matcher(projectName);
		return matcher.find();
	}

	private void addNatures(IProject project, boolean projectExists, IProgressMonitor monitor)
			throws CoreException, YoctoGeneralException {
		YoctoSDKNatureUtils.addNature(project, YoctoSDKProjectNature.YoctoSDK_NATURE_ID, monitor);

		YoctoSDKChecker.checkIfGloballySelectedYoctoProfileIsValid();

		YoctoProfileElement profileElement = YoctoSDKUtils.getProfilesFromDefaultStore();
		ProjectPreferenceUtils.saveProfiles(profileElement, project);

		IPreferenceStore selecteProfileStore = YoctoSDKPlugin.getProfilePreferenceStore(profileElement.getSelectedProfile());
		YoctoUIElement elem = YoctoSDKUtils.getElemFromStore(selecteProfileStore);
		YoctoSDKUtils.setEnvironmentVariables(project, elem);

		if (isEmptyProject) {
			YoctoSDKNatureUtils.addNature(project, YoctoSDKEmptyProjectNature.YoctoSDK_EMPTY_NATURE_ID, monitor);
		}

		if (isAutotoolsProject) {
			AutotoolsNewProjectNature.addAutotoolsNature(project, monitor);

			if (!projectExists) {
				// For each IConfiguration, create a corresponding Autotools Configuration
				for (IConfiguration cfg : pca.getConfigs()) {
					AutotoolsConfigurationManager.getInstance().getConfiguration(project, cfg.getName(), true);
				}
				AutotoolsConfigurationManager.getInstance().saveConfigs(project);
			}

			YoctoSDKNatureUtils.addNature(project, YoctoSDKAutotoolsProjectNature.YoctoSDK_AUTOTOOLS_NATURE_ID, monitor);
			YoctoSDKAutotoolsProjectNature.configureAutotoolsOptions(project);
		} else if (isCMakeProject) {
			YoctoSDKNatureUtils.addNature(project, YoctoSDKCMakeProjectNature.YoctoSDK_CMAKE_NATURE_ID, monitor);
			YoctoSDKCMakeProjectNature.extendProjectEnvironmentForCMake(project);
		}

		YoctoSDKUtils.createRemoteDebugAndQemuLaunchers(project, elem);
	}

	protected final void turnOffAutoBuild(IWorkspace workspace) throws CoreException {
		IWorkspaceDescription workspaceDesc = workspace.getDescription();
		savedAutoBuildingValue = workspaceDesc.isAutoBuilding();
		workspaceDesc.setAutoBuilding(false);
		workspace.setDescription(workspaceDesc);
	}

	protected final void restoreAutoBuild(IWorkspace workspace) throws CoreException {
		IWorkspaceDescription workspaceDesc = workspace.getDescription();
		workspaceDesc.setAutoBuilding(savedAutoBuildingValue);
		workspace.setDescription(workspaceDesc);
	}

	/**
	 * setOptionValue
	 * @param config
	 * @param option
	 * @param val
	 * @throws BuildException
	 */
	protected void setOptionValue(IConfiguration config, IOption option, String val) throws BuildException {
		if (val != null) {
			if (!option.isExtensionElement()) {
				option.setValue(val);
			} else {
				IOption newOption = config.getToolChain().createOption(option, option.getId() + "." + ManagedBuildManager.getRandomNumber(), option.getName(), false); //$NON-NLS-1$
				newOption.setValue(val);
			}
		}
	}
}