Skip to content

Commit d8e7969

Browse files
committed
nanoFramework: Deterministic guids for solution and projects
1 parent 80ae6a6 commit d8e7969

File tree

107 files changed

+874
-855
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+874
-855
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
using System;
5+
using System.Text;
6+
7+
namespace CodeGen.Generators.NanoFrameworkGen
8+
{
9+
/// <summary>
10+
/// Helper methods for consistently getting the same guid for a given string.
11+
/// </summary>
12+
public class HashGuid
13+
{
14+
/// <summary>
15+
/// Returns a guid based on the SHA256 hash of the string,
16+
/// so identical strings will always give the same guid.
17+
/// </summary>
18+
/// <param name="src">A string.</param>
19+
/// <returns>A guid based on the hash of the string.</returns>
20+
public static Guid ToHashGuid(string src)
21+
{
22+
byte[] bytes = Encoding.UTF8.GetBytes(src);
23+
24+
using var sha256 = System.Security.Cryptography.SHA256.Create();
25+
byte[] hashedBytes = sha256.ComputeHash(bytes);
26+
27+
Array.Resize(ref hashedBytes, 16);
28+
return new Guid(hashedBytes);
29+
}
30+
}
31+
}

CodeGen/Generators/NanoFrameworkGen/ProjectGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override string Generate()
2626
<Configuration Condition="" '$(Configuration)' == '' "">Debug</Configuration>
2727
<Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>
2828
<ProjectTypeGuids>{{11A8DD76-328B-46DF-9F39-F559912D0360}};{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}</ProjectTypeGuids>
29-
<ProjectGuid>{{{Guid.NewGuid()}}}</ProjectGuid>
29+
<ProjectGuid>{HashGuid.ToHashGuid(_quantity.Name):B}</ProjectGuid>
3030
<OutputType>Library</OutputType>
3131
<AppDesignerFolder>Properties</AppDesignerFolder>
3232
<FileAlignment>512</FileAlignment>

CodeGen/Generators/NanoFrameworkGen/SolutionGenerator.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Data.Common;
4-
using System.IO;
52
using System.Text;
6-
using System.Text.RegularExpressions;
73
using CodeGen.JsonTypes;
84

95
namespace CodeGen.Generators.NanoFrameworkGen
106
{
117
class SolutionGenerator:GeneratorBase
128
{
139
private readonly Quantity[] _quantities;
14-
private readonly string _outputDir;
15-
private readonly string _globalGuid;
10+
private readonly Guid _globalGuid = new("d608a2b1-6ead-4383-a205-ad1ce69d9ef7");
11+
private readonly Guid _solutionGuid = new("43971d92-3663-4f28-82ac-e63ce06ba1a3");
1612

17-
public SolutionGenerator(Quantity[] quantities, string outputDir)
13+
public SolutionGenerator(Quantity[] quantities)
1814
{
1915
_quantities = quantities;
20-
_outputDir = outputDir;
21-
_globalGuid = Guid.NewGuid().ToString();
2216
}
2317

2418
public override string Generate()
2519
{
26-
StringBuilder sb = new StringBuilder();
20+
StringBuilder sb = new();
2721
Writer.WL($@"Microsoft Visual Studio Solution File, Format Version 12.00
2822
# Visual Studio Version 16
2923
VisualStudioVersion = 16.0.30413.136
@@ -37,23 +31,16 @@ public override string Generate()
3731
continue;
3832
}
3933

40-
// need to grab the project GUID from the project file
41-
using var projectFileReader = new StreamReader(Path.Combine(_outputDir, quantity.Name) + $"\\{quantity.Name}.nfproj");
42-
string projectFileContent = projectFileReader.ReadToEnd();
43-
44-
var pattern = @"(?<=<ProjectGuid>)(.*)(?=<)";
45-
var projectGuid = Regex.Matches(projectFileContent, pattern, RegexOptions.IgnoreCase);
46-
47-
var guid = projectGuid[0];
34+
var projectGuid = HashGuid.ToHashGuid(quantity.Name);
4835
Writer.WL($@"
49-
Project(""{{{_globalGuid}}}"") = ""{quantity.Name}"", ""{quantity.Name}\{quantity.Name}.nfproj"", ""{{{guid}}}""
36+
Project(""{_globalGuid:B}"") = ""{quantity.Name}"", ""UnitsNet.NanoFramework\GeneratedCode\{quantity.Name}\{quantity.Name}.nfproj"", ""{projectGuid:B}""
5037
EndProject");
51-
sb.Append($"{{{guid}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\r\n");
52-
sb.Append($"{{{guid}}}.Debug|Any CPU.Build.0 = Debug|Any CPU\r\n");
53-
sb.Append($"{{{guid}}}.Debug|Any CPU.Deploy.0 = Debug|Any CPU\r\n");
54-
sb.Append($"{{{guid}}}.Release|Any CPU.ActiveCfg = Release|Any CPU\r\n");
55-
sb.Append($"{{{guid}}}.Release|Any CPU.Build.0 = Release|Any CPU\r\n");
56-
sb.Append($"{{{guid}}}.Release|Any CPU.Deploy.0 = Release|Any CPU\r\n");
38+
sb.Append($"{{{projectGuid}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\r\n");
39+
sb.Append($"{{{projectGuid}}}.Debug|Any CPU.Build.0 = Debug|Any CPU\r\n");
40+
sb.Append($"{{{projectGuid}}}.Debug|Any CPU.Deploy.0 = Debug|Any CPU\r\n");
41+
sb.Append($"{{{projectGuid}}}.Release|Any CPU.ActiveCfg = Release|Any CPU\r\n");
42+
sb.Append($"{{{projectGuid}}}.Release|Any CPU.Build.0 = Release|Any CPU\r\n");
43+
sb.Append($"{{{projectGuid}}}.Release|Any CPU.Deploy.0 = Release|Any CPU\r\n");
5744
}
5845

5946
Writer.WL(@"Global
@@ -63,14 +50,14 @@ public override string Generate()
6350
EndGlobalSection
6451
GlobalSection(ProjectConfigurationPlatforms) = postSolution");
6552

66-
Writer.WL(sb.ToString());
53+
Writer.WL(sb.ToString());
6754

6855
Writer.WL($@" EndGlobalSection
6956
GlobalSection(SolutionProperties) = preSolution
7057
HideSolutionNode = FALSE
7158
EndGlobalSection
7259
GlobalSection(ExtensibilityGlobals) = postSolution
73-
SolutionGuid = {{{Guid.NewGuid()}}}
60+
SolutionGuid = {_solutionGuid:B}
7461
EndGlobalSection
7562
EndGlobal
7663
");

UnitsNet.NanoFramework/GeneratedCode/Acceleration/Acceleration.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{3985716e-fa45-4c63-92a7-d656cc46d421}</ProjectGuid>
11+
<ProjectGuid>{8e3a6691-a05f-d71c-f12a-f537c0807f85}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

UnitsNet.NanoFramework/GeneratedCode/AmountOfSubstance/AmountOfSubstance.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{c05d49d4-d8f8-4934-8c84-f3a904aba4d8}</ProjectGuid>
11+
<ProjectGuid>{1cd282f3-3bdc-bc00-a63a-03a11a9b5c4d}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

UnitsNet.NanoFramework/GeneratedCode/AmplitudeRatio/AmplitudeRatio.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{b4008149-1787-4e08-954d-de146766254e}</ProjectGuid>
11+
<ProjectGuid>{59c09a1b-eff0-e367-6372-d16c4c984916}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

UnitsNet.NanoFramework/GeneratedCode/Angle/Angle.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{5cb327bf-25f8-47ae-9a3c-39d5ea939548}</ProjectGuid>
11+
<ProjectGuid>{4b036819-f813-0c4d-3e44-1fcc8b5f3ec7}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

UnitsNet.NanoFramework/GeneratedCode/ApparentEnergy/ApparentEnergy.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{5b80f5f3-c6b8-4380-b4da-0fd28f85773f}</ProjectGuid>
11+
<ProjectGuid>{6f80e841-e953-bc39-6670-8711f2ad9d18}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

UnitsNet.NanoFramework/GeneratedCode/ApparentPower/ApparentPower.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{8b2bc4a0-b11b-4102-a3fc-bf87c44dbcc6}</ProjectGuid>
11+
<ProjectGuid>{88b9a61a-b21b-97cc-ef89-78d1e3ff7767}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

UnitsNet.NanoFramework/GeneratedCode/Area/Area.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
99
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
1010
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11-
<ProjectGuid>{c3abab40-5194-4b8b-943d-7d75b15e1217}</ProjectGuid>
11+
<ProjectGuid>{04c24d02-bad7-a877-ff9b-a36a886c9633}</ProjectGuid>
1212
<OutputType>Library</OutputType>
1313
<AppDesignerFolder>Properties</AppDesignerFolder>
1414
<FileAlignment>512</FileAlignment>

0 commit comments

Comments
 (0)