예제 #1
0
        internal async Task SelfUpdate(string exePath, NuGetVersion version)
        {
            Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandCheckingForUpdates"), NuGetConstants.DefaultFeedUrl);
            var metadataResource = _source.GetResource <MetadataResource>();

            if (metadataResource != null)
            {
                ResolutionContext resolutionContext = new ResolutionContext();
                var latestVersionKeyPair            = await metadataResource.GetLatestVersions(new List <string>() { NuGetCommandLinePackageId },
                                                                                               resolutionContext.IncludePrerelease, resolutionContext.IncludeUnlisted, CancellationToken.None);

                var lastetVersion = latestVersionKeyPair.FirstOrDefault().Value;

                if (version >= lastetVersion)
                {
                    Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandNuGetUpToDate"));
                }
                else
                {
                    Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandUpdatingNuGet"), lastetVersion.Version.ToString());

                    // Get NuGet.exe file
                    using (var targetPackageStream = new MemoryStream())
                    {
                        await PackageDownloader.GetPackageStream(_source, new PackageIdentity(NuGetCommandLinePackageId, lastetVersion), targetPackageStream, CancellationToken.None);

                        // If for some reason this package doesn't have NuGet.exe then we don't want to use it
                        if (targetPackageStream == null)
                        {
                            throw new CommandLineException(LocalizedResourceManager.GetString("UpdateCommandUnableToLocateNuGetExe"));
                        }

                        // 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 renamedPath = exePath + ".old";
                        Move(exePath, renamedPath);

                        // Update the file
                        UpdateFile(exePath, targetPackageStream);
                        Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandUpdateSuccessful"));
                    }
                }
            }
        }