Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eaeb240d2dbd44bc3397d3fb816e03e7aed1bca0 (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
/*******************************************************************************
 * Copyright (c) 2015 Mentor Graphics 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:
 *     Mohamed Azab (Mentor Graphics) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.autotools.ui.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.cdt.autotools.ui.AutotoolsUIPlugin;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
import org.eclipse.cdt.core.settings.model.extension.CConfigurationData;
import org.eclipse.cdt.managedbuilder.core.IBuilder;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IProjectType;
import org.eclipse.cdt.managedbuilder.core.IToolChain;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
import org.eclipse.cdt.managedbuilder.internal.core.ToolChain;
import org.eclipse.cdt.managedbuilder.ui.wizards.CfgHolder;
import org.eclipse.cdt.managedbuilder.ui.wizards.MBSCustomPageManager;
import org.eclipse.cdt.managedbuilder.ui.wizards.NewMakeProjFromExisting;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

@SuppressWarnings("restriction")
public class AutotoolsProjectImportWizard extends NewMakeProjFromExisting {
	private final String TITLE = "Import Existing code as Autotools project";
	private static final String PREFIX = "WizardAutotoolsConversion"; //$NON-NLS-1$
	protected static final String CONF_TITLE = PREFIX + ".config.title"; //$NON-NLS-1$
	protected static final String CONF_DESC = PREFIX + ".config.desc"; //$NON-NLS-1$

	private AutotoolsProjectImportWizardPage page;
	protected CProjectPlatformPage projectConfigurationPage;

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		setWindowTitle(TITLE);
	}

	@Override
	public void addPages() {
		page = new AutotoolsProjectImportWizardPage();
		addPage(page);

		// Add the configuration selection page
		projectConfigurationPage = new CProjectPlatformPage(PREFIX);
		projectConfigurationPage.setTitle(AutotoolsUIPlugin
				.getResourceString(CONF_TITLE));
		projectConfigurationPage.setDescription(AutotoolsUIPlugin
				.getResourceString(CONF_DESC));
		addPage(projectConfigurationPage);

		// add custom pages
		MBSCustomPageManager.init();

		// add stock pages
		MBSCustomPageManager.addStockPage(projectConfigurationPage,
				CProjectPlatformPage.PAGE_ID);

	}

	public IProjectType getProjectType() {
		return projectConfigurationPage.getProjectType();
	}

	public IConfiguration[] getSelectedConfigurations() {
		return projectConfigurationPage.getSelectedConfigurations();
	}

	@Override
	public boolean performFinish() {
		final String projectName = page.getProjectName();
		final String locationStr = page.getLocation();
		final boolean isCPP = page.isCPP();
		final IToolChain toolChain = page.getToolChain();

		IRunnableWithProgress op = new WorkspaceModifyOperation() {
			@Override
			protected void execute(IProgressMonitor monitor) {
				monitor.beginTask("Creating Autotools project", 10);

				// Create Project
				try {
					IWorkspace workspace = ResourcesPlugin.getWorkspace();
					IProject project = workspace.getRoot().getProject(
							projectName);

					IProjectDescription description = workspace
							.newProjectDescription(projectName);
					IPath defaultLocation = workspace.getRoot().getLocation()
							.append(projectName);
					Path location = new Path(locationStr);
					if (!location.isEmpty()
							&& !location.equals(defaultLocation)) {
						description.setLocation(location);
					}

					CCorePlugin.getDefault().createCDTProject(description,
							project, monitor);

					// C++ natures
					if (isCPP) {
						CCProjectNature.addCCNature(project, SubMonitor.convert(monitor, 1));
					}

					// Set up build information
					ICProjectDescriptionManager pdMgr = CoreModel.getDefault()
							.getProjectDescriptionManager();
					ICProjectDescription projDesc = pdMgr
							.createProjectDescription(project, false);
					ManagedBuildInfo info = ManagedBuildManager
							.createBuildInfo(project);
					ManagedProject mProj = new ManagedProject(projDesc);
					info.setManagedProject(mProj);
					monitor.worked(1);

					CfgHolder cfgHolder = new CfgHolder(toolChain, null);
					String s = toolChain == null ? "0" : ((ToolChain) toolChain).getId(); //$NON-NLS-1$
					Configuration config = new Configuration(mProj,
							(ToolChain) toolChain,
							ManagedBuildManager.calculateChildId(s, null),
							cfgHolder.getName());
					IBuilder builder = config.getEditableBuilder();
					builder.setManagedBuildOn(false);
					CConfigurationData data = config.getConfigurationData();
					projDesc.createConfiguration(
							ManagedBuildManager.CFG_DATA_PROVIDER_ID, data);
					monitor.worked(1);

					pdMgr.setProjectDescription(project, projDesc);

					// Convert the created project to an Autotools project
					page.convertProject(
							project, monitor, projectName);
				} catch (Throwable e) {
				}
				monitor.done();
			}
		};

		try {
			getContainer().run(true, true, op);
		} catch (InvocationTargetException | InterruptedException e) {
			return false;
		}
		return true;
	}

}

Back to the top