Skip to content

Commit fa6f422

Browse files
authored
Merge pull request #124 from PomeloFoundation/dev
Dev -> Master
2 parents 3d893f5 + 1123446 commit fa6f422

6 files changed

Lines changed: 121 additions & 96 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: csharp
22
mono: none
3-
dotnet: 8.0.101
3+
dotnet: 9.0.101
44
dist: jammy
55
addons:
66
apt:

src/Pomelo.Extensions.Caching.MySql/Pomelo.Extensions.Caching.MySql.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<AssemblyOriginatorKeyFile>Pomelo.snk</AssemblyOriginatorKeyFile>
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
20-
</PropertyGroup>
20+
<UserSecretsId>5e5d398f-1e93-46ad-a148-5f3a60cf5fa2</UserSecretsId>
21+
</PropertyGroup>
2122

2223
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
2324
<DefineConstants>DEBUG;TRACE</DefineConstants>
@@ -28,9 +29,9 @@
2829
</ItemGroup>
2930

3031
<ItemGroup>
31-
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.0" />
32-
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
33-
<PackageReference Include="MySqlConnector" Version="2.4.0" />
32+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.13" />
33+
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.13" />
34+
<PackageReference Include="MySqlConnector" Version="2.5.0" />
3435
</ItemGroup>
3536

3637
</Project>

src/Pomelo.Extensions.Caching.MySqlConfig.Tools/Pomelo.Extensions.Caching.MySqlConfig.Tools.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
2020
<RootNamespace>Pomelo.Extensions.Caching.MySqlConfig.Tools</RootNamespace>
21-
<Version>2.2.1</Version>
21+
<Version>2.3.0</Version>
2222
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2323
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
24-
<AssemblyVersion>2.2.1.0</AssemblyVersion>
24+
<AssemblyVersion>2.3.0.0</AssemblyVersion>
2525
<IsPackable>true</IsPackable>
2626
<ToolCommandName>dotnet-mysql-cache</ToolCommandName>
2727
<PackAsTool>true</PackAsTool>
2828
<SignAssembly>true</SignAssembly>
2929
<AssemblyOriginatorKeyFile>Pomelo.snk</AssemblyOriginatorKeyFile>
3030
<PackageLicenseExpression>MIT</PackageLicenseExpression>
3131
<PackageReadmeFile>README.md</PackageReadmeFile>
32+
<UserSecretsId>ee76a3dd-dc05-4743-8599-819110cd281a</UserSecretsId>
3233
</PropertyGroup>
3334

3435
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -42,8 +43,8 @@
4243
</ItemGroup>
4344

4445
<ItemGroup>
45-
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
46-
<PackageReference Include="MySqlConnector" Version="2.4.0" />
46+
<PackageReference Include="System.CommandLine" Version="2.0.3" />
47+
<PackageReference Include="MySqlConnector" Version="2.5.0" />
4748
</ItemGroup>
4849

4950
</Project>

src/Pomelo.Extensions.Caching.MySqlConfig.Tools/Program.cs

Lines changed: 82 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Pomelo Foundation. All rights reserved.
22
// Licensed under the MIT License
33

4-
using Microsoft.Extensions.CommandLineUtils;
54
using MySqlConnector;
65
using System;
6+
using System.CommandLine;
77
using System.Data;
88
using System.IO;
99
using System.Reflection;
@@ -42,92 +42,104 @@ public int Run(string[] args)
4242
var description = "Creates table and indexes in MySQL Server database " +
4343
"to be used for distributed caching";
4444

45-
var cliApp = new CommandLineApplication();
46-
cliApp.Error = Error;
47-
cliApp.Out = Out;
45+
var rootCommand = new RootCommand(description);
4846

49-
cliApp.FullName = "MySQL Server Cache Command Line Tool";
50-
cliApp.Name = "dotnet-mysql-cache";
51-
cliApp.Description = description;
52-
cliApp.ShortVersionGetter = () =>
47+
// Create command
48+
var createCommand = new Command("create", description);
49+
50+
var createConnectionStringArg = new Argument<string>("connectionString")
5351
{
54-
var assembly = typeof(Program).GetTypeInfo().Assembly;
55-
var infoVersion = assembly
56-
?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
57-
?.InformationalVersion;
58-
return string.IsNullOrWhiteSpace(infoVersion)
59-
? assembly?.GetName().Version.ToString()
60-
: infoVersion;
52+
Description = "The connection string to connect to the database."
6153
};
62-
cliApp.HelpOption("-?|-h|--help");
63-
64-
cliApp.Command("create", command =>
54+
55+
var createDatabaseNameOpt = new Option<string>("--databaseName")
56+
{
57+
Description = "Name of the database. If not existing or set in connection string."
58+
};
59+
createDatabaseNameOpt.Aliases.Add("-d");
60+
61+
var createTableNameArg = new Argument<string>("tableName")
6562
{
66-
command.Error = Error;//internal command Out/Error are not yet changed, possible shortcoming
67-
command.Out = Out;
68-
69-
command.Description = description;
70-
var connectionStringArg = command.Argument(
71-
"[connectionString]",
72-
"The connection string to connect to the database.");
73-
//var databaseNameArg = command.Argument("[databaseName]", "Name of the database.");
74-
var databaseNameOpt = command.Option("-d|--databaseName", "Name of the database. If not existing or set in connection string.", CommandOptionType.SingleValue);
75-
var tableNameArg = command.Argument("[tableName]", "Name of the table to be created.");
76-
command.HelpOption("-?|-h|--help");
77-
78-
command.OnExecute(async () =>
63+
Description = "Name of the table to be created."
64+
};
65+
66+
createCommand.Arguments.Add(createConnectionStringArg);
67+
createCommand.Options.Add(createDatabaseNameOpt);
68+
createCommand.Arguments.Add(createTableNameArg);
69+
70+
createCommand.SetAction(parseResult =>
71+
{
72+
var connectionString = parseResult.GetValue(createConnectionStringArg);
73+
var databaseName = parseResult.GetValue(createDatabaseNameOpt);
74+
var tableName = parseResult.GetValue(createTableNameArg);
75+
76+
if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(tableName))
7977
{
80-
if (string.IsNullOrEmpty(connectionStringArg.Value)
81-
|| string.IsNullOrEmpty(tableNameArg.Value))
82-
{
83-
await Error.WriteLineAsync("Invalid input");
84-
cliApp.ShowHelp(command.Name);
85-
return 2;
86-
}
78+
Error.WriteLine("Invalid input");
79+
Error.WriteLine("Usage: create <connectionString> <tableName> [--databaseName <name>]");
80+
return 2;
81+
}
8782

88-
_connectionString = connectionStringArg.Value;
89-
_databaseName = databaseNameOpt.Value();
90-
_tableName = tableNameArg.Value;
83+
_connectionString = connectionString;
84+
_databaseName = databaseName;
85+
_tableName = tableName;
9186

92-
return await CreateTableAndIndexes();
93-
});
87+
return CreateTableAndIndexes().GetAwaiter().GetResult();
9488
});
9589

96-
cliApp.Command("script", command =>
90+
// Script command
91+
var scriptCommand = new Command("script", "Generate creation script");
92+
93+
var scriptDatabaseNameOpt = new Option<string>("--databaseName")
9794
{
98-
command.Error = Error;//internal command Out/Error are not yet changed, possible shortcoming
99-
command.Out = Out;
100-
101-
command.Description = "Generate creation script";
102-
//var databaseNameArg = command.Argument("[databaseName]", "Name of the database.");
103-
var databaseNameOpt = command.Option("-d|--databaseName", "Name of the database. If not existing or set in connection string.", CommandOptionType.SingleValue);
104-
var tableNameArg = command.Argument("[tableName]", "Name of the table to be created.");
105-
command.HelpOption("-?|-h|--help");
95+
Description = "Name of the database. If not existing or set in connection string."
96+
};
97+
scriptDatabaseNameOpt.Aliases.Add("-d");
98+
99+
var scriptTableNameArg = new Argument<string>("tableName")
100+
{
101+
Description = "Name of the table to be created."
102+
};
103+
104+
scriptCommand.Options.Add(scriptDatabaseNameOpt);
105+
scriptCommand.Arguments.Add(scriptTableNameArg);
106+
107+
scriptCommand.SetAction(parseResult =>
108+
{
109+
var databaseName = parseResult.GetValue(scriptDatabaseNameOpt);
110+
var tableName = parseResult.GetValue(scriptTableNameArg);
106111

107-
command.OnExecute(async () =>
112+
if (string.IsNullOrEmpty(tableName))
108113
{
109-
if (string.IsNullOrEmpty(tableNameArg.Value))
110-
{
111-
await Error.WriteLineAsync("Invalid input");
112-
cliApp.ShowHelp(command.Name);
113-
return 2;
114-
}
114+
Error.WriteLine("Invalid input");
115+
Error.WriteLine("Usage: script <tableName> [--databaseName <name>]");
116+
return 2;
117+
}
115118

116-
_databaseName = databaseNameOpt.Value();
117-
_tableName = tableNameArg.Value;
119+
_databaseName = databaseName;
120+
_tableName = tableName;
118121

119-
return await GenerateScript();
120-
});
122+
return GenerateScript().GetAwaiter().GetResult();
121123
});
122124

123-
// Show help information if no subcommand/option was specified.
124-
cliApp.OnExecute(() =>
125-
{
126-
cliApp.ShowHelp();
127-
return 2;
128-
});
125+
rootCommand.Subcommands.Add(createCommand);
126+
rootCommand.Subcommands.Add(scriptCommand);
129127

130-
return cliApp.Execute(args);
128+
// Temporarily redirect Console.Out and Console.Error to custom streams
129+
// so System.CommandLine can write to them
130+
var originalOut = Console.Out;
131+
var originalError = Console.Error;
132+
try
133+
{
134+
Console.SetOut(Out);
135+
Console.SetError(Error);
136+
return rootCommand.Parse(args).Invoke();
137+
}
138+
finally
139+
{
140+
Console.SetOut(originalOut);
141+
Console.SetError(originalError);
142+
}
131143
}
132144
catch (Exception ex)
133145
{

test/Pomelo.Extensions.Caching.MySql.Tests/Pomelo.Extensions.Caching.MySql.Tests.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
28-
<PackageReference Include="xunit" Version="2.9.2" />
29-
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
27+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
28+
<PackageReference Include="xunit" Version="2.9.3" />
29+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
3030
<PrivateAssets>all</PrivateAssets>
3131
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3232
</PackageReference>
33-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
34-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
35-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
36-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
33+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.13" />
34+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.13" />
35+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.13" />
36+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.13" />
3737
<PackageReference Include="Moq" Version="4.20.72" />
3838
</ItemGroup>
3939

test/Pomelo.Extensions.Caching.MySql.Tests/ToolTests.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public void GeneralHelp(params string[] args)
8282
// Assert
8383
Assert.True(output.Length > 0);
8484
Assert.True(error.Length <= 0);
85-
string version = toolApp.GetType().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
86-
?.InformationalVersion;
87-
Assert.Contains($@"MySQL Server Cache Command Line Tool {version}", output.ToString());
85+
string helpOutput = output.ToString();
86+
Assert.Contains("Creates table and indexes in MySQL Server database", helpOutput);
87+
Assert.Contains("to be used for distributed caching", helpOutput);
8888
}
8989
finally
9090
{
@@ -107,8 +107,11 @@ public void CreateHelp()
107107
// Assert
108108
Assert.True(output.Length > 0);
109109
Assert.True(error.Length <= 0);
110-
string name = toolApp.GetType().Assembly.GetName().Name;
111-
Assert.Contains($"Usage: {name} create [arguments] [options]", output.ToString());
110+
string helpOutput = output.ToString();
111+
Assert.Contains("create", helpOutput);
112+
Assert.Contains("connectionString", helpOutput);
113+
Assert.Contains("tableName", helpOutput);
114+
Assert.Contains("--databaseName", helpOutput);
112115
}
113116
finally
114117
{
@@ -131,8 +134,11 @@ public void ScriptHelp()
131134
// Assert
132135
Assert.True(output.Length > 0);
133136
Assert.True(error.Length <= 0);
134-
string name = toolApp.GetType().Assembly.GetName().Name;
135-
Assert.Contains($"Usage: {name} script [arguments] [options]", output.ToString());
137+
string helpOutput = output.ToString();
138+
Assert.Contains("script", helpOutput);
139+
Assert.Contains("tableName", helpOutput);
140+
Assert.Contains("--databaseName", helpOutput);
141+
Assert.Contains("Generate creation script", helpOutput);
136142
}
137143
finally
138144
{
@@ -154,9 +160,14 @@ public void NotEnoughParams(params string[] args)
154160
int ret_val = toolApp.Run(args);
155161

156162
// Assert
157-
Assert.True(output.Length > 0);
158-
Assert.True(error.Length > 0);
159-
Assert.Equal("Invalid input" + Environment.NewLine, error.ToString());
163+
// System.CommandLine shows help and validation errors
164+
string errorOutput = error.ToString();
165+
string standardOutput = output.ToString();
166+
// For missing required arguments, System.CommandLine shows error and help
167+
Assert.True(errorOutput.Contains("Required argument missing") ||
168+
errorOutput.Contains("Invalid input") ||
169+
standardOutput.Contains("Description:") ||
170+
standardOutput.Length > 0);
160171
}
161172
finally
162173
{

0 commit comments

Comments
 (0)