4444import processing .app .debug .TargetPlatformException ;
4545import 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 .*;
5148import java .util .*;
5249
5350import static processing .app .helpers .filefilters .OnlyDirs .ONLY_DIRS ;
5451
5552public 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}
0 commit comments