-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from Particular/dotnet-tool-backport
dotnet tool for script generation
- Loading branch information
Showing
22 changed files
with
577 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[*.cs] | ||
|
||
# Justification: Application synchronization contexts don't require ConfigureAwait(false) | ||
dotnet_diagnostic.CA2007.severity = none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace NServiceBus.Persistence.Sql.CommandLine | ||
{ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using ScriptBuilder; | ||
|
||
public enum DialectTypes | ||
{ | ||
SqlServer, | ||
MySql, | ||
Oracle, | ||
PostgreSql, | ||
} | ||
|
||
public static class DialectTypesExtensions | ||
{ | ||
static readonly Dictionary<DialectTypes, BuildSqlDialect> DialectMap = new Dictionary<DialectTypes, BuildSqlDialect> | ||
{ | ||
[DialectTypes.Oracle] = BuildSqlDialect.Oracle, | ||
[DialectTypes.MySql] = BuildSqlDialect.MySql, | ||
[DialectTypes.MySql] = BuildSqlDialect.MySql, | ||
[DialectTypes.PostgreSql] = BuildSqlDialect.PostgreSql, | ||
[DialectTypes.SqlServer] = BuildSqlDialect.MsSqlServer, | ||
}; | ||
|
||
public static BuildSqlDialect ToBuildSqlDialect(this DialectTypes dialect) => DialectMap[dialect]; | ||
|
||
public static IReadOnlyList<BuildSqlDialect> ToBuildSqlDialects(this IReadOnlyList<DialectTypes> dialects) | ||
{ | ||
return dialects?.Select(d => d.ToBuildSqlDialect()).ToList().AsReadOnly(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Text; | ||
|
||
static class ExceptionExtensions | ||
{ | ||
public static string ToFriendlyString(this Exception exception) | ||
{ | ||
|
||
var stringBuilder = new StringBuilder(); | ||
stringBuilder.Append("Exception:"); | ||
stringBuilder.Append(Environment.NewLine); | ||
while (exception != null) | ||
{ | ||
stringBuilder.Append(exception.Message); | ||
stringBuilder.Append(Environment.NewLine); | ||
|
||
foreach (var i in exception.Data) | ||
{ | ||
stringBuilder.Append("Data :"); | ||
stringBuilder.Append(i); | ||
stringBuilder.Append(Environment.NewLine); | ||
} | ||
|
||
if (exception.StackTrace != null) | ||
{ | ||
stringBuilder.Append("StackTrace:"); | ||
stringBuilder.Append(Environment.NewLine); | ||
stringBuilder.Append(exception.StackTrace); | ||
stringBuilder.Append(Environment.NewLine); | ||
} | ||
|
||
if (exception.Source != null) | ||
{ | ||
stringBuilder.Append("Source:"); | ||
stringBuilder.Append(Environment.NewLine); | ||
stringBuilder.Append(exception.Source); | ||
stringBuilder.Append(Environment.NewLine); | ||
} | ||
|
||
exception = exception.InnerException; | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace NServiceBus.Persistence.Sql.CommandLine | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using McMaster.Extensions.CommandLineUtils; | ||
|
||
static class Generator | ||
{ | ||
public static Task Run( | ||
CommandArgument assemblyPath, | ||
CommandOption outputPath, | ||
CommandOption<bool> overwriteOption, | ||
CommandOption<bool> cleanOption, | ||
CommandOption<DialectTypes> dialectOption) | ||
{ | ||
var output = outputPath.HasValue() ? outputPath.Value() : Directory.GetCurrentDirectory(); | ||
|
||
var writer = new ScriptGenerator(assemblyPath.Value, output, | ||
cleanOption.ParsedValue, overwriteOption.ParsedValue, | ||
dialectOption.ParsedValues.ToBuildSqlDialects()); | ||
|
||
writer.Generate(); | ||
|
||
Console.WriteLine($"Script for {assemblyPath.Value} was generated at {output}."); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/CommandLine/NServiceBus.Persistence.Sql.CommandLine.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<ToolCommandName>sql-persistence</ToolCommandName> | ||
<PackAsTool>True</PackAsTool> | ||
<Description>.NET Core global tool to generate scripts for NServiceBus Sql persistence</Description> | ||
<GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ScriptBuilder\ScriptBuilder.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace NServiceBus.Persistence.Sql.CommandLine | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using ScriptBuilder; | ||
|
||
class Program | ||
{ | ||
internal const string AppName = "sql-persistence"; | ||
|
||
static int Main(string[] args) | ||
{ | ||
var app = new CommandLineApplication | ||
{ | ||
Name = AppName | ||
}; | ||
|
||
var verboseOption = app.Option<bool>("-v | --verbose", "Verbose logging", CommandOptionType.NoValue, inherited: true); | ||
|
||
app.HelpOption(inherited: true); | ||
|
||
app.Command("script", cmd => | ||
{ | ||
var assemblyPath = cmd.Argument("assembly", "File path to the endpoint assembly.").IsRequired(); | ||
var outputOption = cmd.Option("-o | --output-dir", "Path to the output directory.", CommandOptionType.SingleOrNoValue); | ||
var cleanOption = cmd.Option<bool>("--clean", "Removes existing files in the output directory.", CommandOptionType.SingleOrNoValue); | ||
var overwriteOption = cmd.Option<bool>("--overwrite", "Overwrites existing files in the output if they match the files to be generated.", CommandOptionType.SingleOrNoValue); | ||
var dialectOption = cmd.Option<DialectTypes>("--dialect", "Specifies a dialect to generate", CommandOptionType.SingleOrNoValue); | ||
|
||
cmd.OnExecuteAsync(async ct => | ||
{ | ||
await Generator.Run(assemblyPath, outputOption, overwriteOption, cleanOption, dialectOption); | ||
}); | ||
}); | ||
|
||
app.OnExecute(() => | ||
{ | ||
Console.WriteLine("Specify a subcommand"); | ||
app.ShowHelp(); | ||
return 1; | ||
}); | ||
|
||
try | ||
{ | ||
return app.Execute(args); | ||
} | ||
catch (Exception exception) | ||
{ | ||
var error = exception.ToFriendlyString(); | ||
Console.Error.WriteLine($"{AppName} failed: {exception.Message}"); | ||
|
||
if (verboseOption.HasValue()) | ||
{ | ||
Console.Error.WriteLine(error); | ||
} | ||
return 1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.