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

Fix the Codacy findings of the message formats

- ScriptFriendlyWriter picks the writer of a kind from a table instead of
  one switch of fifteen cases.
- The JSON writer's list helper is WriteList(json, items), not a method
  and a parameter both named strings.
- docs/commands.md: the code spans of the script-friendly table no longer
  start with spaces; the text says those lines are indented.
Commit
e66dfbdb18b36c72b9e979cf6f128100382ae381
Author
Leitwolf11 ·
Parents
95dc005d39fd

Files · Download .tar.gz

Changes

 docs/commands.md                        |   9 +--
 src/Progmasoft.Tazel/Output/Messages.cs | 105 ++++++++++++++++++--------------
 2 files changed, 63 insertions(+), 51 deletions(-)

diff --git a/docs/commands.md b/docs/commands.md
index dd2badb..44ea283 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -62,16 +62,17 @@ such as the labels of `tazel query`, is an `output` message.
 
 - `rich-stdio`: colours, tables and pictures of problems, drawn with Spectre.Console for a terminal.
 - `script-friendly-stdio`: plain lines for scripts and logs. A message is one line that starts with a fixed word,
-  and the lines it carries (a compiler's output, a test log) follow it indented by two spaces. No colours, boxes or
+  and the lines it carries (a compiler's output, a test log, and the `command:`, `help:` and `here:` lines of the
+  table below) follow it indented by two spaces. No colours, boxes or
   emoji, and nothing is wrapped. An `output` message is printed as it is; a table is tab-separated, its header first.
 
   | Message | Line |
   | --- | --- |
   | `status` | `INFO <text>` |
   | `action` | `DONE <description>` |
-  | `action-failed` | `FAILED <description>`, then `  command: <command line>` |
-  | `error` | `error[<code>]: <message>`, then `  help: <help>` |
-  | `diagnostic` | `error[<code>]: <file>:<line>:<column>: <message>`, then `  here: <label>` and `  help: <help>` |
+  | `action-failed` | `FAILED <description>`, then `command: <command line>` |
+  | `error` | `error[<code>]: <message>`, then `help: <help>` |
+  | `diagnostic` | `error[<code>]: <file>:<line>:<column>: <message>`, then `here: <label>` and `help: <help>` |
   | `warning` | `warning: <message>` |
   | `success` | `SUCCESS <message>` |
   | `test` | `PASSED`, `CACHED`, `FAILED`, `TIMED-OUT`, `FLAKY` or `NOT-BUILT`, then `<label> <seconds> s` |
diff --git a/src/Progmasoft.Tazel/Output/Messages.cs b/src/Progmasoft.Tazel/Output/Messages.cs
index 7dff05e..9cf9a8c 100644
--- a/src/Progmasoft.Tazel/Output/Messages.cs
+++ b/src/Progmasoft.Tazel/Output/Messages.cs
@@ -72,69 +72,80 @@ internal abstract class MessageWriter(TextWriter output) : IDisposable
 /// a message indented by two spaces, no colours, boxes, emoji or light lines, never wrapped. The output of a command
 /// (<c>output</c>) is printed as it is.
 /// </summary>
-internal sealed class ScriptFriendlyWriter(TextWriter output) : MessageWriter(output)
+internal sealed class ScriptFriendlyWriter : MessageWriter
 {
-    public override void Write(Message message)
+    private readonly Dictionary<string, Action<Message>> _kinds;
+
+    public ScriptFriendlyWriter(TextWriter output)
+        : base(output)
     {
-        switch (message.Kind)
+        _kinds = new Dictionary<string, Action<Message>>(StringComparer.Ordinal)
         {
-            case "output":
-                Output.WriteLine(message.Text("text"));
-                break;
-            case "status":
-                Line("INFO", message.Text("text"));
-                break;
-            case "action":
+            ["output"] = message => Output.WriteLine(message.Text("text")),
+            ["status"] = message => Line("INFO", message.Text("text")),
+            ["action"] = message =>
+            {
                 Line("DONE", message.Text("description"));
                 Indented(message.Text("output"));
-                break;
-            case "action-failed":
+            },
+            ["action-failed"] = message =>
+            {
                 Line("FAILED", message.Text("description"));
                 Line("  command:", Reporter.Quote((IReadOnlyList<string>)message["command"]!));
                 Indented(message.Text("output"));
-                break;
-            case "error":
+            },
+            ["error"] = message =>
+            {
                 Line($"error[{message.Text("code")}]:", message.Text("message"));
                 Help(message);
-                break;
-            case "diagnostic":
+            },
+            ["diagnostic"] = message =>
+            {
                 Line($"error[{message.Text("code")}]:",
                     $"{message.Text("file")}:{message["line"]}:{message["column"]}: {message.Text("message")}");
                 Line("  here:", message.Text("label"));
                 Help(message);
-                break;
-            case "warning":
-                Line("warning:", message.Text("message"));
-                break;
-            case "success":
-                Line("SUCCESS", message.Text("message"));
-                break;
-            case "test":
-                Line(message.Text("status")!.ToUpperInvariant(), $"{message.Text("label")} {message["seconds"]:0.0} s");
-                break;
-            case "test-summary":
-                Line("TESTS", $"{message["passed"]} passed, {message["failed"]} failed, {message["total"]} in all");
-                break;
-            case "test-log":
+            },
+            ["warning"] = message => Line("warning:", message.Text("message")),
+            ["success"] = message => Line("SUCCESS", message.Text("message")),
+            ["test"] = message =>
+                Line(message.Text("status")!.ToUpperInvariant(), $"{message.Text("label")} {message["seconds"]:0.0} s"),
+            ["test-summary"] = message =>
+                Line("TESTS", $"{message["passed"]} passed, {message["failed"]} failed, {message["total"]} in all"),
+            ["test-log"] = message =>
+            {
                 Line("LOG", message.Text("label"));
                 Indented(message.Text("log"));
-                break;
-            case "table":
-                Output.WriteLine(string.Join('\t', (IReadOnlyList<string>)message["columns"]!));
-                foreach (var row in (IReadOnlyList<IReadOnlyList<string>>)message["rows"]!)
-                {
-                    Output.WriteLine(string.Join('\t', row));
-                }
-
-                break;
-            case "explanation":
+            },
+            ["table"] = Table,
+            ["explanation"] = message =>
+            {
                 Line(message.Text("code"), $"{message.Text("title")} ({message.Text("where")})");
                 Indented(message.Text("text"));
                 Indented(message.Text("wrong") is { } wrong ? "wrong:\n" + wrong : null);
                 Indented(message.Text("right") is { } right ? "right:\n" + right : null);
-                break;
-            default:
-                throw new ArgumentException($"no message kind {message.Kind}", nameof(message));
+            },
+        };
+    }
+
+    public override void Write(Message message)
+    {
+        ArgumentNullException.ThrowIfNull(message);
+        if (!_kinds.TryGetValue(message.Kind, out var write))
+        {
+            throw new ArgumentException($"no message kind {message.Kind}", nameof(message));
+        }
+
+        write(message);
+    }
+
+    // A table: its header and then its rows, tab-separated.
+    private void Table(Message message)
+    {
+        Output.WriteLine(string.Join('\t', (IReadOnlyList<string>)message["columns"]!));
+        foreach (var row in (IReadOnlyList<IReadOnlyList<string>>)message["rows"]!)
+        {
+            Output.WriteLine(string.Join('\t', row));
         }
     }
 
@@ -203,13 +214,13 @@ internal sealed class JsonlWriter(TextWriter output) : MessageWriter(output)
                         json.WriteStartArray();
                         foreach (var row in rows)
                         {
-                            Strings(json, row);
+                            WriteList(json, row);
                         }
 
                         json.WriteEndArray();
                         break;
                     case IReadOnlyList<string> strings:
-                        Strings(json, strings);
+                        WriteList(json, strings);
                         break;
                     default:
                         throw new ArgumentException($"a field of type {field.Value!.GetType().Name}", nameof(message));
@@ -223,10 +234,10 @@ internal sealed class JsonlWriter(TextWriter output) : MessageWriter(output)
         Output.Flush();
     }
 
-    private static void Strings(Utf8JsonWriter json, IEnumerable<string> strings)
+    private static void WriteList(Utf8JsonWriter json, IEnumerable<string> items)
     {
         json.WriteStartArray();
-        foreach (var text in strings)
+        foreach (var text in items)
         {
             json.WriteStringValue(text);
         }