Skip to content

Commit 9a8013f

Browse files
authored
changed project guid to be deterministic; simplified solution file edits (#33)
1 parent 3f7f822 commit 9a8013f

File tree

1 file changed

+20
-137
lines changed
  • src/nanoFramework.IoT.Device.CodeConverter

1 file changed

+20
-137
lines changed

src/nanoFramework.IoT.Device.CodeConverter/Program.cs

Lines changed: 20 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using System.Text;
67
using System.Text.RegularExpressions;
78

89
namespace nanoFramework.IoT.Device.CodeConverter
@@ -175,29 +176,15 @@ static void Main(string[] args)
175176
searches,
176177
oldProjectReferences);
177178

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-
194179
// NUSPEC File
195180
if (projectType == ProjectType.Regular)
196181
{
197182
CreateNuspecFile(targetDirectoryInfo, projectName, targetDirectory);
198183
}
199184
}
200185

186+
UpdateSolutionFiles(outputDirectoryInfo);
187+
201188
Console.WriteLine("Completed. Press any key to exit.");
202189
Console.ReadLine();
203190
}
@@ -215,7 +202,17 @@ private static void CreateNuspecFile(DirectoryInfo targetDirectoryInfo, string p
215202
targetNuspecFile.MoveTo(Path.Combine(targetDirectory, $"{projectName}.nuspec"), true);
216203
}
217204
}
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+
219216
private static void CreateProjectFile(
220217
ProjectType projectType,
221218
string projectName,
@@ -261,7 +258,7 @@ private static void CreateProjectFile(
261258
}
262259

263260
// new GUID for project
264-
projectGuid = Guid.NewGuid().ToString("B").ToUpper();
261+
projectGuid = ToHashGuid(projectName).ToString("B").ToUpper();
265262
projectReplacements.Add("<!-- NEW PROJECT GUID -->", projectGuid);
266263

267264
// Update project references
@@ -390,133 +387,19 @@ private static void CreatePackagesConfig(
390387
}
391388
}
392389

393-
private static void CreateSolutionFile(string projectName, DirectoryInfo targetDirectoryInfo, string projectGuid)
390+
static void UpdateSolutionFiles(DirectoryInfo outputDirectory)
394391
{
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 }))
511393
{
512394
solutionFile.EditFile(new Dictionary<string, string>
513395
{
514-
{"csproj", "nfproj" },
515-
{"nanoFrameworkIoT", projectName }
396+
{ ".csproj", ".nfproj" },
397+
{ "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC", "11A8DD76-328B-46DF-9F39-F559912D0360" },
516398
});
517399
}
518400
}
519401

402+
520403
private static T InitOptions<T>()
521404
where T : new()
522405
{

0 commit comments

Comments
 (0)