Skip to content

Commit 3c5aaf9

Browse files
committed
Added an option to disable download progress bars.
1 parent 55649c4 commit 3c5aaf9

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

src/PostSharp.Engineering.BuildTools/CommonCommandSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ protected set
120120
[CommandOption( "--timeout" )]
121121
public double? Timeout { get; set; }
122122

123+
[Description( "Does not show the progress bar for downloads." )]
124+
[CommandOption( "--no-progress" )]
125+
public bool NoProgress { get; set; }
126+
123127
public ImmutableDictionary<string, string> Properties { get; protected set; } =
124128
ImmutableDictionary.Create<string, string>( StringComparer.OrdinalIgnoreCase );
125129

src/PostSharp.Engineering.BuildTools/Dependencies/DependenciesHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ private static bool DownloadBuild(
528528
Directory.CreateDirectory( restoreDirectory );
529529
context.Console.WriteMessage( $"Downloading {dependencyName} build #{buildNumber} of {ciBuildTypeId}" );
530530

531-
if ( !teamCity.TryDownloadArtifacts( context.Console, ciBuildTypeId, buildNumber, artifactsPath, restoreDirectory ) )
531+
if ( !teamCity.TryDownloadArtifacts( context.Console, ciBuildTypeId, buildNumber, artifactsPath, restoreDirectory, !context.Settings.NoProgress ) )
532532
{
533533
return false;
534534
}

src/PostSharp.Engineering.BuildTools/Tools/TeamCity/TeamCityClient.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ public bool TryGetBranchFromBuildNumber( ConsoleHelper console, CiBuildId buildI
128128
}
129129
}
130130

131-
public bool TryDownloadArtifacts( ConsoleHelper console, string buildTypeId, int buildNumber, string artifactsPath, string restoreDirectory )
131+
public bool TryDownloadArtifacts(
132+
ConsoleHelper console,
133+
string buildTypeId,
134+
int buildNumber,
135+
string artifactsPath,
136+
string restoreDirectory,
137+
bool showProgress )
132138
{
133139
IEnumerable<DownloadedFile> GetFiles( string urlOrPath, string targetDirectory )
134140
{
@@ -182,7 +188,7 @@ IEnumerable<DownloadedFile> GetFiles( string urlOrPath, string targetDirectory )
182188

183189
var files = GetFiles( basePath, baseTargetDirectory );
184190

185-
var success = FileDownloader.DownloadAsync( files, this._httpClient, console )
191+
var success = FileDownloader.DownloadAsync( files, this._httpClient, console, showProgress )
186192
.GetAwaiter()
187193
.GetResult();
188194

src/PostSharp.Engineering.BuildTools/Utilities/FileDownloader.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,42 @@ internal class FileDownloader : IDisposable
1616
private readonly SemaphoreSlim _throttler = new( 4, 4 );
1717
private readonly IEnumerable<DownloadedFile> _files;
1818
private readonly HttpClient _httpClient;
19-
private readonly ProgressContext _progressContext;
19+
private readonly ProgressContext? _progressContext;
2020
private readonly ConsoleHelper _console;
2121
private readonly StringTrimmer _descriptionTrimmer;
2222
private readonly CancellationToken _cancellationToken;
2323

2424
private bool _used;
2525

26-
public static Task<bool> DownloadAsync( IEnumerable<DownloadedFile> files, HttpClient httpClient, ConsoleHelper console )
26+
public static Task<bool> DownloadAsync( IEnumerable<DownloadedFile> files, HttpClient httpClient, ConsoleHelper console, bool showProgress )
2727
{
2828
var cancellationToken = ConsoleHelper.CancellationToken;
2929

30-
return Task.Run(
31-
() => AnsiConsole.Progress()
32-
.Columns(
33-
new TaskDescriptionColumn(),
34-
new ProgressBarColumn(),
35-
new DownloadedColumn(),
36-
new TransferSpeedColumn(),
37-
new RemainingTimeColumn(),
38-
new ElapsedTimeColumn(),
39-
new SpinnerColumn() )
40-
.StartAsync( ctx => new FileDownloader( files, httpClient, ctx, console, cancellationToken ).DownloadAsync() ),
41-
cancellationToken );
30+
if ( showProgress )
31+
{
32+
return Task.Run(
33+
() => AnsiConsole.Progress()
34+
.Columns(
35+
new TaskDescriptionColumn(),
36+
new ProgressBarColumn(),
37+
new DownloadedColumn(),
38+
new TransferSpeedColumn(),
39+
new RemainingTimeColumn(),
40+
new ElapsedTimeColumn(),
41+
new SpinnerColumn() )
42+
.StartAsync( ctx => new FileDownloader( files, httpClient, ctx, console, cancellationToken ).DownloadAsync() ),
43+
cancellationToken );
44+
}
45+
else
46+
{
47+
return new FileDownloader( files, httpClient, null, console, cancellationToken ).DownloadAsync();
48+
}
4249
}
4350

4451
private FileDownloader(
4552
IEnumerable<DownloadedFile> files,
4653
HttpClient httpClient,
47-
ProgressContext progressContext,
54+
ProgressContext? progressContext,
4855
ConsoleHelper console,
4956
CancellationToken cancellationToken )
5057
{
@@ -57,7 +64,7 @@ private FileDownloader(
5764
}
5865

5966
private async Task<(bool Result, DownloadedFile File, Exception? Exception)> DownloadFileAsync(
60-
ProgressTask progress,
67+
ProgressTask? progress,
6168
DownloadedFile file )
6269
{
6370
var attempt = 0;
@@ -70,13 +77,13 @@ private FileDownloader(
7077
{
7178
if ( attempt > 0 )
7279
{
73-
((IProgress<double>) progress).Report( 0 );
80+
((IProgress<double>?) progress)?.Report( 0 );
7481

7582
var delay = Math.Pow( 2, attempt );
7683

7784
for ( var i = delay; i > 0; i-- )
7885
{
79-
progress.Description( this._descriptionTrimmer.Trim( $"{file.Description} failed, retrying in {i} seconds" ) );
86+
progress?.Description( this._descriptionTrimmer.Trim( $"{file.Description} failed, retrying in {i} seconds" ) );
8087

8188
await Task.Delay( TimeSpan.FromSeconds( 1 ), this._cancellationToken );
8289
}
@@ -86,7 +93,7 @@ private FileDownloader(
8693

8794
if ( attempt == 0 )
8895
{
89-
progress.StartTask();
96+
progress?.StartTask();
9097
}
9198

9299
var response = await this._httpClient.GetAsync( file.SourceUrl, HttpCompletionOption.ResponseHeadersRead, this._cancellationToken );
@@ -110,7 +117,7 @@ private FileDownloader(
110117
while ( (bytesRead = await httpStream.ReadAsync( buffer, 0, buffer.Length, this._cancellationToken )) != 0 )
111118
{
112119
await fileStream.WriteAsync( buffer, 0, bytesRead, this._cancellationToken );
113-
progress.Increment( bytesRead );
120+
progress?.Increment( bytesRead );
114121
}
115122

116123
return (true, file, null);
@@ -124,7 +131,7 @@ private FileDownloader(
124131
continue;
125132
}
126133

127-
progress.Description( this._descriptionTrimmer.Trim( $"{file.Description} failed: {e.Message}" ) );
134+
progress?.Description( this._descriptionTrimmer.Trim( $"{file.Description} failed: {e.Message}" ) );
128135

129136
return (false, file, e);
130137
}
@@ -153,7 +160,7 @@ private async Task<bool> DownloadAsync()
153160

154161
foreach ( var file in this._files )
155162
{
156-
var progress = this._progressContext.AddTask( this._descriptionTrimmer.Trim( file.Description ), false, file.Length );
163+
var progress = this._progressContext?.AddTask( this._descriptionTrimmer.Trim( file.Description ), false, file.Length );
157164
var task = this.DownloadFileAsync( progress, file );
158165

159166
pendingDownloads.Add( task );

0 commit comments

Comments
 (0)