Skip to content

Commit

Permalink
Avoid async/await when unnecessary and other code cleanup (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast authored Jan 7, 2023
1 parent 54d59a5 commit c03504f
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public ConsoleCollectUserInput(InputOutputStreams io, ILogger<ConsoleCollectUser

public async Task<string?> AskUserAsync(string prompt)
{
await _io.Output.WriteLineAsync(prompt);
await _io.Output.WriteAsync(Prompt);
await _io.Output.WriteLineAsync(prompt).ConfigureAwait(false);
await _io.Output.WriteAsync(Prompt).ConfigureAwait(false);

return await _io.Input.ReadLineAsync();
return await _io.Input.ReadLineAsync().ConfigureAwait(false);
}

public async Task<T> ChooseAsync<T>(string message, IEnumerable<T> commands, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public ApplyNextCommand(UpgradeStep step)
// todo - support localization
public override string CommandText => $"Apply next step ({_step.Title})";

public override async Task<bool> ExecuteAsync(IUpgradeContext context, CancellationToken token)
public override Task<bool> ExecuteAsync(IUpgradeContext context, CancellationToken token)
{
return await _step.ApplyAsync(context, token).ConfigureAwait(false);
return _step.ApplyAsync(context, token);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public SkipNextCommand(UpgradeStep step)
// todo - support localization
public override string CommandText => $"Skip next step ({_step.Title})";

public override async Task<bool> ExecuteAsync(IUpgradeContext context, CancellationToken token)
public override Task<bool> ExecuteAsync(IUpgradeContext context, CancellationToken token)
{
return await _step.SkipAsync(token).ConfigureAwait(false);
return _step.SkipAsync(token);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static async Task<ApiCatalogModel> LoadCatalogAsync()
}
catch (Exception ex)
{
Console.Error.WriteLine($"error: can't open catalog: {ex.Message}");
await Console.Error.WriteLineAsync($"error: can't open catalog: {ex.Message}").ConfigureAwait(false);
Environment.Exit(1);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public ConfigUpdaterSubStep(UpgradeStep parentStep, IUpdater<ConfigFile> configU
_configUpdater = configUpdater ?? throw new ArgumentNullException(nameof(configUpdater));
}

protected override async Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
protected override Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
{
// Config updates don't apply until a project is selected
if (context?.CurrentProject is null)
{
return false;
return Task.FromResult(false);
}

// Check the config updater for an [ApplicableComponents] attribute
// If one exists, the step only applies if the project has the indicated components
return await context.CurrentProject.IsApplicableAsync(_configUpdater, token).ConfigureAwait(false);
return context.CurrentProject.IsApplicableAsync(_configUpdater, token).AsTask();
}

protected override async Task<UpgradeStepApplyResult> ApplyImplAsync(IUpgradeContext context, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public PackageAnalyzerResultProvider(IDependencyAnalyzerRunner packageAnalyzer,
_analysisState = null;
}

public async Task<bool> IsApplicableAsync(AnalyzeContext analysis, CancellationToken token)
public Task<bool> IsApplicableAsync(AnalyzeContext analysis, CancellationToken token)
{
return await Task.FromResult(true).ConfigureAwait(false);
return Task.FromResult(true);
}

public async IAsyncEnumerable<OutputResult> AnalyzeAsync(AnalyzeContext analysis, [EnumeratorCancellation] CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public CodeFixerStep(SourceUpdaterStep parentStep, IEnumerable<DiagnosticDescrip
Title = $"Apply fix for {DiagnosticId}{(diagnosticTitles is null ? string.Empty : ": " + string.Join(", ", diagnosticTitles))}";
}

protected override async Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
protected override Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
{
// Code updates don't apply until a project is selected
if (context?.CurrentProject is null)
{
return false;
return Task.FromResult(false);
}

// Check the code fix provider for an [ApplicableComponents] attribute
// If one exists, the step only applies if the project has the indicated components
return await context.CurrentProject.IsApplicableAsync(_fixProvider, token).ConfigureAwait(false);
return context.CurrentProject.IsApplicableAsync(_fixProvider, token).AsTask();
}

protected override Task<UpgradeStepInitializeResult> InitializeImplAsync(IUpgradeContext context, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public ValueTask<ProjectComponents> GetComponentsAsync(IProject project, Cancell

var references = project.References.Select(r => r.Name);

if (references.Any(r => _xamarinAndroidReferences.Contains(r, StringComparer.OrdinalIgnoreCase)))
foreach (var reference in references)
{
components |= ProjectComponents.XamarinAndroid;
}

if (references.Any(r => _xamariniOSReferences.Contains(r, StringComparer.OrdinalIgnoreCase)))
{
components |= ProjectComponents.XamariniOS;
if (_xamarinAndroidReferences.Contains(reference, StringComparer.OrdinalIgnoreCase))
{
components |= ProjectComponents.XamarinAndroid;
}
else if (_xamariniOSReferences.Contains(reference, StringComparer.OrdinalIgnoreCase))
{
components |= ProjectComponents.XamariniOS;
}
}

if (project.NuGetReferences.PackageReferences.Any(x => x.Name.Equals(_xamarinFormsPackage, StringComparison.OrdinalIgnoreCase)) && project.TargetFrameworks.FirstOrDefault().Equals(TargetFrameworkMoniker.NetStandard20))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static string GetTargetFramework(this IProject project)
}
else
{
if (tfv == string.Empty)
if (string.IsNullOrEmpty(tfv))
{
throw new InvalidOperationException($"{MSBuildFacts.LegacyTargetFrameworkVersionNodeName} is not set!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public RazorUpdaterSubStep(RazorUpdaterStep razorUpdaterStep, IUpdater<RazorCode
_updater = updater ?? throw new ArgumentNullException(nameof(updater));
}

protected override async Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
protected override Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
{
// Razor updates don't apply until a project is selected
if (context?.CurrentProject is null)
{
return false;
return Task.FromResult(false);
}

// Check the updater for an [ApplicableComponents] attribute
// If one exists, the step only applies if the project has the indicated components
return await context.CurrentProject.IsApplicableAsync(_updater, token).ConfigureAwait(false);
return context.CurrentProject.IsApplicableAsync(_updater, token).AsTask();
}

protected override async Task<UpgradeStepInitializeResult> InitializeImplAsync(IUpgradeContext context, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public WindowsDesktopUpdaterSubStep(WindowsDesktopUpdateStep windowsDesktopUpdat
_updater = updater ?? throw new ArgumentNullException(nameof(updater));
}

protected override async Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
protected override Task<bool> IsApplicableImplAsync(IUpgradeContext context, CancellationToken token)
{
if (context?.CurrentProject is null)
{
return false;
return Task.FromResult(false);
}

// Check the updater for an [ApplicableComponents] attribute
// If one exists, the step only applies if the project has the indicated components
return await context.CurrentProject.IsApplicableAsync(_updater, token).ConfigureAwait(false);
return context.CurrentProject.IsApplicableAsync(_updater, token).AsTask();
}

protected override async Task<UpgradeStepInitializeResult> InitializeImplAsync(IUpgradeContext context, CancellationToken token)
Expand Down

0 comments on commit c03504f

Please sign in to comment.