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

refactor(cli): split tazel trash parsing and rename Entry.Directory

Motivation: Codacy flagged CommandMakers.MakeTrash at a cyclomatic
complexity of 17 (limit 10) and Trash.Entry.Directory as shadowing a member
of the same name (S3218).

Changes:
- MakeTrash delegates to TrashAction (the word and its suggestion),
  TrashArity (how many words each action takes) and OlderThan (the days).
- Trash.Entry.Directory is now Trash.Entry.Location.
Behavior is unchanged.

Verification: Release build with warnings as errors (0 errors);
TrashTests 15/15.
Commit
6ea14f1ee68eae8191ac4cd58519f41aaa5fe392
Author
Leitwolf11 ·
Parents
f97f2014b518

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Cli/CommandMakers.cs | 52 ++++++++++++++++++++-----------
 src/Progmasoft.Tazel/Project/Trash.cs     | 12 +++----
 2 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/src/Progmasoft.Tazel/Cli/CommandMakers.cs b/src/Progmasoft.Tazel/Cli/CommandMakers.cs
index 227da41..240ac86 100644
--- a/src/Progmasoft.Tazel/Cli/CommandMakers.cs
+++ b/src/Progmasoft.Tazel/Cli/CommandMakers.cs
@@ -79,39 +79,55 @@ internal static class CommandMakers
         : throw new CommandLineException("tazel git takes add: tazel git add" +
             (scanned.Positional.Count > 0 && Suggestions.Nearest(scanned.Positional[0], ["add"]) is { } near ? $"; did you mean {near}?" : ""));
 
+    private static readonly string[] TrashActions = ["list", "size", "restore", "empty"];
+
     private static TrashCommand MakeTrash(ScannedCommand scanned, OutputOptions output)
     {
-        var actions = new[] { "list", "size", "restore", "empty" };
-        var action = scanned.Positional.Count > 0 ? scanned.Positional[0] : null;
-        if (action is null || !actions.Contains(action, StringComparer.Ordinal))
+        var action = TrashAction(scanned.Positional);
+        TrashArity(action, scanned.Positional.Count);
+        if (action != "empty" && (scanned.Has("older-than") || scanned.Has("dry-run")))
         {
-            var near = action is null ? null : Suggestions.Nearest(action, actions);
-            throw new CommandLineException(
-                $"tazel trash takes {string.Join(", ", actions)}{(near is null ? "" : $"; did you mean {near}?")}");
+            throw new CommandLineException("--older-than and --dry-run belong to tazel trash empty");
         }
 
+        var fingerprint = action == "restore" ? scanned.Positional[1] : null;
+        return new TrashCommand(output, action, fingerprint, OlderThan(scanned.Last("older-than")), scanned.Has("dry-run"));
+    }
+
+    private static string TrashAction(IReadOnlyList<string> positional)
+    {
+        var action = positional.Count > 0 ? positional[0] : null;
+        if (action is not null && TrashActions.Contains(action, StringComparer.Ordinal))
+        {
+            return action;
+        }
+
+        var near = action is null ? null : Suggestions.Nearest(action, TrashActions);
+        throw new CommandLineException(
+            $"tazel trash takes {string.Join(", ", TrashActions)}{(near is null ? "" : $"; did you mean {near}?")}");
+    }
+
+    private static void TrashArity(string action, int count)
+    {
         var expected = action == "restore" ? 2 : 1;
-        if (scanned.Positional.Count != expected)
+        if (count != expected)
         {
             throw new CommandLineException(action == "restore"
                 ? "tazel trash restore takes one fingerprint, as tazel trash list shows it"
                 : $"tazel trash {action} takes no other word");
         }
+    }
 
-        if (action != "empty" && (scanned.Has("older-than") || scanned.Has("dry-run")))
-        {
-            throw new CommandLineException("--older-than and --dry-run belong to tazel trash empty");
-        }
-
-        TimeSpan? olderThan = null;
-        if (scanned.Last("older-than") is { } days)
+    private static TimeSpan? OlderThan(string? days)
+    {
+        if (days is null)
         {
-            olderThan = int.TryParse(days, System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out var count) && count >= 0
-                ? TimeSpan.FromDays(count)
-                : throw new CommandLineException($"--older-than takes a whole number of days, not '{days}'");
+            return null;
         }
 
-        return new TrashCommand(output, action, expected == 2 ? scanned.Positional[1] : null, olderThan, scanned.Has("dry-run"));
+        return int.TryParse(days, System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out var count)
+            ? TimeSpan.FromDays(count)
+            : throw new CommandLineException($"--older-than takes a whole number of days, not '{days}'");
     }
 
     private static Command MakeLog(ScannedCommand scanned, OutputOptions output)
diff --git a/src/Progmasoft.Tazel/Project/Trash.cs b/src/Progmasoft.Tazel/Project/Trash.cs
index 6fa6d51..200933e 100644
--- a/src/Progmasoft.Tazel/Project/Trash.cs
+++ b/src/Progmasoft.Tazel/Project/Trash.cs
@@ -37,11 +37,11 @@ public static class Trash
 
     /// <summary>One entry of a Trash.</summary>
     /// <param name="Fingerprint">The 64 hexadecimal digits of the contents' SHA-256.</param>
-    /// <param name="Directory">The entry's directory.</param>
+    /// <param name="Location">The entry's directory.</param>
     /// <param name="ModuleRoot">The canonical root of the module it came from.</param>
     /// <param name="MovedAt">When it was moved in, in UTC.</param>
     /// <param name="Bytes">The total size of its files.</param>
-    public sealed record Entry(string Fingerprint, string Directory, string ModuleRoot, DateTimeOffset MovedAt, long Bytes);
+    public sealed record Entry(string Fingerprint, string Location, string ModuleRoot, DateTimeOffset MovedAt, long Bytes);
 
     /// <summary>
     /// Moves <paramref name="state"/>, the <c>.tazel/</c> of the module rooted at <paramref name="moduleRoot"/>, into the
@@ -142,8 +142,8 @@ public static class Trash
                 $"{state} exists; nothing was restored. Move it to Trash first with tazel clean --expunge");
         }
 
-        Move(entry.Directory, state, entry.Fingerprint);
-        File.Delete(InfoPath(entry.Directory));
+        Move(entry.Location, state, entry.Fingerprint);
+        File.Delete(InfoPath(entry.Location));
         return state;
     }
 
@@ -159,8 +159,8 @@ public static class Trash
         {
             foreach (var entry in chosen)
             {
-                Directory.Delete(entry.Directory, recursive: true);
-                File.Delete(InfoPath(entry.Directory));
+                Directory.Delete(entry.Location, recursive: true);
+                File.Delete(InfoPath(entry.Location));
             }
         }