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:
25 files changed
+2183
-11
No files matched your search
@@ -0,0 +1,105 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Titano.Setup.Cli;
|
||||
|
||||
/// <summary>
|
||||
/// Command-line switches, case-insensitive, with <c>/</c> or <c>-</c>:
|
||||
/// <c>/S</c> silent · <c>/DIR=path</c> · <c>/COMPONENTS=Titano,Uploader</c> · <c>/ALLUSERS</c> · <c>/NODESKTOP</c> ·
|
||||
/// <c>/NOSTARTMENU</c> · <c>/LAUNCH</c> · <c>/LANG=it</c> · <c>/uninstall</c>.
|
||||
/// Internal: <c>/ELEVATED</c> (relaunched for administrator rights), <c>/CLEANUP=path /WAITPID=n</c> (removes the
|
||||
/// uninstaller after the uninstall process exits).
|
||||
/// </summary>
|
||||
internal sealed record SetupArguments
|
||||
{
|
||||
public bool Silent { get; init; }
|
||||
public bool Uninstall { get; init; }
|
||||
public string? Directory { get; init; }
|
||||
public IReadOnlyList<string>? Components { get; init; }
|
||||
public bool AllUsers { get; init; }
|
||||
public bool NoDesktopShortcut { get; init; }
|
||||
public bool NoStartMenu { get; init; }
|
||||
public bool Launch { get; init; }
|
||||
public string? Language { get; init; }
|
||||
public bool Elevated { get; init; }
|
||||
public string? CleanupTarget { get; init; }
|
||||
public int WaitProcessId { get; init; }
|
||||
|
||||
/// <summary>Parses <paramref name="args"/>; throws <see cref="ArgumentException"/> for unknown or malformed switches.</summary>
|
||||
public static SetupArguments Parse(IEnumerable<string> args)
|
||||
{
|
||||
var result = new SetupArguments();
|
||||
foreach (var arg in args)
|
||||
{
|
||||
if (arg.Length < 2 || arg[0] is not ('/' or '-')) throw new ArgumentException($"Unexpected argument '{arg}'.");
|
||||
var eq = arg.IndexOf('=', StringComparison.Ordinal);
|
||||
var name = (eq < 0 ? arg[1..] : arg[1..eq]).ToUpperInvariant();
|
||||
var value = eq < 0 ? null : arg[(eq + 1)..];
|
||||
result = (name, value) switch
|
||||
{
|
||||
("S", null) => result with { Silent = true },
|
||||
("UNINSTALL", null) => result with { Uninstall = true },
|
||||
("ALLUSERS", null) => result with { AllUsers = true },
|
||||
("NODESKTOP", null) => result with { NoDesktopShortcut = true },
|
||||
("NOSTARTMENU", null) => result with { NoStartMenu = true },
|
||||
("LAUNCH", null) => result with { Launch = true },
|
||||
("ELEVATED", null) => result with { Elevated = true },
|
||||
("DIR", { Length: > 0 }) => result with { Directory = value.Trim('"') },
|
||||
("LANG", { Length: > 0 }) => result with { Language = value },
|
||||
("COMPONENTS", { Length: > 0 }) => result with { Components = value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) },
|
||||
("CLEANUP", { Length: > 0 }) => result with { CleanupTarget = value.Trim('"') },
|
||||
("WAITPID", { Length: > 0 }) when int.TryParse(value, out var pid) && pid > 0 => result with { WaitProcessId = pid },
|
||||
_ => throw new ArgumentException($"Unknown or malformed switch '{arg}'."),
|
||||
};
|
||||
}
|
||||
if (result.CleanupTarget is not null && result.WaitProcessId == 0) throw new ArgumentException("/CLEANUP needs /WAITPID.");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>The switches that reproduce these arguments, for relaunching with administrator rights.</summary>
|
||||
public IEnumerable<string> ToSwitches()
|
||||
{
|
||||
if (Uninstall) yield return "/uninstall";
|
||||
if (Silent) yield return "/S";
|
||||
if (Directory is not null) yield return "/DIR=" + Directory;
|
||||
if (Components is not null) yield return "/COMPONENTS=" + string.Join(',', Components);
|
||||
if (AllUsers) yield return "/ALLUSERS";
|
||||
if (NoDesktopShortcut) yield return "/NODESKTOP";
|
||||
if (NoStartMenu) yield return "/NOSTARTMENU";
|
||||
if (Launch) yield return "/LAUNCH";
|
||||
if (Language is not null) yield return "/LANG=" + Language;
|
||||
if (Elevated) yield return "/ELEVATED";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Joins arguments into one command line that <c>CommandLineToArgvW</c> splits back into the same strings
|
||||
/// (quotes around spaces, backslashes doubled before a quote).
|
||||
/// </summary>
|
||||
public static string JoinCommandLine(IEnumerable<string> arguments)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var argument in arguments)
|
||||
{
|
||||
if (sb.Length > 0) sb.Append(' ');
|
||||
if (argument.Length > 0 && argument.IndexOfAny([' ', '\t', '"']) < 0)
|
||||
{
|
||||
sb.Append(argument);
|
||||
continue;
|
||||
}
|
||||
sb.Append('"');
|
||||
var backslashes = 0;
|
||||
foreach (var c in argument)
|
||||
{
|
||||
if (c == '\\')
|
||||
{
|
||||
backslashes++;
|
||||
continue;
|
||||
}
|
||||
sb.Append('\\', c == '"' ? backslashes * 2 + 1 : backslashes);
|
||||
backslashes = 0;
|
||||
sb.Append(c);
|
||||
}
|
||||
sb.Append('\\', backslashes * 2).Append('"');
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace Titano.Setup.Cli;
|
||||
|
||||
/// <summary>Process exit codes of Titano.Setup (stable contract for silent deployments).</summary>
|
||||
internal enum SetupExitCode
|
||||
{
|
||||
Success = 0,
|
||||
|
||||
/// <summary>Installation or removal did not complete; the setup log in %TEMP%\Titano Setup has details.</summary>
|
||||
Failed = 1,
|
||||
|
||||
InvalidArguments = 2,
|
||||
|
||||
/// <summary>The installer carries no payload or the payload is damaged.</summary>
|
||||
PayloadInvalid = 3,
|
||||
|
||||
/// <summary>All-users operation and the administrator prompt was declined.</summary>
|
||||
ElevationDeclined = 4,
|
||||
|
||||
/// <summary>An installed program did not close when asked to.</summary>
|
||||
ProgramRunning = 5,
|
||||
|
||||
/// <summary><c>/uninstall</c> found no installation.</summary>
|
||||
NotInstalled = 6,
|
||||
|
||||
DiskFull = 7,
|
||||
|
||||
/// <summary>The window was closed before anything changed.</summary>
|
||||
Cancelled = 8,
|
||||
|
||||
/// <summary>
|
||||
/// Internal: an instance relaunched with administrator rights finished and asks its unelevated parent to start
|
||||
/// the program, so the program does not run elevated. The parent turns it into <see cref="Success"/>.
|
||||
/// </summary>
|
||||
LaunchRequested = 100,
|
||||
}
|
||||
|
||||
/// <summary>A setup failure that maps to a specific exit code and message.</summary>
|
||||
internal sealed class SetupException(SetupExitCode code, string message) : Exception(message)
|
||||
{
|
||||
public SetupExitCode Code { get; } = code;
|
||||
}
|
||||
Reference in new issue
Block a user