Skip to content

Commit d096392

Browse files
author
jantje
committed
#1347 environment variables should be set (first issue)
1 parent de5dc0e commit d096392

File tree

2 files changed

+83
-100
lines changed

2 files changed

+83
-100
lines changed

io.sloeber.core/src/io/sloeber/core/api/SloeberProject.java

+63-65
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.net.URI;
66
import java.nio.file.Files;
77
import java.nio.file.Path;
8-
import java.util.Collection;
98
import java.util.HashMap;
109
import java.util.Iterator;
1110
import java.util.LinkedList;
@@ -51,7 +50,6 @@
5150
import io.sloeber.core.common.ConfigurationPreferences;
5251
import io.sloeber.core.common.Const;
5352
import io.sloeber.core.listeners.IndexerController;
54-
import io.sloeber.core.toolchain.SloeberConfigurationVariableSupplier;
5553
import io.sloeber.core.tools.Helpers;
5654
import io.sloeber.core.tools.Libraries;
5755
import io.sloeber.core.txt.KeyValueTree;
@@ -62,10 +60,11 @@ public class SloeberProject extends Common {
6260
private Map<String, BoardDescription> myBoardDescriptions = new HashMap<>();
6361
private Map<String, CompileDescription> myCompileDescriptions = new HashMap<>();
6462
private Map<String, OtherDescription> myOtherDescriptions = new HashMap<>();
63+
private Map<String, Map<String, String>> myEnvironmentVariables = new HashMap<>();
6564
private TxtFile myCfgFile = null;
6665
private IProject myProject = null;
6766
private boolean isInMemory = false;
68-
private boolean isDirty = false; // if anything has changed
67+
private boolean myIsDirty = false; // if anything has changed
6968
private boolean myNeedToPersist = false; // Do we need to write data to disk
7069
private boolean myNeedsClean = false; // is there old sloeber data that needs cleaning
7170
private boolean myNeedsSyncWithCDT = false; // Knows CDT all configs Sloeber Knows
@@ -176,12 +175,13 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
176175
Helpers.addIncludeFolder(curConfigDesc, addToIncludePath, true);
177176

178177
String curConfigKey = getConfigKey(curConfigDesc);
179-
sloeberProject.setEnvVars(curConfigKey, sloeberProject.getEnvVars(curConfigKey));
178+
sloeberProject.myEnvironmentVariables.put(curConfigKey,
179+
sloeberProject.getEnvVars(curConfigKey));
180180
configs2.put(curConfigName, curConfigDesc.getId());
181181

182182
}
183183

184-
sloeberProject.createSloeberConfigFiles(configs2);
184+
sloeberProject.createSloeberConfigFiles(prjCDesc);
185185
SubMonitor refreshMonitor = SubMonitor.convert(internalMonitor, 3);
186186
project.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
187187
cCorePlugin.setProjectDescription(project, prjCDesc, true, null);
@@ -302,13 +302,14 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
302302
Libraries.adjustProjectDescription(curConfigDesc, pathMods);
303303
Helpers.addIncludeFolder(curConfigDesc, addToIncludePath, true);
304304

305-
arduinoProjDesc.setEnvVars(getConfigKey(curConfigDesc),
306-
arduinoProjDesc.getEnvVars(getConfigKey(curConfigDesc)));
305+
String curConfigKey = getConfigKey(curConfigDesc);
306+
arduinoProjDesc.myEnvironmentVariables.put(curConfigKey,
307+
arduinoProjDesc.getEnvVars(curConfigKey));
307308
configs2.put(curConfigDesc.getName(), curConfigDesc.getId());
308309

309310
}
310311

311-
arduinoProjDesc.createSloeberConfigFiles(configs2);
312+
arduinoProjDesc.createSloeberConfigFiles(prjCDesc);
312313
SubMonitor refreshMonitor = SubMonitor.convert(internalMonitor, 3);
313314
newProjectHandle.open(refreshMonitor);
314315
newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
@@ -392,17 +393,17 @@ public void configure() {
392393
* @return true if the projectDesc needs to be saved
393394
*/
394395

395-
public boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
396-
Map<String, String> configs = getConfigs(prjCDesc);
396+
public synchronized boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
397397
boolean saveProjDesc = false;
398398
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;
403404
}
404405
if (myNeedToPersist) {
405-
createSloeberConfigFiles(configs);
406+
createSloeberConfigFiles(prjCDesc);
406407
}
407408
if (prjDescWritable) {
408409
if (myNeedsSyncWithCDT) {
@@ -418,9 +419,9 @@ public boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable)
418419

419420
// first read the sloeber files in memory
420421
saveProjDesc = readConfig(prjCDesc, prjDescWritable);
421-
if (myNeedToPersist || isDirty) {
422-
createSloeberConfigFiles(configs);
423-
isDirty = false;
422+
if (myNeedToPersist || myIsDirty) {
423+
createSloeberConfigFiles(prjCDesc);
424+
myIsDirty = false;
424425
}
425426
if (prjDescWritable) {
426427
if (myNeedsClean) {
@@ -433,10 +434,19 @@ public boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable)
433434
saveProjDesc = saveProjDesc || syncWithCDT(prjCDesc, prjDescWritable);
434435
}
435436
}
436-
setEnvironmentVariables(getCfgKeys(configs));
437+
setAllEnvironmentVars(prjCDesc);
437438
return saveProjDesc;
438439
}
439440

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+
440450
/**
441451
* sync the Sloeber configuration info with CDT Currently only Sloeber known
442452
* configurations will be created by Sloeber inside CDT
@@ -650,11 +660,6 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
650660
return runnable.projConfMustBeSaved;
651661
}
652662

653-
private void setEnvironmentVariables(Collection<String> configKeys) {
654-
for (String curConfigKey : configKeys) {
655-
setEnvVars(curConfigKey, getEnvVars(curConfigKey));
656-
}
657-
}
658663

659664
/**
660665
* This methods creates/updates 2 files in the workspace. Together these files
@@ -668,7 +673,7 @@ private void setEnvironmentVariables(Collection<String> configKeys) {
668673
* @param project
669674
* the project to store the data for
670675
*/
671-
private synchronized void createSloeberConfigFiles(Map<String, String> configs) {
676+
private synchronized void createSloeberConfigFiles(ICProjectDescription prjCDesc) {
672677

673678
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
674679
if (workspace.isTreeLocked()) {
@@ -680,8 +685,8 @@ private synchronized void createSloeberConfigFiles(Map<String, String> configs)
680685
Map<String, String> configVars = new TreeMap<>();
681686
Map<String, String> versionVars = new TreeMap<>();
682687

683-
for (Entry<String, String> confDesc : configs.entrySet()) {
684-
String confName = confDesc.getKey();
688+
for (ICConfigurationDescription curconfig : prjCDesc.getConfigurations()) {
689+
String confName = curconfig.getName();
685690
BoardDescription boardDescription = myBoardDescriptions.get(confName);
686691
CompileDescription compileDescription = myCompileDescriptions.get(confName);
687692
OtherDescription otherDescription = myOtherDescriptions.get(confName);
@@ -777,24 +782,10 @@ public void setBoardDescription(String confDescName, BoardDescription boardDescr
777782
Helpers.deleteBuildFolder(myProject, confDescName);
778783
}
779784
myBoardDescriptions.put(confDescName, boardDescription);
780-
isDirty = true;
785+
myIsDirty = true;
781786
}
782787

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-
}
788788

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-
}
798789

799790
/**
800791
* get the Arduino project description based on a project description
@@ -834,13 +825,13 @@ public void setCompileDescription(String confDescName, CompileDescription compil
834825
Helpers.deleteBuildFolder(myProject, confDescName);
835826
}
836827
myCompileDescriptions.put(confDescName, compileDescription);
837-
isDirty = true;
828+
myIsDirty = true;
838829
}
839830

840831
public void setOtherDescription(String confDescName, OtherDescription otherDesc) {
841832
try {
842833
myOtherDescriptions.put(confDescName, otherDesc);
843-
isDirty = true;
834+
myIsDirty = true;
844835
} catch (Exception e) {
845836
e.printStackTrace();
846837
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
939930
List<ICConfigurationDescription> renamedConfigs = new LinkedList<>();
940931

941932
// make sure the dirty flag is set when needed
942-
if (!isDirty) {
933+
if (!myIsDirty) {
943934
// set dirty when the number of configurations changed
944935
int newNumberOfConfigs = newProjDesc.getConfigurations().length;
945936
int oldNumberOfConfigs = oldProjDesc.getConfigurations().length;
946937
if (newNumberOfConfigs != oldNumberOfConfigs) {
947-
isDirty = true;
938+
myIsDirty = true;
948939
} else {
949940
// set dirty if a configname changed
950941
for (ICConfigurationDescription curConfig : newProjDesc.getConfigurations()) {
@@ -968,24 +959,20 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
968959
myOtherDescriptions.remove(oldKey);
969960
}
970961

971-
isDirty = true;
962+
myIsDirty = true;
972963
}
973964
}
974965
}
975966
}
976967
// in many cases we also need to set the active config
977-
boolean needsConfigSet = myNeedsClean || isDirty
968+
boolean needsConfigSet = myNeedsClean || myIsDirty
978969
|| !newActiveConfig.getName().equals(oldActiveConfig.getName());
979970

980971
configure(newProjDesc, true);
981972
if (!renamedConfigs.isEmpty()) {
982973
// 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);
989976
}
990977
if (needsConfigSet) {
991978
setActiveConfigInRunnable(newActiveConfig);
@@ -1001,7 +988,7 @@ public void sloeberCfgChanged() {
1001988
CCorePlugin cCorePlugin = CCorePlugin.getDefault();
1002989
ICProjectDescription projDesc = cCorePlugin.getProjectDescription(myProject);
1003990
ICConfigurationDescription activeConfig = projDesc.getActiveConfiguration();
1004-
isDirty = true;
991+
myIsDirty = true;
1005992
boolean projDescNeedsSaving = configure(projDesc, true);
1006993
Helpers.deleteBuildFolder(myProject, activeConfig.getName());
1007994
projDescNeedsSaving = projDescNeedsSaving || setActiveConfig(activeConfig);
@@ -1042,19 +1029,30 @@ private void internalReloadTxtFile() {
10421029

10431030
}
10441031

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();
10511045
}
10521046

1053-
private Collection<String> getCfgKeys(Map<String, String> configs) {
1054-
return configs.keySet();
1047+
public boolean isDirty() {
1048+
return myIsDirty;
10551049
}
10561050

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);
10591057
}
10601058
}

io.sloeber.core/src/io/sloeber/core/toolchain/SloeberConfigurationVariableSupplier.java

+20-35
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717
import io.sloeber.core.api.SloeberProject;
1818

1919
public class SloeberConfigurationVariableSupplier implements IConfigurationEnvironmentVariableSupplier {
20-
// variables per configuration
21-
private Map<String, Map<String, String>> myConfigValues = new HashMap<>();
22-
private boolean myIsConfigurationBussy = false;
20+
21+
private static SloeberProject getSloeberProject(IConfiguration configuration) {
22+
ICConfigurationDescription confDesc = ManagedBuildManager.getDescriptionForConfiguration(configuration);
23+
ICProjectDescription projDesc = confDesc.getProjectDescription();
24+
IProject project = projDesc.getProject();
25+
return SloeberProject.getSloeberProject(project, false);
26+
}
2327

2428
@Override
2529
public IBuildEnvironmentVariable getVariable(String variableName, IConfiguration configuration,
2630
IEnvironmentVariableProvider provider) {
27-
initializeIfNotYetDone(configuration);
28-
Map<String, String> curConfigVars = myConfigValues.get(configuration.getName());
29-
if (null == curConfigVars) {
31+
String ret = null;
32+
SloeberProject sloeberProject = getSloeberProject(configuration);
33+
if (sloeberProject == null) {
3034
return null;
31-
// This should only happen if a config is existing Sloeber does not know about
32-
// because we configured the sloeber project above
33-
// So this should not happen
3435
}
35-
String ret = curConfigVars.get(variableName);
36+
Map<String, String> boardEnvVars = sloeberProject.getEnvironmentVariables(configuration.getName());
37+
if (null != boardEnvVars) {
38+
ret = boardEnvVars.get(variableName);
39+
}
3640
if (ret == null) {
3741
// when the configuration doesn't hold the env var maybe the workbench does
3842
ret = PackageManager.getEnvironmentVariables().get(variableName);
@@ -46,16 +50,18 @@ public IBuildEnvironmentVariable getVariable(String variableName, IConfiguration
4650
@Override
4751
public IBuildEnvironmentVariable[] getVariables(IConfiguration configuration,
4852
IEnvironmentVariableProvider provider) {
49-
initializeIfNotYetDone(configuration);
5053
Map<String, String> retVars = new HashMap<>();
5154
Map<String, String> workbenchVars = PackageManager.getEnvironmentVariables();
5255
if (workbenchVars != null) {
5356
retVars.putAll(workbenchVars);
5457
}
58+
SloeberProject sloeberProject = getSloeberProject(configuration);
59+
if (sloeberProject != null) {
5560

56-
Map<String, String> curConfigVars = myConfigValues.get(configuration.getName());
57-
if (curConfigVars != null) {
58-
retVars.putAll(curConfigVars);
61+
Map<String, String> boardEnvVars = sloeberProject.getEnvironmentVariables(configuration.getName());
62+
if (boardEnvVars != null) {
63+
retVars.putAll(boardEnvVars);
64+
}
5965
}
6066

6167
IBuildEnvironmentVariable[] ret = new BuildEnvironmentVariable[retVars.size()];
@@ -66,25 +72,4 @@ public IBuildEnvironmentVariable[] getVariables(IConfiguration configuration,
6672
return ret;
6773
}
6874

69-
public void setEnvVars(IConfiguration configuration, Map<String, String> values) {
70-
myConfigValues.put(configuration.getName(), values);
71-
}
72-
73-
private void initializeIfNotYetDone(IConfiguration configuration) {
74-
if (!myConfigValues.isEmpty()) {
75-
// we have some data; asume it is correct
76-
return;
77-
}
78-
if (myIsConfigurationBussy) {
79-
// We are already configurating. Don't go in an endless loop
80-
return;
81-
}
82-
myIsConfigurationBussy = true;
83-
ICConfigurationDescription confDesc = ManagedBuildManager.getDescriptionForConfiguration(configuration);
84-
ICProjectDescription projDesc = confDesc.getProjectDescription();
85-
IProject project = projDesc.getProject();
86-
SloeberProject sloeberProject = SloeberProject.getSloeberProject(project, false);
87-
sloeberProject.configure(projDesc, false);
88-
myIsConfigurationBussy = false;
89-
}
9075
}

0 commit comments

Comments
 (0)