namespace Febe.Installer;
/// The exit codes of the installer and of the uninstaller.
internal static class ExitCodes
{
public const int Done = 0;
/// Alberto said no: «Annulla».
public const int Cancelled = 1;
/// Something went wrong, or Febe is open in silent mode: nothing was changed, or only the part said.
public const int Failed = 2;
}
///
/// The dialogs of the installation: the windows of Windows (DialogInterface) or, with /S, none
/// (). The tests use a fake that answers as told.
///
internal interface ISetupInterface
{
/// The first page; null for «Annulla», otherwise whether the desktop shortcut is wanted.
bool? Welcome(string version, string? installedVersion, string installFolder);
/// Febe is open: true to try again (after closing it), false to give up.
bool RetryWhenFebeIsOpen(bool uninstalling);
/// Runs showing its progress (0 to 1); its exception comes out of here.
void RunWithProgress(string version, string installFolder, Action> work);
/// WebView2 is missing: true to open the page of Microsoft.
bool OfferWebView2Page();
/// The last page of the installation: true to start Febe now.
bool Installed();
bool ConfirmUninstall();
void Uninstalled(string dataFolder, string library);
void Failed(bool uninstalling, string message);
}
///
/// /S: 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.
///
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> 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.
}
}
}
///
/// 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.
///
internal sealed class SetupFlow(ISystemAccess system, Locations locations, ISetupInterface ui)
{
private readonly SetupActions _actions = new(system, locations);
/// The installer: is this program, with Febe.exe inside.
public int Install(string packagePath, PackageInfo package, CommandLine line)
{
// Without /D= an update goes where Febe already is, not to the default folder: otherwise the old program would
// stay there, orphaned, while the entry and the shortcuts moved to a second copy.
var folder = line.InstallFolder ?? _actions.InstalledFolder() ?? 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;
}
///
/// The uninstaller. Started from the program folder ( 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.
///
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;
}
///
/// 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.
///
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;
}
}