Skip to content

Commit ca728b9

Browse files
authored
#150 - отказ от System.CommandLine.Hosting (#156)
* #150 - local nuget config * #150 - update nuget (rm cli-hosting, stj) * migrate to new console version * migrate to new cli version * remove host code * fix exit codes * fix Program.cs * #150 - remove host from integration tests
1 parent f42874a commit ca728b9

19 files changed

+153
-144
lines changed

Directory.Packages.props

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
66
</PackageVersion>
77
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
8-
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
9-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
10-
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.2" />
8+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
9+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
10+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.3" />
11+
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.3" />
1112
<PackageVersion Include="Scrutor" Version="6.0.1" />
12-
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
13-
<PackageVersion Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
14-
<PackageVersion Include="System.IO.Abstractions" Version="22.0.11" />
15-
<PackageVersion Include="System.Text.Json" Version="9.0.2" />
13+
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.24126.1" />
14+
<PackageVersion Include="System.IO.Abstractions" Version="22.0.12" />
1615
<PackageVersion Include="Visitor.NET" Version="4.1.2" />
1716
<PackageVersion Include="Visitor.NET.AutoVisitableGen" Version="1.3.0" />
1817
</ItemGroup>

ExtendedJavaScriptSubset.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
1616
GitVersion.yml = GitVersion.yml
1717
Directory.Build.props = Directory.Build.props
1818
Directory.Packages.props = Directory.Packages.props
19+
NuGet.config = NuGet.config
1920
EndProjectSection
2021
EndProject
2122
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GithubFolder", "GithubFolder", "{54CBE5A7-3C3E-44ED-B877-7B08A818083B}"

NuGet.config

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<configuration>
4+
<packageSources>
5+
<clear />
6+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
7+
<!-- https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet-libraries/NuGet/System.CommandLine/versions -->
8+
<add key="dotnet-libraries" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-libraries/nuget/v3/index.json" />
9+
</packageSources>
10+
<packageSourceMapping>
11+
<packageSource key="nuget.org">
12+
<package pattern="*" />
13+
</packageSource>
14+
<packageSource key="dotnet-libraries">
15+
<package pattern="System.CommandLine" />
16+
</packageSource>
17+
</packageSourceMapping>
18+
</configuration>

src/HydraScript/ExecuteCommand.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
namespace HydraScript;
44

5-
internal class ExecuteCommand : RootCommand
5+
internal class ExecuteCommand : CliRootCommand
66
{
77
internal ExecuteCommand() : base("HydraScript interpreter")
88
{
9-
PathArgument = new Argument<FileInfo>(
10-
name: "path",
11-
description: "Path to input file");
12-
AddArgument(PathArgument);
9+
PathArgument = new CliArgument<FileInfo>(name: "path")
10+
{
11+
Description = "Path to input file"
12+
};
13+
Add(PathArgument);
1314

14-
DumpOption = new Option<bool>(
15-
["-d", "--dump"],
16-
getDefaultValue: () => false,
17-
description: "Show dump data of interpreter");
18-
AddOption(DumpOption);
15+
DumpOption = new CliOption<bool>(name: "--dump", aliases: ["-d", "/d"])
16+
{
17+
Description = "Show dump data of interpreter",
18+
DefaultValueFactory = _ => false
19+
};
20+
Add(DumpOption);
1921
}
2022

21-
internal Argument<FileInfo> PathArgument { get; }
22-
internal Option<bool> DumpOption { get; }
23+
internal CliArgument<FileInfo> PathArgument { get; }
24+
internal CliOption<bool> DumpOption { get; }
2325
}

src/HydraScript/ExitCodes.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/HydraScript/HydraScript.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
1213
<PackageReference Include="System.CommandLine" />
13-
<PackageReference Include="System.CommandLine.Hosting" />
14-
<PackageReference Include="System.Text.Json" />
1514
</ItemGroup>
1615

1716
<ItemGroup>

src/HydraScript/Program.cs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
using System.CommandLine.Builder;
2-
using System.CommandLine.Hosting;
3-
using System.CommandLine.Parsing;
1+
using System.CommandLine.Parsing;
42
using System.Diagnostics.CodeAnalysis;
53
using HydraScript;
64
using HydraScript.Infrastructure;
75
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.Extensions.Hosting;
96
using Microsoft.Extensions.Logging;
107
using Microsoft.Extensions.Logging.Console;
118

12-
return GetRunner(ConfigureHost).Invoke(args);
9+
return CliParser.Parse(GetCommand(), args).Invoke();
1310

1411
[ExcludeFromCodeCoverage]
1512
internal static partial class Program
1613
{
17-
internal static readonly ExecuteCommand Command = new();
18-
19-
internal static Parser GetRunner(Action<IHostBuilder> configureHost, bool useDefault = true)
14+
private static ExecuteCommand GetCommand()
2015
{
21-
var builder = new CommandLineBuilder(Command)
22-
.UseHost(Host.CreateDefaultBuilder, configureHost);
23-
if (useDefault)
24-
builder = builder.UseDefaults();
25-
return builder.Build();
16+
ExecuteCommand command = new();
17+
command.SetAction(parseResult =>
18+
{
19+
var fileInfo = parseResult.GetValue(command.PathArgument)!;
20+
var dump = parseResult.GetValue(command.DumpOption);
21+
var serviceProvider = GetServiceProvider(fileInfo, dump);
22+
var executor = serviceProvider.GetRequiredService<Executor>();
23+
return executor.Invoke();
24+
});
25+
return command;
2626
}
2727

28-
private static void ConfigureHost(IHostBuilder builder) => builder
29-
.ConfigureServices((context, services) =>
30-
{
31-
services.AddLogging(c => c.ClearProviders()
32-
.AddConsole(options => options.FormatterName = nameof(SimplestConsoleFormatter))
33-
.AddConsoleFormatter<SimplestConsoleFormatter, ConsoleFormatterOptions>());
34-
services.Configure<InvocationLifetimeOptions>(options => options.SuppressStatusMessages = true);
35-
var parseResult = context.GetInvocationContext().ParseResult;
36-
var fileInfo = parseResult.GetValueForArgument(Command.PathArgument);
37-
var dump = parseResult.GetValueForOption(Command.DumpOption);
38-
services
39-
.AddDomain()
40-
.AddApplication()
41-
.AddInfrastructure(dump, fileInfo);
42-
})
43-
.UseDefaultServiceProvider((_, options) => options.ValidateScopes = true)
44-
.UseCommandHandler<ExecuteCommand, ExecuteCommandHandler>();
28+
internal static IServiceProvider GetServiceProvider(
29+
FileInfo fileInfo,
30+
bool dump,
31+
Action<IServiceCollection>? configureServices = null)
32+
{
33+
var services = new ServiceCollection();
34+
services.AddLogging(c => c.ClearProviders()
35+
.AddConsole(options => options.FormatterName = nameof(SimplestConsoleFormatter))
36+
.AddConsoleFormatter<SimplestConsoleFormatter, ConsoleFormatterOptions>());
37+
services
38+
.AddDomain()
39+
.AddApplication()
40+
.AddInfrastructure(dump, fileInfo);
41+
configureServices?.Invoke(services);
42+
return services.BuildServiceProvider();
43+
}
4544
}

src/HydraScript/SimplestConsoleFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal class SimplestConsoleFormatter() : ConsoleFormatter(nameof(SimplestCons
1010
{
1111
public override void Write<TState>(
1212
in LogEntry<TState> logEntry,
13-
IExternalScopeProvider scopeProvider,
13+
IExternalScopeProvider? scopeProvider,
1414
TextWriter textWriter)
1515
{
1616
if (logEntry.LogLevel is LogLevel.Error)

src/HydraScript/ExecuteCommandHandler.cs renamed to src/Infrastructure/HydraScript.Infrastructure/Executor.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
using System.CommandLine.Invocation;
21
using HydraScript.Application.CodeGeneration;
32
using HydraScript.Application.StaticAnalysis.Exceptions;
43
using HydraScript.Domain.BackEnd;
54
using HydraScript.Domain.FrontEnd.Lexer;
65
using HydraScript.Domain.FrontEnd.Parser;
7-
using HydraScript.Infrastructure;
86

9-
namespace HydraScript;
7+
namespace HydraScript.Infrastructure;
108

11-
internal class ExecuteCommandHandler(
9+
public class Executor(
1210
ISourceCodeProvider sourceCodeProvider,
1311
IParser parser,
1412
ICodeGenerator codeGenerator,
15-
IVirtualMachine virtualMachine) : ICommandHandler
13+
IVirtualMachine virtualMachine)
1614
{
17-
18-
public int Invoke(InvocationContext context)
15+
public int Invoke()
1916
{
2017
var writer = virtualMachine.ExecuteParams.Writer;
2118
try
@@ -39,6 +36,12 @@ public int Invoke(InvocationContext context)
3936
}
4037
}
4138

42-
public Task<int> InvokeAsync(InvocationContext context) =>
43-
Task.FromResult(Invoke(context));
39+
internal static class ExitCodes
40+
{
41+
public const int Success = 0;
42+
43+
public const int HydraScriptError = 1;
44+
45+
public const int DotnetRuntimeError = 2;
46+
}
4447
}

src/Infrastructure/HydraScript.Infrastructure/HydraScript.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
<ItemGroup>
2323
<InternalsVisibleTo Include="HydraScript.UnitTests" />
24+
<InternalsVisibleTo Include="HydraScript.IntegrationTests" />
2425
</ItemGroup>
2526

2627
</Project>

0 commit comments

Comments
 (0)