diff --git a/VisualStudio.Tests/OutputHelperTextWriter.cs b/VisualStudio.Tests/OutputHelperTextWriter.cs new file mode 100644 index 0000000..ebc9410 --- /dev/null +++ b/VisualStudio.Tests/OutputHelperTextWriter.cs @@ -0,0 +1,21 @@ +using System.IO; +using System.Text; +using Xunit.Abstractions; + +namespace VisualStudio.Tests +{ + internal class OutputHelperTextWriter : TextWriter + { + readonly ITestOutputHelper output; + + public OutputHelperTextWriter(ITestOutputHelper output) => this.output = output; + + public override Encoding Encoding => Encoding.UTF8; + + public override void Write(string value) => WriteLine(value); + + public override void WriteLine(string message) => output.WriteLine(message); + + public override void WriteLine(string format, params object[] args) => output.WriteLine(format, args); + } +} diff --git a/VisualStudio.Tests/ProgramTests.cs b/VisualStudio.Tests/ProgramTests.cs new file mode 100644 index 0000000..89e3b79 --- /dev/null +++ b/VisualStudio.Tests/ProgramTests.cs @@ -0,0 +1,27 @@ +using System.IO; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +namespace VisualStudio.Tests +{ + public class ProgramTests + { + readonly TextWriter output; + + public ProgramTests(ITestOutputHelper output) => + this.output = new OutputHelperTextWriter(output); + + [Fact] + public async Task when_no_command_specified_run_is_default() + { + var program = new Program(output, false, "pre"); + + var exit = await program.RunAsync(); + + Assert.Equal(0, exit); + + Assert.IsType(program.Command); + } + } +} diff --git a/VisualStudio/ConfigCommandDescriptor.cs b/VisualStudio/ConfigCommandDescriptor.cs index 1cc1f63..c469c56 100644 --- a/VisualStudio/ConfigCommandDescriptor.cs +++ b/VisualStudio/ConfigCommandDescriptor.cs @@ -5,18 +5,14 @@ namespace VisualStudio { class ConfigCommandDescriptor : CommandDescriptor { - readonly VisualStudioOptions options = new VisualStudioOptions(channelVerb: "Open", showNickname: false); - bool exp; + readonly VisualStudioOptions options = new VisualStudioOptions(channelVerb: "Open", showExp: true, showNickname: false); - public ConfigCommandDescriptor() => OptionSet = new CompositeOptionSet(options, new OptionSet - { - { "exp", "Use experimental folder instead of regular.", e => exp = e != null }, - }); + public ConfigCommandDescriptor() => OptionSet = new CompositeOptionSet(options); public Channel? Channel => options.Channel; public Sku? Sku => options.Sku; - public bool Experimental => exp; + public bool Experimental => options.IsExperimental; } } diff --git a/VisualStudio/Program.cs b/VisualStudio/Program.cs index c7233c2..2f65bf8 100644 --- a/VisualStudio/Program.cs +++ b/VisualStudio/Program.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using Mono.Options; @@ -7,34 +8,52 @@ namespace VisualStudio { class Program { + static readonly CommandFactory commandFactory = new CommandFactory(); + + TextWriter output; + bool execute; + string[] args; + static async Task Main(string[] args) { - var commandFactory = new CommandFactory(); - if (args.Length == 0 || new[] { "?", "-?", "/?", "-h", "/h", "--help", "/help" }.Contains(args[0])) return ShowUsage(commandFactory); + return await new Program(Console.Out, true, args).RunAsync(); + } + + public Program(TextWriter output, bool execute, params string[] args) + { + this.output = output; + this.execute = execute; + this.args = args; + // Run is the default command if another one is not specified. if (!commandFactory.IsCommandRegistered(args[0])) - args = args.Prepend("run").ToArray(); + this.args = args.Prepend("run").ToArray(); + } + public Command Command { get; private set; } + + public async Task RunAsync() + { var commandName = args[0]; try { - var command = commandFactory.CreateCommand(commandName, args.Skip(1)); + Command = commandFactory.CreateCommand(commandName, args.Skip(1)); - await command.ExecuteAsync(Console.Out); - // TODO: other exceptions might provide an exit code? + if (execute) + await Command.ExecuteAsync(output); } catch (ShowUsageException ex) { - Console.WriteLine($"Usage: {ThisAssembly.Metadata.AssemblyName} {commandName} [options]"); - ex.CommandDescriptor.ShowUsage(Console.Out); + output.WriteLine($"Usage: {ThisAssembly.Metadata.AssemblyName} {commandName} [options]"); + ex.CommandDescriptor.ShowUsage(output); return -1; } catch (OptionException ex) { - Console.WriteLine(ex.Message); + output.WriteLine(ex.Message); return -1; } diff --git a/VisualStudio/Properties/launchSettings.json b/VisualStudio/Properties/launchSettings.json index e647a9b..88b3330 100644 --- a/VisualStudio/Properties/launchSettings.json +++ b/VisualStudio/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "VisualStudio": { "commandName": "Project", - "commandLineArgs": "kill all exp" + "commandLineArgs": "config pre exp" } } -} \ No newline at end of file +}