Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3aba4a82edaf70e5e8d3f5033f923a901c7056ab (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
/*******************************************************************************
 * 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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
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.core.runtime.CoreException;

public class ArduinoPackage {

	// JSON fields
	private String name;
	private String maintainer;
	private String websiteURL;
	private String email;
	private ArduinoHelp help;
	private List<ArduinoPlatform> platforms;
	private List<ArduinoTool> tools;
	// end JSON fields

	private Map<String, ArduinoPlatform> installedPlatforms;

	public String getName() {
		return name;
	}

	public String getMaintainer() {
		return maintainer;
	}

	public String getWebsiteURL() {
		return websiteURL;
	}

	public String getEmail() {
		return email;
	}

	public ArduinoHelp getHelp() {
		return help;
	}

	public Collection<ArduinoPlatform> getPlatforms() {
		return Collections.unmodifiableCollection(platforms);
	}

	void init() {
		for (ArduinoPlatform platform : platforms) {
			platform.init(this);
		}
		for (ArduinoTool tool : tools) {
			tool.init(this);
		}
	}

	public ArduinoPlatform getPlatform(String architecture, String version) {
		if (platforms != null) {
			for (ArduinoPlatform plat : platforms) {
				if (plat.getArchitecture().equals(architecture) && plat.getVersion().equals(version)) {
					return plat;
				}
			}
		}
		return null;
	}

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

	private void initInstalledPlatforms() throws CoreException {
		if (installedPlatforms == null) {
			installedPlatforms = new HashMap<>();

			if (Files.isDirectory(getInstallPath())) {
				Path platformTxt = Paths.get("platform.txt"); //$NON-NLS-1$
				try {
					Files.find(getInstallPath().resolve("hardware"), 2, //$NON-NLS-1$
							(path, attrs) -> path.getFileName().equals(platformTxt))
							.forEach(path -> {
								try (FileReader reader = new FileReader(path.toFile())) {
									Properties platformProperties = new Properties();
									platformProperties.load(reader);
									String arch = path.getName(path.getNameCount() - 2).toString();
									String version = platformProperties.getProperty("version"); //$NON-NLS-1$

									ArduinoPlatform platform = getPlatform(arch, version);
									if (platform != null) {
										platform.setPlatformProperties(platformProperties);
										installedPlatforms.put(arch, platform);
									} // TODO manually add it if was removed from index
								} catch (IOException e) {
									throw new RuntimeException(e);
								}
							});
				} catch (IOException e) {
					throw Activator.coreException(e);
				}
			}
		}
	}

	public Collection<ArduinoPlatform> getInstalledPlatforms() throws CoreException {
		initInstalledPlatforms();
		return installedPlatforms.values();
	}

	public ArduinoPlatform getInstalledPlatform(String architecture) throws CoreException {
		if (architecture == null) {
			return null;
		} else {
			initInstalledPlatforms();
			return installedPlatforms.get(architecture);
		}
	}

	void platformInstalled(ArduinoPlatform platform) {
		installedPlatforms.put(platform.getArchitecture(), platform);
	}

	void platformUninstalled(ArduinoPlatform platform) {
		installedPlatforms.remove(platform.getArchitecture());
	}

	public Collection<ArduinoPlatform> getAvailablePlatforms() throws CoreException {
		initInstalledPlatforms();
		Map<String, ArduinoPlatform> platformMap = new HashMap<>();
		for (ArduinoPlatform platform : platforms) {
			if (!installedPlatforms.containsKey(platform.getArchitecture())) {
				ArduinoPlatform p = platformMap.get(platform.getName());
				if (p == null || ArduinoManager.compareVersions(platform.getVersion(), p.getVersion()) > 0) {
					platformMap.put(platform.getName(), platform);
				}
			}
		}
		return platformMap.values();
	}

	public List<ArduinoTool> getTools() {
		return tools;
	}

	public ArduinoTool getTool(String toolName, String version) {
		for (ArduinoTool tool : tools) {
			if (tool.getName().equals(toolName) && tool.getVersion().equals(version)) {
				return tool;
			}
		}
		return null;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ArduinoPackage) {
			return ((ArduinoPackage) obj).getName().equals(name);
		}
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

}

Back to the top