44
44
import processing .app .debug .TargetPlatformException ;
45
45
import processing .app .helpers .PreferencesMap ;
46
46
47
- import java .io .File ;
48
- import java .io .FileInputStream ;
49
- import java .io .IOException ;
50
- import java .io .InputStream ;
47
+ import java .io .*;
51
48
import java .util .*;
52
49
53
50
import static processing .app .helpers .filefilters .OnlyDirs .ONLY_DIRS ;
54
51
55
52
public class ContributionsIndexer {
56
53
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
+
57
57
private final File packagesFolder ;
58
58
private final File stagingFolder ;
59
- private final File indexFile ;
59
+ private final File preferencesFolder ;
60
60
private ContributionsIndex index ;
61
61
62
62
public ContributionsIndexer (File preferencesFolder ) {
63
+ this .preferencesFolder = preferencesFolder ;
63
64
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" );
67
66
}
68
67
69
68
// public static void main(String args[]) throws Exception {
@@ -81,8 +80,19 @@ public ContributionsIndexer(File preferencesFolder) {
81
80
// }
82
81
83
82
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
+ }
86
96
87
97
List <ContributedPackage > packages = index .getPackages ();
88
98
for (ContributedPackage pack : packages ) {
@@ -98,14 +108,58 @@ public void parseIndex() throws IOException {
98
108
index .fillCategories ();
99
109
}
100
110
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 {
102
156
InputStream indexIn = new FileInputStream (indexFile );
103
157
ObjectMapper mapper = new ObjectMapper ();
104
158
mapper .registerModule (new MrBeanModule ());
105
159
mapper .configure (DeserializationFeature .ACCEPT_SINGLE_VALUE_AS_ARRAY , true );
106
160
mapper .configure (DeserializationFeature .EAGER_DESERIALIZER_FETCH , true );
107
161
mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
108
- index = mapper .readValue (indexIn , ContributionsIndex .class );
162
+ return mapper .readValue (indexIn , ContributionsIndex .class );
109
163
}
110
164
111
165
public void syncWithFilesystem (File hardwareFolder ) throws IOException {
@@ -295,8 +349,8 @@ public File getStagingFolder() {
295
349
return stagingFolder ;
296
350
}
297
351
298
- public File getIndexFile () {
299
- return indexFile ;
352
+ public File getIndexFile (String name ) {
353
+ return new File ( preferencesFolder , name ) ;
300
354
}
301
355
302
356
}
0 commit comments