32
32
import org .eclipse .core .runtime .IProgressMonitor ;
33
33
import org .eclipse .core .runtime .IStatus ;
34
34
import org .eclipse .core .runtime .NullProgressMonitor ;
35
+ import org .eclipse .core .runtime .QualifiedName ;
35
36
import org .eclipse .core .runtime .Status ;
36
37
import org .eclipse .core .runtime .SubMonitor ;
37
38
47
48
import io .sloeber .core .txt .TxtFile ;
48
49
49
50
public class SloeberProjectDescription extends Common {
50
- static private Map <String , SloeberProjectDescription > ArduinoProjectList = new HashMap <>();
51
+ private static QualifiedName sloeberQualifiedName = new QualifiedName (Activator .NODE_ARDUINO ,
52
+ "Sloeber_Project_Description" ); //$NON-NLS-1$
51
53
private Map <String , BoardDescription > myBoardDescriptions = new HashMap <>();
52
54
private Map <String , CompileDescription > myCompileDescriptions = new HashMap <>();
53
- private IProject myProject = null ;
54
-
55
+
55
56
private static final String ENV_KEY_BUILD_SOURCE_PATH = ERASE_START + "build.source.path" ; //$NON-NLS-1$
56
57
private static final String ENV_KEY_BUILD_GENERIC_PATH = ERASE_START + "build.generic.path" ; //$NON-NLS-1$
57
58
private static final String ENV_KEY_COMPILER_PATH = ERASE_START + "compiler.path" ; //$NON-NLS-1$
58
59
private static final String ENV_KEY_JANTJE_MAKE_LOCATION = ENV_KEY_JANTJE_START + "make_location" ; //$NON-NLS-1$
59
60
60
- private SloeberProjectDescription (IProject project ) {
61
- myProject = project ;
62
- ICProjectDescription projDesc = CCorePlugin .getDefault ().getProjectDescription (myProject );
61
+ private SloeberProjectDescription (IProject project , boolean skipChildren ) {
62
+ try {
63
+ project .setSessionProperty (sloeberQualifiedName , this );
64
+ } catch (CoreException e ) {
65
+ // TODO Auto-generated catch block
66
+ e .printStackTrace ();
67
+ }
68
+ if (skipChildren ) {
69
+ return ;
70
+ }
71
+ ICProjectDescription projDesc = CCorePlugin .getDefault ().getProjectDescription (project );
63
72
for (ICConfigurationDescription confDesc : projDesc .getConfigurations ()) {
64
73
readSloeberConfigFile (confDesc );
65
74
setEnvVars (confDesc , getEnvVars (confDesc ), true );
@@ -128,17 +137,17 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
128
137
CCorePlugin cCorePlugin = CCorePlugin .getDefault ();
129
138
ICProjectDescription prjCDesc = cCorePlugin .getProjectDescription (newProjectHandle );
130
139
131
- SloeberProjectDescription arduinoProjectDescriptor = getArduinoProjectDescription (newProjectHandle );
132
-
140
+ SloeberProjectDescription arduinoProjDesc = new SloeberProjectDescription (newProjectHandle , true );
133
141
for (ICConfigurationDescription curConfig : prjCDesc .getConfigurations ()) {
134
142
// Even though we use the same boardDescriptor for all configurations during
135
143
// project creation
136
144
// we need to add them config per config because of the include linking
137
145
Helpers .addArduinoCodeToProject (boardDescriptor , curConfig );
138
146
139
- arduinoProjectDescriptor .setCompileDescription (curConfig , compileDescriptor );
140
- arduinoProjectDescriptor .setBoardDescription (curConfig , boardDescriptor );
141
-
147
+ arduinoProjDesc .putCompileDescription (curConfig , compileDescriptor );
148
+ arduinoProjDesc .putBoardDescription (curConfig , boardDescriptor );
149
+ arduinoProjDesc .saveConfig (curConfig );
150
+ setEnvVars (curConfig , arduinoProjDesc .getEnvVars (curConfig ), true );
142
151
Libraries .addLibrariesToProject (newProjectHandle , curConfig , librariesToAdd );
143
152
}
144
153
@@ -177,13 +186,14 @@ private void saveConfig(ICConfigurationDescription confDesc) {
177
186
}
178
187
179
188
private HashMap <String , String > getEnvVars (ICConfigurationDescription confDesc ) {
189
+ IProject project = confDesc .getProjectDescription ().getProject ();
180
190
181
- BoardDescription boardDescription = myBoardDescriptions . get (confDesc . getName () );
182
- CompileDescription compileOptions = myCompileDescriptions . get (confDesc . getName () );
191
+ BoardDescription boardDescription = getBoardDescription (confDesc );
192
+ CompileDescription compileOptions = getCompileDescription (confDesc );
183
193
184
194
HashMap <String , String > allVars = new HashMap <>();
185
195
186
- allVars .put (ENV_KEY_BUILD_SOURCE_PATH , myProject .getLocation ().toOSString ());
196
+ allVars .put (ENV_KEY_BUILD_SOURCE_PATH , project .getLocation ().toOSString ());
187
197
188
198
if (boardDescription != null ) {
189
199
allVars .putAll (boardDescription .getEnvVars ());
@@ -214,20 +224,27 @@ private HashMap<String, String> getEnvVars(ICConfigurationDescription confDesc)
214
224
215
225
private void readSloeberConfigFile (ICConfigurationDescription confDesc ) {
216
226
String confDescName = confDesc .getName ();
217
- IFile file = myProject .getFile ("sloeber." + confDescName + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
227
+ IProject project = confDesc .getProjectDescription ().getProject ();
228
+ IFile file = project .getFile ("sloeber." + confDescName + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
218
229
if (file .exists ()) {
219
230
TxtFile configFile = new TxtFile (file .getLocation ().toFile ());
220
231
BoardDescription boardDescription = new BoardDescription (configFile );
221
232
CompileDescription compileDescription = new CompileDescription (configFile );
222
- myBoardDescriptions .put (confDescName , boardDescription );
223
- myCompileDescriptions .put (confDescName , compileDescription );
233
+ putBoardDescription (confDesc , boardDescription );
234
+ putCompileDescription (confDesc , compileDescription );
235
+ } else {
236
+ if (!IndexerController .isPosponed (project )) {
237
+ Common .log (new Status (IStatus .ERROR , io .sloeber .core .Activator .getId (),
238
+ "failed to read file: " + file .getName ()));
239
+ }
224
240
}
225
241
226
242
}
227
243
228
244
private void createSloeberConfigFile (ICConfigurationDescription confDesc ) {
229
- BoardDescription boardDescription = myBoardDescriptions .get (confDesc .getName ());
230
- CompileDescription compileDescription = myCompileDescriptions .get (confDesc .getName ());
245
+ IProject project = confDesc .getProjectDescription ().getProject ();
246
+ BoardDescription boardDescription = getBoardDescription (confDesc );
247
+ CompileDescription compileDescription = getCompileDescription (confDesc );
231
248
Map <String , String > configVars = new HashMap <>();
232
249
if (boardDescription != null ) {
233
250
configVars = boardDescription .getEnvVarsConfig ();
@@ -240,7 +257,7 @@ private void createSloeberConfigFile(ICConfigurationDescription confDesc) {
240
257
for (Entry <String , String > curLine : configVars .entrySet ()) {
241
258
newFileContent += curLine .getKey () + '=' + curLine .getValue () + '\n' ;
242
259
}
243
- IFile file = myProject .getFile ("sloeber." + confDesc .getName () + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
260
+ IFile file = project .getFile ("sloeber." + confDesc .getName () + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
244
261
try {
245
262
file .refreshLocal (IResource .DEPTH_INFINITE , null );
246
263
if (file .exists ()) {
@@ -286,21 +303,22 @@ private static void removeAllEraseEnvironmentVariables(IContributedEnvironment c
286
303
*/
287
304
public void setBoardDescription (ICConfigurationDescription confDesc , BoardDescription boardDescription ) {
288
305
try {
306
+ IProject project = confDesc .getProjectDescription ().getProject ();
289
307
boolean isRebuildNeeded = true ;
290
- BoardDescription oldBoardDescription = myBoardDescriptions . get (confDesc . getName () );
308
+ BoardDescription oldBoardDescription = getBoardDescription (confDesc );
291
309
if (oldBoardDescription != null ) {
292
310
isRebuildNeeded = oldBoardDescription .needsRebuild (boardDescription );
293
311
}
294
312
Helpers .addArduinoCodeToProject (boardDescription , confDesc );
295
313
296
314
isRebuildNeeded = isRebuildNeeded || Helpers .removeInvalidIncludeFolders (confDesc );
297
- myBoardDescriptions . put (confDesc . getName () , boardDescription );
315
+ putBoardDescription (confDesc , boardDescription );
298
316
saveConfig (confDesc );
299
317
if (isRebuildNeeded ) {
300
- Helpers .setDirtyFlag (myProject , confDesc );
318
+ Helpers .setDirtyFlag (project , confDesc );
301
319
} else {
302
320
Common .log (new Status (IStatus .INFO , io .sloeber .core .Activator .getId (),
303
- "Ignoring project update; clean may be required: " + myProject .getName ())); //$NON-NLS-1$
321
+ "Ignoring project update; clean may be required: " + project .getName ())); //$NON-NLS-1$
304
322
}
305
323
} catch (Exception e ) {
306
324
e .printStackTrace ();
@@ -338,14 +356,23 @@ private static void setEnvVars(ICConfigurationDescription confDesc, Map<String,
338
356
* @param project
339
357
* @return
340
358
*/
341
- public static SloeberProjectDescription getArduinoProjectDescription (IProject project ) {
359
+ public static synchronized SloeberProjectDescription getArduinoProjectDescription (IProject project ) {
360
+
342
361
if (project .isOpen () && project .getLocation ().toFile ().exists ()) {
343
362
if (Sketch .isSketch (project )) {
344
- SloeberProjectDescription ret = ArduinoProjectList .get (project .getName ());
345
- if (null == ret ) {
346
- ret = new SloeberProjectDescription (project );
363
+ Object sessionProperty = null ;
364
+ try {
365
+ sessionProperty = project .getSessionProperty (sloeberQualifiedName );
366
+ if (null != sessionProperty ) {
367
+ SloeberProjectDescription ret = (SloeberProjectDescription ) sessionProperty ;
368
+ return ret ;
369
+ }
370
+ } catch (CoreException e ) {
371
+ // TODO Auto-generated catch block
372
+ e .printStackTrace ();
347
373
}
348
- ArduinoProjectList .put (project .getName (), ret );
374
+
375
+ SloeberProjectDescription ret = new SloeberProjectDescription (project , false );
349
376
return ret ;
350
377
}
351
378
}
@@ -354,19 +381,20 @@ public static SloeberProjectDescription getArduinoProjectDescription(IProject pr
354
381
355
382
public void setCompileDescription (ICConfigurationDescription confDesc , CompileDescription compileDescription ) {
356
383
try {
384
+ IProject project = confDesc .getProjectDescription ().getProject ();
357
385
boolean isRebuildNeeded = true ;
358
- CompileDescription oldCompileDescription = myCompileDescriptions . get (confDesc . getName () );
386
+ CompileDescription oldCompileDescription = getCompileDescription (confDesc );
359
387
if (oldCompileDescription != null ) {
360
388
isRebuildNeeded = oldCompileDescription .needsRebuild (compileDescription );
361
389
}
362
390
363
- myCompileDescriptions . put (confDesc . getName () , compileDescription );
391
+ putCompileDescription (confDesc , compileDescription );
364
392
saveConfig (confDesc );
365
393
if (isRebuildNeeded ) {
366
- Helpers .setDirtyFlag (myProject , confDesc );
394
+ Helpers .setDirtyFlag (project , confDesc );
367
395
} else {
368
396
Common .log (new Status (IStatus .INFO , io .sloeber .core .Activator .getId (),
369
- "Ignoring project update; clean may be required: " + myProject .getName ())); //$NON-NLS-1$
397
+ "Ignoring project update; clean may be required: " + project .getName ())); //$NON-NLS-1$
370
398
}
371
399
} catch (Exception e ) {
372
400
e .printStackTrace ();
@@ -376,17 +404,33 @@ public void setCompileDescription(ICConfigurationDescription confDesc, CompileDe
376
404
}
377
405
378
406
public BoardDescription getBoardDescription (ICConfigurationDescription confDesc ) {
379
- return myBoardDescriptions .get (confDesc .getName ());
407
+ BoardDescription ret = myBoardDescriptions .get (confDesc .getId ());
408
+ if (ret == null ) {
409
+ readSloeberConfigFile (confDesc );
410
+ ret = myBoardDescriptions .get (confDesc .getId ());
411
+ }
412
+ return ret ;
413
+ }
414
+
415
+ private void putBoardDescription (ICConfigurationDescription confDesc , BoardDescription boardDesc ) {
416
+ myBoardDescriptions .put (confDesc .getId (), boardDesc );
417
+ }
418
+
419
+ private void putCompileDescription (ICConfigurationDescription confDesc , CompileDescription compDesc ) {
420
+ myCompileDescriptions .put (confDesc .getId (), compDesc );
380
421
}
381
422
382
423
public CompileDescription getCompileDescription (ICConfigurationDescription confDesc ) {
383
- return myCompileDescriptions .get (confDesc .getName ());
424
+ CompileDescription ret = myCompileDescriptions .get (confDesc .getId ());
425
+ if (ret == null ) {
426
+ readSloeberConfigFile (confDesc );
427
+ ret = myCompileDescriptions .get (confDesc .getId ());
428
+ }
429
+ return ret ;
384
430
}
385
431
386
- public String getDecoratedText (String text ) {
387
- ICProjectDescription projDesc = CCorePlugin .getDefault ().getProjectDescription (myProject );
388
- ICConfigurationDescription configurationDescription = projDesc .getActiveConfiguration ();
389
- BoardDescription boardDescriptor = getBoardDescription (configurationDescription );
432
+ public String getDecoratedText (ICConfigurationDescription confDesc , String text ) {
433
+ BoardDescription boardDescriptor = getBoardDescription (confDesc );
390
434
if (boardDescriptor == null ) {
391
435
return text + " Project needs to be configured" ;
392
436
}
0 commit comments