From b67d099ee8da500c5859102383cecad0405515b9 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:51:37 +0000 Subject: [PATCH] style: format code with dotnet-format, Prettier and StandardJS This commit fixes the style issues introduced in a599016 according to the output from dotnet-format, Prettier and StandardJS. Details: None --- Tasks/DetermineIfPackageExists.cs | 140 ++++++------- Tasks/GenerateDynamicTargets.cs | 255 +++++++++++++----------- Tasks/GetNuGetSources.cs | 172 ++++++++-------- Tasks/NuGetMSBuildLogger.cs | 196 +++++++++--------- Tasks/NuGetPackageTaskBase.cs | 110 +++++----- Tasks/NuGetTaskBase.cs | 84 ++++---- Tasks/PackageSourceCredentialService.cs | 24 +-- 7 files changed, 500 insertions(+), 481 deletions(-) diff --git a/Tasks/DetermineIfPackageExists.cs b/Tasks/DetermineIfPackageExists.cs index 04edb7bce6e..3bb20ab8168 100644 --- a/Tasks/DetermineIfPackageExists.cs +++ b/Tasks/DetermineIfPackageExists.cs @@ -18,74 +18,74 @@ namespace NuGetPush.Tasks; public class DetermineIfPackageExists() : NuGetPackageTaskBase { - [Required] - public string PackagePath { get; set; } - - [Output] - public bool PackageExists { get; private set; } - - public string PackageId => PackagePath.GetPackageId(); - - public string PackageVersion => PackagePath.GetPackageVersion(); - - public NuGetVersion NuGetVersion => NuGetVersion.Parse(PackageVersion); - - public override bool Execute() - { - try - { - return ValidateParameters(); - } - catch (Exception ex) - { - Log.LogErrorFromException(ex); - return false; - } - } - - public virtual bool DoesPackageExist() - { - try - { - var psi = new ProcessStartInfo("nuget", ["search", "-s", Source, PackageId, "-PreRelease"]) - { - RedirectStandardOutput = true - }; - var ps = Process.Start(psi); - ps.WaitForExit(); - return !ps.StandardOutput.EndOfStream; - - // TODO: Make this work with the NuGet API - // // Get the package metadata resource from the repository - // var resource = await Repository.GetResourceAsync(); - - // // Find all versions of the package - // var packageMetadata = await resource.GetMetadataAsync( - // PackageId, - // true, - // true, - // new SourceCacheContext(), - // Logger, - // CancellationToken.None - // ); - - // // Check if the specific version exists - // foreach (var metadata in packageMetadata.Select(metadata => metadata.Identity)) - // { - // if ( - // metadata.Id.Equals(PackageId, OrdinalIgnoreCase) - // && metadata.Version == NuGetVersion - // ) - // { - // return true; - // } - // } - } - catch (Exception ex) - { - Log.LogErrorFromException(ex); - } - - return false; - } + [Required] +public string PackagePath { get; set; } + +[Output] +public bool PackageExists { get; private set; } + +public string PackageId => PackagePath.GetPackageId(); + +public string PackageVersion => PackagePath.GetPackageVersion(); + +public NuGetVersion NuGetVersion => NuGetVersion.Parse(PackageVersion); + +public override bool Execute() +{ + try + { + return ValidateParameters(); + } + catch (Exception ex) + { + Log.LogErrorFromException(ex); + return false; + } +} + +public virtual bool DoesPackageExist() +{ + try + { + var psi = new ProcessStartInfo("nuget", ["search", "-s", Source, PackageId, "-PreRelease"]) + { + RedirectStandardOutput = true + }; + var ps = Process.Start(psi); + ps.WaitForExit(); + return !ps.StandardOutput.EndOfStream; + + // TODO: Make this work with the NuGet API + // // Get the package metadata resource from the repository + // var resource = await Repository.GetResourceAsync(); + + // // Find all versions of the package + // var packageMetadata = await resource.GetMetadataAsync( + // PackageId, + // true, + // true, + // new SourceCacheContext(), + // Logger, + // CancellationToken.None + // ); + + // // Check if the specific version exists + // foreach (var metadata in packageMetadata.Select(metadata => metadata.Identity)) + // { + // if ( + // metadata.Id.Equals(PackageId, OrdinalIgnoreCase) + // && metadata.Version == NuGetVersion + // ) + // { + // return true; + // } + // } + } + catch (Exception ex) + { + Log.LogErrorFromException(ex); + } + + return false; +} } diff --git a/Tasks/GenerateDynamicTargets.cs b/Tasks/GenerateDynamicTargets.cs index 7d29715864f..542f76ca37c 100644 --- a/Tasks/GenerateDynamicTargets.cs +++ b/Tasks/GenerateDynamicTargets.cs @@ -17,124 +17,143 @@ namespace NuGetPush.Tasks; public partial class GenerateDynamicTargets() : NuGetTaskBase { - public string? MSBuildProjectDirectory => Path.GetDirectoryName(ProjectFile); - - private string _packageId; - public string PackageId - { - get => _packageId ??= PackagePath.GetPackageId(); - set => _packageId = value; - } - - private string _packageVersion; - public string PackageVersion - { - get => _packageVersion ??= PackagePath.GetPackageId(); - set => _packageVersion = value; - } - - [Required] - public string PackagePath { get; set; } - - public string DynamicTargetsProjectFile { get; set; } - - protected GenerateDynamicTargets(ResourceManager taskResources) - : this() - { - Log.TaskResources = taskResources; - } - - protected GenerateDynamicTargets(ResourceManager taskResources, string helpKeywordPrefix) - : this(taskResources) - { - Log.HelpKeywordPrefix = helpKeywordPrefix; - } - - public override bool Execute() - { - if (!ValidateParameters()) - { - return false; - } - - DynamicTargetsProjectFile ??= Constants.DefaultDynamicTargetsFilePath; - - Log.LogWarning($"Generating dynamic targets file '{DynamicTargetsProjectFile}'..."); - - var sources = new List(); - - var packageSourceProvider = new PackageSourceProvider(Settings); - var packageSources = packageSourceProvider - .LoadPackageSources() - .Where(source => source.IsEnabled); - - // Retrieve API keys - var credentialService = new PackageSourceCredentialService(Settings); - - var dynamicTargetsSource = $""" - - {Join("\n", packageSources.Select(source => - $""" - - false - - - - - - - - - - - false - true - - - - - - - - - + public string? MSBuildProjectDirectory => Path.GetDirectoryName(ProjectFile); + +private string _packageId; +public string PackageId +{ + get => _packageId ??= PackagePath.GetPackageId(); + set => _packageId = value; +} + +private string _packageVersion; +public string PackageVersion +{ + get => _packageVersion ??= PackagePath.GetPackageId(); + set => _packageVersion = value; +} + +[Required] +public string PackagePath { get; set; } + +public string DynamicTargetsProjectFile { get; set; } + +protected GenerateDynamicTargets(ResourceManager taskResources) + : this() +{ + Log.TaskResources = taskResources; +} + +protected GenerateDynamicTargets(ResourceManager taskResources, string helpKeywordPrefix) + : this(taskResources) +{ + Log.HelpKeywordPrefix = helpKeywordPrefix; +} + +public override bool Execute() +{ + if (!ValidateParameters()) + { + return false; + } + + DynamicTargetsProjectFile ??= Constants.DefaultDynamicTargetsFilePath; + + Log.LogWarning($"Generating dynamic targets file '{DynamicTargetsProjectFile}'..."); + + var sources = new List(); + + var packageSourceProvider = new PackageSourceProvider(Settings); + var packageSources = packageSourceProvider + .LoadPackageSources() + .Where(source => source.IsEnabled); + + // Retrieve API keys + var credentialService = new PackageSourceCredentialService(Settings); + + var dynamicTargetsSource = $""" + < Project > + { + Join("\n", packageSources.Select(source => + $""" + < PropertyGroup > + + < Push{ SanitizeName(source.Name)} + IsEnabled Condition = "'$(Push{SanitizeName(source.Name)}IsEnabled)' == ''" > false + + + + +< Target Name = "Push{SanitizeName(source.Name)}" AfterTargets = "Pack" Condition = "'$(GeneratePackageOnBuild)' == 'true' And '$(Push{SanitizeName(source.Name)}IsEnabled)' == 'true'" > + +< !--< DetermineIfPackageExists PackagePath = "{PackagePath}" ProjectFile = "{ProjectFile}" Source = "{source.Source}" > + +< Output TaskParameter = "PackageExists" PropertyName = "PackageExists" /> + + --> + + +< Exec Command = "nuget search -s {source.Name} {PackageId} -PreRelease | grep {PackageId}" ConsoleToMsBuild = "true" IgnoreExitCode = "true" > + +< Output TaskParameter = "ConsoleOutput" ItemName = "ConsoleOutput" /> + + + + +< PropertyGroup > + +< PackageExists > false < !--default it to false-- > + +< PackageExists Condition = "'%(Lines.Identity)' != ''" > true + + + + +< Warning Text = "Package {PackageId} version {PackageVersion} already exists in source {source.Name}." Condition = "$(PackageExists)" /> + +< Warning Text = "Package {PackageId} version {PackageVersion} does not exist in source {source.Name}." Condition = "!$(PackageExists)" /> + + +< DeletePackage + PackageId = "{PackageId}" + PackageVersion = "{PackageVersion}" + Source = "{source.Source}" + ApiKey = "{credentialService.GetApiKey(source)}" + ProjectFile = "{ProjectFile}" + Condition = "$(PackageExists)" /> + + < PushPackage + Source = "{source.Source}" + ApiKey = "{credentialService.GetApiKey(source)}" + PackagePath = "{PackagePath}" + ProjectFile = "{ProjectFile}" /> + """ ))} - - """; - - File.WriteAllText(DynamicTargetsProjectFile, dynamicTargetsSource); - - return true; - } - - protected override bool ValidateParameters() - { - if (IsNullOrEmpty(PackagePath)) - { - Log.LogError("PackagePath is required."); - return false; - } - - return base.ValidateParameters(); - } - - private const string SanitizationRegexString = @"[^A-Za-z_]"; - - [GeneratedRegex(SanitizationRegexString)] - private static partial Regex SanitizationRegex(); - - public static string SanitizeName(string name) => SanitizationRegex().Replace(name, ""); + + """; + + File.WriteAllText(DynamicTargetsProjectFile, dynamicTargetsSource); + + return true; +} + +protected override bool ValidateParameters() +{ + if (IsNullOrEmpty(PackagePath)) + { + Log.LogError("PackagePath is required."); + return false; + } + + return base.ValidateParameters(); +} + +private const string SanitizationRegexString = @"[^A-Za-z_]"; + +[GeneratedRegex(SanitizationRegexString)] +private static partial Regex SanitizationRegex(); + +public static string SanitizeName(string name) => SanitizationRegex().Replace(name, ""); } diff --git a/Tasks/GetNuGetSources.cs b/Tasks/GetNuGetSources.cs index 40f96006d40..1b9e342a5fe 100644 --- a/Tasks/GetNuGetSources.cs +++ b/Tasks/GetNuGetSources.cs @@ -13,90 +13,90 @@ namespace NuGetPush.Tasks; public class GetNuGetSources() : NuGetTaskBase { - public string? MSBuildProjectDirectory => Path.GetDirectoryName(ProjectFile); - - public GetNuGetSources(ResourceManager taskResources) - : this() - { - Log.TaskResources = taskResources; - } - - public GetNuGetSources(ResourceManager taskResources, string helpKeywordPrefix) - : this(taskResources) - { - Log.HelpKeywordPrefix = helpKeywordPrefix; - } - - protected PackageSourceCredentialService _credentialService; - protected PackageSourceCredentialService PackageSourceCredentialService => - _credentialService ??= new PackageSourceCredentialService(Settings); - private IEnumerable _packageSources; - protected IEnumerable PackageSources => - _packageSources ??= PackageSourceProvider.LoadPackageSources(Settings); - - [Required] - public override string ProjectFile - { - get => base.ProjectFile; - set => base.ProjectFile = value; - } - - [Output] - public ITaskItem[] NuGetSources { get; set; } - - public override bool Execute() - { - Log.LogWarning("Config file paths: " + Join(", ", Settings.GetConfigFilePaths())); - - NuGetSources = [..PackageSources - .Select(source => - { - var item = new TaskItem(source.Source); - item.SetMetadata(Constants.MetadataNames.Name, source.Name); - - // Add API key if it exists - var apiKey = PackageSourceCredentialService.GetApiKey(source); - if (apiKey is not null) - { - item.SetMetadata(Constants.MetadataNames.ApiKey, apiKey); - } - return item; - })]; - - Log.LogWarning("Package sources: " + Join(", ", PackageSources.Select(src => src.Name))); - - // Log.LogWarning( - // $""" - // MSBuildProjectDirectory: {MSBuildProjectDirectory} - // BuildEngine.ProjectFileOfTaskNode: {BuildEngine.ProjectFileOfTaskNode} - // """ - // ); - - // var sources = new List(); - - // var packageSources = PackageSourceProvider.LoadPackageSources(Settings); - - // // Retrieve API keys - // var credentialService = new PackageSourceCredentialService(Settings); - - // foreach (var source in packageSources) - // { - // var item = new TaskItem(source.Source); - // item.SetMetadata("Include", source.Source); - // item.SetMetadata("Name", source.Name); - - // // Add API key if it exists - // var apiKey = credentialService.GetApiKey(source); - // if (apiKey is not null) - // { - // item.SetMetadata("ApiKey", apiKey); - // } - - // sources.Add(item); - // } - - // Sources = [..sources]; - - return true; - } + public string? MSBuildProjectDirectory => Path.GetDirectoryName(ProjectFile); + +public GetNuGetSources(ResourceManager taskResources) + : this() +{ + Log.TaskResources = taskResources; +} + +public GetNuGetSources(ResourceManager taskResources, string helpKeywordPrefix) + : this(taskResources) +{ + Log.HelpKeywordPrefix = helpKeywordPrefix; +} + +protected PackageSourceCredentialService _credentialService; +protected PackageSourceCredentialService PackageSourceCredentialService => + _credentialService ??= new PackageSourceCredentialService(Settings); +private IEnumerable _packageSources; +protected IEnumerable PackageSources => + _packageSources ??= PackageSourceProvider.LoadPackageSources(Settings); + +[Required] +public override string ProjectFile +{ + get => base.ProjectFile; + set => base.ProjectFile = value; +} + +[Output] +public ITaskItem[] NuGetSources { get; set; } + +public override bool Execute() +{ + Log.LogWarning("Config file paths: " + Join(", ", Settings.GetConfigFilePaths())); + + NuGetSources = [..PackageSources + .Select(source => + { + var item = new TaskItem(source.Source); + item.SetMetadata(Constants.MetadataNames.Name, source.Name); + + // Add API key if it exists + var apiKey = PackageSourceCredentialService.GetApiKey(source); + if (apiKey is not null) + { + item.SetMetadata(Constants.MetadataNames.ApiKey, apiKey); + } + return item; + })]; + + Log.LogWarning("Package sources: " + Join(", ", PackageSources.Select(src => src.Name))); + + // Log.LogWarning( + // $""" + // MSBuildProjectDirectory: {MSBuildProjectDirectory} + // BuildEngine.ProjectFileOfTaskNode: {BuildEngine.ProjectFileOfTaskNode} + // """ + // ); + + // var sources = new List(); + + // var packageSources = PackageSourceProvider.LoadPackageSources(Settings); + + // // Retrieve API keys + // var credentialService = new PackageSourceCredentialService(Settings); + + // foreach (var source in packageSources) + // { + // var item = new TaskItem(source.Source); + // item.SetMetadata("Include", source.Source); + // item.SetMetadata("Name", source.Name); + + // // Add API key if it exists + // var apiKey = credentialService.GetApiKey(source); + // if (apiKey is not null) + // { + // item.SetMetadata("ApiKey", apiKey); + // } + + // sources.Add(item); + // } + + // Sources = [..sources]; + + return true; +} } diff --git a/Tasks/NuGetMSBuildLogger.cs b/Tasks/NuGetMSBuildLogger.cs index 79c53b73573..16ae037d2d6 100644 --- a/Tasks/NuGetMSBuildLogger.cs +++ b/Tasks/NuGetMSBuildLogger.cs @@ -2,102 +2,102 @@ namespace NuGetPush.Tasks; public class NuGetMSBuildTaskLogger(MSBTask task) : NuGet.Common.ILogger { - public void LogDebug(string data) => LogMessage(MessageImportance.Low, data); - - public void LogVerbose(string data) => LogMessage(MessageImportance.Low, data); - - public void LogInformation(string data) => LogMessage(MessageImportance.Low, data); - - public void LogMinimal(string data) => LogMessage(MessageImportance.Low, data); - - public void LogWarning(string data) => LogMessage(MessageImportance.High, data); - - public void LogError(string data) => LogErrorMessage(message: data); - - public void LogError( - string data, - [CallerLineNumber] int lineNumber = -1, - [CallerFilePath] string filename = default!, - [CallerMemberName] string memberName = default! - ) => - LogErrorMessage( - file: filename, - lineNumber: lineNumber, - message: data, - senderName: memberName - ); - - public void LogInformationSummary(string data) => LogMessage(MessageImportance.Low, data); - - public TTask LogAsync(LogLevel level, string data) - { - LogMessage(MessageImportance.Low, data); - return TTask.CompletedTask; - } - - public TTask LogAsync(ILogMessage message) - { - LogMessage(message.ToString()); - return TTask.CompletedTask; - } - - public void Log(LogLevel level, string data) => LogMessage(data); - - public void Log(ILogMessage message) => LogMessage(message.ToString()); - - protected MSBTask Task => task; - protected IBuildEngine BuildEngine => Task.BuildEngine; - - protected void LogMessage(string message) => LogMessage(MessageImportance.Low, message); - - protected void LogMessage( - MessageImportance importance, - string message, - params object[] messageArgs - ) - { - var e = new BuildMessageEventArgs( - message, - helpKeyword: null, - senderName: Task.GetType().Name, - importance, - datetime.UtcNow, - messageArgs - ); - BuildEngine.LogMessageEvent(e); - } - - protected void LogErrorMessage( - string? subcategory = default, - string? code = default, - string? file = default, - int lineNumber = -1, - int columnNumber = -1, - int endLineNumber = -1, - int endColumnNumber = -1, - string? message = default, - string? helpKeyword = default, - string? senderName = default, - string? helpLink = default, - DateTime eventTimestamp = default, - params object[] messageArgs - ) - { - var e = new BuildErrorEventArgs( - subcategory, - code, - file, - lineNumber, - columnNumber, - endLineNumber, - endColumnNumber, - message, - helpKeyword, - senderName, - helpLink, - eventTimestamp, - messageArgs - ); - BuildEngine.LogErrorEvent(e); - } + public void LogDebug(string data) => LogMessage(MessageImportance.Low, data); + +public void LogVerbose(string data) => LogMessage(MessageImportance.Low, data); + +public void LogInformation(string data) => LogMessage(MessageImportance.Low, data); + +public void LogMinimal(string data) => LogMessage(MessageImportance.Low, data); + +public void LogWarning(string data) => LogMessage(MessageImportance.High, data); + +public void LogError(string data) => LogErrorMessage(message: data); + +public void LogError( + string data, + [CallerLineNumber] int lineNumber = -1, + [CallerFilePath] string filename = default!, + [CallerMemberName] string memberName = default! +) => + LogErrorMessage( + file: filename, + lineNumber: lineNumber, + message: data, + senderName: memberName + ); + +public void LogInformationSummary(string data) => LogMessage(MessageImportance.Low, data); + +public TTask LogAsync(LogLevel level, string data) +{ + LogMessage(MessageImportance.Low, data); + return TTask.CompletedTask; +} + +public TTask LogAsync(ILogMessage message) +{ + LogMessage(message.ToString()); + return TTask.CompletedTask; +} + +public void Log(LogLevel level, string data) => LogMessage(data); + +public void Log(ILogMessage message) => LogMessage(message.ToString()); + +protected MSBTask Task => task; +protected IBuildEngine BuildEngine => Task.BuildEngine; + +protected void LogMessage(string message) => LogMessage(MessageImportance.Low, message); + +protected void LogMessage( + MessageImportance importance, + string message, + params object[] messageArgs +) +{ + var e = new BuildMessageEventArgs( + message, + helpKeyword: null, + senderName: Task.GetType().Name, + importance, + datetime.UtcNow, + messageArgs + ); + BuildEngine.LogMessageEvent(e); +} + +protected void LogErrorMessage( + string? subcategory = default, + string? code = default, + string? file = default, + int lineNumber = -1, + int columnNumber = -1, + int endLineNumber = -1, + int endColumnNumber = -1, + string? message = default, + string? helpKeyword = default, + string? senderName = default, + string? helpLink = default, + DateTime eventTimestamp = default, + params object[] messageArgs +) +{ + var e = new BuildErrorEventArgs( + subcategory, + code, + file, + lineNumber, + columnNumber, + endLineNumber, + endColumnNumber, + message, + helpKeyword, + senderName, + helpLink, + eventTimestamp, + messageArgs + ); + BuildEngine.LogErrorEvent(e); +} } diff --git a/Tasks/NuGetPackageTaskBase.cs b/Tasks/NuGetPackageTaskBase.cs index bc1873dd2d2..608292bca16 100644 --- a/Tasks/NuGetPackageTaskBase.cs +++ b/Tasks/NuGetPackageTaskBase.cs @@ -7,59 +7,59 @@ namespace NuGetPush.Tasks; public abstract class NuGetPackageTaskBase() : NuGetTaskBase { protected NuGetPackageTaskBase(ResourceManager taskResources) - : this() - { - Log.TaskResources = taskResources; - } - - protected NuGetPackageTaskBase(ResourceManager taskResources, string helpKeywordPrefix) - : this(taskResources) - { - Log.HelpKeywordPrefix = helpKeywordPrefix; - } - - private PackageSource _packageSource; - public virtual PackageSource PackageSource => - _packageSource ??= PackageSourceProvider.GetPackageSourceByName(Source); - - private SourceRepository _repository; - - public virtual SourceRepository Repository => - _repository ??= NuGet.Protocol.Core.Types.Repository.Factory.GetCoreV3(SourceUrl); - - public virtual string SourceUrl => PackageSource.Source; - - [Required] - public string Source { get; set; } - - public string? MSBuildProjectDirectory => - Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode); - - private string? _apiKey; - public virtual string ApiKey - { - get => - _apiKey ??= SettingsUtility.GetValueForAddItem( - Settings, - ConfigurationConstants.ApiKeys, - PackageSourceProvider.ResolveAndValidateSource(Source) - ); - set => _apiKey = value; - } - - public override bool Execute() - { - return ValidateParameters(); - } - - protected override bool ValidateParameters() - { - if (IsNullOrEmpty(Source)) - { - Log.LogError("Source is required."); - return false; - } - - return base.ValidateParameters(); - } + : this() +{ + Log.TaskResources = taskResources; +} + +protected NuGetPackageTaskBase(ResourceManager taskResources, string helpKeywordPrefix) + : this(taskResources) +{ + Log.HelpKeywordPrefix = helpKeywordPrefix; +} + +private PackageSource _packageSource; +public virtual PackageSource PackageSource => + _packageSource ??= PackageSourceProvider.GetPackageSourceByName(Source); + +private SourceRepository _repository; + +public virtual SourceRepository Repository => + _repository ??= NuGet.Protocol.Core.Types.Repository.Factory.GetCoreV3(SourceUrl); + +public virtual string SourceUrl => PackageSource.Source; + +[Required] +public string Source { get; set; } + +public string? MSBuildProjectDirectory => + Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode); + +private string? _apiKey; +public virtual string ApiKey +{ + get => + _apiKey ??= SettingsUtility.GetValueForAddItem( + Settings, + ConfigurationConstants.ApiKeys, + PackageSourceProvider.ResolveAndValidateSource(Source) + ); + set => _apiKey = value; +} + +public override bool Execute() +{ + return ValidateParameters(); +} + +protected override bool ValidateParameters() +{ + if (IsNullOrEmpty(Source)) + { + Log.LogError("Source is required."); + return false; + } + + return base.ValidateParameters(); +} } diff --git a/Tasks/NuGetTaskBase.cs b/Tasks/NuGetTaskBase.cs index a61c320ffbf..bb682992e0d 100644 --- a/Tasks/NuGetTaskBase.cs +++ b/Tasks/NuGetTaskBase.cs @@ -3,46 +3,46 @@ namespace NuGetPush.Tasks; public abstract class NuGetTaskBase() : MSBTask { protected NuGetTaskBase(ResourceManager taskResources) - : this() - { - Log.TaskResources = taskResources; - } - - protected NuGetTaskBase(ResourceManager taskResources, string helpKeywordPrefix) - : this(taskResources) - { - Log.HelpKeywordPrefix = helpKeywordPrefix; - } - - [Required] - public virtual string ProjectFile { get; set; } - - private string _projectDirectory; - public string? ProjectDirectory => _projectDirectory ??= Path.GetDirectoryName(ProjectFile); - - private ISettings _settings; - protected ISettings Settings => - _settings ??= NuGetSettings.LoadDefaultSettings(ProjectDirectory); - - private PackageSourceProvider _packageSourceProvider; - protected PackageSourceProvider PackageSourceProvider => - _packageSourceProvider ??= new(Settings); - - private NuGetMSBuildTaskLogger? _logger; - public virtual NuGetMSBuildTaskLogger Logger - { - get => _logger ??= new(this); - set => _logger = value; - } - - protected virtual bool ValidateParameters() - { - if (IsNullOrEmpty(ProjectFile)) - { - Log.LogError("ProjectFile is required."); - return false; - } - - return true; - } + : this() +{ + Log.TaskResources = taskResources; +} + +protected NuGetTaskBase(ResourceManager taskResources, string helpKeywordPrefix) + : this(taskResources) +{ + Log.HelpKeywordPrefix = helpKeywordPrefix; +} + +[Required] +public virtual string ProjectFile { get; set; } + +private string _projectDirectory; +public string? ProjectDirectory => _projectDirectory ??= Path.GetDirectoryName(ProjectFile); + +private ISettings _settings; +protected ISettings Settings => + _settings ??= NuGetSettings.LoadDefaultSettings(ProjectDirectory); + +private PackageSourceProvider _packageSourceProvider; +protected PackageSourceProvider PackageSourceProvider => + _packageSourceProvider ??= new(Settings); + +private NuGetMSBuildTaskLogger? _logger; +public virtual NuGetMSBuildTaskLogger Logger +{ + get => _logger ??= new(this); + set => _logger = value; +} + +protected virtual bool ValidateParameters() +{ + if (IsNullOrEmpty(ProjectFile)) + { + Log.LogError("ProjectFile is required."); + return false; + } + + return true; +} } diff --git a/Tasks/PackageSourceCredentialService.cs b/Tasks/PackageSourceCredentialService.cs index 0b7d82f7578..90747b84d33 100644 --- a/Tasks/PackageSourceCredentialService.cs +++ b/Tasks/PackageSourceCredentialService.cs @@ -2,16 +2,16 @@ namespace NuGet.Configuration; public class PackageSourceCredentialService(ISettings settings) { - private ISettings Settings => settings; - - public string? GetApiKey(PackageSource source) - { - var apiKeys = Settings.GetSection(ConfigurationConstants.ApiKeys).Items.OfType(); - - var apiKey = apiKeys - .FirstOrDefault(key => key.Key.Equals(source.Source, OrdinalIgnoreCase)) - ?.Value; - - return apiKey; - } + private ISettings Settings => settings; + +public string? GetApiKey(PackageSource source) +{ + var apiKeys = Settings.GetSection(ConfigurationConstants.ApiKeys).Items.OfType(); + + var apiKey = apiKeys + .FirstOrDefault(key => key.Key.Equals(source.Source, OrdinalIgnoreCase)) + ?.Value; + + return apiKey; +} }