// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Progmasoft.Tazel.Archive; namespace Progmasoft.Tazel.PluginSdk; /// /// Makes $group.$name-$version.tazel.tar.xz and its .sha256sums from the output of a plugin: its /// assemblies and those of its references, with tazel-plugin.json. The plugin API and the DSL are left out, /// whatever brought them, as Tazel has its own. /// public sealed class PackTazelPlugin : Task { private static readonly string[] Shared = ["Progmasoft.Tazel.PluginApi.dll", "Progmasoft.Tazel.Dsl.dll"]; /// The output directory of the build. [Required] public string OutputDirectory { get; set; } = ""; /// The file of the plugin's own assembly. [Required] public string EntryAssembly { get; set; } = ""; /// The group of the plugin. [Required] public string Group { get; set; } = ""; /// The name of the plugin. [Required] public string Name { get; set; } = ""; /// The version of the plugin. [Required] public string Version { get; set; } = ""; /// The version of the plugin API the plugin was built against. [Required] public string PluginApiVersion { get; set; } = ""; /// Where the archive goes. [Required] public string PackageOutputPath { get; set; } = ""; /// The archive that was written. [Output] public string Archive { get; set; } = ""; /// public override bool Execute() { var files = Directory.EnumerateFiles(OutputDirectory, "*.dll", SearchOption.TopDirectoryOnly) .Select(Path.GetFileName).OfType() .Where(file => !Shared.Contains(file, StringComparer.Ordinal)) .Order(StringComparer.Ordinal) .ToList(); if (!files.Contains(EntryAssembly, StringComparer.Ordinal)) { Log.LogError($"{EntryAssembly} is not in {OutputDirectory}"); return false; } var staging = Path.Combine(Path.GetTempPath(), "tazel-plugin-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(staging); try { foreach (var file in files) { File.Copy(Path.Combine(OutputDirectory, file), Path.Combine(staging, file)); } var manifest = new SortedDictionary(StringComparer.Ordinal) { ["entryAssembly"] = EntryAssembly, ["group"] = Group, ["name"] = Name, ["pluginApi"] = PluginApiVersion, ["version"] = Version, }; File.WriteAllText(Path.Combine(staging, "tazel-plugin.json"), JsonSerializer.Serialize(manifest, new JsonSerializerOptions { WriteIndented = true }) + "\n"); files.Add("tazel-plugin.json"); files.Sort(StringComparer.Ordinal); var baseName = $"{Group}.{Name}-{Version}"; Archive = Path.Combine(PackageOutputPath, baseName + ".tazel.tar.xz"); ReproducibleArchive.Write(staging, baseName, Archive, files); File.WriteAllText(Archive + ".sha256sums", ReproducibleArchive.SumsLine(Archive)); Log.LogMessage(MessageImportance.High, $"Tazel plugin: {Archive}"); return true; } catch (IOException error) { Log.LogError($"The plugin archive could not be written: {error.Message}"); return false; } finally { Directory.Delete(staging, recursive: true); } } }