3
3
using System . Collections . Generic ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
+ using System . Text ;
6
7
using System . Text . RegularExpressions ;
7
8
8
9
namespace nanoFramework . IoT . Device . CodeConverter
@@ -175,29 +176,15 @@ static void Main(string[] args)
175
176
searches ,
176
177
oldProjectReferences ) ;
177
178
178
- // SOLUTION File
179
- if ( projectType == ProjectType . Regular )
180
- {
181
- CreateSolutionFile ( projectName , targetDirectoryInfo , projectGuid ) ;
182
- UpdateSolutionFile ( projectName , targetDirectoryInfo , projectGuid ) ;
183
- }
184
- else
185
- {
186
- // fill in project GUID
187
- UpdateProjectGuidInSolutionFile (
188
- targetDirectoryInfo ,
189
- projectType ,
190
- projectName ,
191
- projectGuid ) ;
192
- }
193
-
194
179
// NUSPEC File
195
180
if ( projectType == ProjectType . Regular )
196
181
{
197
182
CreateNuspecFile ( targetDirectoryInfo , projectName , targetDirectory ) ;
198
183
}
199
184
}
200
185
186
+ UpdateSolutionFiles ( outputDirectoryInfo ) ;
187
+
201
188
Console . WriteLine ( "Completed. Press any key to exit." ) ;
202
189
Console . ReadLine ( ) ;
203
190
}
@@ -215,7 +202,17 @@ private static void CreateNuspecFile(DirectoryInfo targetDirectoryInfo, string p
215
202
targetNuspecFile . MoveTo ( Path . Combine ( targetDirectory , $ "{ projectName } .nuspec") , true ) ;
216
203
}
217
204
}
218
-
205
+ public static Guid ToHashGuid ( string src )
206
+ {
207
+ byte [ ] bytes = Encoding . UTF8 . GetBytes ( src ) ;
208
+
209
+ using var sha256 = System . Security . Cryptography . SHA256 . Create ( ) ;
210
+ byte [ ] hashedBytes = sha256 . ComputeHash ( bytes ) ;
211
+
212
+ Array . Resize ( ref hashedBytes , 16 ) ;
213
+ return new Guid ( hashedBytes ) ;
214
+ }
215
+
219
216
private static void CreateProjectFile (
220
217
ProjectType projectType ,
221
218
string projectName ,
@@ -261,7 +258,7 @@ private static void CreateProjectFile(
261
258
}
262
259
263
260
// new GUID for project
264
- projectGuid = Guid . NewGuid ( ) . ToString ( "B" ) . ToUpper ( ) ;
261
+ projectGuid = ToHashGuid ( projectName ) . ToString ( "B" ) . ToUpper ( ) ;
265
262
projectReplacements . Add ( "<!-- NEW PROJECT GUID -->" , projectGuid ) ;
266
263
267
264
// Update project references
@@ -390,133 +387,19 @@ private static void CreatePackagesConfig(
390
387
}
391
388
}
392
389
393
- private static void CreateSolutionFile ( string projectName , DirectoryInfo targetDirectoryInfo , string projectGuid )
390
+ static void UpdateSolutionFiles ( DirectoryInfo outputDirectory )
394
391
{
395
- var solutionFileTemplate = @"
396
- Microsoft Visual Studio Solution File, Format Version 12.00
397
- # Visual Studio Version 16
398
- VisualStudioVersion = 16.0.30413.136
399
- MinimumVisualStudioVersion = 10.0.40219.1
400
- [[ INSERT PROJECTS HERE ]]
401
-
402
- Global
403
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
404
- Debug|Any CPU = Debug|Any CPU
405
- Release|Any CPU = Release|Any CPU
406
- EndGlobalSection
407
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
408
- [[ INSERT BUILD CONFIGURATIONS HERE ]]
409
- EndGlobalSection
410
- EndGlobal" ;
411
- var solutionProjectTemplate = $@ "Project(""{{11A8DD76-328B-46DF-9F39-F559912D0360}}"") = ""nanoFrameworkIoT"", ""nanoFrameworkIoT.nfproj"", ""{ projectGuid } ""
412
- EndProject" ;
413
- var solutionBuildConfigTemplate = $@ "{ projectGuid } .Debug|Any CPU.ActiveCfg = Debug|Any CPU
414
- { projectGuid } .Debug|Any CPU.Build.0 = Debug|Any CPU
415
- { projectGuid } .Debug|Any CPU.Deploy.0 = Debug|Any CPU
416
- { projectGuid } .Release|Any CPU.ActiveCfg = Release|Any CPU
417
- { projectGuid } .Release|Any CPU.Build.0 = Release|Any CPU
418
- { projectGuid } .Release|Any CPU.Deploy.0 = Release|Any CPU" ;
419
-
420
- var solutionProject = solutionProjectTemplate . Replace ( "nanoFrameworkIoT" , projectName ) ;
421
-
422
- // find out if there are sample projects
423
- if ( targetDirectoryInfo . GetDirectories ( "samples" ) . Count ( ) > 0 )
424
- {
425
- solutionProject += $@ "
426
- Project(""{{11A8DD76-328B-46DF-9F39-F559912D0360}}"") = ""nanoFrameworkIoT.Samples"", ""samples\nanoFrameworkIoT.Samples.nfproj"", ""{ _sampleProjectGuidReplacementToken } ""
427
- EndProject" ;
428
- }
429
- else
430
- {
431
- solutionProject += $@ "
432
- <!-- SAMPLES PROJECT PLACEHOLDER -->" ;
433
- }
434
-
435
- // find out if there are unit test projects
436
- if ( targetDirectoryInfo . GetDirectories ( "tests" ) . Count ( ) > 0 )
437
- {
438
- solutionProject += $@ "
439
- Project(""{{11A8DD76-328B-46DF-9F39-F559912D0360}}"") = ""nanoFrameworkIoT.Tests"", ""tests\nanoFrameworkIoT.Tests.nfproj"", ""{ _unitTestProjectGuidReplacementToken } ""
440
- EndProject" ;
441
- }
442
- else
443
- {
444
- solutionProject += $@ "
445
- <!-- UNIT TESTS PROJECT PLACEHOLDER -->" ;
446
- }
447
-
448
- var solutionFileContent = solutionFileTemplate . Replace ( "[[ INSERT PROJECTS HERE ]]" , solutionProject ) ;
449
- solutionFileContent = solutionFileContent . Replace ( "[[ INSERT BUILD CONFIGURATIONS HERE ]]" , solutionBuildConfigTemplate ) ;
450
- File . WriteAllText ( Path . Combine ( targetDirectoryInfo . FullName , $ "{ projectName } .sln") , solutionFileContent ) ;
451
- }
452
-
453
- private static void UpdateProjectGuidInSolutionFile (
454
- DirectoryInfo targetDirectoryInfo ,
455
- ProjectType projectType ,
456
- string projectName ,
457
- string projectGuid )
458
- {
459
- // find the parent solution file
460
- // it's OK to simplify because there will be only one SLN file there
461
- var slnFile = Directory . GetFiles ( targetDirectoryInfo . Parent . FullName , "*.sln" ) . FirstOrDefault ( ) ;
462
-
463
- if ( slnFile != null )
464
- {
465
- // load Solution file content
466
- string slnContent = File . ReadAllText ( slnFile ) ;
467
-
468
- // replace project GUID
469
- if ( projectType == ProjectType . Samples )
470
- {
471
- slnContent = slnContent . Replace ( _sampleProjectGuidReplacementToken , projectGuid ) ;
472
- }
473
- else if ( projectType == ProjectType . UnitTest )
474
- {
475
- slnContent = slnContent . Replace ( _unitTestProjectGuidReplacementToken , projectGuid ) ;
476
- }
477
-
478
- // add project, if not already there
479
-
480
- // find out if there are sample projects
481
-
482
- if ( projectType is ProjectType . Samples or ProjectType . UnitTest )
483
- {
484
- var token = projectType is ProjectType . Samples ? _sampleProjectPlaceholderToken : _unitTestProjectPlaceholderToken ;
485
-
486
- var projFileName = $ "{ projectName } .nfproj";
487
-
488
- try
489
- {
490
- var projectDirName = targetDirectoryInfo . GetFiles ( projFileName ) . Single ( ) . Directory ! . Name ;
491
-
492
- var slnLines = new [ ]
493
- {
494
- $@ "Project(""{{11A8DD76-328B-46DF-9F39-F559912D0360}}"") = ""{ projectName } "", ""{ projectDirName } \\{ projFileName } "", ""{ projectGuid } """,
495
- " EndProject",
496
- token , // leave token in the solution after replacement in case we need to add more projects
497
- } ;
498
-
499
- slnContent = slnContent . Replace ( token , string . Join ( Environment . NewLine , slnLines ) ) ;
500
- } catch ( Exception ) { }
501
- }
502
-
503
- File . WriteAllText ( slnFile , slnContent ) ;
504
- }
505
- }
506
-
507
- private static void UpdateSolutionFile ( string projectName , DirectoryInfo targetDirectoryInfo , string projectGuid )
508
- {
509
- var solutionFiles = targetDirectoryInfo . GetFiles ( "*.sln" ) ;
510
- foreach ( var solutionFile in solutionFiles )
392
+ foreach ( var solutionFile in outputDirectory . GetFiles ( "*.sln" , new EnumerationOptions { RecurseSubdirectories = true } ) )
511
393
{
512
394
solutionFile . EditFile ( new Dictionary < string , string >
513
395
{
514
- { " csproj", "nfproj" } ,
515
- { "nanoFrameworkIoT ", projectName }
396
+ { ". csproj", ". nfproj" } ,
397
+ { "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC ", "11A8DD76-328B-46DF-9F39-F559912D0360" } ,
516
398
} ) ;
517
399
}
518
400
}
519
401
402
+
520
403
private static T InitOptions < T > ( )
521
404
where T : new ( )
522
405
{
0 commit comments