-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionComponents.cs
More file actions
323 lines (249 loc) · 11.2 KB
/
VersionComponents.cs
File metadata and controls
323 lines (249 loc) · 11.2 KB
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
317
318
319
320
321
322
323
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
using Microsoft.Build.Evaluation;
using NuGet.Versioning;
using PostSharp.Engineering.BuildTools.Build.Files;
using PostSharp.Engineering.BuildTools.Build.MSBuild;
using PostSharp.Engineering.BuildTools.Dependencies.Model;
using PostSharp.Engineering.BuildTools.Utilities;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
namespace PostSharp.Engineering.BuildTools.Build.Model;
internal record VersionComponents
{
private VersionComponents(
string mainVersion,
string versionPrefix,
int patchNumber,
string versionSuffix,
BuildConfiguration configuration,
Product product )
{
this.MainVersion = mainVersion;
this.VersionPrefix = versionPrefix;
this.PatchNumber = patchNumber;
this.VersionSuffix = versionSuffix;
this.PreviewVersionSuffix = configuration == BuildConfiguration.Public ? "preview" : this.VersionSuffix;
this.PackageVersionWithoutSuffix = this.PatchNumber == 0 ? this.VersionPrefix : this.VersionPrefix + "." + this.PatchNumber;
// Metalama.Compiler, because of Arcade, requires the version number to be decomposed in a prefix, patch number, and suffix.
// In Arcade, the package naming scheme is different because the patch number is not a part of the package name.
if ( product.GenerateArcadeProperties )
{
var arcadeSuffix = string.IsNullOrEmpty( this.VersionSuffix ) ? "" : this.VersionSuffix;
var previewArcadeSuffix = this.PreviewVersionSuffix;
void AppendToArcadeSuffix( string s )
{
arcadeSuffix += s;
previewArcadeSuffix += s;
}
if ( this.PatchNumber > 0 )
{
if ( arcadeSuffix.Length > 0 )
{
AppendToArcadeSuffix( "-" );
}
else
{
// It should not happen that we have a patch number without a suffix.
AppendToArcadeSuffix( "-patch-" + configuration );
}
AppendToArcadeSuffix( this.PatchNumber.ToString( CultureInfo.InvariantCulture ) );
}
var packageSuffixWithDash = string.IsNullOrEmpty( arcadeSuffix ) ? "" : "-" + arcadeSuffix;
this.PackageVersion = this.VersionPrefix + packageSuffixWithDash;
this.PackagePreviewVersion = this.VersionPrefix + "-" + previewArcadeSuffix;
this.ArcadeSuffix = arcadeSuffix;
}
else
{
var packageSuffix = string.IsNullOrEmpty( this.VersionSuffix ) ? "" : "-" + this.VersionSuffix;
this.PackageVersion = this.PackageVersionWithoutSuffix + packageSuffix;
this.PackagePreviewVersion = this.PackageVersionWithoutSuffix + "-" + this.PreviewVersionSuffix;
}
}
public string AssemblyVersion => this.VersionPrefix + "." + this.PatchNumber;
public string PackageVersionWithoutSuffix { get; set; }
public string PreviewVersionSuffix { get; set; }
public string? ArcadeSuffix { get; }
public string PackagePreviewVersion { get; }
public string PackageVersion { get; }
public string MainVersion { get; }
public string VersionPrefix { get; }
public int PatchNumber { get; }
public string VersionSuffix { get; }
public static bool TryCompute(
BuildContext context,
BuildSettings settings,
BuildConfiguration configuration,
MainVersionFile mainVersionFile,
DependenciesConfigurationFile dependenciesConfigurationFile,
[NotNullWhen( true )] out VersionComponents? version )
{
string? inheritedMainVersion;
if ( context.Product.MainVersionDependency != null )
{
if ( !TryInheritedReadMainVersion( context, dependenciesConfigurationFile, settings, out inheritedMainVersion ) )
{
version = null;
return false;
}
}
else
{
inheritedMainVersion = null;
}
var versionSpec = settings.GetVersionSpec( configuration );
return TryCompute( context, configuration, mainVersionFile, inheritedMainVersion, versionSpec, settings.UserName, out version );
}
public static bool TryCompute(
BuildContext context,
BuildSettings settings,
[NotNullWhen( true )] out VersionComponents? version )
{
if ( !MainVersionFile.TryRead( context, out var mainVersionFile ) )
{
version = null;
return false;
}
return TryCompute(
context,
settings.BuildConfiguration,
mainVersionFile,
null,
settings.GetVersionSpec( settings.BuildConfiguration ),
settings.UserName,
out version );
}
public static bool TryCompute(
BuildContext context,
BuildConfiguration configuration,
MainVersionFile mainVersionFile,
string? mainVersionOverride,
VersionSpec versionSpec,
string? userName,
[NotNullWhen( true )] out VersionComponents? version )
{
var product = context.Product;
var configurationLowerCase = configuration.ToString().ToLowerInvariant();
version = null;
var mainVersion = mainVersionOverride ?? mainVersionFile.MainVersion;
if ( !string.IsNullOrEmpty( mainVersionFile.OverriddenPatchVersion )
&& !mainVersionFile.OverriddenPatchVersion.StartsWith( mainVersionOverride ?? mainVersionFile.MainVersion + ".", StringComparison.Ordinal ) )
{
context.Console.WriteError(
$"The OverriddenPatchVersion property in MainVersion.props ({mainVersionFile.OverriddenPatchVersion}) does not match the MainVersion property value ({mainVersion})." );
return false;
}
var versionPrefix = mainVersion;
string versionSuffix;
int patchNumber;
switch ( versionSpec.Kind )
{
case VersionKind.Local:
{
// Local build with timestamp-based version and randomized package number. For the assembly version we use a local incremental file stored in the user profile.
var localVersionDirectory = PathHelper.GetEngineeringDataDirectory();
var localVersionFile = Path.Combine( localVersionDirectory, $"{product.ProductName}.version" );
int localVersion;
if ( File.Exists( localVersionFile ) )
{
localVersion = int.Parse(
File.ReadAllText( localVersionFile ),
CultureInfo.InvariantCulture ) + 1;
}
else
{
localVersion = 1;
}
if ( localVersion < 1000 )
{
localVersion = 1000;
}
if ( !Directory.Exists( localVersionDirectory ) )
{
Directory.CreateDirectory( localVersionDirectory );
}
File.WriteAllText( localVersionFile, localVersion.ToString( CultureInfo.InvariantCulture ) );
versionSuffix = $"local-{userName}-{configurationLowerCase}";
patchNumber = localVersion;
break;
}
case VersionKind.Numbered:
{
// Build server build with a build number given by the build server
patchNumber = versionSpec.Number;
versionSuffix = $"dev-{configurationLowerCase}";
break;
}
case VersionKind.Public:
// Public build
versionSuffix = mainVersionFile.PackageVersionSuffix.TrimStart( '-' );
patchNumber = 0;
if ( !string.IsNullOrWhiteSpace( mainVersionFile.OverriddenPatchVersion ) )
{
var parsedOverriddenPatchedVersion = Version.Parse( mainVersionFile.OverriddenPatchVersion );
patchNumber = parsedOverriddenPatchedVersion.Revision;
}
break;
default:
throw new InvalidOperationException();
}
version = new VersionComponents( mainVersion, versionPrefix, patchNumber, versionSuffix, configuration, product );
return true;
}
private static bool TryInheritedReadMainVersion(
BuildContext context,
DependenciesConfigurationFile dependenciesConfigurationFile,
BuildSettings settings,
out string? mainVersion )
{
var product = context.Product;
var mainVersionDependencyName = product.MainVersionDependency!.Name;
// The main version is defined in a dependency. Load the import file.
if ( !dependenciesConfigurationFile.Dependencies.TryGetValue( mainVersionDependencyName, out var dependencySource ) )
{
context.Console.WriteError( $"Cannot find a dependency named '{mainVersionDependencyName}'." );
mainVersion = null;
return false;
}
// Note that the version suffix is not copied from the dependency, only the main version.
if ( dependencySource.VersionFile == null )
{
if ( !VersionFile.TryRead( context, settings, out var localVersionFile ) )
{
mainVersion = null;
return false;
}
if ( !localVersionFile.Dependencies.TryGetValue( product.MainVersionDependency.Name, out var mainDependencySource ) )
{
context.Console.WriteError( $"Version file doesn't contain version for {product.MainVersionDependency.Name}." );
mainVersion = null;
return false;
}
var versionString = mainDependencySource.Version;
if ( !NuGetVersion.TryParse( versionString, out var mainFullVersion ) )
{
context.Console.WriteError( $"Could not parse the version '{versionString}'." );
mainVersion = null;
return false;
}
mainVersion = new NuGetVersion( mainFullVersion.Major, mainFullVersion.Minor, mainFullVersion.Patch ).ToString();
}
else
{
var versionFile = Project.FromFile( dependencySource.VersionFile, MSBuildLoadOptions.IgnoreImportErrors );
var propertyName = product.MainVersionDependency!.NameWithoutDot + "MainVersion";
mainVersion = versionFile.Properties.SingleOrDefault( p => p.Name == propertyName )
?.UnevaluatedValue;
if ( string.IsNullOrEmpty( mainVersion ) )
{
context.Console.WriteError( $"The file '{dependencySource.VersionFile}' does not contain the {propertyName}." );
return false;
}
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
}
return true;
}
}