private async Task InstallPackageAsync( string packageId, NuGetVersion version, string installPath) { if (version == null) { // Avoid searching for the highest version in the global packages folder, // it needs to come from the feeds instead. Once found it may come from // the global packages folder unless NoCache is true. ExcludeCacheAsSource = true; } var framework = GetTargetFramework(); // Create the project and set the framework if available. var project = new InstallCommandProject( root: installPath, packagePathResolver: new PackagePathResolver(installPath, !ExcludeVersion), targetFramework: framework); var sourceRepositoryProvider = GetSourceRepositoryProvider(); var packageManager = new NuGetPackageManager(sourceRepositoryProvider, Settings, installPath); var packageSources = GetPackageSources(Settings); var primaryRepositories = packageSources.Select(sourceRepositoryProvider.CreateRepository); Console.PrintPackageSources(packageSources); var allowPrerelease = Prerelease || (version != null && version.IsPrerelease); var dependencyBehavior = GetDependencyBehavior(); using (var sourceCacheContext = new SourceCacheContext()) { var resolutionContext = new ResolutionContext( dependencyBehavior, includePrelease: allowPrerelease, includeUnlisted: true, versionConstraints: VersionConstraints.None, gatherCache: new GatherCache(), sourceCacheContext: sourceCacheContext); if (version == null) { // Write out a helpful message before the http messages are shown Console.Log(LogLevel.Minimal, string.Format( CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallPackageMessage"), packageId, installPath)); // Find the latest version using NuGetPackageManager var resolvePackage = await NuGetPackageManager.GetLatestVersionAsync( packageId, project, resolutionContext, primaryRepositories, Console, CancellationToken.None); if (resolvePackage == null || resolvePackage.LatestVersion == null) { var message = string.Format( CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallCommandUnableToFindPackage"), packageId); throw new CommandLineException(message); } version = resolvePackage.LatestVersion; } // Get a list of packages already in the folder. var installedPackages = await project.GetFolderPackagesAsync(CancellationToken.None); // Find existing versions of the package var alreadyInstalledVersions = new HashSet <NuGetVersion>(installedPackages .Where(e => StringComparer.OrdinalIgnoreCase.Equals(packageId, e.PackageIdentity.Id)) .Select(e => e.PackageIdentity.Version)); var packageIdentity = new PackageIdentity(packageId, version); // Check if the package already exists or a higher version exists already. var skipInstall = project.PackageExists(packageIdentity); // For SxS allow other versions to install. For non-SxS skip if a higher version exists. skipInstall |= (ExcludeVersion && alreadyInstalledVersions.Any(e => e >= version)); if (skipInstall) { var message = string.Format( CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallCommandPackageAlreadyExists"), packageIdentity); Console.LogMinimal(message); } else { var signedPackageVerifier = new PackageSignatureVerifier( SignatureVerificationProviderFactory.GetSignatureVerificationProviders(), SignedPackageVerifierSettings.Default); var projectContext = new ConsoleProjectContext(Console) { PackageExtractionContext = new PackageExtractionContext( Packaging.PackageSaveMode.Defaultv2, PackageExtractionBehavior.XmlDocFileSaveMode, Console, signedPackageVerifier) }; if (EffectivePackageSaveMode != Packaging.PackageSaveMode.None) { projectContext.PackageExtractionContext.PackageSaveMode = EffectivePackageSaveMode; } resolutionContext.SourceCacheContext.NoCache = NoCache; resolutionContext.SourceCacheContext.DirectDownload = DirectDownload; var downloadContext = new PackageDownloadContext(resolutionContext.SourceCacheContext, installPath, DirectDownload); await packageManager.InstallPackageAsync( project, packageIdentity, resolutionContext, projectContext, downloadContext, primaryRepositories, Enumerable.Empty <SourceRepository>(), CancellationToken.None); if (downloadContext.DirectDownload) { GetDownloadResultUtility.CleanUpDirectDownloads(downloadContext); } } } }