Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

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

Make the public constants of Packaging properties and split two checks

Codacy on #33:

- Extension, MaxUnpackedBytes, MaxEntries, FileName, MaxDepth and
  MaxLicenses were public constants of a published library, which the
  compiler copies into every assembly that uses them; they are static
  read-only properties now, and the Tazel aliases follow.
- The parser reads a parenthesised group and the exception after WITH in
  methods of their own, and the manifest checks who the plugin is apart
  from what Tazel loads; both were over the complexity limit.
- The two switches over expressions say what their default is.

529 tests pass, 1 skipped.
Commit
77ba7316b28af3bece523cb2dd621cca53cc4876
Author
Leitwolf11 ·
Parents
7b0fcd12cad2

Files · Download .tar.gz

Changes

 .../Spdx/SpdxExpression.cs                         |  7 ++--
 .../Spdx/SpdxExpressionParser.cs                   | 39 ++++++++++++----------
 .../Spdx/SpdxLicenseList.cs                        |  3 ++
 .../TazelPackageArchive.cs                         |  6 ++--
 .../TazelPluginManifest.cs                         | 11 +++++-
 src/Progmasoft.Tazel/Archive/ModuleArchive.cs      |  2 +-
 src/Progmasoft.Tazel/Project/Plugins.cs            |  2 +-
 7 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpression.cs b/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpression.cs
index c8864e8..65300cf 100644
--- a/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpression.cs
+++ b/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpression.cs
@@ -24,7 +24,8 @@ public abstract record SpdxExpression
         pending.Push(this);
         while (pending.Count > 0)
         {
-            switch (pending.Pop())
+            var node = pending.Pop();
+            switch (node)
             {
                 case SpdxAnd and:
                     pending.Push(and.Right);
@@ -37,8 +38,8 @@ public abstract record SpdxExpression
                 case SpdxWith with:
                     yield return with.License;
                     break;
-                case var leaf:
-                    yield return leaf;
+                default:
+                    yield return node;
                     break;
             }
         }
diff --git a/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpressionParser.cs b/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpressionParser.cs
index adac96d..a947453 100644
--- a/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpressionParser.cs
+++ b/src/Progmasoft.Tazel.Packaging/Spdx/SpdxExpressionParser.cs
@@ -57,13 +57,13 @@ public sealed class SpdxExpressionException : FormatException
 public static class SpdxExpressionParser
 {
     /// <summary>The deepest nesting of parentheses it reads; deeper ones are refused, so no input can exhaust the stack.</summary>
-    public const int MaxDepth = 64;
+    public static int MaxDepth { get; } = 64;
 
     /// <summary>
     /// The most licenses one expression may name. The tree of a longer one would be deep enough that walking it, to write
     /// or compare it, could exhaust the stack.
     /// </summary>
-    public const int MaxLicenses = 1000;
+    public static int MaxLicenses { get; } = 1000;
 
     /// <summary>The expression <paramref name="text"/> writes.</summary>
     /// <exception cref="SpdxExpressionException">The text is not an SPDX license expression.</exception>
@@ -216,19 +216,7 @@ public static class SpdxExpressionParser
             var token = tokens[_at++];
             if (token.Kind == Kind.Open)
             {
-                if (depth + 1 > MaxDepth)
-                {
-                    throw new SpdxExpressionException($"parentheses nest deeper than {MaxDepth}", token.Position);
-                }
-
-                var inner = Or(depth + 1);
-                if (AtEnd || Current.Kind != Kind.Close)
-                {
-                    throw new SpdxExpressionException("a '(' is not closed", token.Position);
-                }
-
-                _at++;
-                return inner;
+                return Group(token, depth);
             }
 
             if (token.Kind == Kind.Close || IsOperator(token, "AND") || IsOperator(token, "OR") || IsOperator(token, "WITH"))
@@ -242,11 +230,28 @@ public static class SpdxExpressionParser
             }
 
             var simple = Simple(token);
-            if (AtEnd || !IsOperator(Current, "WITH"))
+            return AtEnd || !IsOperator(Current, "WITH") ? simple : Exception(simple);
+        }
+
+        private SpdxExpression Group(Token open, int depth)
+        {
+            if (depth + 1 > MaxDepth)
+            {
+                throw new SpdxExpressionException($"parentheses nest deeper than {MaxDepth}", open.Position);
+            }
+
+            var inner = Or(depth + 1);
+            if (AtEnd || Current.Kind != Kind.Close)
             {
-                return simple;
+                throw new SpdxExpressionException("a '(' is not closed", open.Position);
             }
 
+            _at++;
+            return inner;
+        }
+
+        private SpdxWith Exception(SpdxExpression simple)
+        {
             var with = Current.Position;
             _at++;
             if (AtEnd || Current.Kind != Kind.Word || IsOperator(Current, "AND") || IsOperator(Current, "OR") ||
diff --git a/src/Progmasoft.Tazel.Packaging/Spdx/SpdxLicenseList.cs b/src/Progmasoft.Tazel.Packaging/Spdx/SpdxLicenseList.cs
index eb1dec9..1da0772 100644
--- a/src/Progmasoft.Tazel.Packaging/Spdx/SpdxLicenseList.cs
+++ b/src/Progmasoft.Tazel.Packaging/Spdx/SpdxLicenseList.cs
@@ -109,6 +109,9 @@ public sealed class SpdxLicenseList
                 case ExceptionMark mark:
                     CheckException(mark.Id, problems);
                     break;
+                default:
+                    // A LicenseRef- names a license of its author's own, which no list holds.
+                    break;
             }
         }
 
diff --git a/src/Progmasoft.Tazel.Packaging/TazelPackageArchive.cs b/src/Progmasoft.Tazel.Packaging/TazelPackageArchive.cs
index d16e1ff..a8a13df 100644
--- a/src/Progmasoft.Tazel.Packaging/TazelPackageArchive.cs
+++ b/src/Progmasoft.Tazel.Packaging/TazelPackageArchive.cs
@@ -24,13 +24,13 @@ namespace Progmasoft.Tazel.Packaging;
 public static class TazelPackageArchive
 {
     /// <summary>The extension of an archive.</summary>
-    public const string Extension = ".tazel.tar.xz";
+    public static string Extension { get; } = ".tazel.tar.xz";
 
     /// <summary>The most an archive may unpack to; a larger one is refused before it fills the disk.</summary>
-    public const long MaxUnpackedBytes = 1L << 30;
+    public static long MaxUnpackedBytes { get; } = 1L << 30;
 
     /// <summary>The most entries an archive may hold.</summary>
-    public const int MaxEntries = 100_000;
+    public static int MaxEntries { get; } = 100_000;
 
     private const UnixFileMode FileMode =
         UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.GroupRead | UnixFileMode.OtherRead;
diff --git a/src/Progmasoft.Tazel.Packaging/TazelPluginManifest.cs b/src/Progmasoft.Tazel.Packaging/TazelPluginManifest.cs
index 799a20e..7f719db 100644
--- a/src/Progmasoft.Tazel.Packaging/TazelPluginManifest.cs
+++ b/src/Progmasoft.Tazel.Packaging/TazelPluginManifest.cs
@@ -19,7 +19,7 @@ namespace Progmasoft.Tazel.Packaging;
 public sealed record TazelPluginManifest(string Group, string Name, string Version, string PluginApi, string EntryAssembly)
 {
     /// <summary>The name of the manifest file.</summary>
-    public const string FileName = "tazel-plugin.json";
+    public static string FileName { get; } = "tazel-plugin.json";
 
     private static readonly JsonSerializerOptions ReadOptions =
         new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, AllowTrailingCommas = false };
@@ -62,6 +62,12 @@ public sealed record TazelPluginManifest(string Group, string Name, string Versi
     /// </summary>
     /// <exception cref="TazelPackageException">A field is not what it must be; the message says which.</exception>
     public void Check(string shown)
+    {
+        CheckIdentity(shown);
+        CheckLoading(shown);
+    }
+
+    private void CheckIdentity(string shown)
     {
         if (string.IsNullOrEmpty(Group) || !TazelPackageIdentity.IsGroup(Group))
         {
@@ -77,7 +83,10 @@ public sealed record TazelPluginManifest(string Group, string Name, string Versi
         {
             throw new TazelPackageException($"{shown}: '{Version}' is not a SemVer version");
         }
+    }
 
+    private void CheckLoading(string shown)
+    {
         if (!System.Version.TryParse(PluginApi ?? "", out _))
         {
             throw new TazelPackageException($"{shown}: '{PluginApi}' is not the version of a plugin API");
diff --git a/src/Progmasoft.Tazel/Archive/ModuleArchive.cs b/src/Progmasoft.Tazel/Archive/ModuleArchive.cs
index 6f69237..2ce141c 100644
--- a/src/Progmasoft.Tazel/Archive/ModuleArchive.cs
+++ b/src/Progmasoft.Tazel/Archive/ModuleArchive.cs
@@ -18,7 +18,7 @@ namespace Progmasoft.Tazel.Archive;
 internal static class ModuleArchive
 {
     /// <summary>The extension of an archive.</summary>
-    public const string Extension = TazelPackageArchive.Extension;
+    public static string Extension => TazelPackageArchive.Extension;
 
     /// <summary><c>$group.$module-$version</c>, the name of an archive without its extension.</summary>
     public static string BaseName(string group, string module, string version) => $"{group}.{module}-{version}";
diff --git a/src/Progmasoft.Tazel/Project/Plugins.cs b/src/Progmasoft.Tazel/Project/Plugins.cs
index 52e4d2c..cafa7e8 100644
--- a/src/Progmasoft.Tazel/Project/Plugins.cs
+++ b/src/Progmasoft.Tazel/Project/Plugins.cs
@@ -43,7 +43,7 @@ internal sealed class LoadedPlugin(TazelPluginManifest manifest, string director
 /// </summary>
 internal static class Plugins
 {
-    public const string ManifestFile = TazelPluginManifest.FileName;
+    public static string ManifestFile => TazelPluginManifest.FileName;
 
     private static readonly string[] Shared = ["Progmasoft.Tazel.PluginApi", "Progmasoft.Tazel.Dsl"];