Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
feat(cli): add 'engines' to list, install and remove optional engines
Yavuz Mete Afşar13225d869ab3
diff --git a/src/Filee.Cli/Cli.cs b/src/Filee.Cli/Cli.csindex 8009843a..b325db35 100644--- a/src/Filee.Cli/Cli.cs+++ b/src/Filee.Cli/Cli.cs@@ -1,4 +1,4 @@-// The "filee" commands: convert, watch, formats, presets. Writes results for people (one line per file) or, with+// The "filee" commands: convert, watch, formats, presets, engines. Writes results for people (one line per file) or, with// --json, one JSON document for other programs. Exit codes: 0 everything converted, 1 some files failed,// 2 wrong arguments or a missing input, 3 nothing to convert.@@ -47,6 +47,7 @@ internal static class Cli"convert" => await ConvertAsync(options, host, output, errors, cancellationToken),"watch" => await WatchAsync(options, host, output, errors, cancellationToken),"formats" => ListFormats(options, host, output, errors),+ "engines" => await EnginesCommand.RunAsync(options, host, output, errors, cancellationToken),_ => ListPresets(options, host, output),};}@@ -363,7 +364,7 @@ internal static class Clioutput.WriteLine($"{from.DisplayName} ({string.Join(", ", from.Extensions)}) converts to:");output.WriteLine(now.Count > 0 ? " " + string.Join(", ", now) : " (nothing with the engines installed now)");if (later.Count > 0)- output.WriteLine($"With optional engines (install them in Filee, Settings → Engines):{Environment.NewLine} {string.Join(", ", later)}");+ output.WriteLine($"With optional engines (install them with 'filee engines'):{Environment.NewLine} {string.Join(", ", later)}");return Success;}@@ -406,7 +407,7 @@ internal static class Clijson.WriteBoolean("writable", format.Writable);}- private static void WriteStrings(Utf8JsonWriter json, string name, IEnumerable<string> values)+ internal static void WriteStrings(Utf8JsonWriter json, string name, IEnumerable<string> values){json.WriteStartArray(name);foreach (var value in values)@@ -414,7 +415,7 @@ internal static class Clijson.WriteEndArray();}- private static void WriteJsonDocument(TextWriter output, Action<Utf8JsonWriter> write)+ internal static void WriteJsonDocument(TextWriter output, Action<Utf8JsonWriter> write){using var stream = new MemoryStream();using (var json = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }))diff --git a/src/Filee.Cli/CliHost.cs b/src/Filee.Cli/CliHost.csindex 9e26eb38..064973f5 100644--- a/src/Filee.Cli/CliHost.cs+++ b/src/Filee.Cli/CliHost.cs@@ -12,11 +12,15 @@ namespace Filee.Cli;internal sealed class CliHost : IAsyncDisposable{- internal CliHost(UserDataStore store, ConverterCatalog catalog, JobQueue queue)+ private EngineInstaller? _engines;++ /// <param name="engines">Installer of the optional engines; the app's (per-user engines folder) when null.</param>+ internal CliHost(UserDataStore store, ConverterCatalog catalog, JobQueue queue, EngineInstaller? engines = null){Store = store;Catalog = catalog;Queue = queue;+ _engines = engines;}public UserDataStore Store { get; }@@ -24,8 +28,15 @@ internal sealed class CliHost : IAsyncDisposablepublic JobQueue Queue { get; }public ILocalizer Texts { get; } = new EnglishTexts();+ /// <summary>+ /// The installer Settings → Engines uses, on EngineEnvironment.DownloadRoot (the engines folder in FILEE_DATA_DIR+ /// when that is set). Created when 'filee engines' first needs it.+ /// </summary>+ public EngineInstaller Engines => _engines ??= new EngineInstaller(EngineInstaller.CreateHttpClient(Timeout.InfiniteTimeSpan));+/// <param name="dataDirectory">Settings folder; the app's (%APPDATA%\Filee) when null.</param>- public static CliHost Create(string? dataDirectory = null)+ /// <param name="engines">Installer of the optional engines (tests pass one with their own folder and downloads).</param>+ public static CliHost Create(string? dataDirectory = null, EngineInstaller? engines = null){var store = new UserDataStore(dataDirectory ?? UserDataStore.DefaultDirectory);store.Load();@@ -39,7 +50,7 @@ internal sealed class CliHost : IAsyncDisposablevar queue = new JobQueue(catalog, EngineRegistry.FindPdfMerger(converters), combiner: EngineRegistry.FindFileCombiner(converters),tiffMerger: EngineRegistry.FindTiffMerger(converters), mediaJoiner: EngineRegistry.FindMediaJoiner(converters),moveToTrash: Trash());- return new CliHost(store, catalog, queue);+ return new CliHost(store, catalog, queue, engines);}/// <summary>The system's Recycle Bin / Trash for "Replace the original", as in the app; none elsewhere.</summary>diff --git a/src/Filee.Cli/CliOptions.cs b/src/Filee.Cli/CliOptions.csindex 8eb1b250..bee06154 100644--- a/src/Filee.Cli/CliOptions.cs+++ b/src/Filee.Cli/CliOptions.cs@@ -24,7 +24,7 @@ internal sealed record CliOptionspublic double? SettleSeconds { get; set; }public string? Error { get; set; }- public static readonly string[] Commands = ["convert", "watch", "formats", "presets", "help", "version"];+ public static readonly string[] Commands = ["convert", "watch", "formats", "presets", "engines", "help", "version"];public static CliOptions Parse(IReadOnlyList<string> args){@@ -127,6 +127,10 @@ internal sealed record CliOptions"convert" when options.Inputs.Count == 0 => "Give at least one file or folder to convert.","convert" or "watch" when options.To is null == (options.Preset is null) => "Use either --to <format> or --preset <preset>.","watch" when options.Inputs.Count != 1 => "Give exactly one folder to watch.",+ "engines" when options.Inputs.Count > 0 && options.Inputs[0].ToLowerInvariant() is not ("install" or "remove") =>+ $"Unknown engines command '{options.Inputs[0]}'. Use 'filee engines', 'filee engines install <id>' or 'filee engines remove <id>'.",+ "engines" when options.Inputs.Count == 1 => $"Give the engine to {options.Inputs[0].ToLowerInvariant()}, e.g. 'filee engines {options.Inputs[0].ToLowerInvariant()} pandoc'.",+ "engines" when options.Inputs.Count > 2 => "Give one engine at a time.",_ => null,};return options;diff --git a/src/Filee.Cli/EnginesCommand.cs b/src/Filee.Cli/EnginesCommand.csnew file mode 100644index 00000000..63bae710--- /dev/null+++ b/src/Filee.Cli/EnginesCommand.cs@@ -0,0 +1,326 @@+// "filee engines": the optional engines of Settings → Engines, listed, installed and removed with the app's own+// installer (EngineInstaller), so the CLI downloads, verifies and unpacks exactly as the app does. Results go to+// stdout (one JSON document with --json, errors included); progress goes to stderr (one JSON object per line with+// --json, so stderr stays readable line by line).++using System.Text;+using System.Text.Json;+using Filee.Core.Conversion;+using Filee.Engines.Infrastructure;++namespace Filee.Cli;++internal static class EnginesCommand+{+ public static async Task<int> RunAsync(CliOptions options, CliHost host, TextWriter output, TextWriter errors, CancellationToken cancellationToken)+ {+ if (options.Inputs.Count == 0)+ return List(options, host.Engines, output);+ var id = options.Inputs[1];+ if (EngineDownloads.Packages.FirstOrDefault(p => string.Equals(p.Id, id, StringComparison.OrdinalIgnoreCase)) is not { } package)+ {+ errors.WriteLine($"filee: unknown engine '{id}'. Run 'filee engines' for the list.");+ return Cli.UsageError;+ }+ var result = options.Inputs[0].Equals("install", StringComparison.OrdinalIgnoreCase)+ ? await InstallAsync(package, options, host, errors, cancellationToken)+ : Remove(package, host.Engines);+ Report(result, options, output, errors);+ return result.State is "failed" or "cancelled" ? Cli.SomeFailed : Cli.Success;+ }++ /// <summary>What install or remove did: <paramref name="State"/> is installed, removed, skipped, failed or cancelled.</summary>+ /// <param name="Message">The line for people: what was done, why nothing was, or the error.</param>+ /// <param name="Version">The version of Filee's download, once it is installed.</param>+ private sealed record Result(string Id, string State, string Message, string? Version = null, string? ErrorKey = null);++ private static void Report(Result result, CliOptions options, TextWriter output, TextWriter errors)+ {+ var failed = result.State is "failed" or "cancelled";+ if (options.Json)+ {+ Cli.WriteJsonDocument(output, json =>+ {+ json.WriteStartObject();+ json.WriteString("id", result.Id);+ json.WriteString("state", result.State);+ if (result.Version is not null)+ json.WriteString("version", result.Version);+ if (result.State == "skipped")+ json.WriteString("reason", result.Message);+ if (failed)+ json.WriteString("error", result.Message);+ if (failed && result.ErrorKey is not null)+ json.WriteString("errorKey", result.ErrorKey);+ json.WriteEndObject();+ });+ }+ if (options.Json)+ return;+ if (failed)+ errors.WriteLine($"filee: {result.Message}");+ else if (!options.Quiet)+ output.WriteLine(result.Message);+ }++ // ───────────────────────── List ─────────────────────────++ private static int List(CliOptions options, EngineInstaller engines, TextWriter output)+ {+ var rows = EngineDownloads.Packages.Select(p => (Package: p, Copy: engines.StatusOf(p))).ToList();+ if (options.Json)+ {+ Cli.WriteJsonDocument(output, json =>+ {+ json.WriteStartObject();+ json.WriteString("platform", EngineDownloads.RuntimeIdentifier);+ json.WriteStartArray("engines");+ foreach (var (package, copy) in rows)+ WriteEngine(json, engines, package, copy);+ json.WriteEndArray();+ json.WriteEndObject();+ });+ return Cli.Success;+ }++ output.WriteLine($"{"ID",-12} {"STATUS",-16} {"VERSION",-8} SIZE");+ foreach (var (package, copy) in rows)+ {+ var downloadable = CanDownload(engines, package);+ var status = copy.Status switch+ {+ EngineCopyStatus.OwnCopy => copy.FromSystem ? "system copy" : "own copy",+ EngineCopyStatus.Installed => copy.Outdated ? "update available" : "installed",+ _ => "not installed",+ };+ var size = copy.Status switch+ {+ EngineCopyStatus.OwnCopy => copy.Folder!,+ EngineCopyStatus.Installed => $"{Bytes(package.InstalledSize)} on disk",+ _ when downloadable => $"{Bytes(DownloadSize(engines, package))} download, {Bytes(package.InstalledSize)} on disk",+ _ => UnavailableReason(package),+ };+ // The version Filee downloads; next to a copy Filee didn't download it would read as that copy's version.+ var version = downloadable && copy.Status != EngineCopyStatus.OwnCopy ? Version(engines, package) : "";+ output.WriteLine($"{package.Id,-12} {status,-16} {version,-8} {size}");+ }+ output.WriteLine();+ output.WriteLine("Install one with: filee engines install <id>");+ return Cli.Success;+ }++ /// <summary>One package in the JSON of 'engines'; fields that don't apply are left out, as in 'convert --json'.</summary>+ private static void WriteEngine(Utf8JsonWriter json, EngineInstaller engines, EnginePackage package, EngineCopy copy)+ {+ var downloadable = CanDownload(engines, package);+ json.WriteStartObject();+ json.WriteString("id", package.Id);+ json.WriteString("status", copy.Status.ToString().ToLowerInvariant());+ if (copy.Status == EngineCopyStatus.Installed)+ json.WriteBoolean("outdated", copy.Outdated);+ if (copy.Status == EngineCopyStatus.OwnCopy)+ {+ json.WriteBoolean("fromSystem", copy.FromSystem);+ json.WriteString("folder", copy.Folder);+ }+ if (downloadable)+ json.WriteString("version", Version(engines, package));+ json.WriteBoolean("downloadable", downloadable);+ if (downloadable)+ {+ json.WriteNumber("downloadSize", DownloadSize(engines, package));+ json.WriteNumber("installedSize", package.InstalledSize);+ }+ else+ {+ json.WriteString("unavailableReason", UnavailableReason(package));+ }+ Cli.WriteStrings(json, "converters", package.ConverterIds);+ json.WriteEndObject();+ }++ // ───────────────────────── Install and remove ─────────────────────────++ /// <summary>+ /// As the Engines page offers it: a package not installed, Filee's download of an outdated one, and Filee's download+ /// over a copy found on the PC (it is the tested version). The user's own copy is left in use.+ /// </summary>+ private static async Task<Result> InstallAsync(EnginePackage package, CliOptions options, CliHost host, TextWriter errors,+ CancellationToken cancellationToken)+ {+ var engines = host.Engines;+ var copy = engines.StatusOf(package);+ var downloadable = CanDownload(engines, package);+ var version = downloadable ? Version(engines, package) : null;+ if (copy.Status == EngineCopyStatus.Installed && !copy.Outdated)+ return new Result(package.Id, "skipped", $"{package.Id} {version} is already installed.", version);+ if (copy.Status == EngineCopyStatus.OwnCopy && !copy.FromSystem)+ return new Result(package.Id, "skipped", $"{package.Id} uses your own copy at {copy.Folder} (Settings → Engines).");+ if (copy.Status == EngineCopyStatus.OwnCopy && !downloadable)+ return new Result(package.Id, "skipped", $"{package.Id} uses the copy found at {copy.Folder}.");+ if (!downloadable)+ return new Result(package.Id, "failed", UnavailableReason(package));++ var progress = new InstallProgress(package.Id, version!, options.Quiet ? null : errors, options.Json,+ live: ReferenceEquals(errors, Console.Error) && !Console.IsErrorRedirected);+ try+ {+ await engines.InstallAsync(package, progress, cancellationToken);+ }+ catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)+ {+ return new Result(package.Id, "cancelled", $"Installing {package.Id} was cancelled.");+ }+ catch (Exception ex)+ {+ var refused = ex.GetBaseException() as ConversionRefusedException;+ return new Result(package.Id, "failed", ErrorText(ex, host), ErrorKey: refused?.ErrorKey);+ }+ finally+ {+ progress.Finish();+ }+ return new Result(package.Id, "installed",+ copy.Outdated ? $"Updated {package.Id} to {version}." : $"Installed {package.Id} {version}.", version);+ }++ /// <summary>Removes Filee's download, as the Engines page allows it; a copy Filee didn't download is never touched.</summary>+ private static Result Remove(EnginePackage package, EngineInstaller engines)+ {+ var copy = engines.StatusOf(package);+ if (copy.Status == EngineCopyStatus.OwnCopy)+ return new Result(package.Id, "skipped", copy.FromSystem+ ? $"{package.Id} uses the copy found at {copy.Folder}; Filee didn't download it."+ : $"{package.Id} uses your own copy at {copy.Folder}; Filee didn't download it.");+ if (copy.Status == EngineCopyStatus.NotInstalled)+ return new Result(package.Id, "skipped", $"{package.Id} is not installed.");+ try+ {+ engines.Uninstall(package);+ return new Result(package.Id, "removed", $"Removed {package.Id}.");+ }+ catch (EngineBusyException busy)+ {+ return new Result(package.Id, "failed", busy.Message);+ }+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)+ {+ return new Result(package.Id, "failed", $"{package.Id} is in use; close Filee or wait for the conversion to finish.");+ }+ }++ /// <summary>The reason as the Engines card gives it (EngineDownloadService.ErrorText), in English.</summary>+ private static string ErrorText(Exception ex, CliHost host) =>+ ex is HttpRequestException ? ex.Message+ : ex.GetBaseException() is ConversionRefusedException refused+ ? host.Texts[refused.ErrorKey] + (string.IsNullOrWhiteSpace(refused.Detail) ? "" : $" ({refused.Detail})")+ : ex.GetBaseException().Message;++ // ───────────────────────── Package details ─────────────────────────++ private static bool CanDownload(EngineInstaller engines, EnginePackage package) =>+ package.Components.Count > 0 && package.Components.All(engines.Components.ContainsKey);++ /// <summary>The version Filee downloads: its main component's (the first).</summary>+ private static string Version(EngineInstaller engines, EnginePackage package) => engines.Components[package.Components[0]].Version;++ private static long DownloadSize(EngineInstaller engines, EnginePackage package) =>+ package.Components.Sum(c => engines.Components[c].Size);++ private static string UnavailableReason(EnginePackage package) =>+ EngineDownloads.UnavailableReason(package) ?? $"No Filee download of {package.Id} is available for {EngineDownloads.RuntimeIdentifier}.";++ /// <summary>"297 MB", "1.3 GB", as Settings → Engines shows sizes.</summary>+ private static string Bytes(long bytes) => bytes >= 1_000_000_000+ ? FormattableString.Invariant($"{bytes / 1_000_000_000.0:0.0} GB")+ : $"{Math.Max(1, bytes / 1_000_000)} MB";++ /// <summary>"850 KB", "12.3 MB", "1.23 GB" for download progress, as the Engines card shows it.</summary>+ private static string Amount(double bytes) => bytes switch+ {+ >= 1_000_000_000 => FormattableString.Invariant($"{bytes / 1_000_000_000:0.00} GB"),+ >= 100_000_000 => FormattableString.Invariant($"{bytes / 1_000_000:0} MB"),+ >= 1_000_000 => FormattableString.Invariant($"{bytes / 1_000_000:0.0} MB"),+ _ => FormattableString.Invariant($"{Math.Max(0, bytes / 1_000):0} KB"),+ };++ /// <summary>+ /// Writes the installer's progress reports to stderr as they come (the installer reports about every 150 ms):+ /// one JSON object per line with --json; else a line that updates in place on a terminal, and one line per+ /// step and per 10% when stderr is redirected. Nothing with --quiet.+ /// </summary>+ private sealed class InstallProgress(string id, string version, TextWriter? errors, bool json, bool live) : IProgress<EngineInstallProgress>+ {+ private readonly Lock _gate = new();+ private (EngineInstallStage, string)? _step;+ private int _tenth = -1;+ private int _width;++ public void Report(EngineInstallProgress value)+ {+ if (errors is null)+ return;+ lock (_gate)+ {+ if (json)+ {+ errors.WriteLine(JsonLine(value));+ return;+ }+ var line = value.Stage == EngineInstallStage.Downloading+ ? $"Downloading {id} {version}: {Amount(value.BytesDone)} / {Amount(value.BytesTotal)} ({(int)(value.Fraction * 100)}%)"+ : $"Unpacking {value.ComponentId}…";+ var step = (value.Stage, value.ComponentId);+ var newStep = _step != step;+ var tenth = (int)(value.Fraction * 10);+ if (newStep)+ _tenth = -1;+ if (live)+ {+ if (newStep)+ EndLine();+ errors.Write("\r" + line.PadRight(_width));+ _width = line.Length;+ }+ else if (newStep || tenth > _tenth)+ {+ errors.WriteLine(line);+ }+ _step = step;+ _tenth = Math.Max(_tenth, tenth);+ }+ }++ /// <summary>Ends a line that was updating in place.</summary>+ public void Finish()+ {+ lock (_gate)+ EndLine();+ }++ private void EndLine()+ {+ if (errors is null || _width == 0)+ return;+ errors.WriteLine();+ _width = 0;+ }++ private string JsonLine(EngineInstallProgress value)+ {+ using var stream = new MemoryStream();+ using (var writer = new Utf8JsonWriter(stream))+ {+ writer.WriteStartObject();+ writer.WriteString("id", id);+ writer.WriteString("stage", value.Stage.ToString().ToLowerInvariant());+ writer.WriteString("component", value.ComponentId);+ writer.WriteNumber("fraction", Math.Round(value.Fraction, 4));+ writer.WriteNumber("bytesDone", value.BytesDone);+ writer.WriteNumber("bytesTotal", value.BytesTotal);+ writer.WriteEndObject();+ }+ return Encoding.UTF8.GetString(stream.ToArray());+ }+ }+}diff --git a/src/Filee.Cli/Help.cs b/src/Filee.Cli/Help.csindex 6ee4bf27..981bc9b5 100644--- a/src/Filee.Cli/Help.cs+++ b/src/Filee.Cli/Help.cs@@ -18,6 +18,7 @@ internal static class Help"With --json, prints the same as JSON (for scripts and other programs).\n","presets" => "Usage: filee presets [--json]\n\nLists the presets saved in Filee (built-in and your own).\n" +"With --json, prints the same as JSON (for scripts and other programs).\n",+ "engines" => Engines,_ => General,};@@ -30,6 +31,8 @@ internal static class Helpfilee watch <folder> --to <format> [options]filee formats [<format>] [--json]filee presets [--json]+ filee engines [--json]+ filee engines (install | remove) <engine> [--json]filee help [<command>]filee version@@ -38,6 +41,7 @@ internal static class Helpfilee convert *.png --to webp --quality 80 -o convertedfilee convert D:\Scans --recursive --preset to-pdffilee watch D:\Inbox --to pdf --move-originals+ filee engines install pandocPresets and engines are the ones in the Filee app. Exit codes: 0 nothing failed (skippedfiles included), 1 some failed or were cancelled, 2 wrong arguments or a missing input, 3 nothing to@@ -45,6 +49,21 @@ internal static class Help""";+ private const string Engines = """+ Usage: filee engines [--json]+ filee engines install <engine> [--json] [--quiet]+ filee engines remove <engine> [--json] [--quiet]++ Lists the optional engines of Settings → Engines (LibreOffice, Pandoc, FFmpeg, …), or installs or removes+ one with the same download and checks as the app. 'remove' only removes what Filee downloaded; your own copy+ and one found on this computer are left alone.++ Options:+ --json Print the result as JSON; progress goes to stderr, one JSON object per line+ --quiet Print nothing but errors++ """;+private const string Convert = """Usage: filee convert <files or folders...> (--to <format> | --preset <preset>) [options]diff --git a/tests/Filee.Cli.Tests/CliTests.cs b/tests/Filee.Cli.Tests/CliTests.csindex 8df17f75..90b1cb81 100644--- a/tests/Filee.Cli.Tests/CliTests.cs+++ b/tests/Filee.Cli.Tests/CliTests.cs@@ -13,6 +13,7 @@ using Filee.Engines.Tests;namespace Filee.Cli.Tests;+[Collection("Cli")] // shares EngineEnvironment state with EnginesCommandTestspublic sealed class CliTests : IDisposable{private readonly string _folder = Directory.CreateTempSubdirectory("filee-cli-").FullName;diff --git a/tests/Filee.Cli.Tests/EnginesCommandTests.cs b/tests/Filee.Cli.Tests/EnginesCommandTests.csnew file mode 100644index 00000000..7f25c575--- /dev/null+++ b/tests/Filee.Cli.Tests/EnginesCommandTests.cs@@ -0,0 +1,280 @@+// "filee engines" without the network: the installer gets a temporary engines folder and a fake server whose+// downloads stand in for engines.json.++using System.IO.Compression;+using System.Net;+using System.Security.Cryptography;+using System.Text.Json;+using Filee.Core.Settings;+using Filee.Engines.Infrastructure;++namespace Filee.Cli.Tests;++[Collection("Cli")] // EngineEnvironment.OwnCopies and SearchSystem are process-wide, as in CliTests+public sealed class EnginesCommandTests : IDisposable+{+ private readonly string _settings = Directory.CreateTempSubdirectory("filee-engines-settings-").FullName;+ private readonly string _root = Directory.CreateTempSubdirectory("filee-engines-root-").FullName;+ private readonly bool _searchSystem = EngineEnvironment.SearchSystem;+ private readonly FakeServer _server = new();+ private readonly Dictionary<string, EngineComponent> _components = [];+ private readonly byte[] _pandoc;++ public EnginesCommandTests()+ {+ // A pandoc on this PC (PATH) must not change what the tests see.+ EngineEnvironment.SearchSystem = false;+ _pandoc = Zip(($"pandoc-1.0/bin/{EngineEnvironment.OwnCopyPrograms["pandoc"][0]}", "program"));+ var component = new EngineComponent("pandoc", "1.0", "https://example.test/pandoc.zip",+ Convert.ToHexString(SHA256.HashData(_pandoc)), _pandoc.Length, "zip");+ _components[component.Id] = component;+ _server.Files[component.Url] = _pandoc;+ }++ public void Dispose()+ {+ EngineEnvironment.SearchSystem = _searchSystem;+ EngineEnvironment.OwnCopies = new Dictionary<string, string>();+ foreach (var dir in new[] { _settings, _root })+ try { Directory.Delete(dir, true); } catch (IOException) { }+ }++ private sealed class FakeServer : HttpMessageHandler+ {+ public Dictionary<string, byte[]> Files { get; } = [];+ public List<string> Requests { get; } = [];++ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)+ {+ var url = request.RequestUri!.ToString();+ Requests.Add(url);+ return Task.FromResult(Files.TryGetValue(url, out var bytes)+ ? new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(bytes) }+ : new HttpResponseMessage(HttpStatusCode.NotFound));+ }+ }++ private static byte[] Zip(params (string Path, string Text)[] files)+ {+ using var buffer = new MemoryStream();+ using (var zip = new ZipArchive(buffer, ZipArchiveMode.Create, leaveOpen: true))+ {+ foreach (var (path, text) in files)+ {+ using var writer = new StreamWriter(zip.CreateEntry(path).Open());+ writer.Write(text);+ }+ }+ return buffer.ToArray();+ }++ private EngineInstaller Installer() => new(new HttpClient(_server), _root, _components);++ private async Task<(int Code, string Output, string Errors)> Run(params string[] args)+ {+ var output = new StringWriter();+ var errors = new StringWriter();+ var code = await Cli.RunAsync(args, output, errors, TestContext.Current.CancellationToken, () => CliHost.Create(_settings, Installer()));+ return (code, output.ToString(), errors.ToString());+ }++ private static JsonElement Engine(string json, string id) =>+ JsonDocument.Parse(json).RootElement.GetProperty("engines").EnumerateArray().Single(e => e.GetProperty("id").GetString() == id);++ private static string[] Names(JsonElement element) => element.EnumerateObject().Select(p => p.Name).ToArray();++ [Fact]+ public async Task The_list_shows_every_package_with_what_Filee_can_download_here()+ {+ var (code, output, _) = await Run("engines", "--json");+ Assert.Equal(0, code);+ var root = JsonDocument.Parse(output).RootElement;+ Assert.Equal(EngineDownloads.RuntimeIdentifier, root.GetProperty("platform").GetString());+ Assert.Equal(EngineDownloads.Packages.Select(p => p.Id), root.GetProperty("engines").EnumerateArray().Select(e => e.GetProperty("id").GetString()));++ var pandoc = Engine(output, "pandoc");+ Assert.Equal(["id", "status", "version", "downloadable", "downloadSize", "installedSize", "converters"], Names(pandoc));+ Assert.Equal("notinstalled", pandoc.GetProperty("status").GetString());+ Assert.Equal("1.0", pandoc.GetProperty("version").GetString());+ Assert.Equal(_pandoc.Length, pandoc.GetProperty("downloadSize").GetInt64());+ Assert.Equal(EngineDownloads.Packages.Single(p => p.Id == "pandoc").InstalledSize, pandoc.GetProperty("installedSize").GetInt64());+ Assert.Equal(["pandoc"], pandoc.GetProperty("converters").EnumerateArray().Select(c => c.GetString()));++ // No download of it in this list: offered with the reason instead of a version and sizes.+ var libreOffice = Engine(output, "libreoffice");+ Assert.Equal(["id", "status", "downloadable", "unavailableReason", "converters"], Names(libreOffice));+ Assert.False(libreOffice.GetProperty("downloadable").GetBoolean());+ Assert.False(string.IsNullOrWhiteSpace(libreOffice.GetProperty("unavailableReason").GetString()));++ (code, output, _) = await Run("engines");+ Assert.Equal(0, code);+ Assert.Contains(output.Split('\n'), l => l.StartsWith("pandoc ", StringComparison.Ordinal) && l.Contains("not installed") && l.Contains("1.0"));+ Assert.Contains("filee engines install <id>", output);+ }++ [Fact]+ public async Task Install_downloads_once_reports_progress_and_skips_what_is_installed()+ {+ var (code, output, errors) = await Run("engines", "install", "pandoc", "--json");+ Assert.Equal(0, code);+ var result = JsonDocument.Parse(output).RootElement;+ Assert.Equal(["id", "state", "version"], Names(result));+ Assert.Equal("installed", result.GetProperty("state").GetString());+ Assert.Equal("1.0", result.GetProperty("version").GetString());+ Assert.True(File.Exists(Path.Combine(_root, "pandoc", "bin", EngineEnvironment.OwnCopyPrograms["pandoc"][0])));++ // Progress: the installer's reports, one JSON object per line on stderr.+ var reports = errors.Split('\n', StringSplitOptions.RemoveEmptyEntries).Select(l => JsonDocument.Parse(l).RootElement).ToList();+ Assert.Contains(reports, r => r.GetProperty("stage").GetString() == "downloading");+ Assert.Contains(reports, r => r.GetProperty("stage").GetString() == "unpacking");+ Assert.All(reports, r => Assert.Equal(["id", "stage", "component", "fraction", "bytesDone", "bytesTotal"], Names(r)));++ Assert.Equal("installed", Engine((await Run("engines", "--json")).Output, "pandoc").GetProperty("status").GetString());+ Assert.False(Engine((await Run("engines", "--json")).Output, "pandoc").GetProperty("outdated").GetBoolean());++ (code, output, errors) = await Run("engines", "install", "pandoc", "--json");+ Assert.Equal(0, code);+ result = JsonDocument.Parse(output).RootElement;+ Assert.Equal(["id", "state", "version", "reason"], Names(result));+ Assert.Equal("skipped", result.GetProperty("state").GetString());+ Assert.Empty(errors);+ Assert.Single(_server.Requests);++ (code, output, _) = await Run("engines", "install", "pandoc");+ Assert.Equal(0, code);+ Assert.Equal("pandoc 1.0 is already installed.", output.Trim());+ }++ [Fact]+ public async Task Install_without_json_prints_the_result_and_progress_lines()+ {+ var (code, output, errors) = await Run("engines", "install", "pandoc");+ Assert.Equal(0, code);+ Assert.Equal("Installed pandoc 1.0.", output.Trim());+ Assert.Contains("Downloading pandoc 1.0:", errors);+ Assert.Contains("Unpacking pandoc…", errors);++ Assert.Equal(0, (await Run("engines", "remove", "pandoc")).Code);+ (code, output, errors) = await Run("engines", "install", "pandoc", "--quiet");+ Assert.Equal(0, code);+ Assert.Empty(output);+ Assert.Empty(errors);+ }++ [Fact]+ public async Task A_package_another_Filee_is_installing_fails_without_a_download()+ {+ var locks = Directory.CreateDirectory(Path.Combine(_root, ".downloads", "locks")).FullName;+ using (new FileStream(Path.Combine(locks, "pandoc.lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))+ {+ var (code, output, errors) = await Run("engines", "install", "pandoc", "--json");+ Assert.Equal(1, code);+ var result = JsonDocument.Parse(output).RootElement;+ Assert.Equal(["id", "state", "error"], Names(result));+ Assert.Equal("failed", result.GetProperty("state").GetString());+ Assert.Equal("pandoc is being installed by another Filee. Try again when it has finished.", result.GetProperty("error").GetString());+ Assert.Empty(errors);++ (code, output, errors) = await Run("engines", "install", "pandoc");+ Assert.Equal(1, code);+ Assert.Empty(output);+ Assert.Equal("filee: pandoc is being installed by another Filee. Try again when it has finished.", errors.Trim());+ }+ Assert.Empty(_server.Requests);+ Assert.Equal(0, (await Run("engines", "install", "pandoc")).Code);+ }++ [Fact]+ public async Task Remove_takes_Filees_download_and_nothing_else()+ {+ Assert.Equal(0, (await Run("engines", "install", "pandoc")).Code);+ var (code, output, _) = await Run("engines", "remove", "pandoc", "--json");+ Assert.Equal(0, code);+ Assert.Equal("removed", JsonDocument.Parse(output).RootElement.GetProperty("state").GetString());+ Assert.False(Directory.Exists(Path.Combine(_root, "pandoc")));++ (code, output, _) = await Run("engines", "remove", "pandoc", "--json");+ Assert.Equal(0, code);+ var result = JsonDocument.Parse(output).RootElement;+ Assert.Equal(["id", "state", "reason"], Names(result));+ Assert.Equal("skipped", result.GetProperty("state").GetString());+ Assert.Equal("pandoc is not installed.", result.GetProperty("reason").GetString());+ }++ [Fact]+ public async Task The_users_own_copy_is_listed_and_neither_replaced_nor_removed()+ {+ var own = Directory.CreateTempSubdirectory("filee-own-pandoc-").FullName;+ try+ {+ var program = Path.Combine(own, EngineEnvironment.OwnCopyPrograms["pandoc"][0]);+ File.WriteAllText(program, "my pandoc");+ var store = new UserDataStore(_settings);+ store.Load();+ store.Settings.EngineOwnCopies["pandoc"] = own;+ store.SaveSettings();++ var (code, output, _) = await Run("engines", "--json");+ Assert.Equal(0, code);+ var pandoc = Engine(output, "pandoc");+ Assert.Equal(["id", "status", "fromSystem", "folder", "version", "downloadable", "downloadSize", "installedSize", "converters"], Names(pandoc));+ Assert.Equal("owncopy", pandoc.GetProperty("status").GetString());+ Assert.False(pandoc.GetProperty("fromSystem").GetBoolean());+ Assert.Equal(own, pandoc.GetProperty("folder").GetString());+ var row = (await Run("engines")).Output.Split('\n').Single(l => l.StartsWith("pandoc ", StringComparison.Ordinal));+ Assert.Contains("own copy", row);+ Assert.DoesNotContain("1.0", row);++ (code, output, _) = await Run("engines", "install", "pandoc", "--json");+ Assert.Equal(0, code);+ Assert.Equal("skipped", JsonDocument.Parse(output).RootElement.GetProperty("state").GetString());+ Assert.Empty(_server.Requests);++ (code, output, _) = await Run("engines", "remove", "pandoc", "--json");+ Assert.Equal(0, code);+ var result = JsonDocument.Parse(output).RootElement;+ Assert.Equal("skipped", result.GetProperty("state").GetString());+ Assert.Contains("your own copy", result.GetProperty("reason").GetString());+ Assert.Equal("my pandoc", File.ReadAllText(program));+ }+ finally+ {+ Directory.Delete(own, true);+ }+ }++ [Fact]+ public async Task A_package_without_a_download_here_fails_without_the_network()+ {+ var (code, output, errors) = await Run("engines", "install", "libreoffice", "--json");+ Assert.Equal(1, code);+ var result = JsonDocument.Parse(output).RootElement;+ Assert.Equal("failed", result.GetProperty("state").GetString());+ Assert.False(string.IsNullOrWhiteSpace(result.GetProperty("error").GetString()));+ Assert.Empty(errors);+ Assert.Equal(1, (await Run("engines", "install", "libreoffice")).Code);+ Assert.Empty(_server.Requests);+ }++ [Theory]+ [InlineData("engines", "install", "nonesuch")]+ [InlineData("engines", "install")]+ [InlineData("engines", "install", "pandoc", "ffmpeg")]+ [InlineData("engines", "update", "pandoc")]+ public async Task Wrong_engine_arguments_are_usage_errors(params string[] args)+ {+ var (code, output, errors) = await Run(args);+ Assert.Equal(2, code);+ Assert.Empty(output);+ Assert.StartsWith("filee: ", errors);+ Assert.Empty(_server.Requests);+ }++ [Fact]+ public async Task Engines_go_to_the_apps_engines_folder_which_follows_FILEE_DATA_DIR()+ {+ await using var host = CliHost.Create(_settings);+ Assert.Equal(EngineEnvironment.DownloadRoot, host.Engines.Root);+ Assert.Same(EngineDownloads.Components, host.Engines.Components);+ }+}