Skip to content

Commit 2ac83b4

Browse files
author
jantje
committed
Separating CDT description settings from other activities
So before add source to project 1)adds the folders do the project 2)updates the include and exclude info to cdt description after add source to project 1)adds the folders do the project 2)returns the include and exclude info to cdt description
1 parent f7df89b commit 2ac83b4

File tree

5 files changed

+232
-171
lines changed

5 files changed

+232
-171
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sloeber.core.api;
22

33
import java.net.URL;
4+
import java.util.List;
45
import java.util.Map;
56
import java.util.Set;
67
import java.util.TreeMap;
@@ -220,8 +221,9 @@ public static boolean removeLibrariesFromProject(IProject project, ICProjectDesc
220221

221222
public static boolean addLibrariesToProject(IProject project, ICConfigurationDescription confDesc,
222223
Set<String> libraries) {
223-
return Libraries.addLibrariesToProject(project, confDesc, libraries);
224-
224+
Map<String, List<IPath>> foldersToChange = Libraries.addLibrariesToProject(project, confDesc,
225+
libraries);
226+
return Libraries.adjustProjectDescription(confDesc, foldersToChange);
225227
}
226228

227229
public static Map<String, IPath> getAllAvailableLibraries(IProject project) {
@@ -238,18 +240,32 @@ public static Set<String> getAllImportedLibraries(IProject project) {
238240
return Libraries.getAllLibrariesFromProject(project);
239241
}
240242

241-
public static boolean addCodeFolder(IProject project, Path path) throws CoreException {
242-
ICProjectDescription projectDescription = CoreModel.getDefault().getProjectDescription(project);
243-
ICConfigurationDescription configurationDescriptions[] = projectDescription.getConfigurations();
243+
/**
244+
* Adds a folder to the project and adds the folder to the linked folders if
245+
* needed Stores the projectDescription if it has changed
246+
*
247+
* @param project
248+
* the project to add the folder to
249+
* @param path
250+
* the path that needs adding to the project
251+
*
252+
* @throws CoreException
253+
*/
254+
public static void addCodeFolder(IProject project, Path path) throws CoreException {
255+
boolean projDescNeedsSaving = false;
256+
CoreModel coreModel = CoreModel.getDefault();
257+
ICProjectDescription projectDescription = coreModel.getProjectDescription(project);
244258

245-
for (ICConfigurationDescription curConfigurationDescription : configurationDescriptions) {
246-
String NiceName = path.lastSegment();
247-
if (Helpers.addCodeFolder(project, path, NiceName, curConfigurationDescription, false)) {
248-
CoreModel.getDefault().getProjectDescriptionManager().setProjectDescription(project, projectDescription,
249-
true, null);
259+
List<IPath> includeFolders = Helpers.addCodeFolder(project, path, path.lastSegment(), false);
260+
for (ICConfigurationDescription curConfig : projectDescription.getConfigurations()) {
261+
if (Helpers.addIncludeFolder(curConfig, includeFolders, true)) {
262+
projDescNeedsSaving = true;
250263
}
251264
}
252-
return true;
265+
if (projDescNeedsSaving) {
266+
coreModel.getProjectDescriptionManager().setProjectDescription(project, projectDescription,
267+
true, null);
268+
}
253269
}
254270

255271
}

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

Lines changed: 93 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import java.net.URI;
66
import java.nio.file.Files;
77
import java.nio.file.Path;
8+
import java.util.Collection;
89
import java.util.HashMap;
910
import java.util.Iterator;
11+
import java.util.List;
1012
import java.util.Map;
1113
import java.util.Map.Entry;
1214
import java.util.TreeMap;
@@ -26,7 +28,6 @@
2628
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
2729
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
2830
import org.eclipse.core.resources.IFile;
29-
import org.eclipse.core.resources.IFolder;
3031
import org.eclipse.core.resources.IProject;
3132
import org.eclipse.core.resources.IProjectDescription;
3233
import org.eclipse.core.resources.IResource;
@@ -85,6 +86,25 @@ private SloeberProject(IProject project) {
8586
}
8687
}
8788

89+
/**
90+
* convenient method to create project
91+
*
92+
* @param proj1Name
93+
* @param object
94+
* @param proj1BoardDesc
95+
* @param codeDesc
96+
* @param proj1CompileDesc
97+
* @param otherDesc
98+
* @param nullProgressMonitor
99+
* @return
100+
*/
101+
// public static IProject convertToArduinoProject(IProject project,
102+
// IProgressMonitor monitor) {
103+
// return createArduinoProject(projectName, projectURI, boardDescriptor,
104+
// codeDesc, compileDescriptor,
105+
// new OtherDescription(), monitor);
106+
// }
107+
88108
/**
89109
* convenient method to create project
90110
*
@@ -119,17 +139,29 @@ public static IProject createArduinoProject(String projectName, URI projectURI,
119139
public void run(IProgressMonitor internalMonitor) throws CoreException {
120140
IProject newProjectHandle = root.getProject(realProjectName);
121141
IndexerController.doNotIndex(newProjectHandle);
142+
143+
122144
try {
123145
IWorkspaceDescription workspaceDesc = workspace.getDescription();
124146
workspaceDesc.setAutoBuilding(false);
125147
workspace.setDescription(workspaceDesc);
126-
IProjectType sloeberProjType = ManagedBuildManager.getProjectType("io.sloeber.core.sketch"); //$NON-NLS-1$
127148

128149
// create a eclipse project
129150
IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
130151
if (projectURI != null) {
131152
description.setLocationURI(projectURI);
132153
}
154+
newProjectHandle.create(description, internalMonitor);
155+
newProjectHandle.open(internalMonitor);
156+
157+
// Add the sketch code
158+
Map<String, IPath> librariesToAdd = codeDesc.createFiles(newProjectHandle, internalMonitor);
159+
160+
// Add the arduino code folders
161+
List<IPath> addToIncludePath = Helpers.addArduinoCodeToProject(newProjectHandle, boardDescriptor);
162+
163+
Map<String, List<IPath>> pathMods = Libraries.addLibrariesToProject(newProjectHandle,
164+
librariesToAdd);
133165

134166
// make the eclipse project a cdt project
135167
CCorePlugin.getDefault().createCProject(description, newProjectHandle, new NullProgressMonitor(),
@@ -142,6 +174,7 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
142174
ManagedCProjectNature.addNature(newProjectHandle, Const.ARDUINO_NATURE_ID, internalMonitor);
143175

144176
// make the cdt project a managed build project
177+
IProjectType sloeberProjType = ManagedBuildManager.getProjectType("io.sloeber.core.sketch"); //$NON-NLS-1$
145178
ManagedBuildManager.createBuildInfo(newProjectHandle);
146179
IManagedProject newProject = ManagedBuildManager.createManagedProject(newProjectHandle,
147180
sloeberProjType);
@@ -161,28 +194,29 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
161194
}
162195

163196
ManagedBuildManager.setDefaultConfiguration(newProjectHandle, defaultConfig);
164-
Map<String, IPath> librariesToAdd = codeDesc.createFiles(newProjectHandle,
165-
new NullProgressMonitor());
197+
// create a sloeber project
198+
SloeberProject arduinoProjDesc = new SloeberProject(newProjectHandle);
199+
Map<String, String> configs2 = new HashMap<>();
166200

167201
CCorePlugin cCorePlugin = CCorePlugin.getDefault();
168202
ICProjectDescription prjCDesc = cCorePlugin.getProjectDescription(newProjectHandle);
169203

170-
SloeberProject arduinoProjDesc = new SloeberProject(newProjectHandle);
171204
for (ICConfigurationDescription curConfigDesc : prjCDesc.getConfigurations()) {
172-
// Even though we use the same boardDescriptor for all configurations during
173-
// project creation
174-
// we need to add them config per config because of the include linking
175-
Helpers.addArduinoCodeToProject(boardDescriptor, curConfigDesc);
176205

177206
arduinoProjDesc.myCompileDescriptions.put(curConfigDesc.getId(), compileDescriptor);
178207
arduinoProjDesc.myBoardDescriptions.put(curConfigDesc.getId(), boardDescriptor);
179208
arduinoProjDesc.myOtherDescriptions.put(curConfigDesc.getId(), otherDesc);
209+
Libraries.adjustProjectDescription(curConfigDesc, pathMods);
210+
Helpers.addIncludeFolder(curConfigDesc, addToIncludePath, true);
211+
212+
arduinoProjDesc.setEnvVars(curConfigDesc.getId(),
213+
arduinoProjDesc.getEnvVars(curConfigDesc.getId()));
214+
configs2.put(curConfigDesc.getName(), curConfigDesc.getId());
180215

181-
setEnvVars(curConfigDesc, arduinoProjDesc.getEnvVars(curConfigDesc));
182-
Libraries.addLibrariesToProject(newProjectHandle, curConfigDesc, librariesToAdd);
183216
}
184217

185-
arduinoProjDesc.createSloeberConfigFiles(prjCDesc);
218+
219+
arduinoProjDesc.createSloeberConfigFiles(configs2);
186220
SubMonitor refreshMonitor = SubMonitor.convert(internalMonitor, 3);
187221
newProjectHandle.open(refreshMonitor);
188222
newProjectHandle.refreshLocal(IResource.DEPTH_INFINITE, refreshMonitor);
@@ -208,17 +242,14 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
208242
return root.getProject(realProjectName);
209243
}
210244

211-
@SuppressWarnings("nls")
212-
private HashMap<String, String> getEnvVars(ICConfigurationDescription confDesc) {
213-
IProject project = confDesc.getProjectDescription().getProject();
214-
215-
BoardDescription boardDescription = myBoardDescriptions.get(confDesc.getId());
216-
CompileDescription compileOptions = myCompileDescriptions.get(confDesc.getId());
217-
OtherDescription otherOptions = myOtherDescriptions.get(confDesc.getId());
245+
private HashMap<String, String> getEnvVars(String configID) {
246+
BoardDescription boardDescription = myBoardDescriptions.get(configID);
247+
CompileDescription compileOptions = myCompileDescriptions.get(configID);
248+
OtherDescription otherOptions = myOtherDescriptions.get(configID);
218249

219250
HashMap<String, String> allVars = new HashMap<>();
220251

221-
allVars.put(ENV_KEY_BUILD_SOURCE_PATH, project.getLocation().toOSString());
252+
allVars.put(ENV_KEY_BUILD_SOURCE_PATH, myProject.getLocation().toOSString());
222253

223254
if (boardDescription != null) {
224255
allVars.putAll(boardDescription.getEnvVars());
@@ -269,15 +300,16 @@ private void configure() {
269300
*/
270301

271302
private boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
303+
Map<String, String> configs = getConfigs(prjCDesc);
272304
boolean saveProjDesc = false;
273305
if (isInMemory) {
274306
if (isDirty) {
275-
createSloeberConfigFiles(prjCDesc);
276-
setEnvironmentVariables(prjCDesc);
307+
createSloeberConfigFiles(configs);
308+
setEnvironmentVariables(configs.values());
277309
isDirty = false;
278310
}
279311
if (myNeedToPersist) {
280-
createSloeberConfigFiles(prjCDesc);
312+
createSloeberConfigFiles(configs);
281313
}
282314
if (prjDescWritable) {
283315
if (myNeedsSyncWithCDT) {
@@ -292,7 +324,7 @@ private boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable
292324
// first read the sloeber files in memory
293325
saveProjDesc = readSloeberConfig(prjCDesc, prjDescWritable);
294326
if (myNeedToPersist || isDirty) {
295-
createSloeberConfigFiles(prjCDesc);
327+
createSloeberConfigFiles(configs);
296328
isDirty = false;
297329
}
298330
if (prjDescWritable) {
@@ -305,11 +337,19 @@ private boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable
305337
saveProjDesc = saveProjDesc || syncWithCDT(prjCDesc, prjDescWritable);
306338
}
307339
}
308-
setEnvironmentVariables(prjCDesc);
340+
setEnvironmentVariables(configs.values());
309341
isInMemory = true;
310342
return saveProjDesc;
311343
}
312344

345+
private static Map<String, String> getConfigs(ICProjectDescription prjCDesc) {
346+
Map<String, String> ret = new HashMap<>();
347+
for (ICConfigurationDescription curconfig : prjCDesc.getConfigurations()) {
348+
ret.put(curconfig.getName(), curconfig.getId());
349+
}
350+
return ret;
351+
}
352+
313353
/**
314354
* sync the Sloeber configuration info with CDT Currently only Sloeber known
315355
* configurations will be created by Sloeber inside CDT
@@ -426,18 +466,20 @@ private boolean readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDesc
426466
* @param confDesc
427467
* a writable configuration setting to be made active
428468
*
429-
* @return true if the configuration setting has been changed and needs tioo be
469+
* @return true if the configuration setting has been changed and needs to be
430470
* saved
431471
*/
432472
private boolean setActiveConfig(ICConfigurationDescription confDesc) {
433473

434474
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);
475+
List<IPath> pathsToInclude = Helpers.addArduinoCodeToProject(myProject, boardDescription);
476+
boolean projConfMustBeSaved = Helpers.addIncludeFolder(confDesc, pathsToInclude, true);
477+
boolean includeFoldersRemoved = Helpers.removeInvalidIncludeFolders(confDesc);
478+
if (includeFoldersRemoved) {
479+
Helpers.deleteBuildFolder(myProject, confDesc.getName());
439480
}
440-
return projConfMustBeSaved || isRebuildNeeded;
481+
482+
return projConfMustBeSaved || includeFoldersRemoved;
441483
}
442484

443485
/**
@@ -477,10 +519,9 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
477519
return runnable.projConfMustBeSaved;
478520
}
479521

480-
481-
private void setEnvironmentVariables(final ICProjectDescription prjCDesc) {
482-
for (ICConfigurationDescription confDesc : prjCDesc.getConfigurations()) {
483-
setEnvVars(confDesc, getEnvVars(confDesc));
522+
private void setEnvironmentVariables(Collection<String> configIDs) {
523+
for (String curConfigID : configIDs) {
524+
setEnvVars(curConfigID, getEnvVars(curConfigID));
484525
}
485526
}
486527

@@ -496,14 +537,14 @@ private void setEnvironmentVariables(final ICProjectDescription prjCDesc) {
496537
* @param project
497538
* the project to store the data for
498539
*/
499-
private void createSloeberConfigFiles(final ICProjectDescription prjCDesc) {
540+
private void createSloeberConfigFiles(Map<String, String> configs) {
500541

501542
Map<String, String> configVars = new TreeMap<>();
502543
Map<String, String> versionVars = new TreeMap<>();
503544

504-
for (ICConfigurationDescription confDesc : prjCDesc.getConfigurations()) {
505-
String confID = confDesc.getId();
506-
String confName = confDesc.getName();
545+
for (Entry<String, String> confDesc : configs.entrySet()) {
546+
String confID = confDesc.getValue();
547+
String confName = confDesc.getKey();
507548
BoardDescription boardDescription = myBoardDescriptions.get(confID);
508549
CompileDescription compileDescription = myCompileDescriptions.get(confID);
509550
OtherDescription otherDescription = myOtherDescriptions.get(confID);
@@ -596,13 +637,16 @@ public void setBoardDescription(ICConfigurationDescription confDesc, BoardDescri
596637
}
597638
}
598639
if (boardDescription.needsRebuild(oldBoardDescription)) {
599-
Helpers.deleteBuildFolder(myProject, confDesc);
640+
Helpers.deleteBuildFolder(myProject, confDesc.getName());
600641
}
601642
myBoardDescriptions.put(confDesc.getId(), boardDescription);
602643
isDirty = true;
603644
}
604645

605-
private static void setEnvVars(ICConfigurationDescription confDesc, Map<String, String> envVars) {
646+
private void setEnvVars(String configID, Map<String, String> envVars) {
647+
CCorePlugin cCorePlugin = CCorePlugin.getDefault();
648+
ICProjectDescription prjCDesc = cCorePlugin.getProjectDescription(myProject);
649+
ICConfigurationDescription confDesc = prjCDesc.getConfigurationById(configID);
606650
IConfiguration configuration = ManagedBuildManager.getConfigurationForDescription(confDesc);
607651
if (configuration != null) {
608652
SloeberConfigurationVariableSupplier varSup = (SloeberConfigurationVariableSupplier) configuration
@@ -644,14 +688,10 @@ public static synchronized SloeberProject getSloeberProject(IProject project, bo
644688
}
645689

646690
public void setCompileDescription(ICConfigurationDescription confDesc, CompileDescription compileDescription) {
647-
IProject project = confDesc.getProjectDescription().getProject();
648-
IFolder buildFolder = project.getFolder(confDesc.getName());
649691

650-
if (buildFolder.exists()) {
651-
CompileDescription oldCompileDescription = myCompileDescriptions.get(confDesc.getId());
652-
if (compileDescription.needsRebuild(oldCompileDescription)) {
653-
Helpers.deleteBuildFolder(project, confDesc);
654-
}
692+
CompileDescription oldCompileDescription = myCompileDescriptions.get(confDesc.getId());
693+
if (compileDescription.needsRebuild(oldCompileDescription)) {
694+
Helpers.deleteBuildFolder(myProject, confDesc.getName());
655695
}
656696
myCompileDescriptions.put(confDesc.getId(), compileDescription);
657697
isDirty = true;
@@ -783,19 +823,22 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
783823

784824
}
785825

826+
/**
827+
* Call this method when the sloeber.cfg file changed
828+
*
829+
*/
786830
public void sloeberCfgChanged() {
787831
CCorePlugin cCorePlugin = CCorePlugin.getDefault();
788832
ICProjectDescription projDesc = cCorePlugin.getProjectDescription(myProject);
789833
ICConfigurationDescription activeConfig = projDesc.getActiveConfiguration();
790834
isInMemory = false;
791835
boolean projDescNeedsSaving = configure(projDesc, true);
792-
Helpers.deleteBuildFolder(myProject, activeConfig);
836+
Helpers.deleteBuildFolder(myProject, activeConfig.getName());
793837
projDescNeedsSaving = projDescNeedsSaving || setActiveConfig(activeConfig);
794838
if (projDescNeedsSaving) {
795839
try {
796840
cCorePlugin.setProjectDescription(myProject, projDesc);
797841
} catch (CoreException e) {
798-
// TODO Auto-generated catch block
799842
e.printStackTrace();
800843
}
801844
}

0 commit comments

Comments
 (0)