Skip to content

Commit

Permalink
Feature - Update to Strip Async suffix from Generated Commands (#79)
Browse files Browse the repository at this point in the history
* Strip Async suffix from Generated Commands

Breaking change

* Update test
  • Loading branch information
ChrisPulman authored Oct 17, 2024
1 parent 46c504f commit 421035a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ using ReactiveUI.SourceGenerators;
public partial class MyReactiveClass
{
[ReactiveCommand]
private async Task<string> Execute(string parameter) => await Task.FromResult(parameter);
private async Task<string> ExecuteAsync(string parameter) => await Task.FromResult(parameter);

// Generates the following code ExecuteCommand, Note the Async suffix is removed
}
```

Expand Down
18 changes: 9 additions & 9 deletions src/ReactiveUI.SourceGenerators.Execute.Maui/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public TestViewModel()
{
Console.Out.WriteLine(Test1Command);
Console.Out.WriteLine(Test2Command);
Console.Out.WriteLine(Test3AsyncCommand);
Console.Out.WriteLine(Test4AsyncCommand);
Console.Out.WriteLine(Test3Command);
Console.Out.WriteLine(Test4Command);
Console.Out.WriteLine(Test5StringToIntCommand);
Console.Out.WriteLine(Test6ArgOnlyCommand);
Console.Out.WriteLine(Test7ObservableCommand);
Console.Out.WriteLine(Test8ObservableCommand);
Console.Out.WriteLine(Test9AsyncCommand);
Console.Out.WriteLine(Test10AsyncCommand);
Console.Out.WriteLine(Test9Command);
Console.Out.WriteLine(Test10Command);
Test1Command?.Execute().Subscribe();
Test2Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test3AsyncCommand?.Execute().Subscribe();
Test4AsyncCommand?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test3Command?.Execute().Subscribe();
Test4Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test5StringToIntCommand?.Execute("100").Subscribe(Console.Out.WriteLine);
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
Test7ObservableCommand?.Execute().Subscribe();
Expand All @@ -57,12 +57,12 @@ public TestViewModel()
Console.Out.WriteLine($"Test2Property Value: {Test2}");
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");

Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
var cancel = Test9AsyncCommand?.Execute().Subscribe();
Test9Command?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
var cancel = Test9Command?.Execute().Subscribe();
Task.Delay(1000).Wait();
cancel?.Dispose();

Test10AsyncCommand?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
Test10Command?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
}

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ public TestViewModel()

Console.Out.WriteLine(Test1Command);
Console.Out.WriteLine(Test2Command);
Console.Out.WriteLine(Test3AsyncCommand);
Console.Out.WriteLine(Test4AsyncCommand);
Console.Out.WriteLine(Test3Command);
Console.Out.WriteLine(Test4Command);
Console.Out.WriteLine(Test5StringToIntCommand);
Console.Out.WriteLine(Test6ArgOnlyCommand);
Console.Out.WriteLine(Test7ObservableCommand);
Console.Out.WriteLine(Test8ObservableCommand);
Console.Out.WriteLine(Test9AsyncCommand);
Console.Out.WriteLine(Test10AsyncCommand);
Console.Out.WriteLine(Test9Command);
Console.Out.WriteLine(Test10Command);
Test1Command?.Execute().Subscribe();
Test2Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test3AsyncCommand?.Execute().Subscribe();
Test4AsyncCommand?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test3Command?.Execute().Subscribe();
Test4Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test5StringToIntCommand?.Execute("100").Subscribe(Console.Out.WriteLine);
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
Test7ObservableCommand?.Execute().Subscribe();
Expand Down Expand Up @@ -122,12 +122,12 @@ public TestViewModel()
Console.Out.WriteLine(MyReadOnlyNonNullProperty);
Console.Out.WriteLine(_myReadOnlyNonNullProperty);

Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
var cancel = Test9AsyncCommand?.Execute().Subscribe();
Test9Command?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
var cancel = Test9Command?.Execute().Subscribe();
Task.Delay(1000).Wait();
cancel?.Dispose();

Test10AsyncCommand?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
Test10Command?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
TestPrivateCanExecuteCommand?.Execute().Subscribe();

Console.ReadLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal static MemberDeclarationSyntax[] GetCommandProperty(CommandInfo command
{
var outputType = commandExtensionInfo.GetOutputTypeText();
var inputType = commandExtensionInfo.GetInputTypeText();
var commandName = GetGeneratedCommandName(commandExtensionInfo.MethodName);
var commandName = GetGeneratedCommandName(commandExtensionInfo.MethodName, commandExtensionInfo.IsTask);
var fieldName = GetGeneratedFieldName(commandName);

ExpressionSyntax initializer;
Expand Down Expand Up @@ -463,7 +463,7 @@ static void GatherForwardedAttributes(
propertyAttributes = propertyAttributesInfo.ToImmutable();
}

internal static string GetGeneratedCommandName(string methodName)
internal static string GetGeneratedCommandName(string methodName, bool isAsync)
{
var commandName = methodName;

Expand All @@ -476,6 +476,11 @@ internal static string GetGeneratedCommandName(string methodName)
commandName = commandName.TrimStart('_');
}

if (commandName.EndsWith("Async") && isAsync)
{
commandName = commandName.Substring(0, commandName.Length - "Async".Length);
}

return $"{char.ToUpper(commandName[0], CultureInfo.InvariantCulture)}{commandName.Substring(1)}Command";
}

Expand Down

0 comments on commit 421035a

Please sign in to comment.