/
AutomaticUninstallerService.cs
161 lines (141 loc) · 7.76 KB
/
AutomaticUninstallerService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace chocolatey.infrastructure.app.services
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using configuration;
using domain;
using filesystem;
using infrastructure.commands;
using results;
public class AutomaticUninstallerService : IAutomaticUninstallerService
{
private readonly IChocolateyPackageInformationService _packageInfoService;
private readonly IFileSystem _fileSystem;
private readonly IRegistryService _registryService;
private readonly ICommandExecutor _commandExecutor;
private const int SLEEP_TIME = 5;
public AutomaticUninstallerService(IChocolateyPackageInformationService packageInfoService, IFileSystem fileSystem, IRegistryService registryService, ICommandExecutor commandExecutor)
{
_packageInfoService = packageInfoService;
_fileSystem = fileSystem;
_registryService = registryService;
_commandExecutor = commandExecutor;
}
public void run(PackageResult packageResult, ChocolateyConfiguration config)
{
if (!config.Features.AutoUninstaller)
{
this.Log().Info(" Skipping auto uninstaller - AutoUninstaller feature is not enabled.");
return;
}
var pkgInfo = _packageInfoService.get_package_information(packageResult.Package);
if (pkgInfo.RegistrySnapshot == null)
{
this.Log().Info(" Skipping auto uninstaller - No registry snapshot.");
return;
}
var registryKeys = pkgInfo.RegistrySnapshot.RegistryKeys;
if (registryKeys == null || registryKeys.Count == 0)
{
this.Log().Info(" Skipping auto uninstaller - No registry keys in snapshot.");
return;
}
this.Log().Info(" Running auto uninstaller...");
this.Log().Debug("Sleeping for {0} seconds to allow Windows to finish cleaning up.".format_with(SLEEP_TIME));
Thread.Sleep((int)TimeSpan.FromSeconds(SLEEP_TIME).TotalMilliseconds);
foreach (var key in registryKeys.or_empty_list_if_null())
{
this.Log().Debug(() => " Preparing uninstall key '{0}'".format_with(key.UninstallString));
if ((!string.IsNullOrWhiteSpace(key.InstallLocation) && !_fileSystem.directory_exists(key.InstallLocation)) || !_registryService.value_exists(key.KeyPath, ApplicationParameters.RegistryValueInstallLocation))
{
this.Log().Info(" Skipping auto uninstaller - The application appears to have been uninstalled already by other means.");
this.Log().Debug(() => " Searched for install path '{0}' - found? {1}".format_with(key.InstallLocation.escape_curly_braces(), _fileSystem.directory_exists(key.InstallLocation)));
this.Log().Debug(() => " Searched for registry key '{0}' value '{1}' - found? {2}".format_with(key.KeyPath.escape_curly_braces(), ApplicationParameters.RegistryValueInstallLocation, _registryService.value_exists(key.KeyPath, ApplicationParameters.RegistryValueInstallLocation)));
continue;
}
// split on " /" and " -" for quite a bit more accuracy
IList<string> uninstallArgsSplit = key.UninstallString.to_string().Split(new[] {" /", " -"}, StringSplitOptions.RemoveEmptyEntries).ToList();
var uninstallExe = uninstallArgsSplit.DefaultIfEmpty(string.Empty).FirstOrDefault();
var uninstallArgs = key.UninstallString.to_string().Replace(uninstallExe.to_string(), string.Empty);
uninstallExe = uninstallExe.remove_surrounding_quotes();
this.Log().Debug(() => " Uninstaller path is '{0}'".format_with(uninstallExe));
if (!key.HasQuietUninstall)
{
IInstaller installer = new CustomInstaller();
switch (key.InstallerType)
{
case InstallerType.Msi:
installer = new MsiInstaller();
break;
case InstallerType.InnoSetup:
installer = new InnoSetupInstaller();
break;
case InstallerType.Nsis:
installer = new NsisInstaller();
break;
case InstallerType.InstallShield:
installer = new InstallShieldInstaller();
break;
default:
// skip
break;
}
this.Log().Debug(() => " Installer type is '{0}'".format_with(installer.GetType().Name));
//todo: ultimately we should merge keys with logging
uninstallArgs += " " + installer.build_uninstall_command_arguments();
}
if (key.InstallerType == InstallerType.Msi)
{
//because sometimes the key is set with /i to allow for modify :/
uninstallArgs = uninstallArgs.Replace("/I{", "/X{");
uninstallArgs = uninstallArgs.Replace("/i{", "/X{");
uninstallArgs = uninstallArgs.Replace("/I ", "/X ");
uninstallArgs = uninstallArgs.Replace("/i ", "/X ");
}
this.Log().Debug(() => " Args are '{0}'".format_with(uninstallArgs));
var exitCode = _commandExecutor.execute(
uninstallExe,
uninstallArgs.trim_safe(),
config.CommandExecutionTimeoutSeconds,
(s, e) =>
{
if (e == null || string.IsNullOrWhiteSpace(e.Data)) return;
this.Log().Debug(() => " [AutoUninstaller] {0}".format_with(e.Data));
},
(s, e) =>
{
if (e == null || string.IsNullOrWhiteSpace(e.Data)) return;
this.Log().Error(() => " [AutoUninstaller] {0}".format_with(e.Data));
},
updateProcessPath: false);
if (exitCode != 0)
{
Environment.ExitCode = exitCode;
string logMessage = " Auto uninstaller failed. Please remove machine installation manually.";
this.Log().Error(() => logMessage);
packageResult.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
}
else
{
this.Log().Info(() => " Auto uninstaller has successfully uninstalled {0} from your machine.".format_with(packageResult.Package.Id));
}
}
}
}
}