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 ;
8
9
import java .util .HashMap ;
9
10
import java .util .Iterator ;
11
+ import java .util .List ;
10
12
import java .util .Map ;
11
13
import java .util .Map .Entry ;
12
14
import java .util .TreeMap ;
26
28
import org .eclipse .cdt .managedbuilder .core .ManagedBuilderCorePlugin ;
27
29
import org .eclipse .cdt .managedbuilder .core .ManagedCProjectNature ;
28
30
import org .eclipse .core .resources .IFile ;
29
- import org .eclipse .core .resources .IFolder ;
30
31
import org .eclipse .core .resources .IProject ;
31
32
import org .eclipse .core .resources .IProjectDescription ;
32
33
import org .eclipse .core .resources .IResource ;
@@ -85,6 +86,25 @@ private SloeberProject(IProject project) {
85
86
}
86
87
}
87
88
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
+
88
108
/**
89
109
* convenient method to create project
90
110
*
@@ -119,17 +139,29 @@ public static IProject createArduinoProject(String projectName, URI projectURI,
119
139
public void run (IProgressMonitor internalMonitor ) throws CoreException {
120
140
IProject newProjectHandle = root .getProject (realProjectName );
121
141
IndexerController .doNotIndex (newProjectHandle );
142
+
143
+
122
144
try {
123
145
IWorkspaceDescription workspaceDesc = workspace .getDescription ();
124
146
workspaceDesc .setAutoBuilding (false );
125
147
workspace .setDescription (workspaceDesc );
126
- IProjectType sloeberProjType = ManagedBuildManager .getProjectType ("io.sloeber.core.sketch" ); //$NON-NLS-1$
127
148
128
149
// create a eclipse project
129
150
IProjectDescription description = workspace .newProjectDescription (newProjectHandle .getName ());
130
151
if (projectURI != null ) {
131
152
description .setLocationURI (projectURI );
132
153
}
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 );
133
165
134
166
// make the eclipse project a cdt project
135
167
CCorePlugin .getDefault ().createCProject (description , newProjectHandle , new NullProgressMonitor (),
@@ -142,6 +174,7 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
142
174
ManagedCProjectNature .addNature (newProjectHandle , Const .ARDUINO_NATURE_ID , internalMonitor );
143
175
144
176
// make the cdt project a managed build project
177
+ IProjectType sloeberProjType = ManagedBuildManager .getProjectType ("io.sloeber.core.sketch" ); //$NON-NLS-1$
145
178
ManagedBuildManager .createBuildInfo (newProjectHandle );
146
179
IManagedProject newProject = ManagedBuildManager .createManagedProject (newProjectHandle ,
147
180
sloeberProjType );
@@ -161,28 +194,29 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
161
194
}
162
195
163
196
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 <>();
166
200
167
201
CCorePlugin cCorePlugin = CCorePlugin .getDefault ();
168
202
ICProjectDescription prjCDesc = cCorePlugin .getProjectDescription (newProjectHandle );
169
203
170
- SloeberProject arduinoProjDesc = new SloeberProject (newProjectHandle );
171
204
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 );
176
205
177
206
arduinoProjDesc .myCompileDescriptions .put (curConfigDesc .getId (), compileDescriptor );
178
207
arduinoProjDesc .myBoardDescriptions .put (curConfigDesc .getId (), boardDescriptor );
179
208
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 ());
180
215
181
- setEnvVars (curConfigDesc , arduinoProjDesc .getEnvVars (curConfigDesc ));
182
- Libraries .addLibrariesToProject (newProjectHandle , curConfigDesc , librariesToAdd );
183
216
}
184
217
185
- arduinoProjDesc .createSloeberConfigFiles (prjCDesc );
218
+
219
+ arduinoProjDesc .createSloeberConfigFiles (configs2 );
186
220
SubMonitor refreshMonitor = SubMonitor .convert (internalMonitor , 3 );
187
221
newProjectHandle .open (refreshMonitor );
188
222
newProjectHandle .refreshLocal (IResource .DEPTH_INFINITE , refreshMonitor );
@@ -208,17 +242,14 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
208
242
return root .getProject (realProjectName );
209
243
}
210
244
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 );
218
249
219
250
HashMap <String , String > allVars = new HashMap <>();
220
251
221
- allVars .put (ENV_KEY_BUILD_SOURCE_PATH , project .getLocation ().toOSString ());
252
+ allVars .put (ENV_KEY_BUILD_SOURCE_PATH , myProject .getLocation ().toOSString ());
222
253
223
254
if (boardDescription != null ) {
224
255
allVars .putAll (boardDescription .getEnvVars ());
@@ -269,15 +300,16 @@ private void configure() {
269
300
*/
270
301
271
302
private boolean configure (ICProjectDescription prjCDesc , boolean prjDescWritable ) {
303
+ Map <String , String > configs = getConfigs (prjCDesc );
272
304
boolean saveProjDesc = false ;
273
305
if (isInMemory ) {
274
306
if (isDirty ) {
275
- createSloeberConfigFiles (prjCDesc );
276
- setEnvironmentVariables (prjCDesc );
307
+ createSloeberConfigFiles (configs );
308
+ setEnvironmentVariables (configs . values () );
277
309
isDirty = false ;
278
310
}
279
311
if (myNeedToPersist ) {
280
- createSloeberConfigFiles (prjCDesc );
312
+ createSloeberConfigFiles (configs );
281
313
}
282
314
if (prjDescWritable ) {
283
315
if (myNeedsSyncWithCDT ) {
@@ -292,7 +324,7 @@ private boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable
292
324
// first read the sloeber files in memory
293
325
saveProjDesc = readSloeberConfig (prjCDesc , prjDescWritable );
294
326
if (myNeedToPersist || isDirty ) {
295
- createSloeberConfigFiles (prjCDesc );
327
+ createSloeberConfigFiles (configs );
296
328
isDirty = false ;
297
329
}
298
330
if (prjDescWritable ) {
@@ -305,11 +337,19 @@ private boolean configure(ICProjectDescription prjCDesc, boolean prjDescWritable
305
337
saveProjDesc = saveProjDesc || syncWithCDT (prjCDesc , prjDescWritable );
306
338
}
307
339
}
308
- setEnvironmentVariables (prjCDesc );
340
+ setEnvironmentVariables (configs . values () );
309
341
isInMemory = true ;
310
342
return saveProjDesc ;
311
343
}
312
344
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
+
313
353
/**
314
354
* sync the Sloeber configuration info with CDT Currently only Sloeber known
315
355
* configurations will be created by Sloeber inside CDT
@@ -426,18 +466,20 @@ private boolean readSloeberConfig(ICProjectDescription prjCDesc, boolean prjDesc
426
466
* @param confDesc
427
467
* a writable configuration setting to be made active
428
468
*
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
430
470
* saved
431
471
*/
432
472
private boolean setActiveConfig (ICConfigurationDescription confDesc ) {
433
473
434
474
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 ());
439
480
}
440
- return projConfMustBeSaved || isRebuildNeeded ;
481
+
482
+ return projConfMustBeSaved || includeFoldersRemoved ;
441
483
}
442
484
443
485
/**
@@ -477,10 +519,9 @@ public void run(IProgressMonitor internalMonitor) throws CoreException {
477
519
return runnable .projConfMustBeSaved ;
478
520
}
479
521
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 ));
484
525
}
485
526
}
486
527
@@ -496,14 +537,14 @@ private void setEnvironmentVariables(final ICProjectDescription prjCDesc) {
496
537
* @param project
497
538
* the project to store the data for
498
539
*/
499
- private void createSloeberConfigFiles (final ICProjectDescription prjCDesc ) {
540
+ private void createSloeberConfigFiles (Map < String , String > configs ) {
500
541
501
542
Map <String , String > configVars = new TreeMap <>();
502
543
Map <String , String > versionVars = new TreeMap <>();
503
544
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 ();
507
548
BoardDescription boardDescription = myBoardDescriptions .get (confID );
508
549
CompileDescription compileDescription = myCompileDescriptions .get (confID );
509
550
OtherDescription otherDescription = myOtherDescriptions .get (confID );
@@ -596,13 +637,16 @@ public void setBoardDescription(ICConfigurationDescription confDesc, BoardDescri
596
637
}
597
638
}
598
639
if (boardDescription .needsRebuild (oldBoardDescription )) {
599
- Helpers .deleteBuildFolder (myProject , confDesc );
640
+ Helpers .deleteBuildFolder (myProject , confDesc . getName () );
600
641
}
601
642
myBoardDescriptions .put (confDesc .getId (), boardDescription );
602
643
isDirty = true ;
603
644
}
604
645
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 );
606
650
IConfiguration configuration = ManagedBuildManager .getConfigurationForDescription (confDesc );
607
651
if (configuration != null ) {
608
652
SloeberConfigurationVariableSupplier varSup = (SloeberConfigurationVariableSupplier ) configuration
@@ -644,14 +688,10 @@ public static synchronized SloeberProject getSloeberProject(IProject project, bo
644
688
}
645
689
646
690
public void setCompileDescription (ICConfigurationDescription confDesc , CompileDescription compileDescription ) {
647
- IProject project = confDesc .getProjectDescription ().getProject ();
648
- IFolder buildFolder = project .getFolder (confDesc .getName ());
649
691
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 ());
655
695
}
656
696
myCompileDescriptions .put (confDesc .getId (), compileDescription );
657
697
isDirty = true ;
@@ -783,19 +823,22 @@ public void configChangeAboutToApply(ICProjectDescription newProjDesc, ICProject
783
823
784
824
}
785
825
826
+ /**
827
+ * Call this method when the sloeber.cfg file changed
828
+ *
829
+ */
786
830
public void sloeberCfgChanged () {
787
831
CCorePlugin cCorePlugin = CCorePlugin .getDefault ();
788
832
ICProjectDescription projDesc = cCorePlugin .getProjectDescription (myProject );
789
833
ICConfigurationDescription activeConfig = projDesc .getActiveConfiguration ();
790
834
isInMemory = false ;
791
835
boolean projDescNeedsSaving = configure (projDesc , true );
792
- Helpers .deleteBuildFolder (myProject , activeConfig );
836
+ Helpers .deleteBuildFolder (myProject , activeConfig . getName () );
793
837
projDescNeedsSaving = projDescNeedsSaving || setActiveConfig (activeConfig );
794
838
if (projDescNeedsSaving ) {
795
839
try {
796
840
cCorePlugin .setProjectDescription (myProject , projDesc );
797
841
} catch (CoreException e ) {
798
- // TODO Auto-generated catch block
799
842
e .printStackTrace ();
800
843
}
801
844
}
0 commit comments