public static void InstallNuGetPackage(this IProject project, IVsPackageInstallerServices vsPackageInstallerServices, IVsPackageInstaller vsPackageInstaller, IStatusBar StatusBar, string packageName) { try { var version = NugetPackageVersionManager.GetVersionFromCacheForPackage(packageName); if (!String.IsNullOrEmpty(version)) { StatusBar.DisplayMessage(String.Format("Installing Package: {0} {1}...", packageName, version)); try { InstallNugetPackageForSpecifiedVersion(project, vsPackageInstaller, packageName, version); } catch (Exception installException) { StatusBar.DisplayMessage(String.Format("When attempting to install version {0} of the package {1}, the following error occured: {2}.. Going to now try installing the latest version of Package ...", version, packageName, installException.Message)); // There was a problem installing the specified version of the package. Try the installing the latest available package from the source. InstallLatestNugetPackage(project, vsPackageInstallerServices, vsPackageInstaller, packageName); } } else { StatusBar.DisplayMessage(String.Format("Installing the latest version of Package: {0}...", packageName)); InstallLatestNugetPackage(project, vsPackageInstallerServices, vsPackageInstaller, packageName); } StatusBar.DisplayMessage(""); } catch (Exception ex) { throw new Exception(String.Format("NuGet Package {0} cannot be installed ({1}).", packageName, ex.Message), ex); } }
private static void InstallLatestNugetPackage(IProject project, IVsPackageInstallerServices vsPackageInstallerServices, IVsPackageInstaller vsPackageInstaller, string packageName) { vsPackageInstaller.InstallPackage("All", project.As <EnvDTE.Project>(), packageName, (Version)null, false); // Call the installed packages to get the version that was just installed and cache the version. var installedPackages = vsPackageInstallerServices.GetInstalledPackages(); NugetPackageVersionManager.UpdateCache(packageName, installedPackages); }
private static void InstallLatestNugetPackage(IProject project, IVsPackageInstallerServices vsPackageInstallerServices, IVsPackageInstaller vsPackageInstaller, string packageId, int?majorVersion) { // lookup latest version for the given major (or null), and install that var latestVersion = GetLatestVersionForMajor(packageId, majorVersion); vsPackageInstaller.InstallPackage( ServiceMatrixOverrides.GetNugetFeedServiceBaseAddress() ?? "All", project.As <EnvDTE.Project>(), packageId, latestVersion, false); // Call the installed packages to get the version that was just installed and cache the version. // Packages are needed in case latestVersion is null, var installedPackages = vsPackageInstallerServices.GetInstalledPackages(); NugetPackageVersionManager.UpdateCache(packageId, majorVersion, installedPackages); }