1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . IO ;
3
4
using System . Text ;
4
5
using CodeGen . Generators . NanoFrameworkGen ;
11
12
using System . Threading ;
12
13
using CodeGen . Helpers ;
13
14
using System . Linq ;
15
+ using System . Text . RegularExpressions ;
16
+ using ILogger = NuGet . Common . ILogger ;
14
17
15
18
namespace CodeGen . Generators
16
19
{
@@ -48,7 +51,8 @@ internal static class NanoFrameworkGenerator
48
51
/// </summary>
49
52
/// <param name="rootDir">The root directory</param>
50
53
/// <param name="quantities">The quantities to create</param>
51
- public static void Generate ( string rootDir , Quantity [ ] quantities )
54
+ /// <param name="updateNanoFrameworkDependencies">Update nanoFramework nuget dependencies?</param>
55
+ public static void Generate ( string rootDir , Quantity [ ] quantities , bool updateNanoFrameworkDependencies )
52
56
{
53
57
// get latest version of .NET nanoFramework mscorlib
54
58
NuGet . Common . ILogger logger = NullLogger . Instance ;
@@ -58,38 +62,6 @@ public static void Generate(string rootDir, Quantity[] quantities)
58
62
SourceRepository repository = Repository . Factory . GetCoreV3 ( "https://api.nuget.org/v3/index.json" ) ;
59
63
FindPackageByIdResource resource = repository . GetResourceAsync < FindPackageByIdResource > ( ) . Result ;
60
64
61
- // mscorlib
62
- IEnumerable < NuGetVersion > packageVersions = resource . GetAllVersionsAsync (
63
- "nanoFramework.CoreLibrary" ,
64
- cache ,
65
- logger ,
66
- cancellationToken ) . Result ;
67
-
68
- // NuGet package Version
69
- // including preview
70
- var mscorlibPackage = packageVersions . Where ( v => v . IsPrerelease ) . OrderByDescending ( v => v ) . First ( ) ;
71
- // stable only
72
- //var mscorlibPackage = packageVersions.OrderByDescending(v => v).First();
73
-
74
- MscorlibVersion = mscorlibPackage . Version . ToString ( ) ;
75
- MscorlibNuGetVersion = mscorlibPackage . ToNormalizedString ( ) ;
76
-
77
- // Math
78
- packageVersions = resource . GetAllVersionsAsync (
79
- "nanoFramework.System.Math" ,
80
- cache ,
81
- logger ,
82
- cancellationToken ) . Result ;
83
-
84
- // NuGet package Version
85
- // including preview
86
- var mathPackage = packageVersions . Where ( v => v . IsPrerelease ) . OrderByDescending ( v => v ) . First ( ) ;
87
- // stable only
88
- //var mathPackage = MathNuGetVersion = packageVersions.OrderByDescending(v => v).First();
89
-
90
- MathVersion = mathPackage . Version . ToString ( ) ;
91
- MathNuGetVersion = mathPackage . ToNormalizedString ( ) ;
92
-
93
65
var outputDir = Path . Combine ( rootDir , "UnitsNet.NanoFramework" , "GeneratedCode" ) ;
94
66
var outputQuantitites = Path . Combine ( outputDir , "Quantities" ) ;
95
67
var outputUnits = Path . Combine ( outputDir , "Units" ) ;
@@ -102,6 +74,7 @@ public static void Generate(string rootDir, Quantity[] quantities)
102
74
103
75
Log . Information ( $ "Directory NanoFramework creation(OK)") ;
104
76
77
+ SetDependencyVersions ( resource , cache , logger , cancellationToken , updateNanoFrameworkDependencies , outputDir ) ;
105
78
GenerateProperties ( Path . Combine ( outputProperties , "AssemblyInfo.cs" ) ) ;
106
79
Log . Information ( $ "Property(OK)") ;
107
80
@@ -124,8 +97,7 @@ public static void Generate(string rootDir, Quantity[] quantities)
124
97
125
98
GenerateUnitType ( sb , quantity , Path . Combine ( outputUnits , $ "{ quantity . Name } Unit.g.cs") ) ;
126
99
GenerateQuantity ( sb , quantity , Path . Combine ( outputQuantitites , $ "{ quantity . Name } .g.cs") ) ;
127
- GenerateProject ( sb , quantity , projectPath ) ;
128
-
100
+ GenerateProject ( sb , quantity , Path . Combine ( projectPath , $ "{ quantity . Name } .nfproj") ) ;
129
101
130
102
// Convert decimal based units to floats; decimals are not supported by nanoFramework
131
103
if ( quantity . BaseType == "decimal" )
@@ -150,6 +122,81 @@ public static void Generate(string rootDir, Quantity[] quantities)
150
122
Log . Information ( $ "Count of generated projects: { numberQuantity } ") ;
151
123
}
152
124
125
+ private static void SetDependencyVersions ( FindPackageByIdResource resource , SourceCacheContext cache , ILogger logger ,
126
+ CancellationToken cancellationToken , bool updateNanoFrameworkDependencies , string outputDir )
127
+ {
128
+ if ( updateNanoFrameworkDependencies )
129
+ {
130
+ logger . LogInformation ( "Updating nanoFramework dependencies." ) ;
131
+
132
+ // mscorlib
133
+ IEnumerable < NuGetVersion > packageVersions = resource . GetAllVersionsAsync (
134
+ "nanoFramework.CoreLibrary" ,
135
+ cache ,
136
+ logger ,
137
+ cancellationToken ) . Result ;
138
+
139
+ // NuGet package Version
140
+ // including preview
141
+ var mscorlibPackage = packageVersions . Where ( v => v . IsPrerelease ) . OrderByDescending ( v => v ) . First ( ) ;
142
+ // stable only
143
+ //var mscorlibPackage = packageVersions.OrderByDescending(v => v).First();
144
+
145
+ MscorlibVersion = mscorlibPackage . Version . ToString ( ) ;
146
+ MscorlibNuGetVersion = mscorlibPackage . ToNormalizedString ( ) ;
147
+
148
+ // Math
149
+ packageVersions = resource . GetAllVersionsAsync (
150
+ "nanoFramework.System.Math" ,
151
+ cache ,
152
+ logger ,
153
+ cancellationToken ) . Result ;
154
+
155
+ // NuGet package Version
156
+ // including preview
157
+ var mathPackage = packageVersions . Where ( v => v . IsPrerelease ) . OrderByDescending ( v => v ) . First ( ) ;
158
+ // stable only
159
+ //var mathPackage = MathNuGetVersion = packageVersions.OrderByDescending(v => v).First();
160
+
161
+ MathVersion = mathPackage . Version . ToString ( ) ;
162
+ MathNuGetVersion = mathPackage . ToNormalizedString ( ) ;
163
+ }
164
+ else
165
+ {
166
+ // Angle has both mscorlib and System.Math dependency.
167
+ var anyProjectFile = Path . Combine ( outputDir , "Angle" , "Angle.nfproj" ) ;
168
+ var projectFileContent = File . ReadAllText ( anyProjectFile ) ;
169
+
170
+ // <Reference Include="mscorlib, Version=1.10.5.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
171
+ MscorlibVersion = ParseVersion ( projectFileContent ,
172
+ new Regex ( @"<Reference Include=""mscorlib,\s*Version=(?<version>[\d\.]+),.*"">" , RegexOptions . IgnoreCase ) ,
173
+ nameof ( MscorlibVersion ) ) ;
174
+
175
+ // <HintPath>..\packages\nanoFramework.CoreLibrary.1.10.5-preview.18\lib\mscorlib.dll</HintPath>
176
+ MscorlibNuGetVersion = ParseVersion ( projectFileContent ,
177
+ new Regex ( @"<HintPath>.*[\\\/]nanoFramework\.CoreLibrary\.(?<version>.*?)[\\\/]lib[\\\/]mscorlib.dll<" , RegexOptions . IgnoreCase ) ,
178
+ nameof ( MscorlibNuGetVersion ) ) ;
179
+
180
+ // <Reference Include="System.Math, Version=1.4.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
181
+ MathVersion = ParseVersion ( projectFileContent ,
182
+ new Regex ( @"<Reference Include=""System.Math,\s*Version=(?<version>[\d\.]+),.*"">" , RegexOptions . IgnoreCase ) ,
183
+ nameof ( MathVersion ) ) ;
184
+
185
+ // <HintPath>..\packages\nanoFramework.System.Math.1.4.1-preview.7\lib\System.Math.dll</HintPath>
186
+ MathNuGetVersion = ParseVersion ( projectFileContent ,
187
+ new Regex ( @"<HintPath>.*[\\\/]nanoFramework\.System\.Math\.(?<version>.*?)[\\\/]lib[\\\/]System.Math.dll<" , RegexOptions . IgnoreCase ) ,
188
+ nameof ( MathNuGetVersion ) ) ;
189
+ }
190
+ }
191
+
192
+ private static string ParseVersion ( string projectFileContent , Regex versionRegex , string descriptiveName )
193
+ {
194
+ var match = versionRegex . Match ( projectFileContent ) ;
195
+ if ( ! match . Success ) throw new InvalidOperationException ( $ "Unable to parse version { descriptiveName } from project file.") ;
196
+
197
+ return match . Groups [ "version" ] . Value ;
198
+ }
199
+
153
200
private static void GeneratePackageConfig ( string projectPath , string quantityName )
154
201
{
155
202
string filePath = Path . Combine ( projectPath , "packages.config" ) ;
@@ -199,10 +246,8 @@ private static void GenerateQuantity(StringBuilder sb, Quantity quantity, string
199
246
sb . Append ( "quantity(OK) " ) ;
200
247
}
201
248
202
- private static void GenerateProject ( StringBuilder sb , Quantity quantity , string projectPath )
249
+ private static void GenerateProject ( StringBuilder sb , Quantity quantity , string filePath )
203
250
{
204
- var filePath = Path . Combine ( projectPath , $ "{ quantity . Name } .nfproj") ;
205
-
206
251
var content = new ProjectGenerator ( quantity ) . Generate ( ) ;
207
252
File . WriteAllText ( filePath , content , Encoding . UTF8 ) ;
208
253
sb . Append ( "project(OK) " ) ;
0 commit comments