-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (132 loc) · 5.42 KB
/
Program.cs
File metadata and controls
155 lines (132 loc) · 5.42 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
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.Threading.Tasks;
namespace NuGet.Internal.Tools.ShipPublicApis
{
class Program
{
static async Task<int> Main(string[] args)
{
var nugetSlnDirectory = FindNuGetSlnDirectory();
var pathArgument = nugetSlnDirectory == null
? new Argument<DirectoryInfo>("path")
: new Argument<DirectoryInfo>("path") { DefaultValueFactory = _ => nugetSlnDirectory };
var resortOption = new Option<bool>("--resort");
var rootCommand = new RootCommand()
{
pathArgument,
resortOption
};
rootCommand.Description = "Copy and merge contents of PublicAPI.Unshipped.txt to PublicAPI.Shipped.txt. See https://github.com/NuGet/NuGet.Client/tree/dev/docs/nuget-sdk.md#Shipping_NuGet for more details.";
rootCommand.SetAction(async (ParseResult, CancellationToken) =>
{
var path_Argument = ParseResult.GetValue<DirectoryInfo>(pathArgument);
var resort_Option = ParseResult.GetValue<bool>(resortOption);
if (path_Argument is not null)
{
await MainAsync(path_Argument, resort_Option);
}
});
return await rootCommand.Parse(args).InvokeAsync();
}
private static DirectoryInfo? FindNuGetSlnDirectory()
{
var directory = Environment.CurrentDirectory;
while (true)
{
if (File.Exists(Path.Combine(directory, "NuGet.sln")))
{
return new DirectoryInfo(directory);
}
var parent = Path.GetDirectoryName(directory);
if (string.IsNullOrEmpty(parent) || parent == directory)
{
return null;
}
directory = parent;
}
}
static async Task<int> MainAsync(DirectoryInfo path, bool resort)
{
if (path == null)
{
Console.Error.WriteLine("No path provided");
return -1;
}
if (!path.Exists)
{
Console.Error.WriteLine($"Path '{path.FullName}' does not exist");
return -2;
}
bool foundAtLeastOne = false;
foreach (FileInfo unshippedTxtPath in path.EnumerateFiles("PublicAPI.Unshipped.txt", new EnumerationOptions() { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = true }))
{
foundAtLeastOne = true;
if (unshippedTxtPath.Length == 0 && !resort)
{
Console.WriteLine(unshippedTxtPath.FullName + ": Up to date");
continue;
}
if (unshippedTxtPath.DirectoryName == null)
{
throw new Exception("Found a file that's not in a directory?");
}
var shippedTxtPath = Path.Combine(unshippedTxtPath.DirectoryName, "PublicAPI.Shipped.txt");
if (!File.Exists(shippedTxtPath))
{
throw new FileNotFoundException($"Cannot migrate APIs from {unshippedTxtPath.FullName}. {shippedTxtPath} not found.");
}
int unshippedApiCount = await MoveUnshippedApisToShippedAsync(shippedTxtPath, unshippedTxtPath.FullName);
Console.WriteLine($"{unshippedTxtPath.FullName}: Shipped {unshippedApiCount} APIs.");
}
if (!foundAtLeastOne)
{
Console.Error.WriteLine("Did not find any PublicAPI.Unshipped.txt files under " + path.FullName);
return -3;
}
return 0;
}
private static async Task<int> MoveUnshippedApisToShippedAsync(string shippedTxtPath, string unshippedTxtPath)
{
var shippedLines = new List<string>();
var unshippedLines = new List<string>();
int unshippedApiCount = 0;
using (var stream = File.OpenText(unshippedTxtPath))
{
string? line;
while ((line = await stream.ReadLineAsync()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
if (line.StartsWith("#"))
{
unshippedLines.Add(line);
}
else
{
shippedLines.Add(line);
unshippedApiCount++;
}
}
}
}
using (var stream = File.OpenText(shippedTxtPath))
{
string? line;
while ((line = await stream.ReadLineAsync()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
shippedLines.Add(line);
}
}
}
shippedLines.Sort(PublicAPIAnalyzerLineComparer.Instance);
await File.WriteAllLinesAsync(shippedTxtPath, shippedLines);
await File.WriteAllLinesAsync(unshippedTxtPath, unshippedLines);
return unshippedApiCount;
}
}
}