1
1
package io .sloeber .core .api ;
2
2
3
3
import java .io .ByteArrayInputStream ;
4
+ import java .io .IOException ;
4
5
import java .net .URI ;
6
+ import java .nio .file .Files ;
7
+ import java .nio .file .Path ;
5
8
import java .util .HashMap ;
6
9
import java .util .Iterator ;
7
10
import java .util .Map ;
@@ -227,26 +230,26 @@ private HashMap<String, String> getEnvVars(ICConfigurationDescription confDesc)
227
230
allVars .putAll (otherOptions .getEnvVars ());
228
231
}
229
232
// set the paths
230
- String pathDelimiter = makeEnvironmentVar ("PathDelimiter" );
233
+ String pathDelimiter = makeEnvironmentVar ("PathDelimiter" ); //$NON-NLS-1$
231
234
if (Common .isWindows ) {
232
235
allVars .put (SLOEBER_MAKE_LOCATION ,
233
236
ConfigurationPreferences .getMakePath ().addTrailingSeparator ().toOSString ());
234
- String systemroot = makeEnvironmentVar ("SystemRoot" );
235
- allVars .put ("PATH" ,
237
+ String systemroot = makeEnvironmentVar ("SystemRoot" ); //$NON-NLS-1$
238
+ allVars .put ("PATH" , //$NON-NLS-1$
236
239
makeEnvironmentVar (ENV_KEY_COMPILER_PATH ) + pathDelimiter
237
- + makeEnvironmentVar (ENV_KEY_BUILD_GENERIC_PATH ) + pathDelimiter + systemroot + "\\ system32"
238
- + pathDelimiter + systemroot + pathDelimiter + systemroot + "\\ system32\\ Wbem"
239
- + pathDelimiter + makeEnvironmentVar ("sloeber_path_extension" ));
240
+ + makeEnvironmentVar (ENV_KEY_BUILD_GENERIC_PATH ) + pathDelimiter + systemroot + "\\ system32" //$NON-NLS-1$
241
+ + pathDelimiter + systemroot + pathDelimiter + systemroot + "\\ system32\\ Wbem" //$NON-NLS-1$
242
+ + pathDelimiter + makeEnvironmentVar ("sloeber_path_extension" )); //$NON-NLS-1$
240
243
} else {
241
- allVars .put ("PATH" , makeEnvironmentVar (ENV_KEY_COMPILER_PATH ) + pathDelimiter
242
- + makeEnvironmentVar (ENV_KEY_BUILD_GENERIC_PATH ) + pathDelimiter + makeEnvironmentVar ("PATH" ));
244
+ allVars .put ("PATH" , makeEnvironmentVar (ENV_KEY_COMPILER_PATH ) + pathDelimiter //$NON-NLS-1$
245
+ + makeEnvironmentVar (ENV_KEY_BUILD_GENERIC_PATH ) + pathDelimiter + makeEnvironmentVar ("PATH" )); //$NON-NLS-1$
243
246
}
244
247
245
248
// Set the codeAnalyzer compile commands
246
249
allVars .put (CODAN_C_to_O ,
247
- "${recipe.c.o.pattern.1} -D__IN_ECLIPSE__=1 ${recipe.c.o.pattern.2} ${recipe.c.o.pattern.3} ${sloeber.extra.compile} ${sloeber.extra.c.compile} ${sloeber.extra.all}" );
250
+ "${recipe.c.o.pattern.1} -D__IN_ECLIPSE__=1 ${recipe.c.o.pattern.2} ${recipe.c.o.pattern.3} ${sloeber.extra.compile} ${sloeber.extra.c.compile} ${sloeber.extra.all}" ); //$NON-NLS-1$
248
251
allVars .put (CODAN_CPP_to_O ,
249
- "${recipe.cpp.o.pattern.1} -D__IN_ECLIPSE__=1 -x c++ ${recipe.cpp.o.pattern.2} ${recipe.cpp.o.pattern.3} ${sloeber.extra.compile} ${sloeber.extra.cpp.compile} ${sloeber.extra.all}" );
252
+ "${recipe.cpp.o.pattern.1} -D__IN_ECLIPSE__=1 -x c++ ${recipe.cpp.o.pattern.2} ${recipe.cpp.o.pattern.3} ${sloeber.extra.compile} ${sloeber.extra.cpp.compile} ${sloeber.extra.all}" ); //$NON-NLS-1$
250
253
251
254
return allVars ;
252
255
}
@@ -258,7 +261,15 @@ private void configure() {
258
261
configure (prjCDesc , false );
259
262
}
260
263
261
- private void configure (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
264
+ /**
265
+ *
266
+ * @param prjCDesc
267
+ * @param prjDescWritable
268
+ * @return true if the projectDesc needs to be saved
269
+ */
270
+
271
+ private boolean configure (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
272
+ boolean saveProjDesc = false ;
262
273
if (isInMemory ) {
263
274
if (isDirty ) {
264
275
createSloeberConfigFiles (prjCDesc );
@@ -270,16 +281,16 @@ private void configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
270
281
}
271
282
if (prjDescWritable ) {
272
283
if (myNeedsSyncWithCDT ) {
273
- syncWithCDT (prjCDesc , prjDescWritable );
284
+ saveProjDesc = syncWithCDT (prjCDesc , prjDescWritable );
274
285
}
275
286
if (myNeedsClean ) {
276
287
cleanOldData (prjCDesc );
277
288
}
278
289
}
279
- return ;
290
+ return saveProjDesc ;
280
291
}
281
292
// first read the sloeber files in memory
282
- readSloeberConfig (prjCDesc , prjDescWritable );
293
+ saveProjDesc = readSloeberConfig (prjCDesc , prjDescWritable );
283
294
if (myNeedToPersist || isDirty ) {
284
295
createSloeberConfigFiles (prjCDesc );
285
296
isDirty = false ;
@@ -291,20 +302,22 @@ private void configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
291
302
cleanOldData (prjCDesc );
292
303
}
293
304
if (myNeedsSyncWithCDT ) {
294
- syncWithCDT (prjCDesc , prjDescWritable );
305
+ saveProjDesc = saveProjDesc || syncWithCDT (prjCDesc , prjDescWritable );
295
306
}
296
307
}
297
308
setEnvironmentVariables (prjCDesc );
298
309
isInMemory = true ;
310
+ return saveProjDesc ;
299
311
}
300
312
301
313
/**
302
314
* sync the Sloeber configuration info with CDT Currently only Sloeber known
303
- * confighgurations will be created by Sloeber inside CDT
315
+ * configurations will be created by Sloeber inside CDT
304
316
*/
305
- private void syncWithCDT (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
306
- readSloeberConfig (prjCDesc , prjDescWritable );
317
+ private boolean syncWithCDT (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
318
+ boolean ret = readSloeberConfig (prjCDesc , prjDescWritable );
307
319
myNeedsSyncWithCDT = false ;
320
+ return ret ;
308
321
}
309
322
310
323
/**
@@ -338,7 +351,8 @@ private void cleanOldData(ICProjectDescription prjCDesc) {
338
351
* @param confDesc
339
352
* returns true if the config needs saving otherwise false
340
353
*/
341
- private void readSloeberConfig (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
354
+ private boolean readSloeberConfig (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
355
+ boolean projDescNeedsWriting = false ;
342
356
IFile file = getConfigLocalFile ();
343
357
if (file .exists ()) {
344
358
myCfgFile = new TxtFile (file .getLocation ().toFile ());
@@ -362,6 +376,7 @@ private void readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDescWri
362
376
if (prjDescWritable ) {
363
377
String id = CDataUtil .genId (null );
364
378
try {
379
+ projDescNeedsWriting = true ;
365
380
curConfDesc = prjCDesc .createConfiguration (id , curConfName ,
366
381
prjCDesc .getActiveConfiguration ());
367
382
} catch (Exception e ) {
@@ -400,26 +415,69 @@ private void readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDescWri
400
415
401
416
}
402
417
}
418
+ return projDescNeedsWriting ;
403
419
}
404
420
421
+ /**
422
+ * This method set the active configuration This means the core and library
423
+ * folders of the project are updated. To avoid many update notifications this
424
+ * is done in a runnable with AVOID_UPDATE
425
+ *
426
+ * @param confDesc
427
+ * a writable configuration setting to be made active
428
+ *
429
+ * @return true if the configuration setting has been changed and needs tioo be
430
+ * saved
431
+ */
405
432
private boolean setActiveConfig (ICConfigurationDescription confDesc ) {
406
- try {
407
- BoardDescription boardDescription = myBoardDescriptions .get (confDesc .getId ());
408
- boolean projDescMustBeSaved = Helpers .addArduinoCodeToProject (boardDescription , confDesc );
409
- boolean isRebuildNeeded = Helpers .removeInvalidIncludeFolders (confDesc );
410
- if (isRebuildNeeded ) {
411
- Helpers .deleteBuildFolder (myProject , confDesc );
433
+
434
+ BoardDescription boardDescription = myBoardDescriptions .get (confDesc .getId ());
435
+ boolean projConfMustBeSaved = Helpers .addArduinoCodeToProject (boardDescription , confDesc );
436
+ boolean isRebuildNeeded = Helpers .removeInvalidIncludeFolders (confDesc );
437
+ if (isRebuildNeeded ) {
438
+ Helpers .deleteBuildFolder (myProject , confDesc );
439
+ }
440
+ return projConfMustBeSaved || isRebuildNeeded ;
441
+ }
442
+
443
+ /**
444
+ * This method set the active configuration This means the core and library
445
+ * folders of the project are updated. To avoid many update notifications this
446
+ * is done in a runnable with AVOID_UPDATE
447
+ *
448
+ * @param confDesc
449
+ * a writable configuration setting to be made active
450
+ *
451
+ * @return true if the configuration setting has been changed and needs tioo be
452
+ * saved
453
+ */
454
+ private boolean setActiveConfigInRunnable (ICConfigurationDescription confDesc ) {
455
+
456
+ class MyRunnable implements ICoreRunnable {
457
+ public boolean projConfMustBeSaved = false ;
458
+
459
+ @ Override
460
+ public void run (IProgressMonitor internalMonitor ) throws CoreException {
461
+ projConfMustBeSaved = setActiveConfig (confDesc );
412
462
}
413
- return projDescMustBeSaved || isRebuildNeeded ;
463
+ }
414
464
465
+ final IWorkspace workspace = ResourcesPlugin .getWorkspace ();
466
+ IWorkspaceRoot root = workspace .getRoot ();
467
+ MyRunnable runnable = new MyRunnable ();
468
+ try {
469
+ workspace .run (runnable , root , IWorkspace .AVOID_UPDATE , null );
415
470
} catch (Exception e ) {
416
- e .printStackTrace ();
417
- Common .log (new Status (IStatus .ERROR , io .sloeber .core .Activator .getId (), "failed to save the board settings" , //$NON-NLS-1$
418
- e ));
471
+ ICProjectDescription projDesc = confDesc .getProjectDescription ();
472
+ String confDescName = confDesc .getName ();
473
+ String projName = projDesc .getProject ().getName ();
474
+ Common .log (new Status (IStatus .INFO , io .sloeber .core .Activator .getId (),
475
+ "Setting config " + confDescName + " for project " + projName + " failed" , e )); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
419
476
}
420
- return false ;
477
+ return runnable . projConfMustBeSaved ;
421
478
}
422
479
480
+
423
481
private void setEnvironmentVariables (final ICProjectDescription prjCDesc ) {
424
482
for (ICConfigurationDescription confDesc : prjCDesc .getConfigurations ()) {
425
483
setEnvVars (confDesc , getEnvVars (confDesc ));
@@ -500,6 +558,17 @@ private static void storeConfigurationFile(IFile file, Map<String, String> vars)
500
558
}
501
559
502
560
if (file .exists ()) {
561
+ // if the filecontent hasn't changed=>do nothing
562
+ try {
563
+ Path filePath = Path .of (file .getLocation ().toOSString ());
564
+ String fileContent = Files .readString (filePath );
565
+ if (content .equals (fileContent )) {
566
+ return ;
567
+ }
568
+ } catch (IOException e ) {
569
+ // Don't care as a optimization didn't work
570
+ e .printStackTrace ();
571
+ }
503
572
file .delete (true , null );
504
573
}
505
574
@@ -709,7 +778,26 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
709
778
710
779
configure (newProjDesc , true );
711
780
if (needsConfigSet ) {
712
- setActiveConfig (newActiveConfig );
781
+ setActiveConfigInRunnable (newActiveConfig );
782
+ }
783
+
784
+ }
785
+
786
+ public void sloeberCfgChanged () {
787
+ CCorePlugin cCorePlugin = CCorePlugin .getDefault ();
788
+ ICProjectDescription projDesc = cCorePlugin .getProjectDescription (myProject );
789
+ ICConfigurationDescription activeConfig = projDesc .getActiveConfiguration ();
790
+ isInMemory = false ;
791
+ boolean projDescNeedsSaving = configure (projDesc , true );
792
+ Helpers .deleteBuildFolder (myProject , activeConfig );
793
+ projDescNeedsSaving = projDescNeedsSaving || setActiveConfig (activeConfig );
794
+ if (projDescNeedsSaving ) {
795
+ try {
796
+ cCorePlugin .setProjectDescription (myProject , projDesc );
797
+ } catch (CoreException e ) {
798
+ // TODO Auto-generated catch block
799
+ e .printStackTrace ();
800
+ }
713
801
}
714
802
715
803
}
0 commit comments