using System.Globalization; using Titano.Core.Packaging; namespace Titano.Tools.Commands; /// /// pack-installer --setup Titano.Setup.exe --product Titano --version 1.2.3 --component Id=folder … --out file: /// appends a TPK1 payload (BRIEF §18) to the setup program. Known component ids carry their display name, default /// selection, shortcuts and file types; unknown ids are packed as plain, preselected components. /// internal sealed class PackInstallerCommand : ICommand { private sealed record KnownComponent(string DisplayName, bool DefaultSelected, string Program, bool Desktop, PayloadAssociation? Association); private static readonly Dictionary Known = new(StringComparer.OrdinalIgnoreCase) { ["Titano"] = new("Titano", true, "Titano.exe", Desktop: true, new PayloadAssociation { Component = "Titano", Extension = ".titano", ProgId = "Titano.Project", Description = "Titano project", Target = "Titano.exe" }), ["Uploader"] = new("Titano Uploader", true, "Titano.Uploader.exe", Desktop: true, null), ["Server"] = new("Titano Server", false, "Titano.Server.exe", Desktop: false, null), }; public string Name => "pack-installer"; public string Summary => "--setup --product --version --component Id=dir … --out "; public Task RunAsync(string[] args) { var options = Parse(args); var setup = Required(options, "setup"); var output = Required(options, "out"); if (!File.Exists(setup)) throw new ToolException($"Setup program '{setup}' not found."); if (!options.TryGetValue("component", out var specs)) throw new ToolException("At least one --component Id=folder is required."); var components = new List(); var shortcuts = new List(); var associations = new List(); foreach (var spec in specs) { var eq = spec.IndexOf('=', StringComparison.Ordinal); if (eq <= 0 || eq == spec.Length - 1) throw new ToolException($"--component expects Id=folder, got '{spec}'."); var id = spec[..eq]; var folder = spec[(eq + 1)..]; if (!PayloadManifest.IdentifierRegex().IsMatch(id)) throw new ToolException($"Component id '{id}' must be letters and digits."); if (components.Any(c => c.Id.Equals(id, StringComparison.OrdinalIgnoreCase))) throw new ToolException($"Component '{id}' given twice."); if (!Directory.Exists(folder)) throw new ToolException($"Component folder '{folder}' not found."); if (!Known.TryGetValue(id, out var known)) { components.Add(new PayloadComponentSource(id, id, true, folder)); continue; } components.Add(new PayloadComponentSource(id, known.DisplayName, known.DefaultSelected, folder)); if (!File.Exists(Path.Combine(folder, known.Program))) { Console.Error.WriteLine($"warning: {id} has no {known.Program}; no shortcut or file type registered."); continue; } shortcuts.Add(new PayloadShortcut { Component = id, Name = known.DisplayName, Target = known.Program, StartMenu = true, Desktop = known.Desktop }); if (known.Association is { } association) associations.Add(association); } var source = new PayloadSource { Product = Required(options, "product"), Version = Required(options, "version"), Components = components, Shortcuts = shortcuts, Associations = associations, }; try { Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(output))!); var result = PayloadWriter.Write(setup, source, output); Console.WriteLine(string.Create(CultureInfo.InvariantCulture, $"{output}: {result.FileCount} files in {components.Count} component(s), {Megabytes(result.InstallerBytes)} ({result.InstallerBytes:N0} bytes), installs {Megabytes(result.InstalledBytes)}")); return Task.FromResult(0); } catch (Exception ex) when (ex is PayloadException or ArgumentException or IOException) { throw new ToolException("pack-installer: " + ex.Message); } } /// --key value pairs; every key may repeat (only --component is meant to). private static Dictionary> Parse(string[] args) { var allowed = new HashSet(StringComparer.OrdinalIgnoreCase) { "setup", "product", "version", "component", "out" }; var options = new Dictionary>(StringComparer.OrdinalIgnoreCase); for (var i = 0; i < args.Length; i++) { if (!args[i].StartsWith("--", StringComparison.Ordinal) || !allowed.Contains(args[i][2..])) throw new ToolException($"Unexpected argument '{args[i]}'."); if (i + 1 >= args.Length) throw new ToolException($"{args[i]} needs a value."); var key = args[i][2..]; if (!options.TryGetValue(key, out var values)) options[key] = values = []; values.Add(args[++i]); } return options; } private static string Required(Dictionary> options, string key) { if (!options.TryGetValue(key, out var values) || string.IsNullOrWhiteSpace(values[^1])) throw new ToolException($"--{key} is required."); if (values.Count > 1) throw new ToolException($"--{key} given more than once."); return values[0]; } private static string Megabytes(long bytes) => (bytes / 1_000_000d).ToString("0.0", CultureInfo.InvariantCulture) + " MB"; }