-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathSetAllVersions.cs
278 lines (223 loc) · 11.1 KB
/
SetAllVersions.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
// <copyright file="SetAllVersions.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace PrepareRelease
{
public class SetAllVersions
{
public SetAllVersions(string solutionDirectory, Version version, bool? isPreRelease)
{
SolutionDirectory = solutionDirectory;
TracerVersion = version ?? new("1.28.1");
IsPrerelease = isPreRelease ?? false;
}
/// <summary>
/// Gets the root solution directory, where "Datadog.Trace.sln" can be found.
/// </summary>
public string SolutionDirectory { get; }
/// <summary>
/// Gets the current tracer version.
/// This is the single source of truth for the current tracer version.
/// When changing the tracer version, update this value and <see cref="IsPrerelease">,
/// then run the "PrepareRelease" tool to update the entire solution.
/// </summary>
public Version TracerVersion { get; }
/// <summary>
/// Gets a value indicating whether the current tracer version is a prerelease.
/// </summary>
public bool IsPrerelease { get; }
public void Run()
{
Console.WriteLine($"Updating version instances to {VersionString()}");
// Sample application package updates
SynchronizeVersion(
"samples/AutomaticTraceIdInjection/Log4NetExample/Log4NetExample.csproj",
DatadogTraceNugetDependencyVersionReplace);
SynchronizeVersion(
"samples/AutomaticTraceIdInjection/NLog40Example/NLog40Example.csproj",
DatadogTraceNugetDependencyVersionReplace);
SynchronizeVersion(
"samples/AutomaticTraceIdInjection/NLog45Example/NLog45Example.csproj",
DatadogTraceNugetDependencyVersionReplace);
SynchronizeVersion(
"samples/AutomaticTraceIdInjection/NLog46Example/NLog46Example.csproj",
DatadogTraceNugetDependencyVersionReplace);
SynchronizeVersion(
"samples/AutomaticTraceIdInjection/SerilogExample/SerilogExample.csproj",
DatadogTraceNugetDependencyVersionReplace);
// Dockerfile updates
SynchronizeVersion(
"samples/ConsoleApp/Alpine3.9.dockerfile",
text => Regex.Replace(text, $"ARG TRACER_VERSION={VersionPattern()}", $"ARG TRACER_VERSION={VersionString()}"));
SynchronizeVersion(
"samples/ConsoleApp/Alpine3.10.dockerfile",
text => Regex.Replace(text, $"ARG TRACER_VERSION={VersionPattern()}", $"ARG TRACER_VERSION={VersionString()}"));
SynchronizeVersion(
"samples/ConsoleApp/Debian.dockerfile",
text => Regex.Replace(text, $"ARG TRACER_VERSION={VersionPattern()}", $"ARG TRACER_VERSION={VersionString()}"));
SynchronizeVersion(
"test/test-applications/regression/AutomapperTest/Dockerfile",
text => Regex.Replace(text, $"ARG TRACER_VERSION={VersionPattern()}", $"ARG TRACER_VERSION={VersionString()}"));
SynchronizeVersion(
"samples/WindowsContainer/Dockerfile",
text => Regex.Replace(text, $"ARG TRACER_VERSION={VersionPattern()}", $"ARG TRACER_VERSION={VersionString()}"));
// Nuke build
SynchronizeVersion(
"build/_build/Build.cs",
text => Regex.Replace(text, $"readonly string Version = \"{VersionPattern()}\"", $"readonly string Version = \"{VersionString()}\""));
// Managed project / NuGet package updates
SynchronizeVersion(
"src/Datadog.Trace/Datadog.Trace.csproj",
NugetVersionReplace);
SynchronizeVersion(
"src/Datadog.Trace.ClrProfiler.Managed.Loader/Datadog.Trace.ClrProfiler.Managed.Loader.csproj",
NugetVersionReplace);
SynchronizeVersion(
"src/Datadog.Trace.OpenTracing/Datadog.Trace.OpenTracing.csproj",
NugetVersionReplace);
SynchronizeVersion(
"src/Datadog.Trace.MSBuild/Datadog.Trace.MSBuild.csproj",
NugetVersionReplace);
SynchronizeVersion(
"src/Datadog.Trace.Tools.Runner/Datadog.Trace.Tools.Runner.Standalone.csproj",
NugetVersionReplace);
SynchronizeVersion(
"src/Datadog.Trace.Tools.Runner/Datadog.Trace.Tools.Runner.Tool.csproj",
NugetVersionReplace);
// Fully qualified name updates
SynchronizeVersion(
"src/Datadog.Trace.ClrProfiler.Managed.Loader/Startup.cs",
FullAssemblyNameReplace);
SynchronizeVersion(
"src/Datadog.Trace.ClrProfiler.Native/dd_profiler_constants.h",
FullAssemblyNameReplace);
// Four-part AssemblyVersion update
SynchronizeVersion(
"src/Datadog.Trace/TracerConstants.cs",
FourPartVersionReplace);
// Native profiler updates
SynchronizeVersion(
"src/Datadog.Trace.ClrProfiler.Native/CMakeLists.txt",
text => FullVersionReplace(text, ".", prefix: "VERSION "));
SynchronizeVersion(
"src/Datadog.Trace.ClrProfiler.Native/Resource.rc",
text =>
{
text = FullVersionReplace(text, ",");
text = FullVersionReplace(text, ".");
return text;
});
SynchronizeVersion(
"src/Datadog.Trace.ClrProfiler.Native/version.h",
text => FullVersionReplace(text, "."));
// Deployment updates
SynchronizeVersion(
"integrations.json",
FullAssemblyNameReplace);
SynchronizeVersion(
"src/WindowsInstaller/WindowsInstaller.wixproj",
WixProjReplace);
Console.WriteLine($"Completed synchronizing versions to {VersionString()}");
}
private string FourPartVersionReplace(string text)
{
return Regex.Replace(text, VersionPattern(fourPartVersion: true), FourPartVersionString(), RegexOptions.Singleline);
}
private string FullVersionReplace(string text, string split, string prefix = "")
{
return Regex.Replace(text, prefix + VersionPattern(split), prefix + VersionString(split), RegexOptions.Singleline);
}
private string FullAssemblyNameReplace(string text)
{
return Regex.Replace(text, AssemblyString(VersionPattern()), AssemblyString(VersionString()), RegexOptions.Singleline);
}
private string MajorAssemblyVersionReplace(string text, string split)
{
return Regex.Replace(text, VersionPattern(fourPartVersion: true), MajorVersionString(split), RegexOptions.Singleline);
}
private string DatadogTraceNugetDependencyVersionReplace(string text)
{
return Regex.Replace(text, $"<PackageReference Include=\"Datadog.Trace\" Version=\"{VersionPattern(withPrereleasePostfix: true)}\" />", $"<PackageReference Include=\"Datadog.Trace\" Version=\"{VersionString(withPrereleasePostfix: true)}\" />", RegexOptions.Singleline);
}
private string NugetVersionReplace(string text)
{
return Regex.Replace(text, $"<Version>{VersionPattern(withPrereleasePostfix: true)}</Version>", $"<Version>{VersionString(withPrereleasePostfix: true)}</Version>", RegexOptions.Singleline);
}
private string NuspecVersionReplace(string text)
{
return Regex.Replace(text, $"<version>{VersionPattern(withPrereleasePostfix: true)}</version>", $"<version>{VersionString(withPrereleasePostfix: true)}</version>", RegexOptions.Singleline);
}
private string WixProjReplace(string text)
{
text = Regex.Replace(
text,
$"<OutputName>datadog-dotnet-apm-{VersionPattern(withPrereleasePostfix: true)}-\\$\\(Platform\\)</OutputName>",
$"<OutputName>datadog-dotnet-apm-{VersionString(withPrereleasePostfix: true)}-$(Platform)</OutputName>",
RegexOptions.Singleline);
text = Regex.Replace(
text,
$"InstallerVersion={VersionPattern()}",
$"InstallerVersion={VersionString()}",
RegexOptions.Singleline);
return text;
}
private void SynchronizeVersion(string path, Func<string, string> transform)
{
var fullPath = Path.Combine(SolutionDirectory, path);
Console.WriteLine($"Updating version instances for {path}");
if (!File.Exists(fullPath))
{
throw new Exception($"File not found to version: {path}");
}
var fileContent = File.ReadAllText(fullPath);
var newFileContent = transform(fileContent);
File.WriteAllText(fullPath, newFileContent, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
}
private string FourPartVersionString(string split = ".")
{
return $"{TracerVersion.Major}{split}{TracerVersion.Minor}{split}{TracerVersion.Build}{split}0";
}
private string MajorVersionString(string split = ".")
{
return $"{TracerVersion.Major}{split}0{split}0{split}0";
}
private string VersionString(string split = ".", bool withPrereleasePostfix = false)
{
var newVersion = $"{TracerVersion.Major}{split}{TracerVersion.Minor}{split}{TracerVersion.Build}";
// this gets around a compiler warning about unreachable code below
var isPreRelease = IsPrerelease;
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (withPrereleasePostfix && isPreRelease)
{
newVersion = newVersion + "-prerelease";
}
return newVersion;
}
private string VersionPattern(string split = ".", bool withPrereleasePostfix = false, bool fourPartVersion = false)
{
if (split == ".")
{
split = @"\.";
}
var pattern = $@"\d+{split}\d+{split}\d+";
if (fourPartVersion)
{
pattern = pattern + $@"{split}\d+";
}
if (withPrereleasePostfix)
{
pattern = pattern + "(\\-prerelease)?";
}
return pattern;
}
private string AssemblyString(string versionText)
{
return $"Datadog.Trace, Version={versionText}.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb";
}
}
}