From 29e55aca3c6748a2fdce67e80d2212abbec5dea4 Mon Sep 17 00:00:00 2001 From: Kassem Sandarusi Date: Thu, 16 Jul 2020 15:17:22 -0500 Subject: [PATCH] Add nuget-config-dir parameter support. --- src/assembly-differ/Program.cs | 23 +++++++++++++++++-- src/assembly-differ/Providers/NuGet/NuGet.cs | 12 +++++++++- .../PreviousNuget/PreviousNugetLocator.cs | 3 +++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/assembly-differ/Program.cs b/src/assembly-differ/Program.cs index 1593bbf..a78c36e 100644 --- a/src/assembly-differ/Program.cs +++ b/src/assembly-differ/Program.cs @@ -21,6 +21,8 @@ internal static class Program private static SuggestedVersionChange _preventChange = SuggestedVersionChange.None; private static bool _allowEmptyPreviousNuget = false; + private static readonly NuGetInstallerOptions _nugetInstallerOptions = new NuGetInstallerOptions(); + private static HashSet Targets { get; } = new HashSet(StringComparer.OrdinalIgnoreCase); private static int Main(string[] args) @@ -28,8 +30,8 @@ private static int Main(string[] args) var providers = new AssemblyProviderFactoryCollection( new AssemblyProviderFactory(), new DirectoryAssemblyProviderFactory(), - new NuGetAssemblyProviderFactory(new Providers.NuGet.NuGet()), - new PreviousNuGetAssemblyProviderFactory(new PreviousNugetLocator()), + new NuGetAssemblyProviderFactory(new Providers.NuGet.NuGet(_nugetInstallerOptions)), + new PreviousNuGetAssemblyProviderFactory(new PreviousNugetLocator(_nugetInstallerOptions)), new GitHubAssemblyProviderFactory(new Git(Environment.GetEnvironmentVariable("GIT"))) ); @@ -49,6 +51,8 @@ private static int Main(string[] args) c => _preventChange = Enum.Parse(c, true)}, {"a|allow-empty-previous-nuget=", "", n => _allowEmptyPreviousNuget = n != null}, + {"c|nuget-config-dir=", "the search directory for NuGet to find a NuGet.config file. Defaults to `null` which will use the root config. Use only if using NuGet as a provider.", + c => SetNugetConfigSearchDirectory(c) }, {"h|?|help", "show this message and exit", h => _help = h != null}, }; @@ -165,6 +169,21 @@ private static void AddTarget(string input) Targets.Add(part); } + private static void SetNugetConfigSearchDirectory(string searchDirectory) + { + if (string.IsNullOrEmpty(searchDirectory)) + { + throw new ArgumentException("Cannot be null or empty", nameof(searchDirectory)); + } + + if (!Directory.Exists(searchDirectory)) + { + throw new DirectoryNotFoundException($"Could not find directory from supplied value of '{searchDirectory}'."); + } + + _nugetInstallerOptions.NuGetConfigSearchDirectory = searchDirectory; + } + private static void ShowHelp(OptionSet options, AssemblyProviderFactoryCollection providerFactoryCollection) { Console.ForegroundColor = ConsoleColor.Green; diff --git a/src/assembly-differ/Providers/NuGet/NuGet.cs b/src/assembly-differ/Providers/NuGet/NuGet.cs index ba3a4ce..14f7380 100644 --- a/src/assembly-differ/Providers/NuGet/NuGet.cs +++ b/src/assembly-differ/Providers/NuGet/NuGet.cs @@ -19,15 +19,25 @@ public interface INuGet NuGetPackage InstallPackage(string packageName, string version, string targetDirectory); } + public class NuGetInstallerOptions + { + public string NuGetConfigSearchDirectory { get; set; } + } + public class NuGet : INuGet { + protected NuGetInstallerOptions Options { get; } + + public NuGet(NuGetInstallerOptions options) + => Options = options; + public virtual NuGetPackage InstallPackage(string packageName, string version, string targetDirectory) { if (!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory); var packageVersion = NuGetVersion.Parse(version); var nuGetFramework = NuGetFramework.AnyFramework; - var settings = Settings.LoadDefaultSettings(root: null); + var settings = Settings.LoadDefaultSettings(root: Options?.NuGetConfigSearchDirectory); var sourceRepositoryProvider = new SourceRepositoryProvider( new PackageSourceProvider(settings), Repository.Provider.GetCoreV3()); diff --git a/src/assembly-differ/Providers/PreviousNuget/PreviousNugetLocator.cs b/src/assembly-differ/Providers/PreviousNuget/PreviousNugetLocator.cs index a872325..57cf0e0 100644 --- a/src/assembly-differ/Providers/PreviousNuget/PreviousNugetLocator.cs +++ b/src/assembly-differ/Providers/PreviousNuget/PreviousNugetLocator.cs @@ -13,6 +13,9 @@ namespace Differ.Providers.PreviousNuGet { public class PreviousNugetLocator : Differ.Providers.NuGet.NuGet { + public PreviousNugetLocator(NuGetInstallerOptions options) : base(options) + {} + public override NuGetPackage InstallPackage(string packageName, string currentVersion, string targetDirectory) { var nugetVersion = new NuGetVersion(currentVersion);