From 0f3509497df50ae7156872f001b975163079aa0f Mon Sep 17 00:00:00 2001 From: Manuel Grundner Date: Wed, 18 Sep 2019 16:02:15 +0200 Subject: [PATCH 01/56] feat: replace NDesk.Options by System.CommandLine.Experimental #346 --- src/Pretzel.Logic/Pretzel.Logic.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pretzel.Logic/Pretzel.Logic.csproj b/src/Pretzel.Logic/Pretzel.Logic.csproj index 6d5e9bec2..78b1a309f 100644 --- a/src/Pretzel.Logic/Pretzel.Logic.csproj +++ b/src/Pretzel.Logic/Pretzel.Logic.csproj @@ -1,4 +1,4 @@ - + 8.0.30703 net462;netstandard2.0 @@ -34,11 +34,11 @@ - all + From e8a5e68f7d5e982a6ca8239cb1c61a3d4db0d752 Mon Sep 17 00:00:00 2001 From: Manuel Grundner Date: Wed, 18 Sep 2019 16:38:48 +0200 Subject: [PATCH 02/56] feat: CommandParameters use new Option syntax --- .../Commands/CommandParameters.cs | 66 ++++++++++++++----- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/Pretzel.Logic/Commands/CommandParameters.cs b/src/Pretzel.Logic/Commands/CommandParameters.cs index f12a3dcfe..076a9d876 100644 --- a/src/Pretzel.Logic/Commands/CommandParameters.cs +++ b/src/Pretzel.Logic/Commands/CommandParameters.cs @@ -5,6 +5,7 @@ using Pretzel.Logic.Templating.Context; using System; using System.Collections.Generic; +using System.CommandLine; using System.Composition; using System.IO; using System.IO.Abstractions; @@ -21,23 +22,53 @@ public CommandParameters([ImportMany] IEnumerable commandL { this.fileSystem = fileSystem; - port = 8080; - LaunchBrowser = true; - - Settings = new OptionSet + Settings = new[] + { + new Option(new []{ "template", "t" },"The templating engine to use") + { + Argument = new Argument() + }, + new Option(new [] {"port", "p"}, "The port to test the site locally") + { + Argument = new Argument(() => 8080) + }, + new Option(new [] {"import", "i"}, "The import type") + { + Argument = new Argument() + }, + new Option(new [] {"file", "f"}, "Path to import file") + { + Argument = new Argument() + }, + new Option("destination", "The path to the destination site (default _site)") + { + Argument = new Argument(() => "_site") + }, + new Option("drafts", "Add the posts in the drafts folder") { - { "t|template=", "The templating engine to use", v => Template = v }, - { "p|port=", "The port to test the site locally", p => decimal.TryParse(p, out port) }, - { "i|import=", "The import type", v => ImportType = v }, - { "f|file=", "Path to import file", v => ImportPath = v }, - { "destination=", "The path to the destination site (default _site)", d => DestinationPath = d}, - { "drafts", "Add the posts in the drafts folder", v => IncludeDrafts = true }, - { "nobrowser", "Do not launch a browser", v => LaunchBrowser = false }, - { "withproject", "Includes a layout VS Solution, to give intellisense when editing razor layout files", v => WithProject = (v!=null) }, - { "wiki", "Creates a wiki instead of a blog (razor template only)", v => Wiki = (v!=null) }, - { "cleantarget", "Delete the target directory (_site by default)", v => CleanTarget = true }, - { "n|newposttitle=", "The title of the new post (\"New post\" by default)", v => NewPostTitle = v } - }; + Argument = new Argument() + }, + new Option("nobrowser", "Do not launch a browser (false by default)") + { + Argument = new Argument(() => false) + }, + new Option("withproject", "Includes a layout VS Solution, to give intellisense when editing razor layout files") + { + Argument = new Argument() + }, + new Option("wiki", "Creates a wiki instead of a blog (razor template only)") + { + Argument = new Argument() + }, + new Option("cleantarget", "Delete the target directory (_site by default)") + { + Argument = new Argument() + }, + new Option(new [] { "newposttitle", "n" }, "The title of the new post (\"New post\" by default") + { + Argument = new Argument(() => "New post") + } + }; // Allow extensions to register command line args foreach (var commandLineExtension in commandLineExtensions) @@ -76,7 +107,8 @@ public decimal Port get { return port; } } - private OptionSet Settings { get; set; } + [Export] + private IEnumerable pretzel.ico diff --git a/src/Pretzel/Program.cs b/src/Pretzel/Program.cs index 79d0fd50d..38fd4413a 100644 --- a/src/Pretzel/Program.cs +++ b/src/Pretzel/Program.cs @@ -2,19 +2,25 @@ using Pretzel.Logic.Commands; using Pretzel.Logic.Extensions; using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Builder; +using System.CommandLine.Invocation; using System.Composition; using System.Composition.Hosting; using System.IO; using System.IO.Abstractions; using System.Linq; using System.Reflection; +using System.Threading.Tasks; namespace Pretzel { + [Export] internal class Program { [Import] - public CommandCollection Commands { get; set; } + public CommandCollection CommandCollection { get; set; } [Export] public IFileSystem FileSystem { get; set; } = new FileSystem(); @@ -22,68 +28,119 @@ internal class Program [Export("SourcePath")] public string SourcePath { get; } - private BaseParameters parameters { get; } - public Program() { - parameters = BaseParameters.Parse(Args, FileSystem); - SourcePath = parameters.Path; + SourcePath = sourcePath; } - private static string[] Args; - private static void Main(string[] args) + private static string sourcePath; + private static async Task Main(string[] args) { - Args = args; - var program = new Program(); + var rootCommand = new RootCommand + { + TreatUnmatchedTokensAsErrors = false + }; + + var globalOptions = new[] + { + new Option("--debug", "Enable debugging") + { + Argument = new Argument() + }, + new Option("--safe", "Disable custom plugins") + { + Argument = new Argument() + }, + new Option(new[] { "-s", "--source" }, "The path to the source site (default current directory)") + { + Argument = new Argument(() => Environment.CurrentDirectory) + } + }; - program.InitializeTrace(); - Tracing.Info("starting pretzel..."); - Tracing.Debug("V{0}", Assembly.GetExecutingAssembly().GetName().Version); + foreach (var option in globalOptions) + { + rootCommand.AddOption(option); + } - using (program.Compose()) + rootCommand.Handler = CommandHandler.Create(async (bool debug, bool safe, string source) => { + sourcePath = source; + try + { + InitializeTrace(debug); + Tracing.Info("starting pretzel..."); + Tracing.Debug("V{0}", Assembly.GetExecutingAssembly().GetName().Version); + + using (var host = Compose(debug, safe, source)) + { + var program = host.GetExport(); - if (program.parameters.Help || !args.Any()) + return await program.Run(globalOptions, args); + } + } + catch (Exception ex) { - program.ShowHelp(); - return; + Tracing.Error(ex.Message); + WaitForClose(); + return -1; } - - program.Run(); + }); + try + { + return await rootCommand.InvokeAsync(args); + } + catch (Exception ex) + { + Tracing.Error(ex.Message); + WaitForClose(); + return -1; } } - private void InitializeTrace() + private static void InitializeTrace(bool debug) { Tracing.SetTrace(ConsoleTrace.Write); - if (parameters.Debug) + if (debug) { Tracing.SetMinimalLevel(TraceLevel.Debug); } } - private void ShowHelp() + private async Task Run(IEnumerable