Skip to content

Commit

Permalink
extra impl is done
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jun 1, 2024
1 parent 2699a15 commit 9796074
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
7 changes: 2 additions & 5 deletions sandbox/GeneratorSandbox/Filters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ public override async Task InvokeAsync(ConsoleAppContext context, CancellationTo
}
}

internal class PreventMultipleInstanceFilter(ConsoleAppFilter next) : ConsoleAppFilter(next)
internal class PreventMultipleSameCommandInvokeFilter(ConsoleAppFilter next) : ConsoleAppFilter(next)
{
public override async Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
{
// allow another command
// prevent: location + command
var basePath = Assembly.GetEntryAssembly()?.Location.Replace(Path.DirectorySeparatorChar, '_');

var mutexKey = $"{basePath}$$${context.CommandName}";
var mutexKey = $"{basePath}$$${context.CommandName}"; // lock per command-name

using var mutex = new Mutex(true, mutexKey, out var createdNew);
if (!createdNew)
Expand Down
50 changes: 50 additions & 0 deletions tests/ConsoleAppFramework.GeneratorTests/ConsoleAppContextTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace ConsoleAppFramework.GeneratorTests;

public class ConsoleAppContextTest(ITestOutputHelper output)
{
VerifyHelper verifier = new VerifyHelper(output, "CAF");

[Fact]
public void ForLambda()
{
verifier.Execute("""
ConsoleApp.Run(args, (ConsoleAppContext ctx) => { Console.Write(ctx.Arguments.Length); });
""", args: "", expected: "0");
}

[Fact]
public void ForMethod()
{
verifier.Execute("""
var builder = ConsoleApp.Create();

builder.UseFilter<StateFilter>();

builder.Add("", Hello);

builder.Run(args);

void Hello(ConsoleAppContext ctx)
{
Console.Write(ctx.State);
}

internal class StateFilter(ConsoleAppFilter next)
: ConsoleAppFilter(next)
{
public override Task InvokeAsync(ConsoleAppContext context,CancellationToken cancellationToken)
{
Console.Write(1);
return Next.InvokeAsync(context with { State = 2 }, cancellationToken);
}
}
""", args: "", expected: "12");
}
}

0 comments on commit 9796074

Please sign in to comment.