Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b12dbb1de373cc7ca490b17dcd50c6361f61627 (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 * 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:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.arduino.core.internal;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuilder;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class ArduinoProjectGenerator {

	private final IProject project;
	private IFile sourceFile;

	public ArduinoProjectGenerator(IProject project) {
		this.project = project;
	}

	public void setupArduinoProject(IProgressMonitor monitor) throws CoreException {
		// Add natures to project: C, C++, Arduino
		IProjectDescription projDesc = project.getDescription();
		String[] oldIds = projDesc.getNatureIds();
		String[] newIds = new String[oldIds.length + 3];
		System.arraycopy(oldIds, 0, newIds, 0, oldIds.length);
		newIds[newIds.length - 3] = CProjectNature.C_NATURE_ID;
		newIds[newIds.length - 2] = CCProjectNature.CC_NATURE_ID;
		newIds[newIds.length - 1] = ArduinoProjectNature.ID;
		projDesc.setNatureIds(newIds);

		// Add Arduino Builder
		ICommand command = projDesc.newCommand();
		command.setBuilderName(ArduinoBuilder.ID);
		command.setBuilding(IncrementalProjectBuilder.AUTO_BUILD, false);
		projDesc.setBuildSpec(new ICommand[] { command });

		project.setDescription(projDesc, monitor);

		// Generate files
		ArduinoTemplateGenerator templateGen = new ArduinoTemplateGenerator();
		Map<String, Object> fmModel = new HashMap<>();
		fmModel.put("projectName", project.getName()); //$NON-NLS-1$

		IFolder sourceFolder = project.getFolder("src"); //$NON-NLS-1$
		if (!sourceFolder.exists()) {
			sourceFolder.create(true, true, monitor);
		}
		IPathEntry[] entries = new IPathEntry[] { CoreModel.newOutputEntry(sourceFolder.getFullPath()) };
		CoreModel.getDefault().create(project).setRawPathEntries(entries, monitor);

		sourceFile = sourceFolder.getFile(project.getName() + ".cpp"); //$NON-NLS-1$
		templateGen.generateFile(fmModel, "arduino.cpp", sourceFile, monitor); //$NON-NLS-1$
	}

	public IFile getSourceFile() {
		return sourceFile;
	}

}

Back to the top