Skip to content

Commit ddf088f

Browse files
refactor authors file interaction to own method
1 parent 2e59dfe commit ddf088f

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

src/TfvcMigrator/Program.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ public static async Task<int> MigrateAsync(
8080
if (repo is null)
8181
return 1;
8282

83-
await using var authorsFileStream = LoadAuthorsFileStream(authors);
84-
var fileHasNewline = FileEndsInNewline(authorsFileStream);
85-
var authorsLookup = LoadAuthors(authorsFileStream);
86-
8783
Console.WriteLine("Connecting...");
8884

8985
using var connection = new VssConnection(
@@ -117,26 +113,11 @@ pat is not null
117113
top: int.MaxValue)
118114
).ConfigureAwait(false);
119115

120-
var unmappedAuthors = changesets.Select(c => c.Author)
121-
.Concat(changesets.Select(c => c.CheckedInBy))
122-
.Concat(allLabels.Select(l => l.Owner))
123-
.Where(identity => !authorsLookup.ContainsKey(identity.UniqueName))
124-
.DistinctBy(i => i.UniqueName, StringComparer.OrdinalIgnoreCase)
125-
.ToList();
126-
127-
if (unmappedAuthors.Any())
128-
{
129-
Console.WriteLine("A valid email address must be added to the authors file for each of the following TFVC users:");
130-
await using var writer = new StreamWriter(authors, append: true);
131-
if (!fileHasNewline) await writer.WriteLineAsync();
132-
133-
foreach (var user in unmappedAuthors)
134-
{
135-
Console.WriteLine(user.UniqueName);
136-
await writer.WriteLineAsync($"{user.UniqueName} = {user.DisplayName} <email>");
137-
}
138-
return 1;
139-
}
116+
var authorsLookup = await LoadAuthors(
117+
authors,
118+
changesets.Select(c => c.Author)
119+
.Concat(changesets.Select(c => c.CheckedInBy))
120+
.Concat(allLabels.Select(l => l.Owner)));
140121

141122
Console.WriteLine("Downloading changesets and converting to commits...");
142123

@@ -564,11 +545,36 @@ private static FileStream LoadAuthorsFileStream(string authorsPath)
564545
return new FileStream(authorsPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Write);
565546
}
566547

567-
private static ImmutableDictionary<string, Identity> LoadAuthors(Stream authorsFileStream)
548+
private static async Task<ImmutableDictionary<string, Identity>> LoadAuthors(string authorsLookupPath, IEnumerable<IdentityRef> changesetAuthors)
549+
{
550+
await using var authorsFileStream = LoadAuthorsFileStream(authorsLookupPath);
551+
var authorsLookup = LoadAuthors(authorsFileStream, leaveStreamOpen: true);
552+
553+
var unmappedAuthors = changesetAuthors
554+
.Where(identity => !authorsLookup.ContainsKey(identity.UniqueName))
555+
.DistinctBy(i => i.UniqueName, StringComparer.OrdinalIgnoreCase)
556+
.ToList();
557+
558+
if (!unmappedAuthors.Any()) return authorsLookup;
559+
560+
await using var writer = new StreamWriter(authorsLookupPath, append: true);
561+
if (!FileEndsInNewline(authorsFileStream)) await writer.WriteLineAsync();
562+
563+
foreach (var user in unmappedAuthors)
564+
await writer.WriteLineAsync($"{user.UniqueName} = {user.DisplayName} <email>");
565+
566+
var outMessageToWrite = $"""
567+
A valid email address must be added to the authors file for each of the following TFVC users:
568+
{string.Join(Environment.NewLine, unmappedAuthors.Select(a => a.UniqueName))}
569+
""";
570+
throw new CommandLineException(outMessageToWrite);
571+
}
572+
573+
private static ImmutableDictionary<string, Identity> LoadAuthors(Stream authorsFileStream, bool leaveStreamOpen)
568574
{
569575
var builder = ImmutableDictionary.CreateBuilder<string, Identity>(StringComparer.OrdinalIgnoreCase);
570576

571-
using var reader = new StreamReader(authorsFileStream);
577+
using var reader = new StreamReader(authorsFileStream, leaveOpen: leaveStreamOpen);
572578
while (reader.ReadLine() is { } line)
573579
{
574580
if (string.IsNullOrWhiteSpace(line)) continue;
@@ -660,9 +666,10 @@ public static bool FileEndsInNewline(FileStream fileStream)
660666
var streamStartsOnNewLine = true;
661667
if (fileStream.Length <= 0) return streamStartsOnNewLine;
662668

669+
var position = fileStream.Position;
663670
fileStream.Seek(-1, SeekOrigin.End);
664671
streamStartsOnNewLine = (char)((byte)fileStream.ReadByte()) == '\n';
665-
fileStream.Seek(0, SeekOrigin.Begin);
672+
fileStream.Seek(position, SeekOrigin.Begin);
666673

667674
return streamStartsOnNewLine;
668675
}

0 commit comments

Comments
 (0)