6
6
using CodeGen . Generators . NanoFrameworkGen ;
7
7
using CodeGen . JsonTypes ;
8
8
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 ;
9
16
10
17
namespace CodeGen . Generators
11
18
{
@@ -17,6 +24,23 @@ internal static class NanoFrameworkGenerator
17
24
{
18
25
private const int AlignPad = 35 ;
19
26
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
+
20
44
/// <summary>
21
45
/// Create the root folder NanoFramewok
22
46
/// Create all the quantities unit and quantities file
@@ -28,6 +52,46 @@ internal static class NanoFrameworkGenerator
28
52
/// <param name="quantities">The quantities to create</param>
29
53
public static void Generate ( string rootDir , Quantity [ ] quantities )
30
54
{
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
+
31
95
var outputDir = Path . Combine ( rootDir , "UnitsNet.NanoFramework" , "GeneratedCode" ) ;
32
96
var outputQuantitites = Path . Combine ( outputDir , "Quantities" ) ;
33
97
var outputUnits = Path . Combine ( outputDir , "Units" ) ;
@@ -57,13 +121,14 @@ public static void Generate(string rootDir, Quantity[] quantities)
57
121
58
122
var projectPath = Path . Combine ( outputDir , quantity . Name ) ;
59
123
60
- GeneratePackage ( Path . Combine ( projectPath , "packages.config" ) ) ;
124
+ GeneratePackage ( projectPath , quantity . Name ) ;
125
+
61
126
Log . Information ( $ "Package(OK)") ;
62
127
63
128
var sb = new StringBuilder ( $ "{ quantity . Name } :". PadRight ( AlignPad ) ) ;
64
129
GenerateUnitType ( sb , quantity , Path . Combine ( outputUnits , $ "{ quantity . Name } Unit.g.cs") ) ;
65
130
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 ) ;
67
132
Log . Information ( sb . ToString ( ) ) ;
68
133
numberQuantity ++ ;
69
134
}
@@ -73,9 +138,11 @@ public static void Generate(string rootDir, Quantity[] quantities)
73
138
Log . Information ( $ "Total quantities generated: { numberQuantity } ") ;
74
139
}
75
140
76
- private static void GeneratePackage ( string filePath )
141
+ private static void GeneratePackage ( string projectPath , string quantityName )
77
142
{
78
- var content = new PackageGenerator ( ) . Generate ( ) ;
143
+ string filePath = Path . Combine ( projectPath , "packages.config" ) ;
144
+
145
+ var content = Generate ( quantityName ) ;
79
146
File . WriteAllText ( filePath , content , Encoding . UTF8 ) ;
80
147
}
81
148
@@ -105,9 +172,11 @@ private static void GenerateQuantity(StringBuilder sb, Quantity quantity, string
105
172
sb . Append ( "quantity(OK) " ) ;
106
173
}
107
174
108
- private static void GenerateProject ( StringBuilder sb , Quantity quantity , string filePath )
175
+ private static void GenerateProject ( StringBuilder sb , Quantity quantity , string projectPath )
109
176
{
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 ( ) ;
111
180
File . WriteAllText ( filePath , content , Encoding . UTF8 ) ;
112
181
sb . Append ( "project(OK) " ) ;
113
182
}
@@ -120,5 +189,26 @@ private static void GenerateSolution(Quantity[] quantities, string outputDir)
120
189
121
190
File . WriteAllText ( filePath , content , Encoding . UTF8 ) ;
122
191
}
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
+ }
123
213
}
124
214
}
0 commit comments