Skip to content

Commit

Permalink
Fix exceptions in the upsync tool raised while running with redirecte…
Browse files Browse the repository at this point in the history
…d console output. Console cursor location cannot be modified while running with console redirection; in such cases, the tool will print a new line instead of clearing the current line.
  • Loading branch information
Almost-Done committed Oct 13, 2022
1 parent 576d0ef commit 6ba94ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/tools/upsync/ContentSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ private static List<IContentFile> GetAllUpdateFiles(IMetadataStore metadataSourc

static string ContentSyncLastFileDigest = "";

private static void UpdateConsoleForMessageRefresh()
{
if (!Console.IsOutputRedirected)
{
Console.CursorLeft = 0;
}
else
{
Console.WriteLine();
}
}

private static void ContentStore_Progress(object sender, ObjectModel.ContentOperationProgress e)
{
if (e.File.Digest.DigestBase64 != ContentSyncLastFileDigest)
Expand All @@ -98,7 +110,7 @@ private static void ContentStore_Progress(object sender, ObjectModel.ContentOper
switch(e.CurrentOperation)
{
case ObjectModel.PackagesOperationType.DownloadFileProgress:
Console.CursorLeft = 0;
UpdateConsoleForMessageRefresh();
Console.Write("Sync'ing update content [{0}]: {1:000.00}%", e.Maximum, e.PercentDone);
break;
}
Expand Down
16 changes: 14 additions & 2 deletions src/tools/upsync/MetadataSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ private static void FetchMicrosoftUpdatePackages(FetchPackagesOptions options, I
}
}

private static void UpdateConsoleForMessageRefresh()
{
if (!Console.IsOutputRedirected)
{
Console.CursorLeft = 0;
}
else
{
Console.WriteLine();
}
}

/// <summary>
/// Handles progress notifications from a metadata query on an upstream server.
/// Prints progress information to the console
Expand Down Expand Up @@ -234,13 +246,13 @@ private static void Server_MetadataQueryProgress(object sender, MetadataQueryPro
break;

case MetadataQueryStage.GetUpdateMetadataEnd:
Console.CursorLeft = 0;
UpdateConsoleForMessageRefresh();
Console.Write("Retrieving updates metadata [{0}]: 100.00%", e.Maximum);
ConsoleOutput.WriteGreen(" Done!");
break;

case MetadataQueryStage.GetUpdateMetadataProgress:
Console.CursorLeft = 0;
UpdateConsoleForMessageRefresh();
Console.Write("Retrieving updates metadata [{0}]: {1:000.00}%", e.Maximum, e.PercentDone);
break;
}
Expand Down
20 changes: 17 additions & 3 deletions src/tools/upsync/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,23 @@ static void Main(string[] args)

private static readonly object ConsoleWriteLock = new();

private static void UpdateConsoleForMessageRefresh()
{
if (!Console.IsOutputRedirected)
{
Console.CursorLeft = 0;
}
else
{
Console.WriteLine();
}
}

public static void OnPackageCopyProgress(object sender, PackageStoreEventArgs e)
{
lock (ConsoleWriteLock)
{
Console.CursorLeft = 0;
UpdateConsoleForMessageRefresh();

if (e.Total == 0)
{
Expand All @@ -70,7 +82,7 @@ public static void OnOpenProgress(object sender, PackageStoreEventArgs e)
{
lock (ConsoleWriteLock)
{
Console.CursorLeft = 0;
UpdateConsoleForMessageRefresh();

if (e.Total == 0)
{
Expand All @@ -87,7 +99,9 @@ public static void OnPackageIndexingProgress(object sender, PackageStoreEventArg
{
lock(ProgressLock)
{
Console.CursorLeft = 0;
UpdateConsoleForMessageRefresh();


if (e.Total == 0)
{
Console.Write($"Indexing {e.Total} package(s)");
Expand Down

0 comments on commit 6ba94ff

Please sign in to comment.