Skip to content

Commit b5e2f30

Browse files
committed
fix command alias not working(#14)
1 parent 178ede0 commit b5e2f30

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,43 +200,48 @@ static List<Type> GetBatchTypes()
200200

201201
static (Type, MethodInfo) GetTypeFromAssemblies(string arg0)
202202
{
203-
var split = arg0.Split('.');
204-
if (split.Length != 2)
205-
{
206-
return (null, null);
207-
}
208-
var typeName = split[0];
209-
var methodName = split[1];
210-
211203
var batchBaseTypes = GetBatchTypes();
212204
if (batchBaseTypes == null)
213205
{
214206
return (null, null);
215207
}
216208

209+
var split = arg0.Split('.');
217210
Type foundType = null;
218-
foreach (var item in batchBaseTypes)
211+
MethodInfo foundMethod = null;
212+
foreach (var baseType in batchBaseTypes)
219213
{
220-
if (item.Name == typeName)
214+
if (split.Length == 2)
221215
{
222-
if (foundType != null)
216+
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
223217
{
224-
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + item.FullName);
218+
if (foundType != null)
219+
{
220+
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName);
221+
}
222+
foundType = baseType;
223+
foundMethod = baseType.GetMethod(split[1]);
225224
}
226-
foundType = item;
227225
}
228-
}
229-
230-
if (foundType != null)
231-
{
232-
var method = foundType.GetMethod(methodName);
233-
if (method != null)
226+
else
234227
{
235-
return (foundType, method);
228+
foreach (var (method, cmdattr) in baseType.GetMethods().
229+
Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
230+
{
231+
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
232+
{
233+
foundType = baseType;
234+
foundMethod = method;
235+
}
236+
}
236237
}
237238
}
238-
239+
if(foundType != null && foundMethod != null)
240+
{
241+
return (foundType, foundMethod);
242+
}
239243
return (null, null);
244+
240245
}
241246
}
242247
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace MicroBatchFramework.Tests
11+
{
12+
public class CommandAttributeTest
13+
{
14+
class CommandAttributeTestCommand : BatchBase
15+
{
16+
ResultContainer _Result;
17+
public CommandAttributeTestCommand(ResultContainer r)
18+
{
19+
_Result = r;
20+
}
21+
[Command("test")]
22+
public void TestCommand(int value)
23+
{
24+
_Result.X = value;
25+
}
26+
}
27+
class ResultContainer
28+
{
29+
public int X;
30+
}
31+
[Fact]
32+
public async Task TestCommandName()
33+
{
34+
var hostBuilder = BatchHost.CreateDefaultBuilder()
35+
.ConfigureServices((c, services) =>
36+
{
37+
services.AddSingleton<ResultContainer>();
38+
});
39+
await hostBuilder.RunBatchEngineAsync(new string[]{ "test", "-value", "1" });
40+
}
41+
42+
}
43+
}

0 commit comments

Comments
 (0)