5
5
import java .net .URI ;
6
6
import java .nio .file .Files ;
7
7
import java .nio .file .Path ;
8
- import java .util .Collection ;
9
8
import java .util .HashMap ;
10
9
import java .util .Iterator ;
11
10
import java .util .LinkedList ;
51
50
import io .sloeber .core .common .ConfigurationPreferences ;
52
51
import io .sloeber .core .common .Const ;
53
52
import io .sloeber .core .listeners .IndexerController ;
54
- import io .sloeber .core .toolchain .SloeberConfigurationVariableSupplier ;
55
53
import io .sloeber .core .tools .Helpers ;
56
54
import io .sloeber .core .tools .Libraries ;
57
55
import io .sloeber .core .txt .KeyValueTree ;
@@ -62,10 +60,11 @@ public class SloeberProject extends Common {
62
60
private Map <String , BoardDescription > myBoardDescriptions = new HashMap <>();
63
61
private Map <String , CompileDescription > myCompileDescriptions = new HashMap <>();
64
62
private Map <String , OtherDescription > myOtherDescriptions = new HashMap <>();
63
+ private Map <String , Map <String , String >> myEnvironmentVariables = new HashMap <>();
65
64
private TxtFile myCfgFile = null ;
66
65
private IProject myProject = null ;
67
66
private boolean isInMemory = false ;
68
- private boolean isDirty = false ; // if anything has changed
67
+ private boolean myIsDirty = false ; // if anything has changed
69
68
private boolean myNeedToPersist = false ; // Do we need to write data to disk
70
69
private boolean myNeedsClean = false ; // is there old sloeber data that needs cleaning
71
70
private boolean myNeedsSyncWithCDT = false ; // Knows CDT all configs Sloeber Knows
@@ -176,12 +175,13 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
176
175
Helpers .addIncludeFolder (curConfigDesc , addToIncludePath , true );
177
176
178
177
String curConfigKey = getConfigKey (curConfigDesc );
179
- sloeberProject .setEnvVars (curConfigKey , sloeberProject .getEnvVars (curConfigKey ));
178
+ sloeberProject .myEnvironmentVariables .put (curConfigKey ,
179
+ sloeberProject .getEnvVars (curConfigKey ));
180
180
configs2 .put (curConfigName , curConfigDesc .getId ());
181
181
182
182
}
183
183
184
- sloeberProject .createSloeberConfigFiles (configs2 );
184
+ sloeberProject .createSloeberConfigFiles (prjCDesc );
185
185
SubMonitor refreshMonitor = SubMonitor .convert (internalMonitor , 3 );
186
186
project .refreshLocal (IResource .DEPTH_INFINITE , refreshMonitor );
187
187
cCorePlugin .setProjectDescription (project , prjCDesc , true , null );
@@ -302,13 +302,14 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
302
302
Libraries .adjustProjectDescription (curConfigDesc , pathMods );
303
303
Helpers .addIncludeFolder (curConfigDesc , addToIncludePath , true );
304
304
305
- arduinoProjDesc .setEnvVars (getConfigKey (curConfigDesc ),
306
- arduinoProjDesc .getEnvVars (getConfigKey (curConfigDesc )));
305
+ String curConfigKey = getConfigKey (curConfigDesc );
306
+ arduinoProjDesc .myEnvironmentVariables .put (curConfigKey ,
307
+ arduinoProjDesc .getEnvVars (curConfigKey ));
307
308
configs2 .put (curConfigDesc .getName (), curConfigDesc .getId ());
308
309
309
310
}
310
311
311
- arduinoProjDesc .createSloeberConfigFiles (configs2 );
312
+ arduinoProjDesc .createSloeberConfigFiles (prjCDesc );
312
313
SubMonitor refreshMonitor = SubMonitor .convert (internalMonitor , 3 );
313
314
newProjectHandle .open (refreshMonitor );
314
315
newProjectHandle .refreshLocal (IResource .DEPTH_INFINITE , refreshMonitor );
@@ -392,17 +393,17 @@ public void configure() {
392
393
* @return true if the projectDesc needs to be saved
393
394
*/
394
395
395
- public boolean configure (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
396
- Map <String , String > configs = getConfigs (prjCDesc );
396
+ public synchronized boolean configure (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
397
397
boolean saveProjDesc = false ;
398
398
if (isInMemory ) {
399
- if (isDirty ) {
400
- createSloeberConfigFiles (configs );
401
- setEnvironmentVariables (getCfgKeys (configs ));
402
- isDirty = false ;
399
+ if (myIsDirty ) {
400
+ createSloeberConfigFiles (prjCDesc );
401
+ setAllEnvironmentVars (prjCDesc );
402
+
403
+ myIsDirty = false ;
403
404
}
404
405
if (myNeedToPersist ) {
405
- createSloeberConfigFiles (configs );
406
+ createSloeberConfigFiles (prjCDesc );
406
407
}
407
408
if (prjDescWritable ) {
408
409
if (myNeedsSyncWithCDT ) {
@@ -418,9 +419,9 @@ public boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable)
418
419
419
420
// first read the sloeber files in memory
420
421
saveProjDesc = readConfig (prjCDesc , prjDescWritable );
421
- if (myNeedToPersist || isDirty ) {
422
- createSloeberConfigFiles (configs );
423
- isDirty = false ;
422
+ if (myNeedToPersist || myIsDirty ) {
423
+ createSloeberConfigFiles (prjCDesc );
424
+ myIsDirty = false ;
424
425
}
425
426
if (prjDescWritable ) {
426
427
if (myNeedsClean ) {
@@ -433,10 +434,19 @@ public boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable)
433
434
saveProjDesc = saveProjDesc || syncWithCDT (prjCDesc , prjDescWritable );
434
435
}
435
436
}
436
- setEnvironmentVariables ( getCfgKeys ( configs ) );
437
+ setAllEnvironmentVars ( prjCDesc );
437
438
return saveProjDesc ;
438
439
}
439
440
441
+ private void setAllEnvironmentVars (ICProjectDescription prjCDesc ) {
442
+ // set all the environment variables
443
+ myEnvironmentVariables .clear ();
444
+ for (ICConfigurationDescription curconfig : prjCDesc .getConfigurations ()) {
445
+ String curConfigKey = getConfigKey (curconfig );
446
+ myEnvironmentVariables .put (curConfigKey , getEnvVars (curConfigKey ));
447
+ }
448
+ }
449
+
440
450
/**
441
451
* sync the Sloeber configuration info with CDT Currently only Sloeber known
442
452
* configurations will be created by Sloeber inside CDT
@@ -650,11 +660,6 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
650
660
return runnable .projConfMustBeSaved ;
651
661
}
652
662
653
- private void setEnvironmentVariables (Collection <String > configKeys ) {
654
- for (String curConfigKey : configKeys ) {
655
- setEnvVars (curConfigKey , getEnvVars (curConfigKey ));
656
- }
657
- }
658
663
659
664
/**
660
665
* This methods creates/updates 2 files in the workspace. Together these files
@@ -668,7 +673,7 @@ private void setEnvironmentVariables(Collection<String> configKeys) {
668
673
* @param project
669
674
* the project to store the data for
670
675
*/
671
- private synchronized void createSloeberConfigFiles (Map < String , String > configs ) {
676
+ private synchronized void createSloeberConfigFiles (ICProjectDescription prjCDesc ) {
672
677
673
678
final IWorkspace workspace = ResourcesPlugin .getWorkspace ();
674
679
if (workspace .isTreeLocked ()) {
@@ -680,8 +685,8 @@ private synchronized void createSloeberConfigFiles(Map<String, String> configs)
680
685
Map <String , String > configVars = new TreeMap <>();
681
686
Map <String , String > versionVars = new TreeMap <>();
682
687
683
- for (Entry < String , String > confDesc : configs . entrySet ()) {
684
- String confName = confDesc . getKey ();
688
+ for (ICConfigurationDescription curconfig : prjCDesc . getConfigurations ()) {
689
+ String confName = curconfig . getName ();
685
690
BoardDescription boardDescription = myBoardDescriptions .get (confName );
686
691
CompileDescription compileDescription = myCompileDescriptions .get (confName );
687
692
OtherDescription otherDescription = myOtherDescriptions .get (confName );
@@ -777,24 +782,10 @@ public void setBoardDescription(String confDescName, BoardDescription boardDescr
777
782
Helpers .deleteBuildFolder (myProject , confDescName );
778
783
}
779
784
myBoardDescriptions .put (confDescName , boardDescription );
780
- isDirty = true ;
785
+ myIsDirty = true ;
781
786
}
782
787
783
- private void setEnvVars (String configName , Map <String , String > envVars ) {
784
- CCorePlugin cCorePlugin = CCorePlugin .getDefault ();
785
- ICProjectDescription prjCDesc = cCorePlugin .getProjectDescription (myProject );
786
- setEnvVars (prjCDesc , configName , envVars );
787
- }
788
788
789
- private static void setEnvVars (ICProjectDescription prjCDesc , String configName , Map <String , String > envVars ) {
790
- ICConfigurationDescription confDesc = prjCDesc .getConfigurationByName (configName );
791
- IConfiguration configuration = ManagedBuildManager .getConfigurationForDescription (confDesc );
792
- if (configuration != null ) {
793
- SloeberConfigurationVariableSupplier varSup = (SloeberConfigurationVariableSupplier ) configuration
794
- .getEnvironmentVariableSupplier ();
795
- varSup .setEnvVars (configuration , envVars );
796
- }
797
- }
798
789
799
790
/**
800
791
* get the Arduino project description based on a project description
@@ -834,13 +825,13 @@ public void setCompileDescription(String confDescName, CompileDescription compil
834
825
Helpers .deleteBuildFolder (myProject , confDescName );
835
826
}
836
827
myCompileDescriptions .put (confDescName , compileDescription );
837
- isDirty = true ;
828
+ myIsDirty = true ;
838
829
}
839
830
840
831
public void setOtherDescription (String confDescName , OtherDescription otherDesc ) {
841
832
try {
842
833
myOtherDescriptions .put (confDescName , otherDesc );
843
- isDirty = true ;
834
+ myIsDirty = true ;
844
835
} catch (Exception e ) {
845
836
e .printStackTrace ();
846
837
Common .log (new Status (IStatus .ERROR , io .sloeber .core .Activator .getId (), "failed to save the board settings" , //$NON-NLS-1$
@@ -939,12 +930,12 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
939
930
List <ICConfigurationDescription > renamedConfigs = new LinkedList <>();
940
931
941
932
// make sure the dirty flag is set when needed
942
- if (!isDirty ) {
933
+ if (!myIsDirty ) {
943
934
// set dirty when the number of configurations changed
944
935
int newNumberOfConfigs = newProjDesc .getConfigurations ().length ;
945
936
int oldNumberOfConfigs = oldProjDesc .getConfigurations ().length ;
946
937
if (newNumberOfConfigs != oldNumberOfConfigs ) {
947
- isDirty = true ;
938
+ myIsDirty = true ;
948
939
} else {
949
940
// set dirty if a configname changed
950
941
for (ICConfigurationDescription curConfig : newProjDesc .getConfigurations ()) {
@@ -968,24 +959,20 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
968
959
myOtherDescriptions .remove (oldKey );
969
960
}
970
961
971
- isDirty = true ;
962
+ myIsDirty = true ;
972
963
}
973
964
}
974
965
}
975
966
}
976
967
// in many cases we also need to set the active config
977
- boolean needsConfigSet = myNeedsClean || isDirty
968
+ boolean needsConfigSet = myNeedsClean || myIsDirty
978
969
|| !newActiveConfig .getName ().equals (oldActiveConfig .getName ());
979
970
980
971
configure (newProjDesc , true );
981
972
if (!renamedConfigs .isEmpty ()) {
982
973
// a extra setting of the environment vars is neede because the config was not
983
- // found
984
- // due to the rename
985
- for (ICConfigurationDescription curConfig : renamedConfigs ) {
986
- String curConfigKey = getConfigKey (curConfig );
987
- setEnvVars (newProjDesc , curConfigKey , getEnvVars (curConfigKey ));
988
- }
974
+ // found due to the rename
975
+ setAllEnvironmentVars (newProjDesc );
989
976
}
990
977
if (needsConfigSet ) {
991
978
setActiveConfigInRunnable (newActiveConfig );
@@ -1001,7 +988,7 @@ public void sloeberCfgChanged() {
1001
988
CCorePlugin cCorePlugin = CCorePlugin .getDefault ();
1002
989
ICProjectDescription projDesc = cCorePlugin .getProjectDescription (myProject );
1003
990
ICConfigurationDescription activeConfig = projDesc .getActiveConfiguration ();
1004
- isDirty = true ;
991
+ myIsDirty = true ;
1005
992
boolean projDescNeedsSaving = configure (projDesc , true );
1006
993
Helpers .deleteBuildFolder (myProject , activeConfig .getName ());
1007
994
projDescNeedsSaving = projDescNeedsSaving || setActiveConfig (activeConfig );
@@ -1042,19 +1029,30 @@ private void internalReloadTxtFile() {
1042
1029
1043
1030
}
1044
1031
1045
- private static Map <String , String > getConfigs (ICProjectDescription prjCDesc ) {
1046
- Map <String , String > ret = new HashMap <>();
1047
- for (ICConfigurationDescription curconfig : prjCDesc .getConfigurations ()) {
1048
- ret .put (curconfig .getName (), curconfig .getId ());
1049
- }
1050
- return ret ;
1032
+ /**
1033
+ * This method returns the key used to identify the configuraation One can use
1034
+ * the id or the name. Both with advantages and disadvantages
1035
+ *
1036
+ * I doubted between the 2 and finally set on the name. To be able to switch
1037
+ * between the 2 options during investigation I created this method So basically
1038
+ * now this can be replaced by configDesc.getName();
1039
+ *
1040
+ * @param configDesc
1041
+ * @return
1042
+ */
1043
+ private static String getConfigKey (ICConfigurationDescription configDesc ) {
1044
+ return configDesc .getName ();
1051
1045
}
1052
1046
1053
- private Collection < String > getCfgKeys ( Map < String , String > configs ) {
1054
- return configs . keySet () ;
1047
+ public boolean isDirty ( ) {
1048
+ return myIsDirty ;
1055
1049
}
1056
1050
1057
- private static String getConfigKey (ICConfigurationDescription configDesc ) {
1058
- return configDesc .getName ();
1051
+ public Map <String , String > getEnvironmentVariables (String configKey ) {
1052
+ if (isDirty ()) {
1053
+ configure ();
1054
+ }
1055
+
1056
+ return myEnvironmentVariables .get (configKey );
1059
1057
}
1060
1058
}
0 commit comments