-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathNativeVersionInfo.cs
316 lines (255 loc) · 10.3 KB
/
NativeVersionInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
namespace Nerdbank.GitVersioning.Tasks
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class NativeVersionInfo : Task
{
private const int VFT_APP = 0x1;
private const int VFT_DLL = 0x2;
private const string FileHeaderComment = @"------------------------------------------------------------------------------
<auto-generated>
This code was generated by a tool.
Runtime Version:4.0.30319.42000
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
</auto-generated>
------------------------------------------------------------------------------
";
private const string VersionStringDefineContent = @"#if defined(_UNICODE)
#define NBGV_VERSION_STRING(x) L ##x
#else
#define NBGV_VERSION_STRING(x) x
#endif";
private const string VersionInfoContent = @"#ifdef RC_INVOKED
#include <winres.h>
VS_VERSION_INFO VERSIONINFO
FILEVERSION NBGV_FILE_MAJOR_VERSION,NBGV_FILE_MINOR_VERSION,NBGV_FILE_BUILD_VERSION,NBGV_FILE_REVISION_VERSION
PRODUCTVERSION NBGV_PRODUCT_MAJOR_VERSION,NBGV_PRODUCT_MINOR_VERSION,NBGV_PRODUCT_BUILD_VERSION,NBGV_PRODUCT_REVISION_VERSION
FILEFLAGSMASK 0x3FL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE NBGV_FILE_TYPE
FILESUBTYPE 0x0L
BEGIN
BLOCK ""StringFileInfo""
BEGIN
BLOCK NBGV_VERSION_BLOCK
BEGIN
VALUE ""CompanyName"", NGBV_COMPANY
VALUE ""FileDescription"", NGBV_TITLE
VALUE ""FileVersion"", NBGV_FILE_VERSION
VALUE ""InternalName"", NGBV_INTERNAL_NAME
VALUE ""OriginalFilename"", NGBV_FILE_NAME
VALUE ""ProductName"", NGBV_PRODUCT
VALUE ""ProductVersion"", NBGV_INFORMATIONAL_VERSION
VALUE ""LegalCopyright"", NBGV_COPYRIGHT
END
END
BLOCK ""VarFileInfo""
BEGIN
VALUE ""Translation"", NBGV_LCID, NBGV_CODEPAGE
END
END
#endif";
private CodeGenerator generator;
[Required]
public string OutputFile { get; set; }
[Required]
public string CodeLanguage { get; set; }
[Required]
public string ConfigurationType { get; set; }
public string AssemblyName { get; set; }
public string AssemblyVersion { get; set; }
public string AssemblyFileVersion { get; set; }
public string AssemblyInformationalVersion { get; set; }
public string AssemblyTitle { get; set; }
public string AssemblyProduct { get; set; }
public string AssemblyCopyright { get; set; }
public string AssemblyCompany { get; set; }
public string AssemblyLanguage { get; set; }
public string AssemblyCodepage { get; set; }
public string TargetFileName { get; set; }
public override bool Execute()
{
this.generator = this.CreateGenerator();
if (this.generator is not null)
{
this.generator.StartFile();
this.generator.AddComment(FileHeaderComment);
this.generator.AddBlankLine();
this.generator.AddContent(VersionStringDefineContent);
this.generator.AddBlankLine();
this.CreateDefines();
this.generator.AddBlankLine();
this.CreateVersionInfo();
this.generator.EndFile();
Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
Utilities.FileOperationWithRetry(() => File.WriteAllText(this.OutputFile, this.generator.GetCode(), Encoding.Unicode));
}
return !this.Log.HasLoggedErrors;
}
private void CreateDefines()
{
var fileType = 0;
switch (this.ConfigurationType.ToUpperInvariant())
{
case "APPLICATION":
fileType = VFT_APP;
break;
case "DYNAMICLIBRARY":
fileType = VFT_DLL;
break;
default:
this.Log.LogError("Unsupported ConfigurationType '{0}'. Only 'Application' and 'DynamicLibrary' are supported at this time.", this.ConfigurationType);
return;
}
if (!Version.TryParse(this.AssemblyFileVersion, out var fileVersion))
{
this.Log.LogError("Cannot process AssemblyFileVersion '{0}' into a valid four part version.", this.AssemblyFileVersion);
return;
}
if (!Version.TryParse(this.AssemblyVersion, out var productVersion))
{
productVersion = fileVersion;
}
var lcid = 0;
if (!string.IsNullOrWhiteSpace(this.AssemblyLanguage))
{
if (!int.TryParse(this.AssemblyLanguage, out lcid))
{
#if NET461
try
{
var cultureInfo = new CultureInfo(this.AssemblyLanguage);
lcid = cultureInfo.LCID;
}
catch
{
this.Log.LogError("Unknown AssemblyLanguage '{0}'. Cannot determine LCID for that culture.", this.AssemblyLanguage);
return;
}
#else
this.Log.LogError("Unknown AssemblyLanguage '{0}'. Must specify the language as an LCID.", this.AssemblyLanguage);
#endif
}
}
if (!int.TryParse(this.AssemblyCodepage, out var codepage))
{
codepage = 0;
}
var numericFields = new Dictionary<string, int>
{
{ "NBGV_FILE_MAJOR_VERSION", fileVersion.Major },
{ "NBGV_FILE_MINOR_VERSION", fileVersion.Minor },
{ "NBGV_FILE_BUILD_VERSION", fileVersion.Build },
{ "NBGV_FILE_REVISION_VERSION", fileVersion.Revision },
{ "NBGV_PRODUCT_MAJOR_VERSION", productVersion.Major },
{ "NBGV_PRODUCT_MINOR_VERSION", productVersion.Minor },
{ "NBGV_PRODUCT_BUILD_VERSION", productVersion.Build },
{ "NBGV_PRODUCT_REVISION_VERSION", productVersion.Revision },
{ "NBGV_FILE_TYPE", fileType },
{ "NBGV_LCID", lcid },
{ "NBGV_CODEPAGE", codepage },
};
var stringFields = new Dictionary<string, string>
{
{ "NBGV_PRODUCT_VERSION", productVersion.ToString() },
{ "NBGV_FILE_VERSION", fileVersion.ToString() },
{ "NBGV_INFORMATIONAL_VERSION", DefaultIfEmpty(this.AssemblyInformationalVersion, productVersion.ToString()) },
{ "NGBV_FILE_NAME", this.TargetFileName },
{ "NGBV_INTERNAL_NAME", Path.GetFileNameWithoutExtension(this.TargetFileName) },
{ "NGBV_TITLE", DefaultIfEmpty(this.AssemblyTitle, this.AssemblyName) },
{ "NGBV_PRODUCT", DefaultIfEmpty(this.AssemblyProduct, this.AssemblyName) },
{ "NBGV_COPYRIGHT", DefaultIfEmpty(this.AssemblyCopyright, $"Copyright (c) {DateTime.Now.Year}. All rights reserved.") },
{ "NGBV_COMPANY", DefaultIfEmpty(this.AssemblyCompany, this.AssemblyName) },
{ "NBGV_VERSION_BLOCK", (lcid << 16 | codepage).ToString("X8") },
};
foreach (var pair in numericFields)
{
this.generator.AddDefine(pair.Key, pair.Value);
}
foreach (var pair in stringFields)
{
if (!string.IsNullOrWhiteSpace(pair.Value))
{
this.generator.AddDefine(pair.Key, pair.Value);
}
}
}
private void CreateVersionInfo()
{
this.generator.AddContent(VersionInfoContent);
}
private CodeGenerator CreateGenerator()
{
switch (this.CodeLanguage.ToLowerInvariant())
{
case "c++":
return new CodeGenerator();
default:
this.Log.LogError("Code provider not available for language: {0}. No version info will be embedded into assembly.", this.CodeLanguage);
return null;
}
}
private class CodeGenerator
{
protected readonly StringBuilder codeBuilder;
internal CodeGenerator()
{
this.codeBuilder = new StringBuilder();
}
internal void AddComment(string comment)
{
this.AddCodeComment(comment, "//");
}
internal void StartFile()
{
this.codeBuilder.AppendLine("#pragma once");
}
internal void AddContent(string content)
{
this.codeBuilder.AppendLine(content);
}
internal void AddDefine(string name, int value)
{
this.codeBuilder.AppendLine($"#define {name} {value}");
}
internal void AddDefine(string name, string value)
{
var escapedValue = value.Replace("\\", "\\\\");
this.codeBuilder.AppendLine($"#define {name} NBGV_VERSION_STRING(\"{escapedValue}\")");
}
internal void EndFile()
{
}
internal string GetCode() => this.codeBuilder.ToString();
internal void AddBlankLine()
{
this.codeBuilder.AppendLine();
}
protected void AddCodeComment(string comment, string token)
{
var sr = new StringReader(comment);
string line;
while ((line = sr.ReadLine()) is not null)
{
this.codeBuilder.Append(token);
this.codeBuilder.AppendLine(line);
}
}
}
private static string DefaultIfEmpty(string value, string defaultValue)
{
return string.IsNullOrWhiteSpace(value) ? defaultValue : value;
}
}
}