-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (90 loc) · 2.85 KB
/
Program.cs
File metadata and controls
96 lines (90 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.CommandLine.Builder;
using System.CommandLine;
using System.CommandLine.Hosting;
using System.CommandLine.Parsing;
using System.Reflection;
using MediatorCommandLineStuff.Extensions;
using MediatorCommandLineStuff.Models.Requests;
using MediatR;
return
await
BuildCommandLine()
.UseHost(_ => CreateHostBuilder(args))
.UseDefaults()
.Build()
.InvokeAsync(args);
static CommandLineBuilder BuildCommandLine()
{
var root = new RootCommand
{
new Command("demo")
{
new Command("since")
{
new Argument<TimeSpan>("since")
{
Arity = ArgumentArity.ExactlyOne
}
}
.Publish<TimeSpanNotification>(),
new Command("guid")
{
new Option<Guid>("--userguid")
{
IsRequired = true
}
}
.Send<GuidRequest>(),
new Command("names")
{
new Argument<IEnumerable<string>>("names")
{
Arity = ArgumentArity.OneOrMore
}
}
.Publish<StringCollectionNotification>(),
new Command("ids")
{
new Argument<IEnumerable<int>>("ids")
{
Arity = ArgumentArity.OneOrMore
}
}
.Publish<IntegerCollectionNotification>(),
}
};
root.AddGlobalOption(new Option<bool>("--force", "Force the job to run"));
root.AddGlobalOption(new Option<bool>("--dry-run", "Dry run"));
return new CommandLineBuilder(root);
}
static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureAppConfiguration
(
(_, config) =>
{
config
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddCommandLine(args);
}
)
.ConfigureServices
(
(_, collection) =>
{
var assemblies =
(
from file in Directory.GetFiles(Directory.GetCurrentDirectory())
let f = new FileInfo(file)
where f.Name.Contains(typeof(Program).Assembly.GetName().Name!)
where f.Extension == ".dll"
select Assembly.Load(Path.GetFileNameWithoutExtension(f.Name))
)
.ToList();
collection
.AddMediatR(assemblies, _ => { });
}
);