internal static void GeInstalledNuGetPackages(PackageJson package, string requiredVersion, string minimumVersion, string maximumVersion, Dictionary <string, SoftwareIdentity> fastPackReftable, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "GeInstalledNuGetPackages' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", package.Name, requiredVersion, minimumVersion, maximumVersion)); //clone the request //nuget provider may not know the location of the package gets installed. //we need to pass around the destination path to nuget provider var installedRequest = PackageSourceListRequest.ExtendRequest( new Dictionary <string, string[]> { { "Destination", new[] { package.Destination ?? "" } } }, null, package.IsTrustedSource, request); var provider = PackageSourceListRequest.FindProvider(request, package.Type, request); if (provider != null) { //calling NuGet provider var packagesInstalled = provider.GetInstalledPackages(package.Name, requiredVersion, minimumVersion, maximumVersion, installedRequest); if (packagesInstalled != null) { foreach (var i in packagesInstalled) { request.Debug("Found an installed package '{0}.{1} from {2}' ", i.Name, i.Version, i.Source); var info = PackageSourceListRequest.MakeFastPathComplex(i.Source, i.Name, "", i.Version, ""); fastPackReftable.AddOrSet(info, i); // make it semver because in find-package we use semver var version = i.Version.CompareVersion(package.Version) ? package.Version : i.Version; request.YieldSoftwareIdentity(info, i.Name, version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename); } } } }
internal static void InstallPowershellArtifacts(PackageJson package, string fastPath, PackageSourceListRequest request) { var provider1 = PackageSourceListRequest.FindProvider(request, package.Type, request, true); if (provider1 == null) { return; } // As the PowerShellGet may access the -source via $request.Options or PackageSources, // so we need to fill in both options and sources parameters var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary <string, string[]> { { "Source", new[] { package.Source ?? "" } } }, new[] { package.Source ?? "" }, package.IsTrustedSource, request); var psid = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.PowerShellGet); var pkgs = request.PackageManagementService.FindPackageByCanonicalId(psid, installRequest) .Where(each => new SemanticVersion(each.Version) == new SemanticVersion(package.Version)).ToArray(); switch (pkgs.Length) { case 0: request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, psid); break; case 1: InstallPackageViaPowerShellGet(package, request, pkgs); break; default: request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, psid); break; } return; }
private static void InstallPackageViaPowerShellGet(PackageJson packageJson, PackageSourceListRequest request, SoftwareIdentity[] packages) { var provider = PackageSourceListRequest.FindProvider(request, packageJson.Type, request, true); if (provider == null) { return; } IHostApi installRequest = request; if (provider.Name.EqualsIgnoreCase(Constants.ProviderNames.PowerShellGet) && !request.ProviderServices.IsElevated) { // if we're not elevated, we want powershellget to install to the user scope installRequest = PackageSourceListRequest.ExtendRequest( new Dictionary <string, string[]> { { "Scope", new[] { "CurrentUser" } } }, null, packageJson.IsTrustedSource, request); } else { installRequest = PackageSourceListRequest.ExtendRequest( new Dictionary <string, string[]> { { "Destination", new[] { packageJson.Destination ?? "" } } }, null, packageJson.IsTrustedSource, request); } request.Debug("Calling '{0}' provider to install the package '{1}.{2}'", provider.Name, packageJson.Name, packageJson.Version); var installing = provider.InstallPackage(packages[0], installRequest); if (installing == null || !installing.Any()) { request.Verbose(Resources.Messages.NumberOfPackagesRecevied, 0, provider.Name, "InstallPackage"); request.Warning(Resources.Messages.FailToInstallPackage, Constants.ProviderName, packages[0].Name); return; } int packagesReceived = 0; foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } else { request.Verbose(Resources.Messages.SuccessfullyInstalled, "{0}.{1}".format(packageJson.Name, packageJson.Version)); //load provider if (packageJson.IsPackageProvider) { //Per provider development guidance: provider name and module name should be the same otherwise we can not import it. request.PackageManagementService.ImportPackageProvider(request, packageJson.Name, null, null, null, isRooted: false, force: false); } } packagesReceived++; } request.Verbose(Resources.Messages.NumberOfPackagesRecevied, packagesReceived, provider.Name, "install-package"); }
internal static void DownloadNuGetPackage(string fastPath, string location, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "DownloadNuGetPackage' - fastReference='{0}', location='{1}'", fastPath, location)); var package = request.GetPackageByFastPath(fastPath); if (package == null) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } // let the core figure out how to save this package var canonicalId = PackageSourceListRequest.CreateCanonicalId(package, Constants.ProviderNames.NuGet); // "nuget:jquery/2.1.0#http://nuget.org/api/v2"; var pkgs = request.PackageManagementService.FindPackageByCanonicalId(canonicalId, request.As <IHostApi>()) .Where(each => string.IsNullOrWhiteSpace(package.Version) || (new SemanticVersion(each.Version) == new SemanticVersion(package.Version))).ToArray(); switch (pkgs.Length) { case 0: request.Warning(Resources.Messages.CannotFindPackage, Constants.ProviderName, canonicalId); return; case 1: var provider = request.PackageManagementService.GetAvailableProviders(request, new[] { "NuGet" }).FirstOrDefault(); if (provider != null) { var donwloadrequest = PackageSourceListRequest.ExtendRequest( new Dictionary <string, string[]> { { "Destination", new[] { package.Destination ?? "" } } }, new[] { package.Source ?? "" }, package.IsTrustedSource, request); var downloading = provider.DownloadPackage(pkgs[0], location, donwloadrequest); foreach (var i in downloading) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, i.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { downloading.Cancel(); } } } break; default: request.Warning(Resources.Messages.FoundMorePackages, Constants.ProviderName, pkgs.Length, canonicalId); return; } }
private static void InstallPackageFile(PackageJson package, string fastPath, PackageSourceListRequest request) { request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackageFile' - name='{0}'", package.Name)); // get a temp file name, msi or msu var providerType = package.Type.ToLowerInvariant(); var destination = Path.ChangeExtension(Path.GetTempFileName(), providerType); try { //download the msi package to the temp file WebDownloader.DownloadFile(package.Source, destination, request, null); if (!File.Exists(destination)) { return; } // validate the file if (!WebDownloader.VerifyHash(destination, package, request)) { return; } // For unsigned msi packages, the MSI provider will prompt a user if he wants to install it. // However if the signed msi package but the PackageSource is untrusted, MSI provider does not prompt. So we prompt here. if (request.ProviderServices.IsSignedAndTrusted(destination, request)) { if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source)) { request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name); return; } } if (!string.IsNullOrWhiteSpace(package.Destination)) { request.Verbose(Resources.Messages.DestinationNotSupported, package.Type); } var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary <string, string[]> { { "Source", new[] { package.Source ?? "" } } }, new[] { package.Source ?? "" }, request); request.Verbose(Resources.Messages.CallMsiForInstall, package.Name); if (request.ProviderServices.Install(destination, "", installRequest)) { // it installed ok!exit request.YieldFromSwidtag(package, fastPath); return; } else { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.PackageFailedInstall, package.Name); } } finally { if (File.Exists(destination)) { destination.TryHardToDelete(); } } }