Skip to content

Commit

Permalink
refactor authors file interaction to own method
Browse files Browse the repository at this point in the history
  • Loading branch information
viningtechsola committed May 12, 2023
1 parent 2e59dfe commit ddf088f
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/TfvcMigrator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ private static RootPathChange ParseRootPathChange(string token)
if (repo is null)
return 1;

await using var authorsFileStream = LoadAuthorsFileStream(authors);
var fileHasNewline = FileEndsInNewline(authorsFileStream);
var authorsLookup = LoadAuthors(authorsFileStream);

Console.WriteLine("Connecting...");

using var connection = new VssConnection(
Expand Down Expand Up @@ -117,26 +113,11 @@ private static RootPathChange ParseRootPathChange(string token)
top: int.MaxValue)
).ConfigureAwait(false);

var unmappedAuthors = changesets.Select(c => c.Author)
.Concat(changesets.Select(c => c.CheckedInBy))
.Concat(allLabels.Select(l => l.Owner))
.Where(identity => !authorsLookup.ContainsKey(identity.UniqueName))
.DistinctBy(i => i.UniqueName, StringComparer.OrdinalIgnoreCase)
.ToList();

if (unmappedAuthors.Any())
{
Console.WriteLine("A valid email address must be added to the authors file for each of the following TFVC users:");
await using var writer = new StreamWriter(authors, append: true);
if (!fileHasNewline) await writer.WriteLineAsync();

foreach (var user in unmappedAuthors)
{
Console.WriteLine(user.UniqueName);
await writer.WriteLineAsync($"{user.UniqueName} = {user.DisplayName} <email>");
}
return 1;
}
var authorsLookup = await LoadAuthors(
authors,
changesets.Select(c => c.Author)
.Concat(changesets.Select(c => c.CheckedInBy))
.Concat(allLabels.Select(l => l.Owner)));

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

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

private static ImmutableDictionary<string, Identity> LoadAuthors(Stream authorsFileStream)
private static async Task<ImmutableDictionary<string, Identity>> LoadAuthors(string authorsLookupPath, IEnumerable<IdentityRef> changesetAuthors)
{
await using var authorsFileStream = LoadAuthorsFileStream(authorsLookupPath);
var authorsLookup = LoadAuthors(authorsFileStream, leaveStreamOpen: true);

var unmappedAuthors = changesetAuthors
.Where(identity => !authorsLookup.ContainsKey(identity.UniqueName))
.DistinctBy(i => i.UniqueName, StringComparer.OrdinalIgnoreCase)
.ToList();

if (!unmappedAuthors.Any()) return authorsLookup;

await using var writer = new StreamWriter(authorsLookupPath, append: true);
if (!FileEndsInNewline(authorsFileStream)) await writer.WriteLineAsync();

foreach (var user in unmappedAuthors)
await writer.WriteLineAsync($"{user.UniqueName} = {user.DisplayName} <email>");

var outMessageToWrite = $"""
A valid email address must be added to the authors file for each of the following TFVC users:
{string.Join(Environment.NewLine, unmappedAuthors.Select(a => a.UniqueName))}
""";
throw new CommandLineException(outMessageToWrite);
}

private static ImmutableDictionary<string, Identity> LoadAuthors(Stream authorsFileStream, bool leaveStreamOpen)
{
var builder = ImmutableDictionary.CreateBuilder<string, Identity>(StringComparer.OrdinalIgnoreCase);

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

var position = fileStream.Position;
fileStream.Seek(-1, SeekOrigin.End);
streamStartsOnNewLine = (char)((byte)fileStream.ReadByte()) == '\n';
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.Seek(position, SeekOrigin.Begin);

return streamStartsOnNewLine;
}
Expand Down

0 comments on commit ddf088f

Please sign in to comment.