internal static bool TestCatalogFile(string jsonFile, string catalogFile, PackageSourceListRequest request) { try { PSObject result = null; using (PowerShell powershell = PowerShell.Create()) { if (powershell != null) { result = powershell .AddCommand("Test-FileCatalog") .AddParameter("CatalogFilePath", catalogFile) .AddParameter("Path", jsonFile) .Invoke().FirstOrDefault(); } if (result.ToString().EqualsIgnoreCase("Valid")) { return(true); } } } catch (Exception ex) { request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailedWithError, catalogFile, ex.Message.ToString()); return(false); } request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailed, jsonFile); return(false); }
public void DownloadPackage(string fastPath, string location, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "DownloadPackage' - 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; } switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.MsiPackage: //No-op as the msi provider does not support save-package break; case Constants.MediaType.MsuPackage: //No-op as the msu provider does not support save-package break; case Constants.MediaType.AppxPackage: //TODO for future whenever needed to support appx packages break; case Constants.MediaType.NuGetPackage: NupkgInstaller.DownloadNuGetPackage(fastPath, location, request); break; case Constants.MediaType.ZipPackage: //TODO ZipPackageInstaller.DownloadZipPackage(fastPath, location, request); break; case Constants.MediaType.ExePackage: //TODO ExePackageInstaller.DownloadExePackage(fastPath, location, request); break; case Constants.MediaType.PsArtifacts: //TODO break; default: request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type); return; } }
internal static bool UnInstallZipPackage(PackageJson package, PackageSourceListRequest request, string fastPath) { string sourceLocation; string id; string displayName; string version; string path; string providerName; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out path, providerName: out providerName)) { request.Error(ErrorCategory.InvalidOperation, "package", Constants.Messages.UnableToUninstallPackage); return(false); } if (request.AddToPath.Value) { request.Warning(Resources.Messages.AddOrRemovePath, Constants.ProviderName, "AddToPath", "Install-Package"); } if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path)) { //Try to delete the directory to see if we can. Error out if we donot have permission if (Directory.Exists(path)) { try { Directory.Delete(path, true); } catch (System.UnauthorizedAccessException) { request.WriteError(ErrorCategory.InvalidOperation, package.Name, Resources.Messages.UninstallFailed, "UnInstall-Package", "UnauthorizedAccessException. The requested operation likely requires elevation, i.e., launch PowerShell as administer"); } } path.TryHardToDelete(); var dir = Path.Combine(package.Destination, package.Name); // delete an empty directory if (Directory.Exists(dir) && (!Directory.GetDirectories(dir).Any()) && !(Directory.GetFiles(dir).Any())) { dir.TryHardToDelete(); } request.YieldFromSwidtag(package, path); RemoveEnvironmentVariable(request, path, SystemEnvironmentKey); RemoveEnvironmentVariable(request, path, UserEnvironmentKey); return(true); } else { request.WriteError(ErrorCategory.InvalidData, path, Resources.Messages.DirectoryNotExist, Constants.ProviderName, path); return(false); } }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> public void UninstallPackage(string fastPackageReference, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, fastPackageReference); var package = request.GetFastReferenceComplex(fastPackageReference); if (package == null) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.AppxPackage: //TODO for the future break; case Constants.MediaType.ExePackage: ExePackageInstaller.UninstallExePackage(fastPackageReference, request); break; case Constants.MediaType.MsiPackage: UnInstallMsiPackage(request, fastPackageReference, package); break; case Constants.MediaType.ZipPackage: ZipPackageInstaller.UnInstallZipPackage(request, fastPackageReference); break; case Constants.MediaType.NuGetPackage: NupkgInstaller.UninstallNuGetPackage(package, fastPackageReference, request, _fastPackReftable); break; case Constants.MediaType.PsArtifacts: PowerShellArtifactInstaller.UninstallPowershellArtifacts(package, fastPackageReference, request, _fastPackReftable); break; default: request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type); break; } }
internal static bool VerifyHash(string fileFullPath, PackageJson package, PackageSourceListRequest request) { //skip in case the skip switch is specified if (request.SkipHashValidation.Value) { request.Verbose(Resources.Messages.SkipHashValidation); return(true); } PackageHash packageHash = package.Hash; if (packageHash == null || string.IsNullOrWhiteSpace(packageHash.algorithm) || string.IsNullOrWhiteSpace(packageHash.hashCode)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.HashNotSpecified, package.Name); return(false); } try { string hashAlgorithm = packageHash.algorithm.ToLowerInvariant(); if (!("sha256".Equals(hashAlgorithm) || "md5".Equals(hashAlgorithm) || "sha512".Equals(hashAlgorithm))) { request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.InvalidHashAlgorithm, packageHash.algorithm); return(false); } // compute the hash string computedHash = ComputeHash(fileFullPath, hashAlgorithm, request); if (computedHash == null) { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return(false); } // hash from json string hashFromJSON = package.Hash.hashCode; //compare computed hash with hash from json if (!hashFromJSON.Equals(computedHash)) { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return(false); } else { request.Verbose(Resources.Messages.HashValidationSuccessfull); } } catch { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return(false); } return(true); }
internal static void InstallProviderFromInstaller(PackageJson package, string fastPath, PackageSourceListRequest request) { request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallProviderFromInstaller' - name='{0}'", package.Name)); //install any dependency packages InstallDependencies(package, request); switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.MsiPackage: case Constants.MediaType.MsuPackage: #if CORECLR request.WriteError(ErrorCategory.InvalidOperation, package.Type, Resources.Messages.UnsupportedPowerShellEnvironment, Constants.ProviderName, "Install-Package", package.Type); #else InstallPackageFile(package, fastPath, request); #endif break; case Constants.MediaType.AppxPackage: //TODO for future whenever needed to support appx packages break; case Constants.MediaType.NuGetPackage: NupkgInstaller.InstallNuGetPackage(package, fastPath, request); break; case Constants.MediaType.ZipPackage: ZipPackageInstaller.InstallZipPackage(package, fastPath, request); break; case Constants.MediaType.ExePackage: #if CORECLR request.WriteError(ErrorCategory.InvalidOperation, package.Type, Resources.Messages.UnsupportedPowerShellEnvironment, Constants.ProviderName, "Install-Package", package.Type); #else ExePackageInstaller.InstallExePackage(package, fastPath, request); #endif break; case Constants.MediaType.PsArtifacts: PowerShellArtifactInstaller.InstallPowershellArtifacts(package, fastPath, request); break; default: request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type); break; } return; }
public void InstallPackage(string fastPath, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } if (request.PackageSources.Any(each => each.EndsWith(".json", StringComparison.OrdinalIgnoreCase))) { // Warning that PSL won't be able to handle the uninstall-package if the source is not registered. request.Warning(Resources.Messages.UsingSourceLocation, Constants.ProviderName); } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "InstallPackage"); promptForSampleJSON(request); var package = request.GetPackageByFastPath(fastPath); if (package == null) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } InstallProviderFromInstaller(package, fastPath, request); }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="package">package defined in the PackageSourceList</param> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> /// <param name="fastPackReftable"></param> internal static void UninstallNuGetPackage(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary <string, SoftwareIdentity> fastPackReftable) { if (request == null) { throw new ArgumentNullException("request"); } var destination = package.Destination; // PSL will call NuGet to uninstall the package when: 1) a user explictly specifies -providerName psl; or // 2) the psl.json file contains the defintion of Destination. // Otherwise, NuGet will handle the uninstall-package // string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider"); string providerNameFromPipeline = request.GetProviderNameFromFastPathComplex(fastPackageReference); if ((string.IsNullOrWhiteSpace(userSpecifiedProvider) || !Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider) && (string.IsNullOrWhiteSpace(providerNameFromPipeline) || !Constants.ProviderName.EqualsIgnoreCase(providerNameFromPipeline))) && string.IsNullOrWhiteSpace(destination)) { request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.NuGet); return; } request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, "Calling UninstallNuGetPackage"); var unInstallRequest = PackageSourceListRequest.ExtendRequest( new Dictionary <string, string[]> { { "Destination", new[] { destination } } }, null, request); var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true); if (provider != null) { request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name); if (!fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } var p = fastPackReftable[fastPackageReference]; //calling NuGet for uninstall var installing = provider.UninstallPackage(p, unInstallRequest); 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(); } } } }
private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package) { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath)); string sourceLocation; string id; string displayName; string version; string fastPackageReference; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference)) { //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package. request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } // Normally uninstall-package will be handled by MSI provider. Here we added a special case for handling uninstall-package nodejs // which msi provider unable to deal with (node.js only for msi) if (id != null && id.EqualsIgnoreCase("nodejs")) { var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request); if (provider != null) { if (!_fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name); var p = _fastPackReftable[fastPackageReference]; var installing = provider.UninstallPackage(p, request); foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } return; } } } else { //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="package">package defined in the PackageSourceList</param> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> /// <param name="fastPackReftable"></param> internal static void UninstallPowershellArtifacts(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary <string, SoftwareIdentity> fastPackReftable) { if (request == null) { throw new ArgumentNullException("request"); } // If a user explicitly specifies -providerName psl, e.g., uninstall-package -providername psl -name xjea, // PSL will call PowerShellGet to uninstall the package. // Otherwise, it is expected that PowerShellGet will handle the uninstall-package and PSL won't // participate in the uninstall operation, e.g., uninstall-package xjea string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider"); string providerNameFromPipeline = request.GetProviderNameFromFastPathComplex(fastPackageReference); if ((string.IsNullOrWhiteSpace(userSpecifiedProvider) || !Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider)) && string.IsNullOrWhiteSpace(providerNameFromPipeline) || !Constants.ProviderName.EqualsIgnoreCase(providerNameFromPipeline)) { request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.PowerShellGet); return; } request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, "Calling UninstallPowershellArtifacts"); var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true); if (provider != null) { request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name); if (!fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(Internal.ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } var p = fastPackReftable[fastPackageReference]; //calling NuGet for uninstall var installing = provider.UninstallPackage(p, request); 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(); } } } }
private static void InstallPackageFile(PackageJson package, string fastPath, PackageSourceListRequest request) { request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackageFile' - name='{0}', fastPath='{1}'", package.Name, fastPath)); // get a temp file name, msi or msu var providerType = package.Type.ToLowerInvariant(); var destination = Path.ChangeExtension(Path.GetTempFileName(), providerType); //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; } if (!package.IsTrustedSource) { if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source)) { request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name); return; } } var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary <string, string[]> { { "Source", new[] { package.Source ?? "" } } }, new[] { package.Source ?? "" }, package.IsTrustedSource, 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); } }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="package">package defined in the PackageSourceList</param> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> /// <param name="fastPackReftable"></param> internal static void UninstallNuGetPackage(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary <string, SoftwareIdentity> fastPackReftable) { if (request == null) { throw new ArgumentNullException("request"); } request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "UninstallNuGetPackage' - fastReference='{0}'", fastPackageReference)); var unInstallRequest = PackageSourceListRequest.ExtendRequest( new Dictionary <string, string[]> { { "Destination", new[] { package.Destination ?? "" } } }, null, package.IsTrustedSource, request); var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true); if (provider != null) { request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name); if (!fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } var p = fastPackReftable[fastPackageReference]; //calling NuGet for uninstall var installing = provider.UninstallPackage(p, unInstallRequest); 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(); } } } }
private bool ValidatePackageManagementVersion(PackageSourceListRequest request) { var userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider"); if (_currentPackageManagementVersion == null) { var moduleInfo = GetModule("PackageManagement"); if (moduleInfo == null || !moduleInfo.Any()) { request.Verbose(Resources.Messages.CannotFindPackageManagementVersion); return(false); } _currentPackageManagementVersion = moduleInfo.OrderByDescending(each => each.Version).FirstOrDefault().Version; if (_currentPackageManagementVersion < new Version(RequiredPackageManagementVersion)) { _doesPackageManagementVersionMatch = false; } else { _doesPackageManagementVersionMatch = true; } } if (!_doesPackageManagementVersionMatch) { if (userSpecifiedProvider != null && userSpecifiedProvider.IndexOf(Constants.ProviderName, StringComparison.OrdinalIgnoreCase) > -1) { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.MinimumVersionCheck, Constants.ProviderName, RequiredPackageManagementVersion, _currentPackageManagementVersion); } else { request.Verbose(Resources.Messages.MinimumVersionCheck, Constants.ProviderName, RequiredPackageManagementVersion, _currentPackageManagementVersion); } } return(_doesPackageManagementVersionMatch); }
public void InstallPackage(string fastPath, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackage' - fastReference='{0}'", fastPath)); var package = request.GetPackageByFastPath(fastPath); if (package == null) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } InstallProviderFromInstaller(package, fastPath, request); }
private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package) { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}'", package.Name)); string sourceLocation; string id; string displayName; string version; string fastPackageReference; string providerNameFromPipeline; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference, providerName: out providerNameFromPipeline)) { //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package. request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.Msi); return; } // Normally uninstall-package will be handled by MSI provider, so we do not need to handle it here. // But there are some special cases where MSI provider does not work well. // // For example, after installing the nodejs.msi (install-package nodejs), the product name appears to be "node.js". // After installing the PowerShell.msi package (install-package PowerShell), the product name appears to be "PowerShell_<Version>". // The msi provider works well if you pass in the product display name. In this case you need to use "PowerShell_Version" in // the Get-Package and UnInstall-Package. // But the msi provider will not work if you "Uninstall-Package PowerShell" or "Uninstall-Package nodejs" // Here we add some logic to let PSL try both well-known package name and the displayname. string userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider"); if ((!string.IsNullOrWhiteSpace(id) && (!string.IsNullOrWhiteSpace(displayName)) && (!id.EqualsIgnoreCase(displayName)) && (id.EqualsIgnoreCase(package.Name))) || (!string.IsNullOrWhiteSpace(providerNameFromPipeline) && Constants.ProviderName.EqualsIgnoreCase(providerNameFromPipeline)) || (!string.IsNullOrWhiteSpace(userSpecifiedProvider) && Constants.ProviderName.EqualsIgnoreCase(userSpecifiedProvider))) { var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request); if (provider != null) { if (!_fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name); var p = _fastPackReftable[fastPackageReference]; var installing = provider.UninstallPackage(p, request); foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } return; } } } else { //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it request.Verbose(Resources.Messages.UninstallPackageNotSupported, Constants.ProviderName, package.Name, Constants.ProviderNames.Msi); 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(); } } }
/// <summary> /// This is called when the user is adding (or updating) a package source /// /// If this PROVIDER doesn't support user-defined package sources, remove this method. /// </summary> /// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param> /// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param> /// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> public void AddPackageSource(string name, string location, bool trusted, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } try { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "AddPackageSource - ProviderName = '{0}', name='{1}', location='{2}', trusted='{3}'", PackageProviderName, name, location, trusted); // Error out if a user does not provide package source Name if (string.IsNullOrWhiteSpace(name)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Name, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Name); return; } if (string.IsNullOrWhiteSpace(location)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Location, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Location); return; } // Set-PackageSource will update the existing package source. In that case IsUpdate = true. var isUpdate = request.GetOptionValue(Constants.Parameters.IsUpdate).IsTrue(); request.Debug(Resources.Messages.VariableCheck, "IsUpdate", isUpdate); // check first that we're not clobbering an existing source, unless this is an update request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindRegisteredSource -name'{0}'", name)); var src = request.FindRegisteredSource(name); if (src != null && !isUpdate) { // tell the user that there's one here already request.WriteError(ErrorCategory.InvalidArgument, name, Constants.Messages.PackageSourceExists, name); return; } // conversely, if it didn't find one, and it is an update, that's bad too: if (src == null && isUpdate) { // you can't find that package source? Tell that to the user request.WriteError(ErrorCategory.ObjectNotFound, name, Constants.Messages.UnableToResolveSource, name); return; } // ok, we know that we're ok to save this source // next we check if the location is valid (if we support that kind of thing) var validated = false; validated = request.ValidateSourceLocation(location); if (!validated) { request.WriteError(ErrorCategory.InvalidData, name, Constants.Messages.SourceLocationNotValid, location); return; } else { request.Verbose(Resources.Messages.SuccessfullyValidated, name); } bool force = request.GetOptionValue("Force") != null; //if source is UNC location/ copy it to local path; Uri uri; if (Uri.TryCreate(location, UriKind.Absolute, out uri)) { if (uri.IsFile && uri.IsUnc) { string fileName = Path.GetFileNameWithoutExtension(location); string directory = Path.GetDirectoryName(location); string catalogFilePath = Path.Combine(directory, fileName + ".cat"); if (!File.Exists(catalogFilePath)) { request.WriteError(ErrorCategory.InvalidData, location, Resources.Messages.CatalogFileMissing, location); return; } if (!TestCatalogFile(location, catalogFilePath, request)) { return; } if (force || request.ShouldContinue(Resources.Messages.QueryDownloadPackageSourceList.format(location), Resources.Messages.PackageSourceListNotTrusted)) { string destination = Path.Combine(_pslDirLocation, Path.GetFileName(uri.LocalPath)); if (File.Exists(destination)) { if (force || request.ShouldContinue(Resources.Messages.OverwriteFile, Resources.Messages.FileExists)) { File.Copy(location, destination, true); location = destination; } else { return; } } else { File.Copy(location, destination); location = destination; } } else { return; } } } // it's good to check just before you actually write something to see if the user has cancelled the operation if (request.IsCanceled) { return; } // looking good -- store the package source. request.AddPackageSource(name, location, trusted, validated); // Yield the package source back to the caller. request.YieldPackageSource(name, location, trusted, true /*since we just registered it*/, validated); } catch (Exception e) { e.Dump(request); } }
internal static bool VerifyHash(string fileFullPath,PackageJson package, PackageSourceListRequest request) { //skip in case the skip switch is specified if (request.SkipHashValidation.Value) { request.Verbose(Resources.Messages.SkipHashValidation); return true; } PackageHash packageHash = package.Hash; if (packageHash==null || string.IsNullOrWhiteSpace(packageHash.algorithm) || string.IsNullOrWhiteSpace(packageHash.hashCode)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.HashNotSpecified, package.Name); return false; } try { HashAlgorithm hashAlgorithm = null; switch (packageHash.algorithm.ToLowerInvariant()) { case "sha256": hashAlgorithm = SHA256.Create(); break; case "md5": hashAlgorithm = MD5.Create(); break; case "sha512": hashAlgorithm = SHA512.Create(); break; default: request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.InvalidHashAlgorithm, packageHash.algorithm); return false; } using (FileStream stream = File.OpenRead(fileFullPath)) { // compute the hash byte[] computedHash = hashAlgorithm.ComputeHash(stream); // convert the original hash we got from json byte[] hashFromJSON = Convert.FromBase64String(package.Hash.hashCode); if (!Enumerable.SequenceEqual(computedHash, hashFromJSON)) { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return false; } else { request.Verbose(Resources.Messages.HashValidationSuccessful); } } } catch { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return false; } return true; }
/// <summary> /// This is called when the user is adding (or updating) a package source /// /// If this PROVIDER doesn't support user-defined package sources, remove this method. /// </summary> /// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param> /// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param> /// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> public void AddPackageSource(string name, string location, bool trusted, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } try { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "AddPackageSource - ProviderName = '{0}', name='{1}', location='{2}', trusted='{3}'", PackageProviderName, name, location, trusted); // Error out if a user does not provide package source Name if (string.IsNullOrWhiteSpace(name)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Name, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Name); return; } if (string.IsNullOrWhiteSpace(location)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.Parameters.Location, Constants.Messages.MissingRequiredParameter, Constants.Parameters.Location); return; } // Set-PackageSource will update the existing package source. In that case IsUpdate = true. var isUpdate = request.GetOptionValue(Constants.Parameters.IsUpdate).IsTrue(); request.Debug(Resources.Messages.VariableCheck, "IsUpdate", isUpdate); // check first that we're not clobbering an existing source, unless this is an update request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindRegisteredSource -name'{0}'", name)); var src = request.FindRegisteredSource(name); if (src != null && !isUpdate) { // tell the user that there's one here already request.WriteError(ErrorCategory.InvalidArgument, name, Constants.Messages.PackageSourceExists, name); return; } // conversely, if it didn't find one, and it is an update, that's bad too: if (src == null && isUpdate) { // you can't find that package source? Tell that to the user request.WriteError(ErrorCategory.ObjectNotFound, name, Constants.Messages.UnableToResolveSource, name); return; } // ok, we know that we're ok to save this source // next we check if the location is valid (if we support that kind of thing) var validated = false; validated = request.ValidateSourceLocation(location); if (!validated) { request.WriteError(ErrorCategory.InvalidData, name, Constants.Messages.SourceLocationNotValid, location); return; } else { request.Verbose(Resources.Messages.SuccessfullyValidated, name); } bool force = request.GetOptionValue("Force") != null; //if source is UNC location/ copy it to local path; Uri uri; if (Uri.TryCreate(location, UriKind.Absolute, out uri)) { if (uri.IsFile && uri.IsUnc) { string fileName = Path.GetFileNameWithoutExtension(location); string directory = Path.GetDirectoryName(location); string catalogFilePath = Path.Combine(directory, fileName+".cat"); if (!File.Exists(catalogFilePath)) { request.WriteError(ErrorCategory.InvalidData, location, Resources.Messages.CatalogFileMissing, location); return; } if (!TestCatalogFile(location, catalogFilePath, request)) { return; } if (force || request.ShouldContinue(Resources.Messages.QueryDownloadPackageSourceList.format(location), Resources.Messages.PackageSourceListNotTrusted)) { string destination = Path.Combine(_pslDirLocation, Path.GetFileName(uri.LocalPath)); if (File.Exists(destination)) { if (force || request.ShouldContinue(Resources.Messages.OverwriteFile, Resources.Messages.FileExists)) { File.Copy(location, destination, true); location = destination; } else { return; } } else { File.Copy(location, destination); location = destination; } } else { return; } } } // it's good to check just before you actually write something to see if the user has cancelled the operation if (request.IsCanceled) { return; } // looking good -- store the package source. request.AddPackageSource(name, location, trusted, validated); // Yield the package source back to the caller. request.YieldPackageSource(name, location, trusted, true /*since we just registered it*/, validated); } catch (Exception e) { e.Dump(request); } }
internal static void InstallProviderFromInstaller(PackageJson package, string fastPath, PackageSourceListRequest request) { request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "InstallProviderFromInstaller' - name='{0}', fastPath='{1}'", package.Name, fastPath)); //install any dependency packages InstallDependencies(package, request); switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.MsiPackage: case Constants.MediaType.MsuPackage: InstallPackageFile(package, fastPath, request); break; case Constants.MediaType.AppxPackage: //TODO for future whenever needed to support appx packages break; case Constants.MediaType.NuGetPackage: NupkgInstaller.InstallNuGetPackage(package, fastPath, request); break; case Constants.MediaType.ZipPackage: ZipPackageInstaller.InstallZipPackage(package, fastPath, request); break; case Constants.MediaType.ExePackage: ExePackageInstaller.InstallExePackage(package, fastPath, request); break; case Constants.MediaType.PsArtifacts: PowerShellArtifactInstaller.InstallPowershellArtifacts(package, fastPath, request); break; default: request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type); break; } return; }
internal static string DownloadFile(string queryUrl, string destination, PackageSourceListRequest request, ProgressTracker progressTracker) { try { request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(System.Globalization.CultureInfo.InvariantCulture, "DownloadFile - url='{0}', destination='{1}'", queryUrl, destination)); if (string.IsNullOrWhiteSpace(destination)) { throw new ArgumentNullException("destination"); } // make sure that the parent folder is created first. var folder = Path.GetDirectoryName(destination); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } if (File.Exists(destination)) { destination.TryHardToDelete(); } if (progressTracker == null) { progressTracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.DownloadingPackage, queryUrl)); } Uri uri; if (!Uri.TryCreate(queryUrl, UriKind.Absolute, out uri)) { request.WriteError(Internal.ErrorCategory.InvalidOperation, "", Resources.Messages.UnsuportedUriFormat, Constants.ProviderName, queryUrl); return(null); } if (uri.IsFile) { // downloading from a file share using (var input = File.OpenRead(queryUrl)) { using (var output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.Read)) { request.Progress(progressTracker.ProgressID, progressTracker.StartPercent, Resources.Messages.Downloading); input.CopyTo(output); } } request.CompleteProgress(progressTracker.ProgressID, true); } else { //Downloading from url var result = DownloadDataToFileAsync(destination, queryUrl, request, PathUtility.GetNetworkCredential(request.CredentialUsername, request.CredentialPassword), progressTracker).Result; } if (File.Exists(destination)) { request.Verbose(Resources.Messages.CompletedDownload, queryUrl); return(destination); } else { request.WriteError(ErrorCategory.InvalidOperation, queryUrl, Resources.Messages.FailedToDownload, Constants.ProviderName, queryUrl); return(null); } } catch (Exception ex) { ex.Dump(request); request.Warning(ex.Message); return(null); } }
private void UnInstallMsiPackage(PackageSourceListRequest request, string fastPath, PackageJson package) { request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "UnInstallMsiPackage' - name='{0}', fastPath='{1}'", package.Name, fastPath)); string sourceLocation; string id; string displayName; string version; string fastPackageReference; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference)) { //we don't need to error out even if fastpath is not correct because msi provider is expected to handle the uninstall-package. request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } // Normally uninstall-package will be handled by MSI provider. Here we added a special case for handling uninstall-package nodejs // which msi provider unable to deal with (node.js only for msi) if (id != null && id.EqualsIgnoreCase("nodejs")) { var provider = PackageSourceListRequest.FindProvider(request, Constants.ProviderNames.Msi, request); if (provider != null) { if (!_fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } request.Verbose(Resources.Messages.UninstallingPackage, Constants.ProviderName, package.Name); var p = _fastPackReftable[fastPackageReference]; var installing = provider.UninstallPackage(p, request); foreach (var i in installing) { request.YieldSoftwareIdentity(i.FastPackageReference, i.Name, i.Version, i.VersionScheme, i.Summary, package.Source, i.SearchKey, i.FullPath, i.PackageFilename); if (request.IsCanceled) { installing.Cancel(); } return; } } } else { //no-op for uninstalling the msi packages. only install-package nodejs is supported because msi can not handle it request.Verbose(Resources.Messages.UnsupportMSIUninstall, Constants.ProviderName, package.Name); return; } }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> public void UninstallPackage(string fastPackageReference, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, fastPackageReference); var package = request.GetFastReferenceComplex(fastPackageReference); if (package == null) { request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.AppxPackage: //TODO for the future break; case Constants.MediaType.ExePackage: ExePackageInstaller.UninstallExePackage(fastPackageReference, request); break; case Constants.MediaType.MsiPackage: UnInstallMsiPackage(request, fastPackageReference, package); break; case Constants.MediaType.ZipPackage: ZipPackageInstaller.UnInstallZipPackage(request, fastPackageReference); break; case Constants.MediaType.NuGetPackage: NupkgInstaller.UninstallNuGetPackage(package, fastPackageReference, request, _fastPackReftable); break; case Constants.MediaType.PsArtifacts: PowerShellArtifactInstaller.UninstallPowershellArtifacts(package, fastPackageReference, request, _fastPackReftable); break; default: request.WriteError(ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type); break; } }
internal static bool InstallExePackage(PackageJson package, string fastPath, PackageSourceListRequest request) { ProgressTracker tracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.Installing)); var providerType = package.Type.ToLowerInvariant(); var exePackage = Path.ChangeExtension(Path.GetTempFileName(), providerType); if (!string.IsNullOrWhiteSpace(package.Destination)) { request.Verbose(Resources.Messages.DestinationNotSupported, package.Type); } try { WebDownloader.DownloadFile(package.Source, exePackage, request, tracker); if (File.Exists(exePackage)) { request.Verbose("Package: '{0}'", exePackage); // validate the file if (!WebDownloader.VerifyHash(exePackage, package, request)) { return(false); } if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source)) { request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name); return(false); } // Prepare the process to run var processArguments = string.IsNullOrWhiteSpace(package.InstallArguments) ? "/VERYSILENT /CLOSEAPPLICATIONS /NORESTART /NOCANCEL /SP /qn" : package.InstallArguments; var start = new ProcessStartInfo { FileName = exePackage, Arguments = processArguments, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, //LoadUserProfile = true, }; double percent = tracker.StartPercent; Timer timer = null; object timerLock = new object(); bool cleanUp = false; Action cleanUpAction = () => { lock (timerLock) { // check whether clean up is already done before or not if (!cleanUp) { try { if (timer != null) { // stop timer timer.Change(Timeout.Infinite, Timeout.Infinite); // dispose it timer.Dispose(); } } catch { } cleanUp = true; } } }; // Run the external process & wait for it to finish using (var proc = Process.Start(start)) { var timer1 = timer; timer = new Timer(_ => { percent += 0.025; // percent between startProgress and endProgress var progressPercent = tracker.ConvertPercentToProgress(percent); if (progressPercent < 90) { request.Progress(tracker.ProgressID, (int)progressPercent, Resources.Messages.InstallingPackage, package.Source); } if (request.IsCanceled) { cleanUpAction(); } }, null, 100, 3000); proc.WaitForExit(); // Retrieve the app's exit code var exitCode = proc.ExitCode; if (exitCode != 0) { request.WriteError(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.InstallFailed, package.Name, proc.StandardError.ReadToEnd()); request.CompleteProgress(tracker.ProgressID, false); return(false); } else { request.CompleteProgress(tracker.ProgressID, true); request.YieldFromSwidtag(package, fastPath); request.Verbose(Resources.Messages.SuccessfullyInstalled, package.Name); } cleanUpAction(); } return(true); } else { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.FailedToDownload, Constants.ProviderName, package.Source); } return(false); } catch (Exception ex) { request.Error(ErrorCategory.InvalidOperation, "install-package", ex.Message); ex.Dump(request); return(false); } finally { if (File.Exists(exePackage)) { exePackage.TryHardToDelete(); } } }
internal static bool VerifyHash(string fileFullPath, PackageJson package, PackageSourceListRequest request) { //skip in case the skip switch is specified if (request.SkipHashValidation.Value) { request.Verbose(Resources.Messages.SkipHashValidation); return(true); } PackageHash packageHash = package.Hash; if (packageHash == null || string.IsNullOrWhiteSpace(packageHash.algorithm) || string.IsNullOrWhiteSpace(packageHash.hashCode)) { request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.HashNotSpecified, package.Name); return(false); } try { HashAlgorithm hashAlgorithm = null; switch (packageHash.algorithm.ToLowerInvariant()) { case "sha256": hashAlgorithm = SHA256.Create(); break; case "md5": hashAlgorithm = MD5.Create(); break; case "sha512": hashAlgorithm = SHA512.Create(); break; default: request.WriteError(ErrorCategory.InvalidArgument, Constants.ProviderName, Resources.Messages.InvalidHashAlgorithm, packageHash.algorithm); return(false); } using (FileStream stream = File.OpenRead(fileFullPath)) { // compute the hash byte[] computedHash = hashAlgorithm.ComputeHash(stream); // convert the original hash we got from json byte[] hashFromJSON = Convert.FromBase64String(package.Hash.hashCode); if (!Enumerable.SequenceEqual(computedHash, hashFromJSON)) { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return(false); } else { request.Verbose(Resources.Messages.HashValidationSuccessful); } } } catch { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.HashVerificationFailed, package.Name, package.Source); return(false); } return(true); }
public void DownloadPackage(string fastPath, string location, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "DownloadPackage' - 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; } switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.MsiPackage: //No-op as the msi provider does not support save-package break; case Constants.MediaType.MsuPackage: //No-op as the msu provider does not support save-package break; case Constants.MediaType.AppxPackage: //TODO for future whenever needed to support appx packages break; case Constants.MediaType.NuGetPackage: NupkgInstaller.DownloadNuGetPackage(fastPath, location, request); break; case Constants.MediaType.ZipPackage: //TODO ZipPackageInstaller.DownloadZipPackage(fastPath, location, request); break; case Constants.MediaType.ExePackage: //TODO ExePackageInstaller.DownloadExePackage(fastPath, location, request); break; case Constants.MediaType.PsArtifacts: //TODO break; default: request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.UnknownMediaType, package.Name, package.Source, package.Type); 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}', fastPath='{1}'", package.Name, fastPath)); // get a temp file name, msi or msu var providerType = package.Type.ToLowerInvariant(); var destination = Path.ChangeExtension(Path.GetTempFileName(), providerType); //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; } if (!package.IsTrustedSource) { if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source)) { request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name); return; } } var installRequest = PackageSourceListRequest.ExtendRequest(new Dictionary<string, string[]> { {"Source", new[] {package.Source ?? ""}} }, new[] { package.Source ?? "" }, package.IsTrustedSource, 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); } }
public void GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "GetInstalledPackages' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion)); IEnumerable <PackageJson> packagesDefinedInJsonSpec; if (string.IsNullOrWhiteSpace(name) || WildcardPattern.ContainsWildcardCharacters(name)) { // In the case of the package name is null or contains wildcards, error out if a user puts version info if (!string.IsNullOrWhiteSpace(requiredVersion) || !string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrWhiteSpace(maximumVersion)) { request.Warning(Resources.Messages.WildCardCharsAreNotSupported, name); return; } packagesDefinedInJsonSpec = request.GetPackages(name).ToArray(); } else { //return all installed packagesDefinedInJsonSpec = request.GetPackages(name, requiredVersion, minimumVersion, maximumVersion).ToArray(); } _fastPackReftable.Clear(); if (!packagesDefinedInJsonSpec.Any()) { request.Verbose(Resources.Messages.NoPackageFound, Constants.ProviderName); return; } foreach (var package in packagesDefinedInJsonSpec) { switch (package.Type.ToLowerInvariant()) { case Constants.MediaType.AppxPackage: //TODO for future break; case Constants.MediaType.PsArtifacts: PowerShellArtifactInstaller.GetInstalledPowershellArtifacts(package, requiredVersion, minimumVersion, maximumVersion, _fastPackReftable, request); break; case Constants.MediaType.ExePackage: #if CORECLR request.WriteError(ErrorCategory.InvalidOperation, package.Type, Resources.Messages.UnsupportedPowerShellEnvironment, Constants.ProviderName, "Get-Package", package.Type); #else //program provider can handle get-package git for git.exe ExePackageInstaller.GetInstalledExePackages(package, requiredVersion, minimumVersion, minimumVersion, request); #endif break; case Constants.MediaType.MsiPackage: #if CORECLR request.WriteError(ErrorCategory.InvalidOperation, package.Type, Resources.Messages.UnsupportedPowerShellEnvironment, Constants.ProviderName, "Get-Package", package.Type); #else //msi provider can handle get-package node.js for node.js.msi GetMsiInstalledPackage(name, package, requiredVersion, minimumVersion, maximumVersion, request); #endif break; case Constants.MediaType.ZipPackage: ZipPackageInstaller.GetInstalledZipPackage(package, request); break; case Constants.MediaType.NuGetPackage: NupkgInstaller.GeInstalledNuGetPackages(package, requiredVersion, minimumVersion, maximumVersion, _fastPackReftable, request); break; } //switch } }
internal static bool InstallZipPackage(PackageJson package, string fastPath, PackageSourceListRequest request) { if (request.RemoveFromPath.Value) { request.Warning(Resources.Messages.AddOrRemovePath, Constants.ProviderName, "RemoveFromPath", "Uninstall-Package"); } // download the exe package var providerType = package.Type.ToLowerInvariant(); var file = Path.ChangeExtension(Path.GetTempFileName(), providerType); if (string.IsNullOrWhiteSpace(package.Destination)) { request.Error(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.DestinationRequired); return(false); } WebDownloader.DownloadFile(package.Source, file, request, null); if (!File.Exists(file)) { return(false); } // validate the file if (!WebDownloader.VerifyHash(file, package, request)) { file.TryHardToDelete(); return(false); } if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source)) { request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name); file.TryHardToDelete(); return(false); } Timer timer = null; object timerLock = new object(); bool cleanUp = false; ProgressTracker tracker = new ProgressTracker(request.StartProgress(0, "Installing Zip Package............")); double percent = tracker.StartPercent; Action cleanUpAction = () => { lock (timerLock) { // check whether clean up is already done before or not if (!cleanUp) { try { if (timer != null) { // stop timer timer.Change(Timeout.Infinite, Timeout.Infinite); timer.Dispose(); timer = null; } } catch { } cleanUp = true; } } }; // extracted folder string extractedFolder = string.Concat(file.GenerateTemporaryFilename()); var versionFolder = ""; try { timer = new Timer(_ => { percent += 0.025; var progressPercent = tracker.ConvertPercentToProgress(percent); if (progressPercent < 90) { request.Progress(tracker.ProgressID, (int)progressPercent, string.Format(CultureInfo.CurrentCulture, "Copying files ...")); } if (request.IsCanceled) { cleanUpAction(); } }, null, 0, 1000); //unzip the file ZipFile.ExtractToDirectory(file, extractedFolder); if (Directory.Exists(extractedFolder)) { versionFolder = Path.Combine(package.Destination, package.Name, package.Version); // create the directory version folder if not exist if (!Directory.Exists(versionFolder)) { Directory.CreateDirectory(versionFolder); } // The package will be installed to destination\packageName\version\ // However, a few packages have a package name as its top level folder after zip. // So the installed folder will look like this: // \destination\foobarPackage\1.0.1\foobarPackage // In this case we directly copy the files to \destination\foobarPackage\1.0.1. var extractedTopLevelFolder = Directory.EnumerateDirectories(extractedFolder, "*", SearchOption.TopDirectoryOnly); while (!Directory.GetFiles(extractedFolder).Any() && extractedTopLevelFolder.Count() == 1) { extractedFolder = extractedTopLevelFolder.FirstOrDefault(); //in case the zip contains version folder extractedTopLevelFolder = Directory.EnumerateDirectories(extractedFolder, "*", SearchOption.TopDirectoryOnly); } FileUtility.CopyDirectory(extractedFolder, versionFolder, true); request.YieldFromSwidtag(package, fastPath); request.Verbose(Resources.Messages.SuccessfullyInstalledToDestination, package.Name, package.Destination); AddEnvironmentVariable(request, versionFolder); return(true); } else { request.Warning("Failed to download a Zip package {0} from {1}", package.Name, package.Source); } } catch (Exception e) { request.Debug(e.StackTrace); if (e is System.UnauthorizedAccessException) { request.WriteError(ErrorCategory.InvalidOperation, package.Name, Resources.Messages.InstallFailed, package.Name, "UnauthorizedAccessException. The requested operation likely requires elevation, i.e., launch PowerShell as administer"); } else { request.WriteError(ErrorCategory.InvalidOperation, package.Name, Resources.Messages.InstallFailed, package.Name, e.Message); } if (!(e is UnauthorizedAccessException || e is IOException)) { // something wrong, delete the version folder versionFolder.TryHardToDelete(); } } finally { cleanUpAction(); file.TryHardToDelete(); extractedFolder.TryHardToDelete(); request.CompleteProgress(tracker.ProgressID, true); } return(false); }
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 bool ValidatePackageManagementVersion(PackageSourceListRequest request) { var userSpecifiedProvider = request.GetOptionValue("ProviderName") ?? request.GetOptionValue("Provider"); if (_currentPackageManagementVersion == null) { var moduleInfo = GetModule("PackageManagement"); if(moduleInfo == null || !moduleInfo.Any()) { request.Verbose(Resources.Messages.CannotFindPackageManagementVersion); return false; } _currentPackageManagementVersion = moduleInfo.OrderByDescending(each => each.Version).FirstOrDefault().Version; if (_currentPackageManagementVersion < new Version(RequiredPackageManagementVersion)) { _doesPackageManagementVersionMatch = false; } else { _doesPackageManagementVersionMatch = true; } } if (!_doesPackageManagementVersionMatch) { if (userSpecifiedProvider != null && userSpecifiedProvider.IndexOf(Constants.ProviderName, StringComparison.OrdinalIgnoreCase) > -1) { request.WriteError(ErrorCategory.InvalidOperation, Constants.ProviderName, Resources.Messages.MinimumVersionCheck, Constants.ProviderName, RequiredPackageManagementVersion, _currentPackageManagementVersion); } else { request.Verbose(Resources.Messages.MinimumVersionCheck, Constants.ProviderName, RequiredPackageManagementVersion, _currentPackageManagementVersion); } } return _doesPackageManagementVersionMatch; }
public void InstallPackage(string fastPath, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "InstallPackage' - fastReference='{0}'", fastPath)); var package = request.GetPackageByFastPath(fastPath); if (package == null) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } InstallProviderFromInstaller(package, fastPath, request); }
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 downloadrequest = PackageSourceListRequest.ExtendRequest( new Dictionary<string, string[]> { {"Destination", new[] {package.Destination ?? ""}} }, new[] {package.Source ?? ""}, package.IsTrustedSource, request); var downloading = provider.DownloadPackage(pkgs[0], location, downloadrequest); 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; } }
internal static bool TestCatalogFile(string jsonFile, string catalogFile, PackageSourceListRequest request) { try { PSObject result = null; using (PowerShell powershell = PowerShell.Create()) { if (powershell != null) { result = powershell .AddCommand("Test-FileCatalog") .AddParameter("CatalogFilePath", catalogFile) .AddParameter("Path", jsonFile) .Invoke().FirstOrDefault(); } if (result.ToString().EqualsIgnoreCase("Valid")) return true; } } catch(Exception ex) { request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailedWithError, catalogFile, ex.Message.ToString()); return false; } request.WriteError(ErrorCategory.InvalidData, catalogFile, Resources.Messages.CatalogFileVerificationFailed, jsonFile); return false; }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="package">package defined in the PackageSourceList</param> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> /// <param name="fastPackReftable"></param> internal static void UninstallPowershellArtifacts(PackageJson package, string fastPackageReference, PackageSourceListRequest request, Dictionary<string, SoftwareIdentity> fastPackReftable) { if (request == null) { throw new ArgumentNullException("request"); } request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(CultureInfo.InvariantCulture, "UninstallNuGetPackage' - fastReference='{0}'", fastPackageReference)); var provider = PackageSourceListRequest.FindProvider(request, package.Type, request, true); if (provider != null) { request.Debug("{0}: Using the provider '{1} to uninstall the package '{2}'", Constants.ProviderName, provider.Name, package.Name); if (!fastPackReftable.ContainsKey(fastPackageReference)) { request.WriteError(Internal.ErrorCategory.InvalidData, fastPackageReference, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPackageReference); return; } var p = fastPackReftable[fastPackageReference]; //calling NuGet for uninstall var installing = provider.UninstallPackage(p, request); 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(); } } } }
internal static void UninstallExePackage(string fastPath, PackageSourceListRequest request) { if (string.IsNullOrWhiteSpace(fastPath)) { return; } if (request == null) { throw new ArgumentNullException("request"); } string sourceLocation; string id; string displayName; string version; string fastPackageReference; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference)) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } var ver = (new SemanticVersion(version)).ToString(); var package = request.GetPackage(id, ver) ?? request.GetPackage(displayName, ver); request.Debug("Calling '{0}::UninstallPackage' '{1}'", Constants.ProviderName, fastPackageReference); var path = fastPackageReference.Split(new[] { '\\' }, 3); var uninstallCommand = string.Empty; Dictionary<string, string> properties = null; if (path.Length == 3) { switch (path[0].ToLowerInvariant()) { case "hklm64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hklm32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; } if (properties == null) { return; } var result = false; if (!string.IsNullOrWhiteSpace(uninstallCommand)) { do { if (File.Exists(uninstallCommand)) { result = ExecuteUninstallCommand(fastPackageReference, request, uninstallCommand, package.UnInstallAdditionalArguments); break; } // not a single file. // check if it's just quoted. var c = uninstallCommand.Trim('\"'); if (File.Exists(c)) { result = ExecuteUninstallCommand(fastPackageReference, request, c, package.UnInstallAdditionalArguments); break; } if (uninstallCommand[0] == '"') { var p = uninstallCommand.IndexOf('"', 1); if (p > 0) { var file = uninstallCommand.Substring(1, p - 1); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); } } } else { var p = uninstallCommand.IndexOf(' '); if (p > 0) { var file = uninstallCommand.Substring(0, p); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); continue; } var s = 0; do { s = uninstallCommand.IndexOf(' ', s + 1); if (s == -1) { break; } file = uninstallCommand.Substring(0, s); if (File.Exists(file)) { args = uninstallCommand.Substring(s + 1); args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); break; } } while (s > -1); if (s == -1) { // never found a way to parse the command :( request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); return; } } } } while (false); if (result) { YieldPackage(fastPackageReference, fastPackageReference, properties, request); return; } } request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); } }
internal static void UninstallExePackage(string fastPath, PackageSourceListRequest request) { if (string.IsNullOrWhiteSpace(fastPath)) { return; } if (request == null) { throw new ArgumentNullException("request"); } string sourceLocation; string id; string displayName; string version; string fastPackageReference; string providerName; if (!request.TryParseFastPathComplex(fastPath: fastPath, regex: PackageSourceListRequest.RegexFastPathComplex, location: out sourceLocation, id: out id, displayname: out displayName, version: out version, fastpath: out fastPackageReference, providerName: out providerName)) { request.WriteError(ErrorCategory.InvalidData, fastPath, Resources.Messages.FailedToGetPackageObject, Constants.ProviderName, fastPath); return; } var ver = (new SemanticVersion(version)).ToString(); var package = request.GetPackage(id, ver) ?? request.GetPackage(displayName, ver); request.Debug("Calling '{0}::UninstallPackage' '{1}'", Constants.ProviderName, fastPackageReference); var path = fastPackageReference.Split(new[] { '\\' }, 3); var uninstallCommand = string.Empty; Dictionary <string, string> properties = null; if (path.Length == 3) { switch (path[0].ToLowerInvariant()) { case "hklm64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu64": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hklm32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; case "hkcu32": #if !CORECLR using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2], RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)) #else using (var product = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(path[2])) #endif { if (product == null) { return; } properties = product.GetValueNames().ToDictionaryNicely(each => each.ToString(), each => (product.GetValue(each) ?? string.Empty).ToString(), StringComparer.OrdinalIgnoreCase); uninstallCommand = properties.Get("QuietUninstallString") ?? properties.Get("UninstallString") ?? ""; } break; } if (properties == null) { return; } var result = false; if (!string.IsNullOrWhiteSpace(uninstallCommand)) { do { if (File.Exists(uninstallCommand)) { result = ExecuteUninstallCommand(fastPackageReference, request, uninstallCommand, package.UnInstallAdditionalArguments); break; } // not a single file. // check if it's just quoted. var c = uninstallCommand.Trim('\"'); if (File.Exists(c)) { result = ExecuteUninstallCommand(fastPackageReference, request, c, package.UnInstallAdditionalArguments); break; } if (uninstallCommand[0] == '"') { var p = uninstallCommand.IndexOf('"', 1); if (p > 0) { var file = uninstallCommand.Substring(1, p - 1); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); } } } else { var p = uninstallCommand.IndexOf(' '); if (p > 0) { var file = uninstallCommand.Substring(0, p); var args = uninstallCommand.Substring(p + 1); if (File.Exists(file)) { args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); continue; } var s = 0; do { s = uninstallCommand.IndexOf(' ', s + 1); if (s == -1) { break; } file = uninstallCommand.Substring(0, s); if (File.Exists(file)) { args = uninstallCommand.Substring(s + 1); args = string.Join(" ", args, package.UnInstallAdditionalArguments); result = ExecuteUninstallCommand(fastPackageReference, request, file, args); break; } } while (s > -1); if (s == -1) { // never found a way to parse the command :( request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); return; } } } } while (false); if (result) { YieldPackage(fastPackageReference, fastPackageReference, properties, request); return; } } request.WriteError(Internal.ErrorCategory.InvalidOperation, "DisplayName", properties["DisplayName"], Constants.Messages.UnableToUninstallPackage); } }
internal static bool InstallExePackage(PackageJson package, string fastPath, PackageSourceListRequest request) { ProgressTracker tracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.Installing)); var exePackage = Path.ChangeExtension(Path.GetTempFileName(), "exe"); WebDownloader.DownloadFile(package.Source, exePackage, request, tracker); if (File.Exists(exePackage)) { request.Verbose("Package: '{0}'", exePackage); // validate the file if (!WebDownloader.VerifyHash(exePackage,package, request)) { return false; } if (!package.IsTrustedSource) { if (!request.ShouldContinueWithUntrustedPackageSource(package.Name, package.Source)) { request.Warning(Constants.Messages.UserDeclinedUntrustedPackageInstall, package.Name); return false; } } // Prepare the process to run var processArguments = string.IsNullOrWhiteSpace(package.InstallArguments) ? "/VERYSILENT /CLOSEAPPLICATIONS /NORESTART /NOCANCEL /SP /qn" : package.InstallArguments; var start = new ProcessStartInfo { FileName = exePackage, Arguments = processArguments, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, //LoadUserProfile = true, }; double percent = tracker.StartPercent; Timer timer = null; object timerLock = new object(); bool cleanUp = false; Action cleanUpAction = () => { lock (timerLock) { // check whether clean up is already done before or not if (!cleanUp) { try { if (timer != null) { // stop timer timer.Change(Timeout.Infinite, Timeout.Infinite); // dispose it timer.Dispose(); } } catch { } cleanUp = true; } } }; // Run the external process & wait for it to finish using (var proc = Process.Start(start)) { var timer1 = timer; timer = new Timer(_ => { percent += 0.025; // percent between startProgress and endProgress var progressPercent = tracker.ConvertPercentToProgress(percent); if (progressPercent < 90) { request.Progress(tracker.ProgressID, (int)progressPercent, Resources.Messages.InstallingPackage, package.Source); } if (request.IsCanceled) { cleanUpAction(); } }, null, 100, 3000); proc.WaitForExit(); // Retrieve the app's exit code var exitCode = proc.ExitCode; if (exitCode != 0) { request.WriteError(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.InstallFailed, package.Name, proc.StandardError.ReadToEnd()); request.CompleteProgress(tracker.ProgressID, false); return false; } else { request.CompleteProgress(tracker.ProgressID, true); request.YieldFromSwidtag(package, fastPath); request.Verbose(Resources.Messages.SuccessfullyInstalled, package.Name); } cleanUpAction(); } return true; } else { request.Error(ErrorCategory.InvalidOperation, Resources.Messages.FailedToDownload, Constants.ProviderName, package.Source, exePackage); } return false; }