XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Text.Json;
using XFEToolBox.Core.Tools;
using XFEToolBox.QuickTools;

namespace QuickTools.Validation;

internal static partial class Program
{
    private static string WorkspaceRoot => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "XFEToolBox", "CrossVersion", "EditorWorkspaces");
    private static partial async Task PackageTools()
    {
        GenerateIcons();
        string packages = Path.Combine(root, "Packages"); Directory.CreateDirectory(packages);
        var host = typeof(XFEToolBox.Client.Utilities.QuickToolsBridge).Assembly;
        var service = host.GetType("XFEToolBox.Client.Utilities.ToolProjectRunService", true)!;
        var build = service.GetMethod("BuildAsync", BindingFlags.Public | BindingFlags.Static)!;
        var extract = service.GetMethod("ExtractAndValidatePackageAsync", BindingFlags.NonPublic | BindingFlags.Static)!;
        foreach (string folder in Folders)
        {
            await Test("Production package / unpack / compile " + folder, async () =>
            {
                string workspace = Path.Combine(WorkspaceRoot, folder);
                var manifest = JsonSerializer.Deserialize<ToolPackageManifest>(File.ReadAllText(Path.Combine(workspace, "manifest.json")), new JsonSerializerOptions(JsonSerializerDefaults.Web))!;
                var buildTask = (Task)build.Invoke(null, [workspace, manifest, CancellationToken.None, null])!;
                await buildTask.WaitAsync(TimeSpan.FromMinutes(4));
                object result = buildTask.GetType().GetProperty("Result")!.GetValue(buildTask)!;
                Ensure((bool)result.GetType().GetProperty("Success")!.GetValue(result)!, (string)result.GetType().GetProperty("Message")!.GetValue(result)!);
                string package = Path.Combine(packages, manifest.Id + "-" + manifest.Version + ".xfetool");
                ZipFile.CreateFromDirectory(workspace, package, CompressionLevel.Optimal, false);
                string unpacked = Path.Combine(root, "unpacked", folder); Directory.CreateDirectory(unpacked);
                var unpack = (Task<ToolPackageManifest>)extract.Invoke(null, [package, unpacked, manifest.Id, manifest.Version, CancellationToken.None])!;
                var validated = await unpack;
                Equal(manifest.Id, validated.Id); Ensure(File.Exists(Path.Combine(unpacked, validated.Icon!)), "Icon packaged");
                var imported = (Task)build.Invoke(null, [unpacked, validated, CancellationToken.None, null])!;
                await imported.WaitAsync(TimeSpan.FromMinutes(4));
                var importedResult = imported.GetType().GetProperty("Result")!.GetValue(imported)!;
                Ensure((bool)importedResult.GetType().GetProperty("Success")!.GetValue(importedResult)!, (string)importedResult.GetType().GetProperty("Message")!.GetValue(importedResult)!);
                await ValidateActualPackage(package, manifest, service);
            });
        }
        await Test("Independent production runtime / all eight pages / dependencies", async () =>
        {
            string fixture = Path.Combine(AppContext.BaseDirectory, "RuntimeFixture");
            var manifest = JsonSerializer.Deserialize<ToolPackageManifest>(File.ReadAllText(Path.Combine(fixture, "manifest.json")), new JsonSerializerOptions(JsonSerializerDefaults.Web))!;
            string marker = Path.Combine(root, "runtime-result.json");
            string? previous = Environment.GetEnvironmentVariable("XFE_QUICKTOOLS_SMOKE_RESULT");
            System.Diagnostics.Process? process = null;
            try
            {
                Environment.SetEnvironmentVariable("XFE_QUICKTOOLS_SMOKE_RESULT", marker);
                var launch = (Task)service.GetMethod("BuildAndRunAsync", BindingFlags.Public | BindingFlags.Static)!.Invoke(null, [fixture, manifest, CancellationToken.None, null])!;
                await launch.WaitAsync(TimeSpan.FromMinutes(4));
                object result = launch.GetType().GetProperty("Result")!.GetValue(launch)!;
                Ensure((bool)result.GetType().GetProperty("Success")!.GetValue(result)!, (string)result.GetType().GetProperty("Message")!.GetValue(result)!);
                int pid = (int)result.GetType().GetProperty("ProcessId")!.GetValue(result)!;
                process = System.Diagnostics.Process.GetProcessById(pid);
                await process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(30));
                Ensure(File.Exists(marker), "No runtime completion marker");
                using var report = JsonDocument.Parse(File.ReadAllText(marker));
                Ensure(report.RootElement.GetProperty("success").GetBoolean(), report.RootElement.ToString());
                Equal(8, report.RootElement.GetProperty("pages").GetInt32());
                Equal(0, process.ExitCode);
            }
            finally
            {
                Environment.SetEnvironmentVariable("XFE_QUICKTOOLS_SMOKE_RESULT", previous);
                if (process is not null) { if (!process.HasExited) { process.Kill(); await process.WaitForExitAsync(); } process.Dispose(); }
            }
        });
    }

}