forked from ChilliCream/graphql-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.PublicApiAnalyzer.cs
205 lines (167 loc) · 7.17 KB
/
Build.PublicApiAnalyzer.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nuke.Common;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tools.DotNet;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
using static Nuke.Common.Tools.Git.GitTasks;
using static Helpers;
partial class Build
{
readonly string _shippedApiFile = "PublicAPI.Shipped.txt";
readonly string _unshippedApiFile = "PublicAPI.Unshipped.txt";
readonly string _removedApiPrefix = "*REMOVED*";
[Parameter] readonly string From;
[Parameter] readonly string To;
[Parameter] readonly bool Breaking;
Target CheckPublicApi => _ => _
.DependsOn(Restore)
.Executes(() =>
{
TryDelete(PublicApiSolutionFile);
DotNetBuildSonarSolution(AllSolutionFile);
var projectFiles = ProjectModelTasks.ParseSolution(AllSolutionFile)
.AllProjects
.Where(t => t.GetProperty<string>("AddPublicApiAnalyzers") != "false")
.Where(t => !Path.GetDirectoryName(t.Path)
.EndsWith("tests", StringComparison.OrdinalIgnoreCase))
.Where(t => !Path.GetDirectoryName(t.Path)
.EndsWith("test", StringComparison.OrdinalIgnoreCase))
.Select(t => Path.GetDirectoryName(t.Path)!)
.ToArray();
DotNetBuildSonarSolution(PublicApiSolutionFile, projectFiles);
DotNetBuild(c => c
.SetProjectFile(PublicApiSolutionFile)
.SetNoRestore(InvokedTargets.Contains(Restore))
.SetConfiguration(Configuration)
.SetProperty("RequireDocumentationOfPublicApiChanges", true));
});
Target AddUnshippedApi => _ => _
.DependsOn(Restore)
.Executes(() =>
{
TryDelete(PublicApiSolutionFile);
DotNetBuildSonarSolution(AllSolutionFile);
var projectFiles = ProjectModelTasks.ParseSolution(AllSolutionFile)
.AllProjects
.Where(t => t.GetProperty<string>("AddPublicApiAnalyzers") != "false")
.Where(t => !Path.GetDirectoryName(t.Path)
.EndsWith("tests", StringComparison.OrdinalIgnoreCase))
.Where(t => !Path.GetDirectoryName(t.Path)
.EndsWith("test", StringComparison.OrdinalIgnoreCase))
.Select(t => Path.GetDirectoryName(t.Path)!)
.ToArray();
DotNetBuildSonarSolution(PublicApiSolutionFile, projectFiles);
// last we run the actual dotnet format command.
DotNet($@"format ""{PublicApiSolutionFile}"" analyzers --diagnostics=RS0016", workingDirectory: RootDirectory);
});
Target DiffShippedApi => _ => _
.Executes(() =>
{
var from = string.IsNullOrEmpty(From) ? GitRepository.Branch : From;
var to = string.IsNullOrEmpty(To) ? "main" : To;
if (from == to)
{
Colorful.Console.WriteLine("Nothing to diff here.", Color.Yellow);
return;
}
AbsolutePath shippedPath = SourceDirectory / "**" / _shippedApiFile;
Git($@" --no-pager diff --minimal -U0 --word-diff ""{from}"" ""{to}"" -- ""{shippedPath}""", RootDirectory);
});
Target DisplayUnshippedApi => _ => _
.Executes(async () =>
{
var unshippedFiles = Directory.GetFiles(SourceDirectory, _unshippedApiFile, SearchOption.AllDirectories);
if (Breaking)
{
Colorful.Console.WriteLine("Unshipped breaking changes:", Color.Red);
}
else
{
Colorful.Console.WriteLine("Unshipped changes:");
}
Colorful.Console.WriteLine();
foreach (var unshippedFile in unshippedFiles)
{
IEnumerable<string> unshippedApis = await GetNonEmptyLinesAsync(unshippedFile);
if (Breaking)
{
unshippedApis = unshippedApis.Where(u => u.StartsWith(_removedApiPrefix)).ToList();
}
if (!unshippedApis.Any())
{
continue;
}
foreach (var unshippedApi in unshippedApis)
{
if (unshippedApi.StartsWith(_removedApiPrefix))
{
var value = unshippedApi[_removedApiPrefix.Length..];
Colorful.Console.WriteLine(value);
}
else
{
Colorful.Console.WriteLine(unshippedApi);
}
}
}
});
Target MarkApiShipped => _ => _
.Executes(async () =>
{
Colorful.Console.WriteLine("This is only supposed to be executed after a release has been published.", Color.Yellow);
Colorful.Console.WriteLine("If you just want to stage your API changes, use the AddUnshippedApi script.", Color.Yellow);
Colorful.Console.WriteLine("Continue? (y/n)", Color.Yellow);
if (Colorful.Console.ReadKey().Key != ConsoleKey.Y)
{
return;
}
Colorful.Console.WriteLine();
var shippedFiles = Directory.GetFiles(SourceDirectory, _shippedApiFile, SearchOption.AllDirectories);
foreach (var shippedFile in shippedFiles)
{
var projectDir = Path.GetDirectoryName(shippedFile);
var unshippedFile = Path.Join(projectDir, _unshippedApiFile);
if (!File.Exists(unshippedFile))
{
continue;
}
List<string> unshippedApis = await GetNonEmptyLinesAsync(unshippedFile);
if (!unshippedApis.Any())
{
continue;
}
List<string> shippedApis = await GetNonEmptyLinesAsync(shippedFile);
List<string> removedApis = new();
foreach (var unshippedApi in unshippedApis)
{
if (unshippedApi.StartsWith(_removedApiPrefix))
{
var value = unshippedApi[_removedApiPrefix.Length..];
removedApis.Add(value);
}
else
{
shippedApis.Add(unshippedApi);
}
}
IOrderedEnumerable<string> newShippedApis = shippedApis
.Where(s => !removedApis.Contains(s))
.Distinct()
.OrderBy(s => s);
await File.WriteAllLinesAsync(shippedFile, newShippedApis, Encoding.ASCII);
await File.WriteAllTextAsync(unshippedFile, "", Encoding.ASCII);
}
});
static async Task<List<string>> GetNonEmptyLinesAsync(string filepath)
{
var lines = await File.ReadAllLinesAsync(filepath);
return lines.Where(c => !string.IsNullOrWhiteSpace(c)).ToList();
}
}