Skip to content

Commit 421035a

Browse files
authored
Feature - Update to Strip Async suffix from Generated Commands (#79)
* Strip Async suffix from Generated Commands Breaking change * Update test
1 parent 46c504f commit 421035a

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ using ReactiveUI.SourceGenerators;
233233
public partial class MyReactiveClass
234234
{
235235
[ReactiveCommand]
236-
private async Task<string> Execute(string parameter) => await Task.FromResult(parameter);
236+
private async Task<string> ExecuteAsync(string parameter) => await Task.FromResult(parameter);
237+
238+
// Generates the following code ExecuteCommand, Note the Async suffix is removed
237239
}
238240
```
239241

src/ReactiveUI.SourceGenerators.Execute.Maui/TestViewModel.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ public TestViewModel()
3535
{
3636
Console.Out.WriteLine(Test1Command);
3737
Console.Out.WriteLine(Test2Command);
38-
Console.Out.WriteLine(Test3AsyncCommand);
39-
Console.Out.WriteLine(Test4AsyncCommand);
38+
Console.Out.WriteLine(Test3Command);
39+
Console.Out.WriteLine(Test4Command);
4040
Console.Out.WriteLine(Test5StringToIntCommand);
4141
Console.Out.WriteLine(Test6ArgOnlyCommand);
4242
Console.Out.WriteLine(Test7ObservableCommand);
4343
Console.Out.WriteLine(Test8ObservableCommand);
44-
Console.Out.WriteLine(Test9AsyncCommand);
45-
Console.Out.WriteLine(Test10AsyncCommand);
44+
Console.Out.WriteLine(Test9Command);
45+
Console.Out.WriteLine(Test10Command);
4646
Test1Command?.Execute().Subscribe();
4747
Test2Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
48-
Test3AsyncCommand?.Execute().Subscribe();
49-
Test4AsyncCommand?.Execute().Subscribe(r => Console.Out.WriteLine(r));
48+
Test3Command?.Execute().Subscribe();
49+
Test4Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
5050
Test5StringToIntCommand?.Execute("100").Subscribe(Console.Out.WriteLine);
5151
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
5252
Test7ObservableCommand?.Execute().Subscribe();
@@ -57,12 +57,12 @@ public TestViewModel()
5757
Console.Out.WriteLine($"Test2Property Value: {Test2}");
5858
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");
5959

60-
Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
61-
var cancel = Test9AsyncCommand?.Execute().Subscribe();
60+
Test9Command?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
61+
var cancel = Test9Command?.Execute().Subscribe();
6262
Task.Delay(1000).Wait();
6363
cancel?.Dispose();
6464

65-
Test10AsyncCommand?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
65+
Test10Command?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
6666
}
6767

6868
/// <summary>

src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ public TestViewModel()
6060

6161
Console.Out.WriteLine(Test1Command);
6262
Console.Out.WriteLine(Test2Command);
63-
Console.Out.WriteLine(Test3AsyncCommand);
64-
Console.Out.WriteLine(Test4AsyncCommand);
63+
Console.Out.WriteLine(Test3Command);
64+
Console.Out.WriteLine(Test4Command);
6565
Console.Out.WriteLine(Test5StringToIntCommand);
6666
Console.Out.WriteLine(Test6ArgOnlyCommand);
6767
Console.Out.WriteLine(Test7ObservableCommand);
6868
Console.Out.WriteLine(Test8ObservableCommand);
69-
Console.Out.WriteLine(Test9AsyncCommand);
70-
Console.Out.WriteLine(Test10AsyncCommand);
69+
Console.Out.WriteLine(Test9Command);
70+
Console.Out.WriteLine(Test10Command);
7171
Test1Command?.Execute().Subscribe();
7272
Test2Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
73-
Test3AsyncCommand?.Execute().Subscribe();
74-
Test4AsyncCommand?.Execute().Subscribe(r => Console.Out.WriteLine(r));
73+
Test3Command?.Execute().Subscribe();
74+
Test4Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
7575
Test5StringToIntCommand?.Execute("100").Subscribe(Console.Out.WriteLine);
7676
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
7777
Test7ObservableCommand?.Execute().Subscribe();
@@ -122,12 +122,12 @@ public TestViewModel()
122122
Console.Out.WriteLine(MyReadOnlyNonNullProperty);
123123
Console.Out.WriteLine(_myReadOnlyNonNullProperty);
124124

125-
Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
126-
var cancel = Test9AsyncCommand?.Execute().Subscribe();
125+
Test9Command?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
126+
var cancel = Test9Command?.Execute().Subscribe();
127127
Task.Delay(1000).Wait();
128128
cancel?.Dispose();
129129

130-
Test10AsyncCommand?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
130+
Test10Command?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
131131
TestPrivateCanExecuteCommand?.Execute().Subscribe();
132132

133133
Console.ReadLine();

src/ReactiveUI.SourceGenerators/ReactiveCommand/ReactiveCommandGenerator.Execute.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal static MemberDeclarationSyntax[] GetCommandProperty(CommandInfo command
5757
{
5858
var outputType = commandExtensionInfo.GetOutputTypeText();
5959
var inputType = commandExtensionInfo.GetInputTypeText();
60-
var commandName = GetGeneratedCommandName(commandExtensionInfo.MethodName);
60+
var commandName = GetGeneratedCommandName(commandExtensionInfo.MethodName, commandExtensionInfo.IsTask);
6161
var fieldName = GetGeneratedFieldName(commandName);
6262

6363
ExpressionSyntax initializer;
@@ -463,7 +463,7 @@ static void GatherForwardedAttributes(
463463
propertyAttributes = propertyAttributesInfo.ToImmutable();
464464
}
465465

466-
internal static string GetGeneratedCommandName(string methodName)
466+
internal static string GetGeneratedCommandName(string methodName, bool isAsync)
467467
{
468468
var commandName = methodName;
469469

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

479+
if (commandName.EndsWith("Async") && isAsync)
480+
{
481+
commandName = commandName.Substring(0, commandName.Length - "Async".Length);
482+
}
483+
479484
return $"{char.ToUpper(commandName[0], CultureInfo.InvariantCulture)}{commandName.Substring(1)}Command";
480485
}
481486

0 commit comments

Comments
 (0)