From 3bdecc556c33ce2864c3868a988ff05bc0706a65 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 30 Jul 2018 11:21:57 +0200 Subject: [PATCH] refact --- cli/.paul-ci.json | 5 ++ cli/Commands/GenerateJsonSchema.cs | 73 ++++++++++++++++++++++++++++++ cli/Commands/SendMailCommand.cs | 14 +++--- cli/Misc/ICliCommander.cs | 8 ++-- cli/Program.cs | 11 ++++- cli/project.json | 1 + 6 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 cli/.paul-ci.json create mode 100644 cli/Commands/GenerateJsonSchema.cs diff --git a/cli/.paul-ci.json b/cli/.paul-ci.json new file mode 100644 index 00000000..4f71532d --- /dev/null +++ b/cli/.paul-ci.json @@ -0,0 +1,5 @@ +{ + "build": { + + } +} \ No newline at end of file diff --git a/cli/Commands/GenerateJsonSchema.cs b/cli/Commands/GenerateJsonSchema.cs new file mode 100644 index 00000000..b118b64e --- /dev/null +++ b/cli/Commands/GenerateJsonSchema.cs @@ -0,0 +1,73 @@ + +using Microsoft.AspNet.Hosting; +using Microsoft.Extensions.CommandLineUtils; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using cli.Services; +using System.Threading.Tasks; +using NJsonSchema; +using System.IO; + +namespace cli +{ + + public class GenerateJsonSchema + { + public CommandLineApplication Integrates(CommandLineApplication rootApp) + { + CommandArgument genargclass = null; + CommandArgument genargjson = null; + CommandOption genopthelp = null; + var cmd = rootApp.Command("gen", + (target) => + { + target.FullName = "Generete"; + target.Description = "generates some objects ..."; + genopthelp = target.HelpOption("-? | -h | --help"); + genargclass = target.Argument( + "class", + "class name of generation to execute (actually, only 'jsonSchema') .", + multipleValues: false); + genargjson = target.Argument( + "json", + "Json file to generate a schema for.", + multipleValues: false); + }, false); + cmd.OnExecute( + async () => { + if (genargjson.Value == "jsonSchema") { + if (genargjson.Value == null) + await GenerateCiBuildSettingsSchema(); + else + await GenerateCiBuildSettingsSchema(genargjson.Value); + } else { + cmd.ShowHint(); + return 1; + } + return 0; + } + ); + return cmd; + } + public static async Task GenerateCiBuildSettingsSchema(string outputFileName = "pauls-ci-schema.json") + { + var schema = await JsonSchema4.FromTypeAsync(); + var schemaData = schema.ToJson(); + + FileInfo ofi = new FileInfo(outputFileName); + var ostream = ofi.OpenWrite(); + var owritter = new StreamWriter(ostream); + owritter.WriteLine(schemaData); + owritter.Close(); + ostream.Close(); + /* var errors = schema.Validate("{...}"); + + foreach (var error in errors) + Console.WriteLine(error.Path + ": " + error.Kind); + + schema = await JsonSchema4.FromJsonAsync(schemaData); */ + } + + } + +} \ No newline at end of file diff --git a/cli/Commands/SendMailCommand.cs b/cli/Commands/SendMailCommand.cs index b03d412a..aee985f9 100644 --- a/cli/Commands/SendMailCommand.cs +++ b/cli/Commands/SendMailCommand.cs @@ -7,19 +7,19 @@ using cli.Services; namespace cli { -public class SendMailCommandProvider { - public static CommandLineApplication Handle(CommandLineApplication rootApp) +public class SendMailCommandProvider : ICliCommand { + public CommandLineApplication Integrates(CommandLineApplication rootApp) { - CommandArgument _sendMailCommandArg = null; - CommandOption _sendHelpOption = null; + CommandArgument sendMailCommandArg = null; + CommandOption sendHelpOption = null; CommandLineApplication sendMailCommandApp = rootApp.Command("send", (target) => { target.FullName = "Send email"; target.Description = "Sends emails using given template"; - _sendHelpOption = target.HelpOption("-? | -h | --help"); - _sendMailCommandArg = target.Argument( + sendHelpOption = target.HelpOption("-? | -h | --help"); + sendMailCommandArg = target.Argument( "class", "class name of mailling to execute (actually, only 'monthly') .", multipleValues: true); @@ -27,7 +27,7 @@ public class SendMailCommandProvider { sendMailCommandApp.OnExecute(() => { - if (_sendMailCommandArg.Value == "monthly") + if (sendMailCommandArg.Value == "monthly") { var host = new WebHostBuilder(); var hostengnine = host.UseEnvironment("Development") diff --git a/cli/Misc/ICliCommander.cs b/cli/Misc/ICliCommander.cs index 38580618..69fb810a 100644 --- a/cli/Misc/ICliCommander.cs +++ b/cli/Misc/ICliCommander.cs @@ -1,4 +1,6 @@ -public interface ICliTAsk -{ +using Microsoft.Extensions.CommandLineUtils; -} \ No newline at end of file +public interface ICliCommand +{ + CommandLineApplication Integrates(CommandLineApplication rootApp); +} diff --git a/cli/Program.cs b/cli/Program.cs index 8739ca21..b4f86a2c 100644 --- a/cli/Program.cs +++ b/cli/Program.cs @@ -1,11 +1,15 @@  using System; +using System.IO; +using System.Threading.Tasks; using Microsoft.Extensions.CommandLineUtils; +using NJsonSchema; namespace cli { public partial class Program { + public static void Main(string[] args) { CommandOption rootCommandHelpOption = null; @@ -13,12 +17,15 @@ namespace cli CommandLineApplication cliapp = new CommandLineApplication(false); cliapp.Name = "cli"; cliapp.FullName = "Yavsc command line interface"; - cliapp.Description = "Dnx console for yavsc server side"; + cliapp.Description = "Dnx console app for yavsc server side"; cliapp.ShortVersionGetter = () => "v1.0"; cliapp.LongVersionGetter = () => "version 1.0 (stable)"; rootCommandHelpOption = cliapp.HelpOption("-? | -h | --help"); + var command = new SendMailCommandProvider(); + command.Integrates(cliapp); + var gencmd = new GenerateJsonSchema(); + gencmd.Integrates(cliapp); - var sb = SendMailCommandProvider.Handle(cliapp); if (args.Length == 0) { cliapp.ShowHint(); diff --git a/cli/project.json b/cli/project.json index d29c65a9..a4164a43 100644 --- a/cli/project.json +++ b/cli/project.json @@ -45,6 +45,7 @@ "Microsoft.Framework.Configuration.Json": "1.0.0-beta8", "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4", "Newtonsoft.Json": "9.0.1", + "NJsonSchema.CodeGeneration.CSharp": "9.10.65", "Yavsc": { "version": "1.0.5-rc22", "target": "package"