5
5
import java .util .HashMap ;
6
6
import java .util .Map ;
7
7
import java .util .Map .Entry ;
8
+ import java .util .TreeMap ;
8
9
9
10
import org .eclipse .cdt .core .CCorePlugin ;
10
11
import org .eclipse .cdt .core .envvar .IContributedEnvironment ;
@@ -52,7 +53,7 @@ public class SloeberProjectDescription extends Common {
52
53
"Sloeber_Project_Description" ); //$NON-NLS-1$
53
54
private Map <String , BoardDescription > myBoardDescriptions = new HashMap <>();
54
55
private Map <String , CompileDescription > myCompileDescriptions = new HashMap <>();
55
-
56
+
56
57
private static final String ENV_KEY_BUILD_SOURCE_PATH = ERASE_START + "build.source.path" ; //$NON-NLS-1$
57
58
private static final String ENV_KEY_BUILD_GENERIC_PATH = ERASE_START + "build.generic.path" ; //$NON-NLS-1$
58
59
private static final String ENV_KEY_COMPILER_PATH = ERASE_START + "compiler.path" ; //$NON-NLS-1$
@@ -180,7 +181,7 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
180
181
181
182
private void saveConfig (ICConfigurationDescription confDesc ) {
182
183
183
- createSloeberConfigFile (confDesc );
184
+ createSloeberConfigFiles (confDesc );
184
185
setEnvVars (confDesc , getEnvVars (confDesc ), true );
185
186
186
187
}
@@ -241,33 +242,63 @@ private void readSloeberConfigFile(ICConfigurationDescription confDesc) {
241
242
242
243
}
243
244
244
- private void createSloeberConfigFile (ICConfigurationDescription confDesc ) {
245
+ private static IFile getConfigLocalFile (ICConfigurationDescription confDesc ) {
246
+ IProject project = confDesc .getProjectDescription ().getProject ();
247
+ return project .getFile (".sloeber." + confDesc .getName () + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
248
+ }
249
+
250
+ private static IFile getConfigVersionFile (ICConfigurationDescription confDesc ) {
251
+ IProject project = confDesc .getProjectDescription ().getProject ();
252
+ return project .getFile ("sloeber." + confDesc .getName () + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
253
+ }
254
+
255
+ /**
256
+ * This methods creates/updates 2 files in the workspace. Together these files
257
+ * contain the Sloeber project configuration info The info is split into 2 files
258
+ * because you probably do not want to add all the info to a version control
259
+ * tool.
260
+ *
261
+ * sloeber.[config name].txt is the file you can add to a version control
262
+ * .sloeber.[config name].txt is the file with settings you do not want to add
263
+ * to version control
264
+ *
265
+ * @param confDesc
266
+ */
267
+ private void createSloeberConfigFiles (ICConfigurationDescription confDesc ) {
245
268
IProject project = confDesc .getProjectDescription ().getProject ();
246
269
BoardDescription boardDescription = getBoardDescription (confDesc );
247
270
CompileDescription compileDescription = getCompileDescription (confDesc );
248
- Map <String , String > configVars = new HashMap <>();
249
- if (boardDescription != null ) {
250
- configVars = boardDescription .getEnvVarsConfig ();
251
- }
252
- if (compileDescription == null ) {
253
- compileDescription = new CompileDescription ();
254
- }
271
+ Map <String , String > configVars = new TreeMap <>();
272
+ configVars .putAll (boardDescription .getEnvVarsConfig ());
255
273
configVars .putAll (compileDescription .getEnvVarsConfig ());
256
- String newFileContent = EMPTY ;
274
+
275
+ String versionFileContent = EMPTY ;
276
+ String localFileContent = EMPTY ;
257
277
for (Entry <String , String > curLine : configVars .entrySet ()) {
258
- newFileContent += curLine .getKey () + '=' + curLine .getValue () + '\n' ;
278
+ // TODO add filter to seperate local file info from
279
+ // version file info
280
+ versionFileContent += curLine .getKey () + '=' + curLine .getValue () + '\n' ;
259
281
}
260
- IFile file = project .getFile ("sloeber." + confDesc .getName () + ".txt" ); //$NON-NLS-1$ //$NON-NLS-2$
282
+ IFile versionFile = getConfigVersionFile (confDesc );
283
+ IFile localFile = getConfigLocalFile (confDesc );
261
284
try {
262
- file .refreshLocal (IResource .DEPTH_INFINITE , null );
263
- if (file .exists ()) {
264
- file .delete (true , null );
265
- file .refreshLocal (IResource .DEPTH_INFINITE , null );
285
+ project .refreshLocal (IResource .DEPTH_INFINITE , null );
286
+ if (versionFile .exists ()) {
287
+ versionFile .delete (true , null );
266
288
}
267
289
268
- if (!file .exists () && (!newFileContent .isBlank ())) {
269
- ByteArrayInputStream stream = new ByteArrayInputStream (newFileContent .getBytes ());
270
- file .create (stream , true , null );
290
+ if (!versionFile .exists () && (!versionFileContent .isBlank ())) {
291
+ ByteArrayInputStream stream = new ByteArrayInputStream (versionFileContent .getBytes ());
292
+ versionFile .create (stream , true , null );
293
+ }
294
+
295
+ if (localFile .exists ()) {
296
+ localFile .delete (true , null );
297
+ }
298
+
299
+ if (!localFile .exists () && (!localFileContent .isBlank ())) {
300
+ ByteArrayInputStream stream = new ByteArrayInputStream (localFileContent .getBytes ());
301
+ localFile .create (stream , true , null );
271
302
}
272
303
} catch (CoreException e ) {
273
304
// TODO Auto-generated catch block
@@ -403,11 +434,26 @@ public void setCompileDescription(ICConfigurationDescription confDesc, CompileDe
403
434
}
404
435
}
405
436
437
+ /**
438
+ * Method that tries to give you the boardDescription settings for this
439
+ * project/configuration This method tries folowing things 1)memory (after 2 or
440
+ * 3) 2)configuration files in the project (at project creation) 3)CDT
441
+ * environment variables (to update projects created by previous versions of
442
+ * Sloeber)
443
+ *
444
+ * @param confDesc
445
+ * @return
446
+ */
406
447
public BoardDescription getBoardDescription (ICConfigurationDescription confDesc ) {
407
448
BoardDescription ret = myBoardDescriptions .get (confDesc .getId ());
408
449
if (ret == null ) {
409
- readSloeberConfigFile (confDesc );
410
- ret = myBoardDescriptions .get (confDesc .getId ());
450
+ IFile file = getConfigVersionFile (confDesc );
451
+ if (file .exists ()) {
452
+ readSloeberConfigFile (confDesc );
453
+ ret = myBoardDescriptions .get (confDesc .getId ());
454
+ } else {
455
+ ret = BoardDescription .getFromCDTEnvVars ();
456
+ }
411
457
}
412
458
return ret ;
413
459
}
@@ -423,16 +469,21 @@ private void putCompileDescription(ICConfigurationDescription confDesc, CompileD
423
469
public CompileDescription getCompileDescription (ICConfigurationDescription confDesc ) {
424
470
CompileDescription ret = myCompileDescriptions .get (confDesc .getId ());
425
471
if (ret == null ) {
426
- readSloeberConfigFile (confDesc );
427
- ret = myCompileDescriptions .get (confDesc .getId ());
472
+ IFile file = getConfigVersionFile (confDesc );
473
+ if (file .exists ()) {
474
+ readSloeberConfigFile (confDesc );
475
+ ret = myCompileDescriptions .get (confDesc .getId ());
476
+ } else {
477
+ ret = CompileDescription .getFromCDTEnvVars ();
478
+ }
428
479
}
429
480
return ret ;
430
481
}
431
482
432
483
public String getDecoratedText (ICConfigurationDescription confDesc , String text ) {
433
484
BoardDescription boardDescriptor = getBoardDescription (confDesc );
434
485
if (boardDescriptor == null ) {
435
- return text + " Project needs to be configured" ;
486
+ return text + " Project not configured" ; //$NON-NLS-1$
436
487
}
437
488
String boardName = boardDescriptor .getBoardName ();
438
489
String portName = boardDescriptor .getActualUploadPort ();
0 commit comments