Programma di installazione per Windows scritto in casa, senza NSIS (C6, 2/4)

Decisione di Alberto del 18 novembre 2026: niente componenti esterni. Il
programma di installazione è ora src/Febe.Installer, solo .NET: pubblicato per
win-x64 autosufficiente, in un file solo, ridotto e compresso (circa 11 MB);
lo stesso progetto, compilato per il sistema che costruisce, gli appende
Febe.exe compresso con Brotli, la versione e un piè di pagina con SHA-256
(--impacchetta), lo ricontrolla (--verifica) e scrive lo zip portabile (--zip).

Fa ciò che faceva lo script NSIS: installazione per utente in
%LOCALAPPDATA%\Programs\Febe senza permessi di amministratore, collegamenti nel
menu Start e (a scelta) sul desktop con ShellLink, voce in App installate sotto
HKCU, controllo di WebView2, rifiuto con Febe aperto, aggiornamento che lascia
i dati, «Disinstalla Febe.exe» che toglie solo il programma, i collegamenti,
l'avvio con il computer e le librerie estratte, mai dati né libreria. Pagine
con la finestra di dialogo di Windows; /S, /D= e /PROVA= per le prove.

Tolti installer/febe.nsi, NSIS e zip dalla CI e dai documenti: costruisci.sh
vuole solo il .NET SDK. Prove: pacchetto, riga di comando, installazione,
aggiornamento e disinstallazione su un Windows finto (anche su Linux); su
Windows il programma pubblicato davvero, installato e disinstallato senza
domande in una cartella di prova, e la prima pagina aperta e annullata.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 13:57:25 +01:00
1 parent c54d16946a
commit 7a54f1a460
37 files changed
+3275 -378

No files matched your search

+228
View File
@@ -0,0 +1,228 @@
namespace Febe.Installer;
/// <summary>The exit codes of the installer and of the uninstaller.</summary>
internal static class ExitCodes
{
public const int Done = 0;
/// <summary>Alberto said no: «Annulla».</summary>
public const int Cancelled = 1;
/// <summary>Something went wrong, or Febe is open in silent mode: nothing was changed, or only the part said.</summary>
public const int Failed = 2;
}
/// <summary>
/// The dialogs of the installation: the windows of Windows (<c>DialogInterface</c>) or, with <c>/S</c>, none
/// (<see cref="SilentInterface"/>). The tests use a fake that answers as told.
/// </summary>
internal interface ISetupInterface
{
/// <summary>The first page; null for «Annulla», otherwise whether the desktop shortcut is wanted.</summary>
bool? Welcome(string version, string? installedVersion, string installFolder);
/// <summary>Febe is open: true to try again (after closing it), false to give up.</summary>
bool RetryWhenFebeIsOpen(bool uninstalling);
/// <summary>Runs <paramref name="work"/> showing its progress (0 to 1); its exception comes out of here.</summary>
void RunWithProgress(string version, string installFolder, Action<Action<double>> work);
/// <summary>WebView2 is missing: true to open the page of Microsoft.</summary>
bool OfferWebView2Page();
/// <summary>The last page of the installation: true to start Febe now.</summary>
bool Installed();
bool ConfirmUninstall();
void Uninstalled(string dataFolder, string library);
void Failed(bool uninstalling, string message);
}
/// <summary>
/// <c>/S</c>: no dialog, the answers of the silent mode before: the desktop shortcut yes, Febe open means stop, the page
/// of WebView2 no, Febe not started. A failure goes to the standard error, for whoever started the installer.
/// </summary>
internal sealed class SilentInterface(TextWriter errors) : ISetupInterface
{
public bool? Welcome(string version, string? installedVersion, string installFolder) => true;
public bool RetryWhenFebeIsOpen(bool uninstalling) => false;
public void RunWithProgress(string version, string installFolder, Action<Action<double>> work) => work(_ => { });
public bool OfferWebView2Page() => false;
public bool Installed() => false;
public bool ConfirmUninstall() => true;
public void Uninstalled(string dataFolder, string library)
{
}
public void Failed(bool uninstalling, string message)
{
try
{
errors.WriteLine((uninstalling ? Texts.UninstallFailed : Texts.InstallFailed) + ": " + message);
errors.Flush();
}
catch (IOException)
{
// No one is reading: the exit code says it.
}
}
}
/// <summary>
/// The steps of the installer and of the uninstaller, in the order they always had: first page, Febe closed, copy,
/// WebView2, last page; for the uninstaller: a copy of itself in %TEMP% (an executable cannot delete itself),
/// confirmation, Febe closed, removal, where the data stayed.
/// </summary>
internal sealed class SetupFlow(ISystemAccess system, Locations locations, ISetupInterface ui)
{
private readonly SetupActions _actions = new(system, locations);
/// <summary>The installer: <paramref name="packagePath"/> is this program, with Febe.exe inside.</summary>
public int Install(string packagePath, PackageInfo package, CommandLine line)
{
var folder = line.InstallFolder ?? locations.DefaultInstallFolder;
var desktop = ui.Welcome(package.Version, _actions.InstalledVersion(), folder);
if (desktop is null)
{
return ExitCodes.Cancelled;
}
if (!WaitUntilFebeIsClosed(folder, uninstalling: false, line.Silent))
{
return line.Silent ? ExitCodes.Failed : ExitCodes.Cancelled;
}
try
{
ui.RunWithProgress(package.Version, folder, progress =>
_actions.Install(new InstallRequest(packagePath, package, folder, desktop.Value), progress));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or InvalidOperationException)
{
ui.Failed(uninstalling: false, Texts.Failure(ex));
return ExitCodes.Failed;
}
// WebView2: without it Febe cannot show anything. Febe says it too on its first start.
if (_actions.WebView2Version() is null && ui.OfferWebView2Page())
{
system.OpenAddress(Product.WebView2Page);
}
if (ui.Installed())
{
try
{
system.Start(Path.Combine(folder, Product.Executable), [], folder);
}
catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidOperationException or IOException)
{
// Febe is installed all the same; it opens from the Start menu.
}
}
return ExitCodes.Done;
}
/// <summary>
/// The uninstaller. Started from the program folder (<paramref name="selfPath"/> is «Disinstalla Febe.exe» there), it
/// copies itself into %TEMP%, starts the copy and ends; the copy waits for it, then does the work and can delete
/// the original.
/// </summary>
public int Uninstall(string selfPath, CommandLine line)
{
if (line.UninstallFolder is null)
{
var installFolder = Path.GetDirectoryName(Path.GetFullPath(selfPath))!;
Directory.CreateDirectory(locations.Temp);
var copy = Path.Combine(locations.Temp, $"{Product.UninstallerCopyPrefix}{Environment.ProcessId}.exe");
File.Copy(selfPath, copy, overwrite: true);
system.Start(copy, [.. line.PassOn(), "/_?=" + installFolder, $"/ATTENDI={Environment.ProcessId}"], locations.Temp);
return ExitCodes.Done;
}
if (line.WaitFor is { } parent)
{
system.WaitForExit(parent, TimeSpan.FromSeconds(30));
}
var folder = line.UninstallFolder;
if (!ui.ConfirmUninstall())
{
return ExitCodes.Cancelled;
}
if (!WaitUntilFebeIsClosed(folder, uninstalling: true, line.Silent))
{
return line.Silent ? ExitCodes.Failed : ExitCodes.Cancelled;
}
// Read before anything is removed, to say at the end where the data stayed.
var dataFolder = _actions.DataFolder();
try
{
_actions.Uninstall(folder);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
ui.Failed(uninstalling: true, Texts.Failure(ex));
return ExitCodes.Failed;
}
ui.Uninstalled(dataFolder, locations.Library);
return ExitCodes.Done;
}
/// <summary>
/// Copies of the uninstaller left in %TEMP% by earlier uninstallations (a running program cannot delete itself):
/// removed when they are no longer running. Never anything else.
/// </summary>
public static void RemoveOldUninstallerCopies(Locations locations, string? selfPath)
{
try
{
if (!Directory.Exists(locations.Temp))
{
return;
}
foreach (var copy in Directory.EnumerateFiles(locations.Temp, Product.UninstallerCopyPrefix + "*.exe"))
{
if (selfPath is null || !string.Equals(Path.GetFullPath(copy), Path.GetFullPath(selfPath), StringComparison.OrdinalIgnoreCase))
{
Package.TryDelete(copy);
}
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
}
}
private bool WaitUntilFebeIsClosed(string folder, bool uninstalling, bool silent)
{
while (SetupActions.IsFebeOpen(folder))
{
if (silent)
{
ui.Failed(uninstalling, Texts.FebeOpenSilent);
return false;
}
if (!ui.RetryWhenFebeIsOpen(uninstalling))
{
return false;
}
}
return true;
}
}