/
RemoveApp.cs
69 lines (58 loc) · 2.55 KB
/
RemoveApp.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
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
using ZeroInstall.DesktopIntegration;
using ZeroInstall.Model.Capabilities;
namespace ZeroInstall.Commands.Desktop;
/// <summary>
/// Remove an application from the <see cref="AppList"/> and undoes any desktop environment integration.
/// </summary>
public class RemoveApp(ICommandHandler handler) : AppCommand(handler)
{
public const string Name = "remove";
public const string AltName = "remove-app";
public const string AltName2 = "destory";
public override string Description => Resources.DescriptionRemoveApp;
public override string Usage => "[OPTIONS] (ALIAS|INTERFACE)";
/// <inheritdoc/>
protected override ExitCode ExecuteHelper()
{
if (IntegrationManager.AppList.GetEntry(InterfaceUri) is not {} appEntry)
{
Log.Warn(string.Format(Resources.AliasNotFound, InterfaceUri));
return ExitCode.NoChanges;
}
if (appEntry.AccessPoints != null)
CheckInstallBase();
foreach (var hook in appEntry.CapabilityLists.CompatibleCapabilities().OfType<RemoveHook>())
RunRemoveHook(hook);
IntegrationManager.RemoveApp(appEntry);
if (ZeroInstallInstance.IsLibraryMode
&& !ExistingDesktopIntegration()
&& (!ZeroInstallInstance.IsMachineWide || !ExistingDesktopIntegration(machineWide: true)))
{
Log.Info("Last app removed, auto-removing library mode Zero Install instance");
StartCommandBackground(Self.Name, Self.Remove.Name, "--batch");
}
return ExitCode.OK;
}
/// <summary>
/// Runs a remove <paramref name="hook"/> if the app is already cached.
/// </summary>
private void RunRemoveHook(RemoveHook hook)
{
if (Handler.Verbosity == Verbosity.Batch)
{
Log.Info($"Skipped remove hook {hook.ID} for {InterfaceUri} because running in batch mode");
return;
}
Log.Debug($"Solving remove hook {hook.ID} for {InterfaceUri}");
if (TrySolveOffline(new(InterfaceUri, hook.Command)) is {} selections)
{
Executor.Inject(selections)
.AddArguments(hook.Arguments.Select(x => x.Value).ToArray())
.Start()
?.WaitForExitCode(); // Log the exit code but continue regardless of its value
}
else Log.Info($"Skipped remove hook {hook.ID} for {InterfaceUri} because the app is not cached");
}
}