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 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); } }
public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion)); // no package name or the name with wildcard 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; } var packages = request.GetPackages(name); if (request.GetOptionValue("AllVersions").IsTrue()) { // if version is specified then name can not be empty or with wildcard. in this case the cmdlet has been errored out already. // here we just return all packages we can find if (request.FilterOnVersion(packages, requiredVersion, minimumVersion, maximumVersion, minInclusive: true, maxInclusive: true, latest: false).OrderBy(p => p.Name).Any(p => !request.YieldFromSwidtag(p, p.Name))) { return; } return; } //return the latest version if (packages.GroupBy(p => p.Name) .Select(each => each.OrderByDescending(pp => pp.Version).FirstOrDefault()).OrderBy(p => p.Name).Any(item => !request.YieldFromSwidtag(item, item.Name))) { return; } } else { // a user specifies name // asked for a specific version? if (!string.IsNullOrWhiteSpace(requiredVersion)) { request.YieldFromSwidtag(request.GetPackage(name, requiredVersion), name); return; } var allVersion = request.GetOptionValue("AllVersions").IsTrue(); // asked for a version range? if (!string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrEmpty(maximumVersion) || allVersion) { var packages = request.GetPackagesWithinVersionRange(name, minimumVersion, maximumVersion); if (allVersion) { if (packages.Any(p => !request.YieldFromSwidtag(p, name))) { return; } } else { if (request.YieldFromSwidtag(packages.FirstOrDefault(), name)) { return; } } return; } // just return by name request.YieldFromSwidtag(request.GetPackage(name), name); } }
public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, PackageSourceListRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!ValidatePackageManagementVersion(request)) { return; } request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion)); // no package name or the name with wildcard 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; } var packages = request.GetPackages(name); if (request.GetOptionValue("AllVersions").IsTrue()) { // if version is specified then name can not be empty or with wildcard. in this case the cmdlet has been errored out already. // here we just return all packages we can find if (request.FilterOnVersion(packages, requiredVersion, minimumVersion, maximumVersion, minInclusive: true, maxInclusive: true, latest: false).OrderBy(p => p.Name).Any(p => !request.YieldFromSwidtag(p, p.Name))) { return; } return; } //return the latest version if (packages.GroupBy(p => p.Name) .Select(each => each.OrderByDescending(pp => pp.Version).FirstOrDefault()).OrderBy(p=>p.Name).Any( item =>!request.YieldFromSwidtag(item, item.Name))) { return; } } else { // a user specifies name // asked for a specific version? if (!string.IsNullOrWhiteSpace(requiredVersion)) { request.YieldFromSwidtag(request.GetPackage(name, requiredVersion), name); return; } var allVersion = request.GetOptionValue("AllVersions").IsTrue(); // asked for a version range? if (!string.IsNullOrWhiteSpace(minimumVersion) || !string.IsNullOrEmpty(maximumVersion) || allVersion) { var packages = request.GetPackagesWithinVersionRange(name, minimumVersion, maximumVersion); if (allVersion) { if (packages.Any(p => !request.YieldFromSwidtag(p, name))) { return; } } else { if (request.YieldFromSwidtag(packages.FirstOrDefault(), name)) { return; } } return; } // just return by name request.YieldFromSwidtag(request.GetPackage(name), name); } }