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 Codacy findings in GraphEngine

Replace optional parameters on public Cedre Celeste methods with explicit
overloads, remove unused parameter from VerifyInputAsync, and simplify
RunEvaluatorAsync exception filtering to reduce cyclomatic complexity
below 10.
Commit
e4ceaf0339b8643d922a247c299d97776b9550c6
Author
Leitwolf11 ·
Parents
ad41e3e2e36d

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel.CedreCeleste/GraphEngine.cs | 56 ++++++++++++++++--------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/src/Progmasoft.Tazel.CedreCeleste/GraphEngine.cs b/src/Progmasoft.Tazel.CedreCeleste/GraphEngine.cs
index dbd1e1d..1168e8c 100644
--- a/src/Progmasoft.Tazel.CedreCeleste/GraphEngine.cs
+++ b/src/Progmasoft.Tazel.CedreCeleste/GraphEngine.cs
@@ -15,10 +15,16 @@ namespace Progmasoft.Tazel.CedreCeleste;
 public interface IEvaluationContext
 {
     /// <summary>Evaluates and records <paramref name="key"/> as an ordered dependency of the current node.</summary>
-    ValueTask<TValue> GetAsync<TValue>(NodeKey key, CancellationToken cancel = default);
+    ValueTask<TValue> GetAsync<TValue>(NodeKey key);
+
+    /// <summary>Evaluates and records <paramref name="key"/> as an ordered dependency of the current node.</summary>
+    ValueTask<TValue> GetAsync<TValue>(NodeKey key, CancellationToken cancel);
 
     /// <summary>Evaluates and records <paramref name="key"/> as an ordered dependency, returning the untyped value.</summary>
-    ValueTask<object?> GetAsync(NodeKey key, CancellationToken cancel = default);
+    ValueTask<object?> GetAsync(NodeKey key);
+
+    /// <summary>Evaluates and records <paramref name="key"/> as an ordered dependency, returning the untyped value.</summary>
+    ValueTask<object?> GetAsync(NodeKey key, CancellationToken cancel);
 }
 
 /// <summary>
@@ -71,10 +77,14 @@ public sealed class GraphEngine
     /// Sets an input node's value. Advances the global revision only if the content hash changed,
     /// so saving an unchanged file invalidates nothing.
     /// </summary>
-    public bool SetInput(NodeKey key, object? value, ContentHash? hash = null)
+    public bool SetInput(NodeKey key, object? value) => SetInput(key, value, ContentHash.FromValue(value));
+
+    /// <summary>
+    /// Sets an input node's value with an explicit content hash. Advances the global revision only if the hash changed.
+    /// </summary>
+    public bool SetInput(NodeKey key, object? value, ContentHash contentHash)
     {
         var id = _interner.Intern(key);
-        var contentHash = hash ?? ContentHash.FromValue(value);
         lock (_stateGate)
         {
             if (_nodes.TryGetValue(id, out var existing) && existing.IsInput && !existing.Unknown && existing.Hash == contentHash)
@@ -158,14 +168,20 @@ public sealed class GraphEngine
     }
 
     /// <summary>Evaluates <paramref name="key"/> at the current revision.</summary>
-    public async ValueTask<TValue> GetAsync<TValue>(NodeKey key, CancellationToken cancel = default)
+    public ValueTask<TValue> GetAsync<TValue>(NodeKey key) => GetAsync<TValue>(key, CancellationToken.None);
+
+    /// <summary>Evaluates <paramref name="key"/> at the current revision.</summary>
+    public async ValueTask<TValue> GetAsync<TValue>(NodeKey key, CancellationToken cancel)
     {
         var value = await GetAsync(key, cancel).ConfigureAwait(false);
         return value is null ? default! : (TValue)value;
     }
 
     /// <summary>Evaluates <paramref name="key"/> at the current revision, returning the untyped value.</summary>
-    public async ValueTask<object?> GetAsync(NodeKey key, CancellationToken cancel = default)
+    public ValueTask<object?> GetAsync(NodeKey key) => GetAsync(key, CancellationToken.None);
+
+    /// <summary>Evaluates <paramref name="key"/> at the current revision, returning the untyped value.</summary>
+    public async ValueTask<object?> GetAsync(NodeKey key, CancellationToken cancel)
     {
         var record = await EvaluateInternalAsync(key, Revision, ImmutableList<int>.Empty, cancel).ConfigureAwait(false);
         if (record.Error is not null)
@@ -263,7 +279,7 @@ public sealed class GraphEngine
 
         if (previous is { IsInput: true })
         {
-            return await VerifyInputAsync(id, key, previous, targetRevision, cancel).ConfigureAwait(false);
+            return await VerifyInputAsync(key, previous, targetRevision, cancel).ConfigureAwait(false);
         }
 
         if (TrackIncrementalState && previous is not null && (previous.ValuePresent || previous.Error is not null))
@@ -284,7 +300,7 @@ public sealed class GraphEngine
     }
 
     private async Task<NodeRecord> VerifyInputAsync(
-        int id, NodeKey key, NodeRecord input, long targetRevision, CancellationToken cancel)
+        NodeKey key, NodeRecord input, long targetRevision, CancellationToken cancel)
     {
         if (input.Unknown)
         {
@@ -354,20 +370,18 @@ public sealed class GraphEngine
             value = await evaluator(key, context, cancel).ConfigureAwait(false);
             hash = ContentHash.FromValue(value);
         }
-        catch (CycleException)
-        {
-            throw;
-        }
-        catch (OperationCanceledException)
-        {
-            throw;
-        }
-        catch (Exception ex)
+        catch (Exception ex) when (ex is not CycleException and not OperationCanceledException)
         {
             error = ex;
             hash = ContentHash.FromString($"ERROR:{ex.GetType().FullName}:{ex.Message}");
         }
 
+        return StoreEvaluatedRecord(id, previous, targetRevision, value, error, hash, recordedDeps);
+    }
+
+    private NodeRecord StoreEvaluatedRecord(
+        int id, NodeRecord? previous, long targetRevision, object? value, Exception? error, ContentHash hash, List<int> recordedDeps)
+    {
         lock (_stateGate)
         {
             var pruned = previous is not null && previous.Hash == hash && (previous.Error is null) == (error is null);
@@ -397,13 +411,17 @@ public sealed class GraphEngine
     private sealed class RecordingContext(
         GraphEngine engine, long targetRevision, ImmutableList<int> stack, List<int> recordedDeps) : IEvaluationContext
     {
-        public async ValueTask<TValue> GetAsync<TValue>(NodeKey key, CancellationToken cancel = default)
+        public ValueTask<TValue> GetAsync<TValue>(NodeKey key) => GetAsync<TValue>(key, CancellationToken.None);
+
+        public async ValueTask<TValue> GetAsync<TValue>(NodeKey key, CancellationToken cancel)
         {
             var value = await GetAsync(key, cancel).ConfigureAwait(false);
             return value is null ? default! : (TValue)value;
         }
 
-        public async ValueTask<object?> GetAsync(NodeKey key, CancellationToken cancel = default)
+        public ValueTask<object?> GetAsync(NodeKey key) => GetAsync(key, CancellationToken.None);
+
+        public async ValueTask<object?> GetAsync(NodeKey key, CancellationToken cancel)
         {
             var record = await engine.EvaluateInternalAsync(key, targetRevision, stack, cancel).ConfigureAwait(false);
             recordedDeps.Add(record.Id);