Skip to content

Commit 804480c

Browse files
author
Federico Fissore
committed
ContributionIndexer now loads and merges content of files named package_SOMETHING_index.json
1 parent e0ee5ab commit 804480c

File tree

4 files changed

+76
-19
lines changed

4 files changed

+76
-19
lines changed

arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ public List<String> updateIndex() throws Exception {
244244
String statusText = _("Downloading platforms index...");
245245

246246
URL url = new URL(PACKAGE_INDEX_URL);
247-
File outputFile = indexer.getIndexFile();
247+
String[] urlPathParts = url.getFile().split("/");
248+
File outputFile = indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]);
248249
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
249250
downloader.download(url, tmpFile, progress, statusText);
250251
progress.stepDone();

arduino-core/src/cc/arduino/contributions/packages/ContributionsIndex.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ public void fillCategories() {
137137
}
138138
}
139139

140-
public ContributedPackage getPackage(String packager) {
141-
for (ContributedPackage pack : getPackages())
142-
if (pack.getName().equals(packager))
140+
public ContributedPackage getPackage(String packageName) {
141+
for (ContributedPackage pack : getPackages()) {
142+
if (pack.getName().equals(packageName)) {
143143
return pack;
144+
}
145+
}
144146
return null;
145147
}
146148

arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,25 @@
4444
import processing.app.debug.TargetPlatformException;
4545
import processing.app.helpers.PreferencesMap;
4646

47-
import java.io.File;
48-
import java.io.FileInputStream;
49-
import java.io.IOException;
50-
import java.io.InputStream;
47+
import java.io.*;
5148
import java.util.*;
5249

5350
import static processing.app.helpers.filefilters.OnlyDirs.ONLY_DIRS;
5451

5552
public class ContributionsIndexer {
5653

54+
private static final String DEFAULT_INDEX_FILE_NAME = "package_index.json";
55+
private static final List<String> PROTECTED_PACKAGE_NAMES = Arrays.asList("arduino", "Intel");
56+
5757
private final File packagesFolder;
5858
private final File stagingFolder;
59-
private final File indexFile;
59+
private final File preferencesFolder;
6060
private ContributionsIndex index;
6161

6262
public ContributionsIndexer(File preferencesFolder) {
63+
this.preferencesFolder = preferencesFolder;
6364
packagesFolder = new File(preferencesFolder, "packages");
64-
stagingFolder = new File(preferencesFolder, "staging" + File.separator +
65-
"packages");
66-
indexFile = new File(preferencesFolder, "package_index.json");
65+
stagingFolder = new File(preferencesFolder, "staging" + File.separator + "packages");
6766
}
6867

6968
// public static void main(String args[]) throws Exception {
@@ -81,8 +80,19 @@ public ContributionsIndexer(File preferencesFolder) {
8180
// }
8281

8382
public void parseIndex() throws IOException {
84-
// Parse index file
85-
parseIndex(indexFile);
83+
index = parseIndex(getIndexFile(DEFAULT_INDEX_FILE_NAME));
84+
85+
File[] indexFiles = preferencesFolder.listFiles(new FilenameFilter() {
86+
@Override
87+
public boolean accept(File file, String name) {
88+
return !DEFAULT_INDEX_FILE_NAME.equals(name) && name.startsWith("package_") && name.endsWith("_index.json");
89+
}
90+
});
91+
92+
for (File indexFile : indexFiles) {
93+
ContributionsIndex contributionsIndex = parseIndex(indexFile);
94+
mergeContributions(contributionsIndex, indexFile);
95+
}
8696

8797
List<ContributedPackage> packages = index.getPackages();
8898
for (ContributedPackage pack : packages) {
@@ -98,14 +108,58 @@ public void parseIndex() throws IOException {
98108
index.fillCategories();
99109
}
100110

101-
private void parseIndex(File indexFile) throws IOException {
111+
private void mergeContributions(ContributionsIndex contributionsIndex, File indexFile) {
112+
for (ContributedPackage contributedPackage : contributionsIndex.getPackages()) {
113+
ContributedPackage targetPackage = index.getPackage(contributedPackage.getName());
114+
115+
if (targetPackage == null) {
116+
index.getPackages().add(contributedPackage);
117+
} else {
118+
if (mergeAllowed(contributedPackage, indexFile)) {
119+
List<ContributedPlatform> platforms = contributedPackage.getPlatforms();
120+
if (platforms == null) {
121+
platforms = new LinkedList<ContributedPlatform>();
122+
}
123+
for (ContributedPlatform contributedPlatform : platforms) {
124+
ContributedPlatform platform = targetPackage.findPlatform(contributedPlatform.getArchitecture(), contributedPlatform.getVersion());
125+
if (platform != null) {
126+
targetPackage.getPlatforms().remove(platform);
127+
}
128+
targetPackage.getPlatforms().add(contributedPlatform);
129+
}
130+
List<ContributedTool> tools = contributedPackage.getTools();
131+
if (tools == null) {
132+
tools = new LinkedList<ContributedTool>();
133+
}
134+
for (ContributedTool contributedTool : tools) {
135+
ContributedTool tool = targetPackage.findTool(contributedTool.getName(), contributedTool.getVersion());
136+
if (tool != null) {
137+
targetPackage.getTools().remove(tool);
138+
}
139+
targetPackage.getTools().add(contributedTool);
140+
}
141+
}
142+
}
143+
}
144+
}
145+
146+
private boolean mergeAllowed(ContributedPackage contributedPackage, File indexFile) {
147+
return !PROTECTED_PACKAGE_NAMES.contains(contributedPackage.getName()) || isSigned(indexFile);
148+
}
149+
150+
//TODO stub implementation
151+
private boolean isSigned(File indexFile) {
152+
return true;
153+
}
154+
155+
private ContributionsIndex parseIndex(File indexFile) throws IOException {
102156
InputStream indexIn = new FileInputStream(indexFile);
103157
ObjectMapper mapper = new ObjectMapper();
104158
mapper.registerModule(new MrBeanModule());
105159
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
106160
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
107161
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
108-
index = mapper.readValue(indexIn, ContributionsIndex.class);
162+
return mapper.readValue(indexIn, ContributionsIndex.class);
109163
}
110164

111165
public void syncWithFilesystem(File hardwareFolder) throws IOException {
@@ -295,8 +349,8 @@ public File getStagingFolder() {
295349
return stagingFolder;
296350
}
297351

298-
public File getIndexFile() {
299-
return indexFile;
352+
public File getIndexFile(String name) {
353+
return new File(preferencesFolder, name);
300354
}
301355

302356
}

arduino-core/src/processing/app/BaseNoGui.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static public void initLogger() {
577577

578578
static public void initPackages() throws Exception {
579579
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
580-
File indexFile = indexer.getIndexFile();
580+
File indexFile = indexer.getIndexFile("package_index.json");
581581
File defaultPackageJsonFile = new File(getContentFile("dist"), "package_index.json");
582582
if (!indexFile.isFile() || (defaultPackageJsonFile.isFile() && defaultPackageJsonFile.lastModified() > indexFile.lastModified())) {
583583
FileUtils.copyFile(defaultPackageJsonFile, indexFile);

0 commit comments

Comments
 (0)