-
Notifications
You must be signed in to change notification settings - Fork 22
/
UpdateTarget.cs
88 lines (75 loc) · 3.39 KB
/
UpdateTarget.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
// ReSharper disable StringLiteralTypo
// ReSharper disable HeapView.DelegateAllocation
// ReSharper disable HeapView.ClosureAllocation
// ReSharper disable ClassNeverInstantiated.Global
namespace Build;
using NuGet.Versioning;
internal class UpdateTarget(
Settings settings,
Commands commands,
Env env)
: IInitializable, ITarget<NuGetVersion>
{
private const string VersionPrefix = "PUREDI_API_V";
public Task InitializeAsync(CancellationToken cancellationToken) => commands.RegisterAsync(
this, "Upgrading the internal version of DI to the latest public version", "upgrade", "u");
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public async Task<NuGetVersion> RunAsync(CancellationToken cancellationToken)
{
var solutionDirectory = env.GetPath(PathType.SolutionDirectory);
var currentVersion = settings.CurrentVersion;
var propsFile = Path.Combine(solutionDirectory, "Directory.Build.props");
var props = await File.ReadAllLinesAsync(propsFile, cancellationToken);
var contents = new List<string>();
foreach (var prop in props)
{
var line = prop;
var index = line.IndexOf("<InternalVersion>", StringComparison.InvariantCulture);
if (index >= 0)
{
line = $"{new string(' ', index)}<InternalVersion>{currentVersion}</InternalVersion>";
}
contents.Add(line);
}
await File.WriteAllLinesAsync(propsFile, contents, cancellationToken);
WriteLine($"The internal version of Pure.DI has been updated to {currentVersion}.", Color.Details);
var projectDir = Path.Combine(solutionDirectory, "src", "Pure.DI.Core");
var files =
Directory.EnumerateFiles(projectDir, "*.cs", SearchOption.AllDirectories)
.Concat(Enumerable.Repeat(Path.Combine(projectDir, "Pure.DI.Core.csproj"), 1));
foreach (var file in files)
{
contents.Clear();
var hasVersion = false;
foreach (var line in File.ReadLines(file))
{
var newLine = line;
var index = newLine.IndexOf(VersionPrefix, StringComparison.InvariantCulture);
if (index >= 0 && index + VersionPrefix.Length < line.Length)
{
var version = newLine[index + VersionPrefix.Length];
switch (version)
{
case '1':
newLine = newLine.Replace(VersionPrefix + "1", VersionPrefix + "2");
hasVersion = true;
break;
case '2':
newLine = newLine.Replace(VersionPrefix + "2", VersionPrefix + "1");
hasVersion = true;
break;
}
}
contents.Add(newLine);
}
if (hasVersion)
{
await File.WriteAllLinesAsync(file, contents, cancellationToken);
}
}
await new DotNetBuildServerShutdown().RunAsync(cancellationToken: cancellationToken);
await new DotNetRestore().RunAsync(cancellationToken: cancellationToken);
await new DotNetBuild().BuildAsync(cancellationToken: cancellationToken);
return currentVersion;
}
}