public static int Main(string[] args) { try { Console.WriteLine("NuGet bootstrapper {0}", typeof(Program).Assembly.GetName().Version); // Setup the proxy for gallery requests Uri galleryUri = new Uri(GalleryUrl); // Register a console based credentials provider so that the user get's prompted if a password // is required for the proxy HttpClient.DefaultCredentialProvider = new ConsoleCredentialProvider(); // Setup IHttpClient for the Gallery to locate packages var httpClient = new HttpClient(galleryUri); // Get the package from the feed var repository = new DataServicePackageRepository(httpClient); var packageMetadata = repository.GetPackages().Where(p => p.Id.ToLower() == NuGetCommandLinePackageId) .AsEnumerable() .OrderByDescending(p => Version.Parse(p.Version)) .FirstOrDefault(); if (packageMetadata != null) { Console.WriteLine("Found NuGet.exe version {0}.", packageMetadata.Version); Console.WriteLine("Downloading..."); Uri uri = repository.GetReadStreamUri(packageMetadata); var downloadClient = new HttpClient(uri); var packageStream = new MemoryStream(downloadClient.DownloadData()); using (Package package = Package.Open(packageStream)) { var fileUri = PackUriHelper.CreatePartUri(new Uri(NuGetExeFilePath, UriKind.Relative)); PackagePart nugetExePart = package.GetPart(fileUri); if (nugetExePart != null) { // Get the exe path and move it to a temp file (NuGet.exe.old) so we can replace the running exe with the bits we got // from the package repository string exePath = typeof(Program).Assembly.Location; string renamedPath = exePath + ".old"; Move(exePath, renamedPath); // Update the file UpdateFile(exePath, nugetExePart); Console.WriteLine("Update complete."); } } } return 0; } catch (Exception e) { WriteError(e); } return 1; }
public void RunFromGallery() { Directory.CreateDirectory(_targetDirectory); var client = new HttpClient(DeveloperFeed); client.SendingRequest += (sender, e) => { e.Request.Credentials = _credentials; e.Request.PreAuthenticate = true; }; var remoteRepo = new DataServicePackageRepository(client); var targetRepo = new LocalPackageRepository(_targetDirectory); var packages = remoteRepo.GetPackages() .Where(p => p.IsAbsoluteLatestVersion) .ToList(); Parallel.ForEach(packages, new ParallelOptions { MaxDegreeOfParallelism = 4 }, package => { // Some packages are updated without revving the version. We'll only opt not to re-download // a package if an identical version does not exist on disk. var existingPackage = targetRepo.FindPackage(package.Id, package.Version); var dataServicePackage = (DataServicePackage)package; if (existingPackage == null || !existingPackage.GetHash(dataServicePackage.PackageHashAlgorithm).Equals(dataServicePackage.PackageHash, StringComparison.Ordinal)) { Trace.WriteLine(string.Format("{0}: Adding package {1}", DateTime.Now, package.GetFullName())); var packagePath = GetPackagePath(package); using (var input = package.GetStream()) using (var output = File.Create(packagePath)) { input.CopyTo(output); } PurgeOldVersions(targetRepo, package); } }); }
static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Syntax: NugetDownloader targetFolder numOfPackages"); return; } var downloadFolder = new DirectoryInfo(args[0]); var packageCount = Int32.Parse(args[1]); var localRepository = new LocalPackageRepository(downloadFolder.FullName); DataServicePackageRepository remoteRepo = null; try { remoteRepo = new DataServicePackageRepository(nugetUri); var packages = remoteRepo.GetPackages() .Where(p => p.IsLatestVersion) .OrderByDescending(p => p.DownloadCount) .Take(packageCount); foreach (IPackage eachPackage in packages) { try { Console.Write(eachPackage.Id + " " + eachPackage.Version + " ... "); localRepository.AddPackage(eachPackage); Console.WriteLine("OK"); } catch (Exception ex) { Console.WriteLine("FAILED"); Console.WriteLine("[EXCEPTION] " + ex.Message); } } } catch (Exception ex) { Console.WriteLine("[EXCEPTION] " + ex.Message); return; } foreach (var file in downloadFolder.EnumerateFiles("*.nupkg", SearchOption.AllDirectories)) { try { var targetPath = Path.Combine(downloadFolder.FullName, file.Name); if (!File.Exists(targetPath)) file.MoveTo(targetPath); } catch (Exception ex) { Console.WriteLine("[EXCEPTION] " + ex.Message); } } foreach (var dir in downloadFolder.EnumerateDirectories()) { dir.Delete(true); } }