private string DownloadOrUseExistingNuGetPackage(string repoUrl, string packageName, string gitReference) { var nugetPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages", packageName, gitReference); if (Directory.Exists(nugetPath)) { return(nugetPath); } var packagesUrl = repoUrl + "/Packages(Id='" + packageName + "',Version='" + gitReference + "')"; var client = new RetryableWebClient(); var xmlString = client.DownloadString(packagesUrl); var xDocument = XDocument.Parse(xmlString); RedirectableConsole.WriteLine("Retrieved package information"); var title = xDocument.Root.Elements().First(x => x.Name.LocalName == "title"); var content = xDocument.Root.Elements().First(x => x.Name.LocalName == "content"); RedirectableConsole.WriteLine("Found NuGet package '" + title.Value + "'"); var downloadUrl = content.Attributes().First(x => x.Name.LocalName == "src").Value; // Some packages (like Protobuild packages), have a component of their version after a + symbol. Even // though NuGet gives us a download URL with that component in it, it doesn't actually exist when you // request it. var plusUrl = downloadUrl.IndexOf("%2b", StringComparison.InvariantCultureIgnoreCase); if (plusUrl != -1) { downloadUrl = downloadUrl.Substring(0, plusUrl); } plusUrl = downloadUrl.IndexOf("+", StringComparison.InvariantCultureIgnoreCase); if (plusUrl != -1) { downloadUrl = downloadUrl.Substring(0, plusUrl); } var downloadedZipData = _progressiveWebOperation.Get(downloadUrl); // Save the ZIP file onto disk. var tempFile = Path.GetTempFileName(); try { using (var writer = new FileStream(tempFile, FileMode.Truncate, FileAccess.Write)) { writer.Write(downloadedZipData, 0, downloadedZipData.Length); } Directory.CreateDirectory(nugetPath); RedirectableConsole.WriteLine("Extracting package to " + nugetPath + "..."); using (var zip = ZipStorer.Open(tempFile, FileAccess.Read)) { var files = zip.ReadCentralDir(); foreach (var file in files) { var targetPath = Path.Combine(nugetPath, file.FilenameInZip); var directory = new FileInfo(targetPath).DirectoryName; if (directory != null) { Directory.CreateDirectory(directory); } zip.ExtractFile(file, targetPath); } } RedirectableConsole.WriteLine("Extraction complete."); } finally { File.Delete(tempFile); } return(nugetPath); }
private string DownloadOrUseExistingNuGetPackage(string repoUrl, string packageName, string gitReference) { var nugetPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages", packageName, gitReference); if (Directory.Exists(nugetPath)) { return(nugetPath); } var packagesUrl = repoUrl + "/Packages(Id='" + packageName + "',Version='" + gitReference + "')"; var client = new RetryableWebClient(); var xmlString = client.DownloadString(packagesUrl); var xDocument = XDocument.Parse(xmlString); Console.WriteLine("Retrieved package information"); var title = xDocument.Root.Elements().First(x => x.Name.LocalName == "title"); var content = xDocument.Root.Elements().First(x => x.Name.LocalName == "content"); Console.WriteLine("Found NuGet package '" + title.Value + "'"); var downloadUrl = content.Attributes().First(x => x.Name.LocalName == "src").Value; var downloadedZipData = _progressiveWebOperation.Get(downloadUrl); // Save the ZIP file onto disk. var tempFile = Path.GetTempFileName(); try { using (var writer = new FileStream(tempFile, FileMode.Truncate, FileAccess.Write)) { writer.Write(downloadedZipData, 0, downloadedZipData.Length); } Directory.CreateDirectory(nugetPath); Console.WriteLine("Extracting package to " + nugetPath + "..."); using (var zip = ZipStorer.Open(tempFile, FileAccess.Read)) { var files = zip.ReadCentralDir(); foreach (var file in files) { var targetPath = Path.Combine(nugetPath, file.FilenameInZip); var directory = new FileInfo(targetPath).DirectoryName; if (directory != null) { Directory.CreateDirectory(directory); } zip.ExtractFile(file, targetPath); } } Console.WriteLine("Extraction complete."); } finally { File.Delete(tempFile); } return(nugetPath); }