using Chronos.Core.Diagnostics;
using Chronos.Core.Packaging;
using Chronos.Setup.Cli;
namespace Chronos.Setup.Engine;
///
/// Where and what to install before the user changes anything: an existing installation fixes folder and scope
/// (upgrade in place) and suggests its components; otherwise the switches, then the manifest defaults, apply. A
/// component that installs for all users only (, the Server) makes a new
/// installation an all-users one, in a folder inside Program Files; an installation upgraded in place elsewhere (for
/// the current user, or in another folder) cannot take it.
///
internal sealed record InstallPlan(InstallScope Scope, string Directory, IReadOnlyList Components, ExistingInstall? Existing)
{
public static InstallPlan Resolve(PayloadManifest manifest, SetupArguments arguments)
{
var existing = SetupRegistry.FindInstall(SetupPaths.ProductKey(manifest.Product));
var scope = existing?.Scope ?? (arguments.AllUsers ? InstallScope.AllUsers : InstallScope.CurrentUser);
IReadOnlyList components;
if (arguments.Components is { } requested)
{
var unknown = requested.FirstOrDefault(id => manifest.FindComponent(id) is null);
if (unknown is not null) throw new SetupException(SetupExitCode.InvalidArguments, $"Unknown component '{unknown}'.");
components = [.. requested.Select(id => manifest.FindComponent(id)!.Id).Distinct()];
}
else
{
var previous = existing is null ? null : InstallLog.TryLoad(existing.Directory)?.Components;
var kept = previous?.Select(manifest.FindComponent).OfType().Select(c => c.Id).ToList();
components = kept is { Count: > 0 } ? kept : [.. manifest.Components.Where(c => c.DefaultSelected).Select(c => c.Id)];
}
var allUsersOnly = components.Where(id => manifest.FindComponent(id)!.AllUsersOnly).ToList();
if (allUsersOnly.Count > 0 && existing is null) scope = InstallScope.AllUsers;
var directory = existing?.Directory ?? arguments.Directory ?? SetupPaths.DefaultDirectory(scope, manifest.Product);
if (allUsersOnly.Count > 0 && existing is not null && !IsServiceSafe(existing.Scope, existing.Directory))
{
if (arguments.Components is not null)
{
throw new SetupException(SetupExitCode.InvalidArguments,
$"{string.Join(", ", allUsersOnly)} installs for all users inside Program Files only; {manifest.Product} is installed in '{existing.Directory}' for {existing.Scope}.");
}
// An earlier installation elsewhere carried it: the upgrade drops it, and its service with it (Prune).
Log.Warning("setup", $"{string.Join(", ", allUsersOnly)} not kept: it installs for all users inside Program Files only");
components = [.. components.Except(allUsersOnly, StringComparer.OrdinalIgnoreCase)];
}
return new InstallPlan(scope, directory, components, existing);
}
///
/// False for a component that installs for all users only when an installation that is not an all-users one inside
/// Program Files is upgraded.
///
public bool CanInstall(PayloadComponent component) => !component.AllUsersOnly || Existing is null || IsServiceSafe(Existing.Scope, Existing.Directory);
///
/// True when may be installed in for
/// : a folder setup can use and, when one of them runs as a LocalSystem service, an
/// all-users folder inside Program Files, which standard users cannot change.
///
public static bool IsValidDirectory(PayloadManifest manifest, InstallScope scope, string directory, IEnumerable components) =>
SetupPaths.IsUsableInstallDirectory(directory)
&& (!components.Any(id => manifest.FindComponent(id)?.AllUsersOnly == true) || IsServiceSafe(scope, directory));
/// True for an all-users installation inside Program Files: where a program a LocalSystem service starts may live.
public static bool IsServiceSafe(InstallScope scope, string directory) => scope == InstallScope.AllUsers && SetupPaths.IsInsideProgramFiles(directory);
/// Executables to look for among running processes: the new payload's and the previous install's.
public static IEnumerable Programs(PayloadManifest manifest, string directory) =>
manifest.Files.Select(f => f.Path).Concat(InstallLog.TryLoad(directory)?.Files ?? []);
}