public void DownloadPrivate() { var repoConfig = new PackageRepositoryConfiguration { Source = "https://ci.appveyor.com/nuget/XXX", ClearTextPassword = "******", Username = "******", SourceName = "ThisIsGalleryName", ProtocolVersion = 2, }; var defaultWorkingDirectory = @"D:\Temp\NewNuGet"; var pm = new PackageRetriever(defaultWorkingDirectory, repoConfig); var package = pm.GetPackage(new PackageDescription { Id = "ThisIsPackage" }); Assert.NotNull(package.PackageFileBytes); }
/// <summary> /// Initializes a new instance of the <see cref="PackageRetriever"/> class. /// </summary> /// <param name="defaultWorkingDirectory"> /// Working directory to download temporary files to. /// </param> /// <param name="repoConfig"> /// Optional. Package repository configuration. /// If null then only the public gallery will be configured. /// </param> /// <param name="nugetExeFilePath"> /// Optional. Path to nuget.exe. /// If null then an embedded copy of nuget.exe will be used. /// </param> /// <param name="nugetConfigFilePath"> /// Optional. Path a nuget config xml file. If null then a config /// file will be written/used. This file will enable the public gallery /// as well as the gallery specified in <paramref name="repoConfig"/>. /// </param> public PackageRetriever(string defaultWorkingDirectory, PackageRepositoryConfiguration repoConfig = null, string nugetExeFilePath = null, string nugetConfigFilePath = null) { // check parameters if (!Directory.Exists(defaultWorkingDirectory)) { throw new ArgumentException("The specified default working directory does not exist."); } if ((repoConfig != null) && (nugetConfigFilePath != null)) { throw new ArgumentException("A repo config was specified along with a config XML file path. If you would like to include a package repository, either include it in the config XML or don't specify a config XML."); } // setup temp directory this.defaultWorkingDirectory = defaultWorkingDirectory; this.tempDirectory = Path.Combine(this.defaultWorkingDirectory, Path.GetRandomFileName()); Directory.CreateDirectory(this.tempDirectory); // write nuget.exe to disk if needed if (nugetExeFilePath == null) { nugetExeFilePath = Path.Combine(this.tempDirectory, NugetExeFileName); using (var nugetExeStream = AssemblyHelper.OpenEmbeddedResourceStream(NugetExeFileName)) { using (var fileStream = new FileStream(nugetExeFilePath, FileMode.Create, FileAccess.Write)) { nugetExeStream.CopyTo(fileStream); } } } this.nugetExeFilePath = nugetExeFilePath; // write NuGet.config to disk if needed if (nugetConfigFilePath == null) { nugetConfigFilePath = Path.Combine(this.tempDirectory, "NuGet.config"); string packageSource = string.Empty; string packageSourceCredentials = string.Empty; if (repoConfig != null) { packageSource = $@"<add key=""{repoConfig.SourceName}"" value=""{repoConfig.Source}"" />"; packageSourceCredentials = $@"<packageSourceCredentials> <{repoConfig.SourceName}> <add key=""Username"" value=""{repoConfig.Username}"" /> <add key=""ClearTextPassword"" value=""{repoConfig.ClearTextPassword}"" /> </{repoConfig.SourceName}> </packageSourceCredentials>"; } var configXml = $@"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> {packageSource} <add key=""nugetv2"" value=""https://www.nuget.org/api/v2/"" /> <add key=""nugetv3"" value=""https://api.nuget.org/v3/index.json"" /> </packageSources> {packageSourceCredentials} </configuration>"; File.WriteAllText(nugetConfigFilePath, configXml, Encoding.UTF8); } this.nugetConfigFilePath = nugetConfigFilePath; }