Progmasoft Git

progmaweb

The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.

Clonegit clone https://git.progmasoft.com/progmaweb

Take who a Tazel plugin is from the publish command

Tazel no longer puts a publisher, a name or a version into the
descriptor of a plugin JAR (plugin API 3): who a plugin is comes from
ViGet, which records it when the plugin is pushed, or from the path of
a localPlugin.

- viget publish-tazel-plugin takes <Publisher> <Name> <Version> <jar>.
- TazelPluginDescriptor.TryRead and TazelPluginPublishing.PublishAsync
  take the three from the caller and check them as before: one
  identifier each for publisher and name, an exact version of three to
  five numbers.
- A descriptor that holds publisher, name or version is refused, with
  the keys it names.
- ops/README.md shows the new command line.
Commit
4a00a68309a3605dafedbc522569fc1b2c8aca1e
Author
Leitwolf11 ·
Parents
35e092148175

Files · Download .tar.gz

Changes

 apps/api-tests/ViGetTests.cs            | 75 +++++++++++++++++++++------------
 apps/api/ViGet/TazelPluginDescriptor.cs | 50 +++++++++++++---------
 apps/api/ViGet/TazelPluginPublishing.cs | 11 +++--
 apps/api/ViGet/ViGetCommand.cs          | 10 +++--
 ops/README.md                           |  5 ++-
 5 files changed, 95 insertions(+), 56 deletions(-)

diff --git a/apps/api-tests/ViGetTests.cs b/apps/api-tests/ViGetTests.cs
index bcffd4e..2037d30 100644
--- a/apps/api-tests/ViGetTests.cs
+++ b/apps/api-tests/ViGetTests.cs
@@ -49,28 +49,43 @@ public sealed class ViGetTests : IDisposable
     [Fact]
     public void TheDescriptorOfAPluginJarIsReadAndChecked()
     {
-        Assert.Null(TazelPluginDescriptor.TryRead(Jar(), out TazelPluginDescriptor? descriptor));
+        Assert.Null(Read(Jar(), out TazelPluginDescriptor? descriptor));
         Assert.Equal(
             new TazelPluginDescriptor(
-                "Progmasoft", "CMake", "0.1.0", 2, "Builds CMake projects.", ["First Author", "Second Author"],
+                "Progmasoft", "CMake", "0.1.0", 3, "Builds CMake projects.", ["First Author", "Second Author"],
                 "MPL-2.0", "https://example.com/cmake", null),
             descriptor!,
             new DescriptorComparer());
 
         Assert.Equal("The JAR has no META-INF/services/com.progmasoft.api.tazel.dsl.plugin.ProjectPlugin.",
-            TazelPluginDescriptor.TryRead(Jar(service: false), out _));
-        Assert.Equal("The descriptor needs an exact version of three to five numbers.",
-            TazelPluginDescriptor.TryRead(Jar(version: "1.0"), out _));
-        Assert.Equal("The descriptor needs an exact version of three to five numbers.",
-            TazelPluginDescriptor.TryRead(Jar(version: "1.2.3.4.5.6"), out _));
+            Read(Jar(service: false), out _));
+        Assert.Equal("The version must be exact, of three to five numbers.",
+            TazelPluginDescriptor.TryRead(Jar(), "Progmasoft", "CMake", "1.0", out _));
+        Assert.Equal("The version must be exact, of three to five numbers.",
+            TazelPluginDescriptor.TryRead(Jar(), "Progmasoft", "CMake", "1.2.3.4.5.6", out _));
         Assert.True(TazelPluginDescriptor.IsVersion("0.1.1.1"));
         Assert.True(TazelPluginDescriptor.IsVersion("0.1.1.1.1"));
         Assert.True(TazelPluginDescriptor.IsVersion("0.1.1.1.1-build.2"));
-        Assert.Equal("The descriptor needs a name that is one identifier.",
-            TazelPluginDescriptor.TryRead(Jar(name: "c-make"), out _));
+        Assert.Equal("The name of the plugin must be one identifier.",
+            TazelPluginDescriptor.TryRead(Jar(), "Progmasoft", "c-make", "0.1.0", out _));
+        Assert.Equal("The publisher must be one identifier.",
+            TazelPluginDescriptor.TryRead(Jar(), "Progma soft", "CMake", "0.1.0", out _));
         Assert.Equal("The homepage of the descriptor is not an https address.",
-            TazelPluginDescriptor.TryRead(Jar(homepage: "http\\://example.com"), out _));
-        Assert.Equal("The file is not a JAR.", TazelPluginDescriptor.TryRead("not a zip"u8.ToArray(), out _));
+            Read(Jar(homepage: "http\\://example.com"), out _));
+        Assert.Equal("The file is not a JAR.", Read("not a zip"u8.ToArray(), out _));
+    }
+
+    [Fact]
+    public void ADescriptorThatNamesThePluginIsRefused()
+    {
+        Assert.Equal(
+            "The descriptor names publisher, name, version; " +
+            "ViGet takes who a plugin is from the command that publishes it, so its JAR names none.",
+            Read(Jar(identity: ["publisher=Progmasoft", "name=CMake", "version=0.1.0"]), out _));
+        Assert.Equal(
+            "The descriptor names version; " +
+            "ViGet takes who a plugin is from the command that publishes it, so its JAR names none.",
+            Read(Jar(identity: ["version="]), out _));
     }
 
     [Fact]
@@ -79,18 +94,18 @@ public sealed class ViGetTests : IDisposable
         (TazelPluginPublishing publishing, InMemoryAccountStore accounts, ITazelPluginCatalog catalog) = Publishing();
         CancellationToken cancellationToken = TestContext.Current.CancellationToken;
 
-        PublishResult noAccount = await publishing.PublishAsync(Jar(), cancellationToken);
+        PublishResult noAccount = await publishing.PublishAsync(Jar(), "Progmasoft", "CMake", "0.1.0", cancellationToken);
         Assert.Equal("No Progmasoft Account is named 'Progmasoft'.", noAccount.Error);
 
         await AddAccountAsync(accounts, "Progmasoft");
         await AddAccountAsync(accounts, "Someone");
-        PublishResult notAllowed = await publishing.PublishAsync(Jar(publisher: "Someone"), cancellationToken);
+        PublishResult notAllowed = await publishing.PublishAsync(Jar(), "Someone", "CMake", "0.1.0", cancellationToken);
         Assert.Equal(
             "Publishing Tazel plugins is open to Progmasoft for now; 'Someone' cannot publish yet.",
             notAllowed.Error);
 
         byte[] jar = Jar();
-        PublishResult published = await publishing.PublishAsync(jar, cancellationToken);
+        PublishResult published = await publishing.PublishAsync(jar, "Progmasoft", "CMake", "0.1.0", cancellationToken);
         Assert.True(published.Published, published.Error);
         string file = Path.Combine(artifactRoot, "dslplugins", "tazel", "Progmasoft", "CMake", "0.1.0", "CMake-0.1.0.jar");
         Assert.Equal(jar, await File.ReadAllBytesAsync(file, cancellationToken));
@@ -98,11 +113,12 @@ public sealed class ViGetTests : IDisposable
         Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(jar)), plugin.Versions.Single().Sha256);
         Assert.Equal("MPL-2.0", plugin.License);
 
-        PublishResult again = await publishing.PublishAsync(Jar(description: "Another build."), cancellationToken);
+        PublishResult again = await publishing.PublishAsync(
+            Jar(description: "Another build."), "Progmasoft", "CMake", "0.1.0", cancellationToken);
         Assert.Equal("Progmasoft.CMake 0.1.0 is published already.", again.Error);
         Assert.Equal(jar, await File.ReadAllBytesAsync(file, cancellationToken));
 
-        PublishResult folded = await publishing.PublishAsync(Jar(name: "CMAKE", version: "0.2.0"), cancellationToken);
+        PublishResult folded = await publishing.PublishAsync(Jar(), "Progmasoft", "CMAKE", "0.2.0", cancellationToken);
         Assert.Equal(
             "Progmasoft has a plugin whose name differs from 'CMAKE' only in letter case.", folded.Error);
         Assert.False(File.Exists(Path.Combine(
@@ -114,7 +130,8 @@ public sealed class ViGetTests : IDisposable
     {
         (TazelPluginPublishing publishing, InMemoryAccountStore accounts, _) = Publishing();
         await AddAccountAsync(accounts, "Progmasoft");
-        PublishResult result = await publishing.PublishAsync(Jar(license: null), TestContext.Current.CancellationToken);
+        PublishResult result = await publishing.PublishAsync(
+            Jar(license: null), "Progmasoft", "CMake", "0.1.0", TestContext.Current.CancellationToken);
         Assert.Equal(
             "ViGet needs a description, at least one author and a license in the descriptor.", result.Error);
     }
@@ -125,7 +142,7 @@ public sealed class ViGetTests : IDisposable
         await using AccountApiFactory factory = new();
         ITazelPluginCatalog catalog = factory.Services.GetRequiredService<ITazelPluginCatalog>();
         CancellationToken cancellationToken = TestContext.Current.CancellationToken;
-        Assert.Null(TazelPluginDescriptor.TryRead(Jar(), out TazelPluginDescriptor? descriptor));
+        Assert.Null(Read(Jar(), out TazelPluginDescriptor? descriptor));
         await catalog.AddVersionAsync(descriptor!, new string('a', 64), 10, DateTimeOffset.UnixEpoch, cancellationToken);
         await catalog.AddVersionAsync(
             descriptor! with { Version = "0.2.0" }, new string('b', 64), 10, DateTimeOffset.UnixEpoch.AddDays(1),
@@ -140,7 +157,7 @@ public sealed class ViGetTests : IDisposable
         JsonElement[] versions = [.. index.RootElement.GetProperty("versions").EnumerateArray()];
         Assert.Equal(["0.2.0", "0.1.0"], versions.Select(version => version.GetProperty("version").GetString()));
         Assert.True(versions[0].GetProperty("yanked").GetBoolean());
-        Assert.Equal(2, versions[1].GetProperty("apiVersion").GetInt32());
+        Assert.Equal(3, versions[1].GetProperty("apiVersion").GetInt32());
         Assert.Equal(new string('a', 64), versions[1].GetProperty("sha256").GetString());
 
         using JsonDocument detail = JsonDocument.Parse(await client.GetStringAsync(
@@ -175,8 +192,9 @@ public sealed class ViGetTests : IDisposable
         StringWriter output = new();
         StringWriter errors = new();
 
+        string[] publish = ["publish-tazel-plugin", "Progmasoft", "CMake", "0.1.0", jar];
         Assert.Equal(0, await ViGetCommand.RunAsync(
-            services, ["publish-tazel-plugin", jar], output, errors, TestContext.Current.CancellationToken));
+            services, publish, output, errors, TestContext.Current.CancellationToken));
         Assert.Contains("Published Progmasoft.CMake 0.1.0", output.ToString(), StringComparison.Ordinal);
         Assert.Equal(0, await ViGetCommand.RunAsync(
             services, ["yank-tazel-plugin", "Progmasoft", "CMake", "0.1.0"], output, errors,
@@ -184,6 +202,8 @@ public sealed class ViGetTests : IDisposable
         Assert.True((await catalog.FindAsync("Progmasoft", "CMake", TestContext.Current.CancellationToken))!
             .Versions.Single().Yanked);
         Assert.Equal(1, await ViGetCommand.RunAsync(
+            services, publish, output, errors, TestContext.Current.CancellationToken));
+        Assert.Equal(2, await ViGetCommand.RunAsync(
             services, ["publish-tazel-plugin", jar], output, errors, TestContext.Current.CancellationToken));
         Assert.Equal(2, await ViGetCommand.RunAsync(
             services, ["publish"], output, errors, TestContext.Current.CancellationToken));
@@ -214,24 +234,23 @@ public sealed class ViGetTests : IDisposable
                 DateTimeOffset.UnixEpoch),
             TestContext.Current.CancellationToken);
 
+    private static string? Read(byte[] jar, out TazelPluginDescriptor? descriptor) =>
+        TazelPluginDescriptor.TryRead(jar, "Progmasoft", "CMake", "0.1.0", out descriptor);
+
     private static byte[] Jar(
-        string publisher = "Progmasoft",
-        string name = "CMake",
-        string version = "0.1.0",
         string? description = "Builds CMake projects.",
         string? license = "MPL-2.0",
         string homepage = "https\\://example.com/cmake",
-        bool service = true)
+        bool service = true,
+        string[]? identity = null)
     {
         using MemoryStream bytes = new();
         using (ZipArchive archive = new(bytes, ZipArchiveMode.Create, leaveOpen: true))
         {
             List<string> lines =
             [
-                "publisher=" + publisher,
-                "name=" + name,
-                "version=" + version,
-                "apiVersion=2",
+                .. identity ?? [],
+                "apiVersion=3",
                 "author.0=First Author",
                 "author.1=Second Author",
                 "homepage=" + homepage
diff --git a/apps/api/ViGet/TazelPluginDescriptor.cs b/apps/api/ViGet/TazelPluginDescriptor.cs
index 2433cc7..7cf3ca1 100644
--- a/apps/api/ViGet/TazelPluginDescriptor.cs
+++ b/apps/api/ViGet/TazelPluginDescriptor.cs
@@ -8,8 +8,9 @@ using System.Text.RegularExpressions;
 namespace Progmasoft.Progmaweb.Api.ViGet;
 
 /// <summary>
-/// The descriptor of a Tazel plugin, <c>META-INF/tazel-plugin.properties</c> of its JAR, as Tazel reads it, with the
-/// fields ViGet shows on the page of the plugin.
+/// A Tazel plugin as ViGet publishes it: who it is, which the command that publishes it names, and the descriptor of
+/// its JAR, <c>META-INF/tazel-plugin.properties</c>, as Tazel reads it, with the fields ViGet shows on the page of the
+/// plugin. The descriptor names neither the publisher, the name nor the version.
 /// </summary>
 /// <param name="Publisher">The Progmasoft Account the plugin is published under.</param>
 /// <param name="Name">The name of the plugin; one identifier.</param>
@@ -37,19 +38,41 @@ internal sealed partial record TazelPluginDescriptor(
     /// <summary>The service entry that names the class Tazel loads.</summary>
     public const string ServiceEntry = "META-INF/services/com.progmasoft.api.tazel.dsl.plugin.ProjectPlugin";
 
+    /// <summary>The keys of who a plugin is, which its descriptor may not hold.</summary>
+    private static readonly string[] IdentityKeys = ["publisher", "name", "version"];
+
     /// <summary>Whether a text is one identifier, as Tazel requires of a publisher and a plugin name.</summary>
     public static bool IsIdentifier(string value) => IdentifierPattern().IsMatch(value);
 
     /// <summary>Whether a text is an exact version of three to five numbers, as Tazel requires of a plugin version.</summary>
     public static bool IsVersion(string value) => VersionPattern().IsMatch(value);
 
-    /// <summary>Reads and checks the descriptor of a plugin JAR.</summary>
+    /// <summary>Reads and checks the descriptor of a plugin JAR, published as the plugin the command names.</summary>
     /// <param name="jar">The bytes of the JAR.</param>
+    /// <param name="publisher">The publisher the command names.</param>
+    /// <param name="name">The name of the plugin the command names.</param>
+    /// <param name="version">The version the command names.</param>
     /// <param name="descriptor">The descriptor, when the JAR is a valid plugin.</param>
     /// <returns>Why the JAR is refused, or <see langword="null"/> when it is valid.</returns>
-    public static string? TryRead(byte[] jar, out TazelPluginDescriptor? descriptor)
+    public static string? TryRead(
+        byte[] jar, string publisher, string name, string version, out TazelPluginDescriptor? descriptor)
     {
         descriptor = null;
+        if (!IsIdentifier(publisher))
+        {
+            return "The publisher must be one identifier.";
+        }
+
+        if (!IsIdentifier(name))
+        {
+            return "The name of the plugin must be one identifier.";
+        }
+
+        if (!IsVersion(version))
+        {
+            return "The version must be exact, of three to five numbers.";
+        }
+
         IReadOnlyDictionary<string, string> properties;
         try
         {
@@ -81,22 +104,11 @@ internal sealed partial record TazelPluginDescriptor(
         string? Text(string key) =>
             properties.TryGetValue(key, out string? value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null;
 
-        string? publisher = Text("publisher");
-        string? name = Text("name");
-        string? version = Text("version");
-        if (publisher is null || !IsIdentifier(publisher))
-        {
-            return "The descriptor needs a publisher that is one identifier.";
-        }
-
-        if (name is null || !IsIdentifier(name))
-        {
-            return "The descriptor needs a name that is one identifier.";
-        }
-
-        if (version is null || !IsVersion(version))
+        string[] identity = [.. IdentityKeys.Where(properties.ContainsKey)];
+        if (identity.Length > 0)
         {
-            return "The descriptor needs an exact version of three to five numbers.";
+            return $"The descriptor names {string.Join(", ", identity)}; " +
+                "ViGet takes who a plugin is from the command that publishes it, so its JAR names none.";
         }
 
         if (!int.TryParse(Text("apiVersion"), out int apiVersion) || apiVersion < 1)
diff --git a/apps/api/ViGet/TazelPluginPublishing.cs b/apps/api/ViGet/TazelPluginPublishing.cs
index 02c1d6b..1c5cdee 100644
--- a/apps/api/ViGet/TazelPluginPublishing.cs
+++ b/apps/api/ViGet/TazelPluginPublishing.cs
@@ -53,18 +53,23 @@ internal sealed class TazelPluginPublishing(
     public static string RelativePath(string publisher, string name, string version) =>
         Path.Combine("dslplugins", "tazel", publisher, name, version, $"{name}-{version}.jar");
 
-    /// <summary>Publishes the plugin in a JAR.</summary>
+    /// <summary>Publishes the plugin in a JAR as the version of a plugin of a publisher.</summary>
     /// <param name="jar">The bytes of the JAR.</param>
+    /// <param name="publisher">The Progmasoft Account it is published under.</param>
+    /// <param name="name">The name of the plugin.</param>
+    /// <param name="version">The version it is published as.</param>
     /// <param name="cancellationToken">Cancels the operation.</param>
     /// <returns>The published descriptor, or why the JAR was refused.</returns>
-    public async Task<PublishResult> PublishAsync(byte[] jar, CancellationToken cancellationToken)
+    public async Task<PublishResult> PublishAsync(
+        byte[] jar, string publisher, string name, string version, CancellationToken cancellationToken)
     {
         if (jar.LongLength > MaximumSize)
         {
             return Refused($"The JAR is larger than {MaximumSize / 1024 / 1024} MiB.");
         }
 
-        if (TazelPluginDescriptor.TryRead(jar, out TazelPluginDescriptor? descriptor) is { } invalid)
+        if (TazelPluginDescriptor.TryRead(jar, publisher, name, version, out TazelPluginDescriptor? descriptor) is
+            { } invalid)
         {
             return Refused(invalid);
         }
diff --git a/apps/api/ViGet/ViGetCommand.cs b/apps/api/ViGet/ViGetCommand.cs
index ad99aee..f91f46f 100644
--- a/apps/api/ViGet/ViGetCommand.cs
+++ b/apps/api/ViGet/ViGetCommand.cs
@@ -7,7 +7,8 @@ namespace Progmasoft.Progmaweb.Api.ViGet;
 
 /// <summary>
 /// The commands that manage the Tazel plugin catalog on the server, while publishing is not open through the API:
-/// <c>viget publish-tazel-plugin &lt;jar&gt;</c> and <c>viget yank-tazel-plugin &lt;Publisher&gt; &lt;Name&gt;
+/// <c>viget publish-tazel-plugin &lt;Publisher&gt; &lt;Name&gt; &lt;Version&gt; &lt;jar&gt;</c>, which names who the
+/// plugin is because its JAR does not, and <c>viget yank-tazel-plugin &lt;Publisher&gt; &lt;Name&gt;
 /// &lt;Version&gt; [--undo]</c>. They run with the configuration of the service, as its user.
 /// </summary>
 internal static class ViGetCommand
@@ -37,7 +38,7 @@ internal static class ViGetCommand
 
         switch (args)
         {
-            case ["publish-tazel-plugin", string path]:
+            case ["publish-tazel-plugin", string publisher, string name, string version, string path]:
             {
                 if (!File.Exists(path))
                 {
@@ -47,7 +48,8 @@ internal static class ViGetCommand
 
                 TazelPluginPublishing publishing = services.GetRequiredService<TazelPluginPublishing>();
                 PublishResult result = await publishing.PublishAsync(
-                    await File.ReadAllBytesAsync(path, cancellationToken), cancellationToken);
+                    await File.ReadAllBytesAsync(path, cancellationToken), publisher, name, version,
+                    cancellationToken);
                 if (!result.Published)
                 {
                     await errors.WriteLineAsync(result.Error);
@@ -77,7 +79,7 @@ internal static class ViGetCommand
             }
             default:
                 await errors.WriteLineAsync(
-                    "usage: viget publish-tazel-plugin <jar>\n" +
+                    "usage: viget publish-tazel-plugin <Publisher> <Name> <Version> <jar>\n" +
                     "       viget yank-tazel-plugin <Publisher> <Name> <Version> [--undo]");
                 return 2;
         }
diff --git a/ops/README.md b/ops/README.md
index 6b8ad50..f0c7654 100644
--- a/ops/README.md
+++ b/ops/README.md
@@ -202,12 +202,13 @@ systemd-run --quiet --wait --pipe --uid=progmaweb --gid=progmaweb \
   -p EnvironmentFile=/etc/progmaweb/account.env -p Environment=ASPNETCORE_ENVIRONMENT=Production \
   -p WorkingDirectory=/srv/progmaweb/current/api \
   /usr/bin/dotnet /srv/progmaweb/current/api/Progmaweb.Api.dll \
-  viget publish-tazel-plugin /srv/viget/incoming/CMake-0.1.0.jar
+  viget publish-tazel-plugin Progmasoft CMake 0.1.0 /srv/viget/incoming/CMake-0.1.0.jar
 rm /srv/viget/incoming/CMake-0.1.0.jar
 restorecon -R /srv/viget/files
 ```
 
-The publisher of the descriptor must be the name of an existing Progmasoft Account, spelled the same way.
+The command names who the plugin is: its publisher, which must be the name of an existing Progmasoft Account spelled
+the same way, its name and its version. The descriptor of the JAR names none of them, and ViGet refuses one that does.
 
 The command checks the descriptor of the JAR, the publisher, its account and the version, which is never replaced.
 `viget yank-tazel-plugin <Publisher> <Name> <Version>` yanks a version, and `--undo` takes the yank back.