summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java')
-rw-r--r--plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java270
1 files changed, 270 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java
new file mode 100644
index 0000000..d8c1232
--- /dev/null
+++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/wizard/NewYoctoProjectTemplateProcess.java
@@ -0,0 +1,270 @@
1/*******************************************************************************
2 * Copyright (c) 2010 Intel Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Intel - initial API and implementation
10 *******************************************************************************/
11package org.yocto.sdk.ide.wizard;
12
13import java.util.Arrays;
14import java.util.LinkedHashMap;
15import java.util.List;
16import java.util.regex.Matcher;
17import java.util.regex.Pattern;
18
19import org.eclipse.cdt.autotools.core.AutotoolsNewProjectNature;
20import org.eclipse.cdt.core.CCorePlugin;
21import org.eclipse.cdt.core.templateengine.TemplateCore;
22import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
23import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
24import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
25import org.eclipse.cdt.core.templateengine.process.processes.Messages;
26import org.eclipse.cdt.internal.autotools.core.configure.AutotoolsConfigurationManager;
27import org.eclipse.cdt.make.core.MakeCorePlugin;
28import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager;
29import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IDiscoveredPathInfo;
30import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IPerProjectDiscoveredPathInfo;
31import org.eclipse.cdt.make.internal.core.scannerconfig.util.SymbolEntry;
32import org.eclipse.cdt.managedbuilder.core.BuildException;
33import org.eclipse.cdt.managedbuilder.core.IConfiguration;
34import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
35import org.eclipse.cdt.managedbuilder.core.IOption;
36import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
37import org.eclipse.cdt.managedbuilder.templateengine.ProjectCreatedActions;
38import org.eclipse.cdt.managedbuilder.ui.wizards.MBSCustomPageManager;
39import org.eclipse.jface.wizard.IWizardPage;
40import org.eclipse.cdt.ui.wizards.CDTMainWizardPage;
41import org.eclipse.cdt.internal.ui.wizards.ICDTCommonProjectWizard;
42import org.eclipse.core.resources.IProject;
43import org.eclipse.core.resources.IWorkspace;
44import org.eclipse.core.resources.IWorkspaceDescription;
45import org.eclipse.core.resources.ResourcesPlugin;
46import org.eclipse.core.runtime.CoreException;
47import org.eclipse.core.runtime.IPath;
48import org.eclipse.core.runtime.IProgressMonitor;
49import org.eclipse.core.runtime.OperationCanceledException;
50import org.eclipse.core.runtime.Path;
51import org.eclipse.jface.preference.IPreferenceStore;
52import org.yocto.sdk.ide.YoctoGeneralException;
53import org.yocto.sdk.ide.YoctoProfileElement;
54import org.yocto.sdk.ide.YoctoSDKChecker;
55import org.yocto.sdk.ide.YoctoSDKMessages;
56import org.yocto.sdk.ide.YoctoSDKPlugin;
57import org.yocto.sdk.ide.YoctoUIElement;
58import org.yocto.sdk.ide.natures.YoctoSDKAutotoolsProjectNature;
59import org.yocto.sdk.ide.natures.YoctoSDKCMakeProjectNature;
60import org.yocto.sdk.ide.natures.YoctoSDKEmptyProjectNature;
61import org.yocto.sdk.ide.natures.YoctoSDKNatureUtils;
62import org.yocto.sdk.ide.natures.YoctoSDKProjectNature;
63import org.yocto.sdk.ide.utils.ProjectPreferenceUtils;
64import org.yocto.sdk.ide.utils.YoctoSDKUtils;
65
66@SuppressWarnings("restriction")
67public class NewYoctoProjectTemplateProcess extends ProcessRunner {
68 protected boolean savedAutoBuildingValue;
69 protected ProjectCreatedActions pca;
70 protected IManagedBuildInfo info;
71 protected List<Character> illegalChars = Arrays.asList('$', '"','#','%','&','\'','(',')','*', '+', ',','.','/',':',';','<','=','>','?','@','[','\\',']','^','`','{','|','}','~');
72 private static final String PROJECT_NAME_ERROR = "Wizard.SDK.Error.ProjectName";
73
74 private boolean isCProject;
75 private boolean isEmptyProject;
76 private boolean isAutotoolsProject;
77 private boolean isCMakeProject;
78
79 public NewYoctoProjectTemplateProcess() {
80 pca = new ProjectCreatedActions();
81
82 isCProject = false;
83 isEmptyProject = false;
84 isAutotoolsProject = false;
85 isCMakeProject = false;
86 }
87
88 private String printIllegalChars(){
89 String print = "";
90 for (Character ch : illegalChars)
91 print += ch + ", ";
92 print = print.substring(0, print.length() - 2);
93 return print;
94 }
95
96 public void process(TemplateCore template, ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {
97
98 String projectName = args[0].getSimpleValue();
99 String location = args[1].getSimpleValue();
100 String artifactExtension = args[2].getSimpleValue();
101 String isCProjectValue = args[3].getSimpleValue();
102 String isEmptyProjetValue = args[4].getSimpleValue();
103 String isAutotoolsProjectValue = args[5].getSimpleValue();
104 String isCMakeProjectValue = args[6].getSimpleValue();
105
106 isCProject = Boolean.valueOf(isCProjectValue).booleanValue();
107 isEmptyProject = Boolean.valueOf(isEmptyProjetValue).booleanValue();
108 isAutotoolsProject = Boolean.valueOf(isAutotoolsProjectValue).booleanValue();
109 isCMakeProject = Boolean.valueOf(isCMakeProjectValue).booleanValue();
110
111 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
112 try {
113 if (!isValidProjectName(projectName)) {
114
115 IWizardPage[] pages = MBSCustomPageManager.getPages();
116 if(pages != null && pages.length > 0) {
117 CDTMainWizardPage cdtMainPage = (CDTMainWizardPage)pages[0];
118 cdtMainPage.setPageComplete(false);
119 ICDTCommonProjectWizard wizard = (ICDTCommonProjectWizard) pages[0].getWizard();
120 wizard.performCancel();
121
122 project.delete(true, null);
123 }
124 throw new ProcessFailureException(YoctoSDKMessages.getFormattedString(PROJECT_NAME_ERROR, new Object[]{projectName, printIllegalChars()}));
125 }
126
127 if (!project.exists()) {
128 IWorkspace workspace = ResourcesPlugin.getWorkspace();
129 turnOffAutoBuild(workspace);
130
131 IPath locationPath = null;
132 if (location != null && !location.trim().equals("")) { //$NON-NLS-1$
133 locationPath = Path.fromPortableString(location);
134 }
135
136 List<?> configs = template.getTemplateInfo().getConfigurations();
137
138 if (configs == null || configs.size() == 0) {
139 throw new ProcessFailureException(Messages.getString("NewManagedProject.4") + projectName); //$NON-NLS-1$
140 }
141
142 pca.setProject(project);
143 pca.setProjectLocation(locationPath);
144 pca.setConfigs((IConfiguration[]) configs.toArray(new IConfiguration[configs.size()]));
145 pca.setArtifactExtension(artifactExtension);
146 info = pca.createProject(monitor, CCorePlugin.DEFAULT_INDEXER, isCProject);
147
148 addNatures(project, false, monitor);
149
150 info.setValid(true);
151 ManagedBuildManager.saveBuildInfo(project, true);
152
153 restoreAutoBuild(workspace);
154 } else {
155
156 IWorkspace workspace = ResourcesPlugin.getWorkspace();
157 turnOffAutoBuild(workspace);
158
159 YoctoSDKChecker.checkIfGloballySelectedYoctoProfileIsValid();
160
161 addNatures(project, true, monitor);
162
163 //restoreAutoBuild(workspace);
164 IDiscoveredPathManager manager = MakeCorePlugin.getDefault().getDiscoveryManager();
165 IDiscoveredPathInfo pathInfo = manager.getDiscoveredInfo(project);
166
167 if (pathInfo instanceof IPerProjectDiscoveredPathInfo) {
168 IPerProjectDiscoveredPathInfo projectPathInfo =
169 (IPerProjectDiscoveredPathInfo) pathInfo;
170 projectPathInfo.setIncludeMap(new LinkedHashMap<String, Boolean>());
171 projectPathInfo.setSymbolMap(new LinkedHashMap<String, SymbolEntry>());
172 manager.removeDiscoveredInfo(project);
173 }
174 }
175 }
176 catch (CoreException e)
177 {
178 throw new ProcessFailureException(Messages.getString("NewManagedProject.3") + e.getMessage(), e); //$NON-NLS-1$
179 }
180 catch (BuildException e)
181 {
182 throw new ProcessFailureException(Messages.getString("NewManagedProject.3") + e.getMessage()); //$NON-NLS-1$
183 }
184 catch (YoctoGeneralException e)
185 {
186 try {
187 project.delete(true, monitor);
188 } catch (CoreException err) {
189 throw new ProcessFailureException(Messages.getString("NewManagedProject.3") + e.getMessage() //$NON-NLS-1$
190 + " " + err.getMessage()); //$NON-NLS-1$
191 }
192 throw new OperationCanceledException(Messages.getString("NewManagedProject.3") + e.getMessage()); //$NON-NLS-1$
193 }
194 }
195
196 private boolean isValidProjectName(String projectName) {
197 Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_\\-]*$"); //$NON-NLS-1$
198 Matcher matcher = pattern.matcher(projectName);
199 return matcher.find();
200 }
201
202 private void addNatures(IProject project, boolean projectExists, IProgressMonitor monitor)
203 throws CoreException, YoctoGeneralException {
204 YoctoSDKNatureUtils.addNature(project, YoctoSDKProjectNature.YoctoSDK_NATURE_ID, monitor);
205
206 YoctoSDKChecker.checkIfGloballySelectedYoctoProfileIsValid();
207
208 YoctoProfileElement profileElement = YoctoSDKUtils.getProfilesFromDefaultStore();
209 ProjectPreferenceUtils.saveProfiles(profileElement, project);
210
211 IPreferenceStore selecteProfileStore = YoctoSDKPlugin.getProfilePreferenceStore(profileElement.getSelectedProfile());
212 YoctoUIElement elem = YoctoSDKUtils.getElemFromStore(selecteProfileStore);
213 YoctoSDKUtils.setEnvironmentVariables(project, elem);
214
215 if (isEmptyProject) {
216 YoctoSDKNatureUtils.addNature(project, YoctoSDKEmptyProjectNature.YoctoSDK_EMPTY_NATURE_ID, monitor);
217 }
218
219 if (isAutotoolsProject) {
220 AutotoolsNewProjectNature.addAutotoolsNature(project, monitor);
221
222 if (!projectExists) {
223 // For each IConfiguration, create a corresponding Autotools Configuration
224 for (IConfiguration cfg : pca.getConfigs()) {
225 AutotoolsConfigurationManager.getInstance().getConfiguration(project, cfg.getName(), true);
226 }
227 AutotoolsConfigurationManager.getInstance().saveConfigs(project);
228 }
229
230 YoctoSDKNatureUtils.addNature(project, YoctoSDKAutotoolsProjectNature.YoctoSDK_AUTOTOOLS_NATURE_ID, monitor);
231 YoctoSDKAutotoolsProjectNature.configureAutotoolsOptions(project);
232 } else if (isCMakeProject) {
233 YoctoSDKNatureUtils.addNature(project, YoctoSDKCMakeProjectNature.YoctoSDK_CMAKE_NATURE_ID, monitor);
234 YoctoSDKCMakeProjectNature.extendProjectEnvironmentForCMake(project);
235 }
236
237 YoctoSDKUtils.createRemoteDebugAndQemuLaunchers(project, elem);
238 }
239
240 protected final void turnOffAutoBuild(IWorkspace workspace) throws CoreException {
241 IWorkspaceDescription workspaceDesc = workspace.getDescription();
242 savedAutoBuildingValue = workspaceDesc.isAutoBuilding();
243 workspaceDesc.setAutoBuilding(false);
244 workspace.setDescription(workspaceDesc);
245 }
246
247 protected final void restoreAutoBuild(IWorkspace workspace) throws CoreException {
248 IWorkspaceDescription workspaceDesc = workspace.getDescription();
249 workspaceDesc.setAutoBuilding(savedAutoBuildingValue);
250 workspace.setDescription(workspaceDesc);
251 }
252
253 /**
254 * setOptionValue
255 * @param config
256 * @param option
257 * @param val
258 * @throws BuildException
259 */
260 protected void setOptionValue(IConfiguration config, IOption option, String val) throws BuildException {
261 if (val != null) {
262 if (!option.isExtensionElement()) {
263 option.setValue(val);
264 } else {
265 IOption newOption = config.getToolChain().createOption(option, option.getId() + "." + ManagedBuildManager.getRandomNumber(), option.getName(), false); //$NON-NLS-1$
266 newOption.setValue(val);
267 }
268 }
269 }
270}