feat(setup): add one-screen installer, uninstaller and silent mode

Titano.Setup reads the TPK1 payload from its own executable and installs per
user (%LOCALAPPDATA%\Programs) or for all users (Program Files, relaunched
elevated through ShellExecuteEx "runas"). Extraction is hash-verified into a
staging folder and committed with rollback; upgrades replace files in place,
remove obsolete ones and never touch %LOCALAPPDATA%\Titano.

- Components, scope, folder (IFileOpenDialog picker), desktop and Start menu
  shortcuts (IShellLinkW), start-after-install, language picker
- Add/Remove Programs entry, .titano file type, install log
- /uninstall [/S] removes only logged files, shortcuts and registry entries;
  user data only when "Remove my data" is ticked; the uninstaller deletes
  itself through a temporary helper copy
- Silent switches /S /DIR= /COMPONENTS= /ALLUSERS /NODESKTOP /NOSTARTMENU
  /LAUNCH /LANG= with documented exit codes; running programs are asked to
  close, never killed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:16:20 +00:00
1 parent 5b5f38c91d
commit c97b4fb82c
25 files changed
+2183 -11

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
using System.IO;
using Titano.Core.Diagnostics;
using Titano.Core.Packaging;
using Titano.Setup.Cli;
using Titano.Setup.Interop;
namespace Titano.Setup.Engine;
/// <summary>An installation found on disk: its folder and log.</summary>
internal sealed record InstalledProduct(string Directory, InstallLog Log);
/// <summary>
/// Removes an installation using only its log: shortcuts, file types, logged files, the log and the Add/Remove
/// Programs entry, then the uninstaller itself. Files the log does not list are left alone; the user data folder
/// is deleted only when <c>removeUserData</c> is set by an explicit choice in the window.
/// </summary>
internal static class UninstallEngine
{
/// <summary>The installation to remove: <c>/DIR</c>, the uninstaller's own folder, or the registered location.</summary>
public static InstalledProduct? Locate(string? directory, string? productFromPayload)
{
var candidates = new List<string?> { directory, Path.GetDirectoryName(Environment.ProcessPath) };
if (productFromPayload is not null) candidates.Add(SetupRegistry.FindInstall(SetupPaths.ProductKey(productFromPayload))?.Directory);
foreach (var candidate in candidates)
{
if (string.IsNullOrEmpty(candidate)) continue;
if (InstallLog.TryLoad(candidate) is { } log) return new InstalledProduct(Path.GetFullPath(candidate), log);
}
return null;
}
public static void Uninstall(InstalledProduct product, bool removeUserData, IProgress<SetupProgress>? progress)
{
var (directory, log) = product;
Log.Info("setup", $"Removing {log.Product} {log.Version} from '{directory}' (remove data: {removeUserData})");
// First, so that a failure here leaves the program installed and the uninstaller able to retry.
if (removeUserData && Directory.Exists(SetupPaths.UserData))
{
Log.Info("setup", $"Removing user data '{SetupPaths.UserData}' at the user's request");
Directory.Delete(SetupPaths.UserData, recursive: true);
}
foreach (var link in log.Shortcuts) DeleteShortcut(link, log.Scope);
progress?.Report(new SetupProgress(0.1, string.Empty));
var fileTypes = log.Associations.Where(a => SetupRegistry.IsValidFileType(a.Extension, a.ProgId)).ToList();
foreach (var a in fileTypes) SetupRegistry.UnregisterFileType(log.Scope, a.Extension, a.ProgId);
if (fileTypes.Count > 0) NativeMethods.NotifyAssociationsChanged();
progress?.Report(new SetupProgress(0.2, string.Empty));
var failed = InstalledFiles.Remove(directory, log.Files.Where(PayloadPath.IsValid));
progress?.Report(new SetupProgress(0.8, string.Empty));
if (failed.Count > 0)
{
// Keep the entry and a log of what is left, so the user can run the uninstaller again.
log.Files = [.. failed];
log.Shortcuts = [];
log.Associations = [];
log.Save(directory);
throw new SetupException(SetupExitCode.Failed, $"{failed.Count} file(s) could not be removed.");
}
SetupRegistry.DeleteUninstallEntry(log.Scope, log.ProductKey);
File.Delete(Path.Combine(directory, InstallLog.FileName));
var uninstaller = Path.Combine(directory, log.UninstallerName);
try
{
if (Environment.ProcessPath is { } self && Path.GetFullPath(self).Equals(Path.GetFullPath(uninstaller), StringComparison.OrdinalIgnoreCase))
{
SelfCleanup.Schedule(uninstaller);
}
else
{
if (File.Exists(uninstaller)) File.Delete(uninstaller);
InstalledFiles.Remove(directory, [], removeEmptyRoot: true);
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.ComponentModel.Win32Exception)
{
// Everything else is gone; a leftover uninstaller is not worth failing the removal.
Log.Warning("setup", $"Uninstaller '{uninstaller}' not removed", ex);
}
progress?.Report(new SetupProgress(1, string.Empty));
}
/// <summary>Deletes a shortcut recorded in a log, but only a <c>.lnk</c> directly in the Start menu or desktop folder.</summary>
public static void DeleteShortcut(string link, InstallScope scope)
{
var folder = Path.GetDirectoryName(link);
var allowed = folder is not null && link.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase)
&& (folder.Equals(SetupPaths.StartMenu(scope), StringComparison.OrdinalIgnoreCase)
|| folder.Equals(SetupPaths.Desktop(scope), StringComparison.OrdinalIgnoreCase));
if (!allowed)
{
Log.Warning("setup", $"Ignoring shortcut outside the shortcut folders: '{link}'");
return;
}
try
{
File.Delete(link);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("setup", $"Shortcut '{link}' not removed", ex);
}
}
}