Skip to content

Commit 6265ede

Browse files
author
jantje
committed
#1268 sloeber.cfg changes are picked up
This seems to work all right, cant believe it. I added a configuration I changed a configuration They were picked up fine I always delete the active config build folder as it is likely no longer valid.
1 parent 7e2dc82 commit 6265ede

File tree

5 files changed

+214
-44
lines changed

5 files changed

+214
-44
lines changed

io.sloeber.core/src/io/sloeber/core/Activator.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.eclipse.cdt.core.CCorePlugin;
1111
import org.eclipse.cdt.core.model.CoreModel;
1212
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
13+
import org.eclipse.core.resources.IResourceChangeListener;
1314
import org.eclipse.core.resources.ResourcesPlugin;
1415
import org.eclipse.core.runtime.CoreException;
1516
import org.eclipse.core.runtime.IPath;
@@ -35,6 +36,7 @@
3536
import io.sloeber.core.common.InstancePreferences;
3637
import io.sloeber.core.listeners.ConfigurationChangeListener;
3738
import io.sloeber.core.listeners.IndexerListener;
39+
import io.sloeber.core.listeners.resourceChangeListener;
3840
import io.sloeber.core.managers.InternalPackageManager;
3941

4042

@@ -63,6 +65,8 @@ public class Activator extends Plugin {
6365
'g', 'i', 'n', 'S', 't', 'a', 'r', 't', '.', 'h', 't', 'm', 'l', '?', 's', '=' };
6466
private static final String PLUGIN_ID = "io.sloeber.core";
6567

68+
private static IResourceChangeListener myResourceChangelistener = new resourceChangeListener();
69+
6670
@Override
6771
public void start(BundleContext context) throws Exception {
6872
instance = this;
@@ -182,6 +186,9 @@ private static void registerListeners() {
182186
CoreModel singCoreModel = CoreModel.getDefault();
183187
singCoreModel.addCProjectDescriptionListener(new ConfigurationChangeListener(),
184188
CProjectDescriptionEvent.ABOUT_TO_APPLY);
189+
190+
ResourcesPlugin.getWorkspace().addResourceChangeListener(myResourceChangelistener);
191+
185192
}
186193

187194
private static void initializeImportantVariables() {
@@ -251,7 +258,7 @@ protected IStatus run(IProgressMonitor monitor) {
251258
*/
252259
@Override
253260
public void stop(BundleContext context) throws Exception {
254-
261+
ResourcesPlugin.getWorkspace().removeResourceChangeListener(myResourceChangelistener);
255262
instance = null;
256263
super.stop(context);
257264
}

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

+119-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.sloeber.core.api;
22

33
import java.io.ByteArrayInputStream;
4+
import java.io.IOException;
45
import java.net.URI;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
58
import java.util.HashMap;
69
import java.util.Iterator;
710
import java.util.Map;
@@ -227,26 +230,26 @@ private HashMap<String, String> getEnvVars(ICConfigurationDescription confDesc)
227230
allVars.putAll(otherOptions.getEnvVars());
228231
}
229232
// set the paths
230-
String pathDelimiter = makeEnvironmentVar("PathDelimiter");
233+
String pathDelimiter = makeEnvironmentVar("PathDelimiter"); //$NON-NLS-1$
231234
if (Common.isWindows) {
232235
allVars.put(SLOEBER_MAKE_LOCATION,
233236
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$
236239
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$
240243
} 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$
243246
}
244247

245248
// Set the codeAnalyzer compile commands
246249
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$
248251
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$
250253

251254
return allVars;
252255
}
@@ -258,7 +261,15 @@ private void configure() {
258261
configure(prjCDesc, false);
259262
}
260263

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;
262273
if (isInMemory) {
263274
if (isDirty) {
264275
createSloeberConfigFiles(prjCDesc);
@@ -270,16 +281,16 @@ private void configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
270281
}
271282
if (prjDescWritable) {
272283
if (myNeedsSyncWithCDT) {
273-
syncWithCDT(prjCDesc, prjDescWritable);
284+
saveProjDesc = syncWithCDT(prjCDesc, prjDescWritable);
274285
}
275286
if (myNeedsClean) {
276287
cleanOldData(prjCDesc);
277288
}
278289
}
279-
return;
290+
return saveProjDesc;
280291
}
281292
// first read the sloeber files in memory
282-
readSloeberConfig(prjCDesc, prjDescWritable);
293+
saveProjDesc = readSloeberConfig(prjCDesc, prjDescWritable);
283294
if (myNeedToPersist || isDirty) {
284295
createSloeberConfigFiles(prjCDesc);
285296
isDirty = false;
@@ -291,20 +302,22 @@ private void configure(ICProjectDescription prjCDesc, boolean prjDescWritable) {
291302
cleanOldData(prjCDesc);
292303
}
293304
if (myNeedsSyncWithCDT) {
294-
syncWithCDT(prjCDesc, prjDescWritable);
305+
saveProjDesc = saveProjDesc || syncWithCDT(prjCDesc, prjDescWritable);
295306
}
296307
}
297308
setEnvironmentVariables(prjCDesc);
298309
isInMemory = true;
310+
return saveProjDesc;
299311
}
300312

301313
/**
302314
* 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
304316
*/
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);
307319
myNeedsSyncWithCDT = false;
320+
return ret;
308321
}
309322

310323
/**
@@ -338,7 +351,8 @@ private void cleanOldData(ICProjectDescription prjCDesc) {
338351
* @param confDesc
339352
* returns true if the config needs saving otherwise false
340353
*/
341-
private void readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDescWritable) {
354+
private boolean readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDescWritable) {
355+
boolean projDescNeedsWriting = false;
342356
IFile file = getConfigLocalFile();
343357
if (file.exists()) {
344358
myCfgFile = new TxtFile(file.getLocation().toFile());
@@ -362,6 +376,7 @@ private void readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDescWri
362376
if (prjDescWritable) {
363377
String id = CDataUtil.genId(null);
364378
try {
379+
projDescNeedsWriting = true;
365380
curConfDesc = prjCDesc.createConfiguration(id, curConfName,
366381
prjCDesc.getActiveConfiguration());
367382
} catch (Exception e) {
@@ -400,26 +415,69 @@ private void readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDescWri
400415

401416
}
402417
}
418+
return projDescNeedsWriting;
403419
}
404420

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+
*/
405432
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);
412462
}
413-
return projDescMustBeSaved || isRebuildNeeded;
463+
}
414464

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);
415470
} 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$
419476
}
420-
return false;
477+
return runnable.projConfMustBeSaved;
421478
}
422479

480+
423481
private void setEnvironmentVariables(final ICProjectDescription prjCDesc) {
424482
for (ICConfigurationDescription confDesc : prjCDesc.getConfigurations()) {
425483
setEnvVars(confDesc, getEnvVars(confDesc));
@@ -500,6 +558,17 @@ private static void storeConfigurationFile(IFile file, Map<String, String> vars)
500558
}
501559

502560
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+
}
503572
file.delete(true, null);
504573
}
505574

@@ -709,7 +778,26 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
709778

710779
configure(newProjDesc, true);
711780
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+
}
713801
}
714802

715803
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.sloeber.core.listeners;
2+
3+
import java.util.ArrayList;
4+
5+
import org.eclipse.core.resources.IProject;
6+
import org.eclipse.core.resources.IResourceChangeEvent;
7+
import org.eclipse.core.resources.IResourceChangeListener;
8+
import org.eclipse.core.resources.IResourceDelta;
9+
import org.eclipse.core.resources.IWorkspace;
10+
import org.eclipse.core.resources.IWorkspaceRoot;
11+
import org.eclipse.core.resources.ResourcesPlugin;
12+
import org.eclipse.core.runtime.CoreException;
13+
import org.eclipse.core.runtime.ICoreRunnable;
14+
import org.eclipse.core.runtime.IProgressMonitor;
15+
import org.eclipse.core.runtime.IStatus;
16+
import org.eclipse.core.runtime.Path;
17+
import org.eclipse.core.runtime.Status;
18+
import org.eclipse.core.runtime.jobs.Job;
19+
20+
import io.sloeber.core.api.SloeberProject;
21+
import io.sloeber.core.common.Common;
22+
23+
public class resourceChangeListener implements IResourceChangeListener {
24+
25+
@Override
26+
public void resourceChanged(IResourceChangeEvent event) {
27+
// we are only interested in POST_CHANGE events
28+
if (event.getType() != IResourceChangeEvent.POST_CHANGE)
29+
return;
30+
final ArrayList<IProject> changedSloeberCfgFiles = new ArrayList<>();
31+
IResourceDelta rootDelta = event.getDelta();
32+
for (IResourceDelta projectDelta : rootDelta.getAffectedChildren()) {
33+
IResourceDelta sloeberCfgDelta = projectDelta.findMember(new Path("sloeber.cfg"));
34+
if (sloeberCfgDelta != null)
35+
if (sloeberCfgDelta.getKind() != IResourceDelta.REMOVED) {
36+
changedSloeberCfgFiles.add(sloeberCfgDelta.getResource().getProject());
37+
}
38+
}
39+
40+
// no sloeber.cfg files have been modified
41+
if (changedSloeberCfgFiles.size() == 0)
42+
return;
43+
44+
Job job = new Job("Sloeber.cfg modification processor") {
45+
@Override
46+
protected IStatus run(IProgressMonitor monitor) {
47+
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
48+
IWorkspaceRoot root = workspace.getRoot();
49+
ICoreRunnable runnable = new ICoreRunnable() {
50+
51+
@Override
52+
public void run(IProgressMonitor monitor) throws CoreException {
53+
for (IProject curProject : changedSloeberCfgFiles) {
54+
SloeberProject curSloeberProject = SloeberProject.getSloeberProject(curProject, false);
55+
curSloeberProject.sloeberCfgChanged();
56+
}
57+
}
58+
};
59+
60+
try {
61+
workspace.run(runnable, root, IWorkspace.AVOID_UPDATE, null);
62+
} catch (Exception e) {
63+
Common.log(new Status(IStatus.INFO, io.sloeber.core.Activator.getId(),
64+
"failed to start sloeber.cfg updater", e)); //$NON-NLS-1$
65+
}
66+
67+
return Status.OK_STATUS;
68+
}
69+
};
70+
job.setPriority(Job.SHORT);
71+
job.schedule();
72+
73+
}
74+
75+
}

0 commit comments

Comments
 (0)