Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad38778714069a8557a272efb75f1e7e75d49b14 (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
/*******************************************************************************
 * 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
 *******************************************************************************/
package org.eclipse.cdt.arduino.core.internal.board;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.eclipse.cdt.arduino.core.internal.Activator;
import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences;
import org.eclipse.cdt.arduino.core.internal.HierarchicalProperties;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

public class ArduinoPlatform {

	private String name;
	private String architecture;
	private String version;
	private String category;
	private String url;
	private String archiveFileName;
	private String checksum;
	private String size;
	private List<ArduinoBoard> boards;
	private List<ToolDependency> toolsDependencies;

	private transient ArduinoPackage pkg;
	private transient HierarchicalProperties boardsFile;

	void setOwner(ArduinoPackage pkg) {
		this.pkg = pkg;
		for (ArduinoBoard board : boards) {
			board.setOwners(this);
		}
		for (ToolDependency toolDep : toolsDependencies) {
			toolDep.setOwner(this);
		}
	}

	public ArduinoPackage getPackage() {
		return pkg;
	}

	public String getName() {
		return name;
	}

	public String getArchitecture() {
		return architecture;
	}

	public String getVersion() {
		return version;
	}

	public String getCategory() {
		return category;
	}

	public String getUrl() {
		return url;
	}

	public String getArchiveFileName() {
		return archiveFileName;
	}

	public String getChecksum() {
		return checksum;
	}

	public String getSize() {
		return size;
	}

	public List<ArduinoBoard> getBoards() throws CoreException {
		if (isInstalled() && boardsFile == null) {
			Properties boardProps = new Properties();
			try (Reader reader = new FileReader(getInstallPath().resolve("boards.txt").toFile())) { //$NON-NLS-1$
				boardProps.load(reader);
			} catch (IOException e) {
				throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Loading boards.txt", e));
			}

			boardsFile = new HierarchicalProperties(boardProps);

			// Replace the boards with a real ones
			boards = new ArrayList<>();
			for (Map.Entry<String, HierarchicalProperties> entry : boardsFile.getChildren().entrySet()) {
				if (entry.getValue().getChild("name") != null) { //$NON-NLS-1$
					// assume things with names are boards
					boards.add(new ArduinoBoard(entry.getKey(), entry.getValue()).setOwners(this));
				}
			}
		}
		return boards;
	}

	public ArduinoBoard getBoard(String name) throws CoreException {
		for (ArduinoBoard board : getBoards()) {
			if (name.equals(board.getName())) {
				return board;
			}
		}
		return null;
	}

	public List<ToolDependency> getToolsDependencies() {
		return toolsDependencies;
	}

	public Properties getPlatformProperties() throws CoreException {
		Properties properties = new Properties();
		try (Reader reader = new FileReader(getInstallPath().resolve("platform.txt").toFile())) { //$NON-NLS-1$
			properties.load(reader);
			return properties;
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Loading platform.txt", e));
		}
	}

	public boolean isInstalled() {
		return getInstallPath().resolve("boards.txt").toFile().exists(); //$NON-NLS-1$
	}

	public Path getInstallPath() {
		return ArduinoPreferences.getArduinoHome().resolve("hardware").resolve(pkg.getName()).resolve(architecture) //$NON-NLS-1$
				.resolve(version);
	}

	public List<Path> getIncludePath() {
		Path installPath = getInstallPath();
		return Arrays.asList(installPath.resolve("cores/{build.core}"), //$NON-NLS-1$
				installPath.resolve("variants/{build.variant}")); //$NON-NLS-1$
	}

	public IStatus install(IProgressMonitor monitor) {
		// Check if we're installed already
		if (isInstalled()) {
			return Status.OK_STATUS;
		}

		// Download platform archive
		IStatus status = ArduinoBoardManager.downloadAndInstall(url, archiveFileName, getInstallPath(), monitor);
		if (!status.isOK()) {
			return status;
		}

		// Install the tools
		MultiStatus mstatus = null;
		for (ToolDependency toolDep : toolsDependencies) {
			status = toolDep.install(monitor);
			if (!status.isOK()) {
				if (mstatus == null) {
					mstatus = new MultiStatus(status.getPlugin(), status.getCode(), status.getMessage(),
							status.getException());
				} else {
					mstatus.add(status);
				}
			}
		}

		// TODO on Windows install make from equations.org

		return mstatus != null ? mstatus : Status.OK_STATUS;
	}

}

Back to the top