Skip to content

Commit 1cd8956

Browse files
committed
Improve generator
- Now projects requiring Math NuGet packages are properly processed. - Remove PackageGenerator as it's not required anymore.
1 parent de19d4d commit 1cd8956

File tree

3 files changed

+115
-66
lines changed

3 files changed

+115
-66
lines changed

CodeGen/Generators/NanoFrameworkGen/PackageGenerator.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

CodeGen/Generators/NanoFrameworkGen/ProjectGenerator.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@ namespace CodeGen.Generators.NanoFrameworkGen
88
class ProjectGenerator: GeneratorBase
99
{
1010
private readonly Quantity _quantity;
11-
private readonly string _nanoFrameworkPath;
1211

13-
public ProjectGenerator(Quantity quantity, string nanoFrameworkPath)
12+
public ProjectGenerator(Quantity quantity)
1413
{
1514
_quantity = quantity ?? throw new ArgumentNullException(nameof(quantity));
16-
_nanoFrameworkPath = nanoFrameworkPath;
1715
}
1816

1917
public override string Generate()
2018
{
21-
Writer.W($@"<?xml version=""1.0"" encoding=""utf-8""?>
19+
Writer.WL($@"<?xml version=""1.0"" encoding=""utf-8""?>
2220
<Project ToolsVersion=""15.0"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
2321
<PropertyGroup Label=""Globals"">
24-
<NanoFrameworkProjectSystemPath>{_nanoFrameworkPath}</NanoFrameworkProjectSystemPath>
22+
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
2523
</PropertyGroup>
2624
<Import Project=""$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props"" Condition=""Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')"" />
2725
<PropertyGroup>
2826
<Configuration Condition="" '$(Configuration)' == '' "">Debug</Configuration>
2927
<Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>
3028
<ProjectTypeGuids>{{11A8DD76-328B-46DF-9F39-F559912D0360}};{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}</ProjectTypeGuids>
31-
<ProjectGuid>{Guid.NewGuid()}</ProjectGuid>
29+
<ProjectGuid>{{{Guid.NewGuid()}}}</ProjectGuid>
3230
<OutputType>Library</OutputType>
3331
<AppDesignerFolder>Properties</AppDesignerFolder>
3432
<FileAlignment>512</FileAlignment>
@@ -43,11 +41,23 @@ public override string Generate()
4341
<Compile Include=""..\Properties\AssemblyInfo.cs"" />
4442
</ItemGroup>
4543
<ItemGroup>
46-
<Reference Include=""mscorlib, Version=1.10.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731"">
47-
<HintPath>..\packages\nanoFramework.CoreLibrary.1.10.3-preview.7\lib\mscorlib.dll</HintPath>
44+
<Reference Include=""mscorlib, Version={NanoFrameworkGenerator.MscorlibVersion}, Culture=neutral, PublicKeyToken=c07d481e9758c731"">
45+
<HintPath>..\packages\nanoFramework.CoreLibrary.{NanoFrameworkGenerator.MscorlibNuGetVersion}\lib\mscorlib.dll</HintPath>
4846
<Private>True</Private>
4947
<SpecificVersion>True</SpecificVersion>
50-
</Reference>
48+
</Reference>");
49+
50+
if(NanoFrameworkGenerator.ProjectsRequiringMath.Contains(_quantity.Name))
51+
{
52+
Writer.WL($@"
53+
<Reference Include=""System.Math, Version={NanoFrameworkGenerator.MathVersion}, Culture=neutral, PublicKeyToken=c07d481e9758c731"">
54+
<HintPath>..\packages\nanoFramework.System.Math.{NanoFrameworkGenerator.MathNuGetVersion}\lib\System.Math.dll</HintPath>
55+
<Private>True</Private>
56+
<SpecificVersion>True</SpecificVersion>
57+
</Reference>");
58+
}
59+
60+
Writer.WL(@"
5161
</ItemGroup>
5262
<ItemGroup>
5363
<None Include=""packages.config"" />

CodeGen/Generators/NanoFrameworkGenerator.cs

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
using CodeGen.Generators.NanoFrameworkGen;
77
using CodeGen.JsonTypes;
88
using Serilog;
9+
using NuGet.Common;
10+
using NuGet.Protocol;
11+
using NuGet.Protocol.Core.Types;
12+
using NuGet.Versioning;
13+
using System.Threading;
14+
using CodeGen.Helpers;
15+
using System.Linq;
916

1017
namespace CodeGen.Generators
1118
{
@@ -17,6 +24,23 @@ internal static class NanoFrameworkGenerator
1724
{
1825
private const int AlignPad = 35;
1926

27+
static internal string MscorlibVersion = "";
28+
static internal string MscorlibNuGetVersion = "";
29+
static internal string MathVersion = "";
30+
static internal string MathNuGetVersion = "";
31+
32+
/// <summary>
33+
/// These projects require inclusion of Math NuGet package.
34+
/// </summary>
35+
internal static readonly List<string> ProjectsRequiringMath = new()
36+
{
37+
"Angle",
38+
"Frequency",
39+
"Pressure",
40+
"Turbidity",
41+
"WarpingMomentOfInertia"
42+
};
43+
2044
/// <summary>
2145
/// Create the root folder NanoFramewok
2246
/// Create all the quantities unit and quantities file
@@ -28,6 +52,46 @@ internal static class NanoFrameworkGenerator
2852
/// <param name="quantities">The quantities to create</param>
2953
public static void Generate(string rootDir, Quantity[] quantities)
3054
{
55+
// get latest version of .NET nanoFramework mscorlib
56+
NuGet.Common.ILogger logger = NullLogger.Instance;
57+
CancellationToken cancellationToken = CancellationToken.None;
58+
59+
SourceCacheContext cache = new SourceCacheContext();
60+
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
61+
FindPackageByIdResource resource = repository.GetResourceAsync<FindPackageByIdResource>().Result;
62+
63+
// mscorlib
64+
IEnumerable<NuGetVersion> packageVersions = resource.GetAllVersionsAsync(
65+
"nanoFramework.CoreLibrary",
66+
cache,
67+
logger,
68+
cancellationToken).Result;
69+
70+
// NuGet package Version
71+
// including preview
72+
var mscorlibPackage = packageVersions.Where(v => v.IsPrerelease).OrderByDescending(v => v.Version).First();
73+
// stable only
74+
//var mscorlibPackage = packageVersions.OrderByDescending(v => v.Version).First();
75+
76+
MscorlibVersion = mscorlibPackage.Version.ToString();
77+
MscorlibNuGetVersion = mscorlibPackage.ToNormalizedString();
78+
79+
// Math
80+
packageVersions = resource.GetAllVersionsAsync(
81+
"nanoFramework.System.Math",
82+
cache,
83+
logger,
84+
cancellationToken).Result;
85+
86+
// NuGet package Version
87+
// including preview
88+
var mathPackage = packageVersions.Where(v => v.IsPrerelease).OrderByDescending(v => v.Version).First();
89+
// stable only
90+
//var mathPackage = MathNuGetVersion = packageVersions.OrderByDescending(v => v.Version).First();
91+
92+
MathVersion = mathPackage.Version.ToString();
93+
MathNuGetVersion = mathPackage.ToNormalizedString();
94+
3195
var outputDir = Path.Combine(rootDir, "UnitsNet.NanoFramework", "GeneratedCode");
3296
var outputQuantitites = Path.Combine(outputDir, "Quantities");
3397
var outputUnits = Path.Combine(outputDir, "Units");
@@ -57,13 +121,14 @@ public static void Generate(string rootDir, Quantity[] quantities)
57121

58122
var projectPath = Path.Combine(outputDir, quantity.Name);
59123

60-
GeneratePackage(Path.Combine(projectPath, "packages.config"));
124+
GeneratePackage(projectPath, quantity.Name);
125+
61126
Log.Information($"Package(OK)");
62127

63128
var sb = new StringBuilder($"{quantity.Name}:".PadRight(AlignPad));
64129
GenerateUnitType(sb, quantity, Path.Combine(outputUnits, $"{quantity.Name}Unit.g.cs"));
65130
GenerateQuantity(sb, quantity, Path.Combine(outputQuantitites, $"{quantity.Name}.g.cs"));
66-
GenerateProject(sb, quantity, Path.Combine(projectPath, $"{quantity.Name}.nfproj"));
131+
GenerateProject(sb, quantity, projectPath);
67132
Log.Information(sb.ToString());
68133
numberQuantity++;
69134
}
@@ -73,9 +138,11 @@ public static void Generate(string rootDir, Quantity[] quantities)
73138
Log.Information($"Total quantities generated: {numberQuantity}");
74139
}
75140

76-
private static void GeneratePackage(string filePath)
141+
private static void GeneratePackage(string projectPath, string quantityName)
77142
{
78-
var content = new PackageGenerator().Generate();
143+
string filePath = Path.Combine(projectPath, "packages.config");
144+
145+
var content = Generate(quantityName);
79146
File.WriteAllText(filePath, content, Encoding.UTF8);
80147
}
81148

@@ -105,9 +172,11 @@ private static void GenerateQuantity(StringBuilder sb, Quantity quantity, string
105172
sb.Append("quantity(OK) ");
106173
}
107174

108-
private static void GenerateProject(StringBuilder sb, Quantity quantity, string filePath)
175+
private static void GenerateProject(StringBuilder sb, Quantity quantity, string projectPath)
109176
{
110-
var content = new ProjectGenerator(quantity, @"$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\").Generate();
177+
var filePath = Path.Combine(projectPath, $"{quantity.Name}.nfproj");
178+
179+
var content = new ProjectGenerator(quantity).Generate();
111180
File.WriteAllText(filePath, content, Encoding.UTF8);
112181
sb.Append("project(OK) ");
113182
}
@@ -120,5 +189,26 @@ private static void GenerateSolution(Quantity[] quantities, string outputDir)
120189

121190
File.WriteAllText(filePath, content, Encoding.UTF8);
122191
}
192+
193+
private static string Generate(string quantityName)
194+
{
195+
MyTextWriter Writer = new MyTextWriter();
196+
197+
Writer.WL($@"
198+
<?xml version=""1.0"" encoding=""utf-8""?>
199+
<packages>
200+
<package id=""nanoFramework.CoreLibrary"" version=""{MscorlibNuGetVersion}"" targetFramework=""netnanoframework10"" />");
201+
202+
203+
if (NanoFrameworkGenerator.ProjectsRequiringMath.Contains(quantityName))
204+
{
205+
Writer.WL($@"
206+
<package id=""nanoFramework.System.Math"" version=""{MathNuGetVersion}"" targetFramework=""netnanoframework10"" />");
207+
}
208+
209+
Writer.WL($@"</packages>");
210+
211+
return Writer.ToString();
212+
}
123213
}
124214
}

0 commit comments

Comments
 (0)