Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db11c42b44fdb9f47950b53a3a52f8a12471742e (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*******************************************************************************
 * 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.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
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.build.ArduinoBuildConfiguration;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class ArduinoLibrary {

	// JSON fields
	private String name;
	private String version;
	private String author;
	private String maintainer;
	private String sentence;
	private String paragraph;
	private String website;
	private String category;
	private List<String> architectures;
	private List<String> types;
	private String url;
	private String archiveFileName;
	private int size;
	private String checksum;
	// end JSON fields

	private Path installPath;
	private ArduinoPlatform platform;

	public ArduinoLibrary() {
	}

	public ArduinoLibrary(Path propertiesFile) throws CoreException {
		installPath = propertiesFile.getParent();

		Properties props = new Properties();
		try (FileReader reader = new FileReader(propertiesFile.toFile())) {
			props.load(reader);
		} catch (IOException e) {
			throw Activator.coreException(e);
		}

		name = props.getProperty("name"); //$NON-NLS-1$
		version = props.getProperty("version"); //$NON-NLS-1$
		author = props.getProperty("author"); //$NON-NLS-1$
		maintainer = props.getProperty("maintainer"); //$NON-NLS-1$
		sentence = props.getProperty("sentence"); //$NON-NLS-1$
		paragraph = props.getProperty("paragraph"); //$NON-NLS-1$
		category = props.getProperty("category"); //$NON-NLS-1$
		architectures = Arrays.asList(props.getProperty("architectures").split(",")); //$NON-NLS-1$ //$NON-NLS-2$
	}

	public ArduinoLibrary(Path propertiesFile, ArduinoPlatform platform) throws CoreException {
		this(propertiesFile);
		this.platform = platform;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getMaintainer() {
		return maintainer;
	}

	public void setMaintainer(String maintainer) {
		this.maintainer = maintainer;
	}

	public String getSentence() {
		return sentence;
	}

	public void setSentence(String sentence) {
		this.sentence = sentence;
	}

	public String getParagraph() {
		return paragraph;
	}

	public void setParagraph(String paragraph) {
		this.paragraph = paragraph;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public List<String> getArchitectures() {
		return architectures;
	}

	public void setArchitectures(List<String> architectures) {
		this.architectures = architectures;
	}

	public List<String> getTypes() {
		return types;
	}

	public void setTypes(List<String> types) {
		this.types = types;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getArchiveFileName() {
		return archiveFileName;
	}

	public void setArchiveFileName(String archiveFileName) {
		this.archiveFileName = archiveFileName;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getChecksum() {
		return checksum;
	}

	public void setChecksum(String checksum) {
		this.checksum = checksum;
	}

	public Path getInstallPath() {
		return installPath == null
				? ArduinoPreferences.getArduinoHome().resolve("libraries").resolve(name.replaceAll("[ ()]", "_")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				: installPath;
	}

	public ArduinoPlatform getPlatform() {
		return platform;
	}

	public void install(IProgressMonitor monitor) throws CoreException {
		if (Files.exists(getInstallPath())) {
			uninstall(monitor);
		}

		try {
			ArduinoManager.downloadAndInstall(url, archiveFileName, getInstallPath(), monitor);
		} catch (IOException e) {
			throw Activator.coreException(e);
		}
	}

	public void uninstall(IProgressMonitor monitor) throws CoreException {
		try {
			ArduinoManager.recursiveDelete(getInstallPath());
		} catch (IOException e) {
			throw Activator.coreException(e);
		}
	}

	public Collection<Path> getIncludePath() {
		Path installPath = getInstallPath();
		Path srcPath = installPath.resolve("src"); //$NON-NLS-1$
		if (srcPath.toFile().isDirectory()) {
			return Collections.singletonList(srcPath);
		} else {
			Path utilityPath = installPath.resolve("utility"); //$NON-NLS-1$
			if (utilityPath.toFile().isDirectory()) {
				return Arrays.asList(installPath, utilityPath);
			} else {
				return Collections.singletonList(installPath);
			}
		}
	}

	private void getSources(Collection<String> sources, Path dir, boolean recurse) {
		for (File file : dir.toFile().listFiles()) {
			if (file.isDirectory()) {
				if (recurse) {
					getSources(sources, file.toPath(), recurse);
				}
			} else {
				if (ArduinoBuildConfiguration.isSource(file.getName())) {
					sources.add(ArduinoBuildConfiguration.pathString(file.toPath()));
				}
			}
		}
	}

	public Collection<String> getSources() {
		List<String> sources = new ArrayList<>();
		Path installPath = getInstallPath();
		Path srcPath = installPath.resolve("src"); //$NON-NLS-1$
		if (srcPath.toFile().isDirectory()) {
			getSources(sources, srcPath, true);
		} else {
			getSources(sources, installPath, false);
			Path utilityPath = installPath.resolve("utility"); //$NON-NLS-1$
			if (utilityPath.toFile().isDirectory()) {
				getSources(sources, utilityPath, false);
			}
		}
		return sources;
	}

	@Override
	public String toString() {
		return getName();
	}

	private String fixText(String text) {
		String fixed = text.replaceAll("&", "&amp;"); //$NON-NLS-1$ //$NON-NLS-2$
		fixed = fixed.replaceAll("<", "&lt;"); //$NON-NLS-1$ //$NON-NLS-2$
		return fixed;
	}

	public String toFormText() {
		StringBuilder text = new StringBuilder();

		text.append("<form>"); //$NON-NLS-1$
		text.append(String.format("<p><b>%s: %s</b></p>", "Library", fixText(getName()))); //$NON-NLS-1$

		if (getMaintainer() != null) {
			text.append(String.format("<p>%s: %s</p>", "Maintainer", fixText(getMaintainer()))); //$NON-NLS-1$
		}

		if (getWebsite() != null) {
			text.append(String.format("<p><a href=\"%s\">%s</a></p>", getWebsite(), "Online help")); //$NON-NLS-1$
		}

		text.append(String.format("<p>%s</p>", getSentence()));
		if (getParagraph() != null && !getParagraph().equals(getSentence())) {
			text.append(String.format("<p>%s</p>", getParagraph()));
		}

		text.append("</form>");

		return text.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((platform == null) ? 0 : platform.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ArduinoLibrary other = (ArduinoLibrary) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (platform == null) {
			if (other.platform != null)
				return false;
		} else if (!platform.equals(other.platform))
			return false;
		return true;
	}

}

Back to the top