Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved files around #24

Merged
merged 3 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions VisualStudio.Tests/CommandFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ public void when_creating_builtin_command_then_then_command_is_created(string co

class TestCommandDescriptor : CommandDescriptor
{
public TestCommandDescriptor()
{
OptionSet = new CompositeOptionSet();
}
}

class TestCommand : Command<TestCommandDescriptor>
Expand Down
101 changes: 89 additions & 12 deletions VisualStudio.Tests/VisualStudioOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ namespace VisualStudio.Tests
public class VisualStudioOptionsTests
{
[Theory]
[InlineData("", default)]
[InlineData("rel", Channel.Release)]
[InlineData("release", Channel.Release)]
[InlineData("Release", Channel.Release)]
[InlineData("--rel", Channel.Release)]
[InlineData("--release", Channel.Release)]
[InlineData("pre", Channel.Preview)]
[InlineData("preview", Channel.Preview)]
[InlineData("Preview", Channel.Preview)]
[InlineData("--pre", Channel.Preview)]
[InlineData("--preview", Channel.Preview)]
[InlineData("int", Channel.IntPreview)]
Expand All @@ -16,53 +23,123 @@ public class VisualStudioOptionsTests
[InlineData("--internal", Channel.IntPreview)]
[InlineData("master", Channel.Master)]
[InlineData("--master", Channel.Master)]
public void when_parsing_channel_argument_then_channel_is_set(string channelArgument, Channel expectedChannel)
public void when_parsing_channel_argument_then_channel_is_set(string argument, Channel? expectedValue)
{
var options = new VisualStudioOptions();
var options = VisualStudioOptions.Empty().WithChannel();

options.Parse(new[] { channelArgument });
options.Parse(new[] { argument });

Assert.Equal(expectedChannel, options.Channel);
Assert.Equal(expectedValue, options.Channel);
}

[Theory]
[InlineData("", default)]
[InlineData("e", Sku.Enterprise)]
[InlineData("ent", Sku.Enterprise)]
[InlineData("enterprise", Sku.Enterprise)]
[InlineData("Ent", Sku.Enterprise)]
[InlineData("Enterprise", Sku.Enterprise)]
[InlineData("--sku=e", Sku.Enterprise)]
[InlineData("--sku=ent", Sku.Enterprise)]
[InlineData("--sku=enterprise", Sku.Enterprise)]
[InlineData("--sku=Enterprise", Sku.Enterprise)]
[InlineData("p", Sku.Professional)]
[InlineData("pro", Sku.Professional)]
[InlineData("professional", Sku.Professional)]
[InlineData("Pro", Sku.Professional)]
[InlineData("Professional", Sku.Professional)]
[InlineData("--sku=p", Sku.Professional)]
[InlineData("--sku=pro", Sku.Professional)]
[InlineData("--sku=professional", Sku.Professional)]
[InlineData("--sku=Professional", Sku.Professional)]
[InlineData("c", Sku.Community)]
[InlineData("com", Sku.Community)]
[InlineData("community", Sku.Community)]
[InlineData("Com", Sku.Community)]
[InlineData("Community", Sku.Community)]
[InlineData("--sku=c", Sku.Community)]
[InlineData("--sku=com", Sku.Community)]
[InlineData("--sku=community", Sku.Community)]
public void when_parsing_sku_argument_then_sku_is_set(string skuArgument, Sku expectedSku)
[InlineData("--sku=Community", Sku.Community)]
public void when_parsing_sku_argument_then_sku_is_set(string argument, Sku? expectedValue)
{
var options = new VisualStudioOptions();
var options = VisualStudioOptions.Empty().WithSku();

options.Parse(new[] { skuArgument });
options.Parse(new[] { argument });

Assert.Equal(expectedSku, options.Sku);
Assert.Equal(expectedValue, options.Sku);
}

[Theory]
[InlineData("", default)]
[InlineData("--nick=nick1", "nick1")]
[InlineData("--nickname=nick2", "nick2")]
public void when_parsing_nickname_argument_then_nickname_is_set(string nicknameArgument, string expectedNickname)
public void when_parsing_nickname_argument_then_nickname_is_set(string argument, string expectedValue)
{
var options = VisualStudioOptions.Empty().WithNickname();

options.Parse(new[] { argument });

Assert.Equal(expectedValue, options.Nickname);
}

[Theory]
[InlineData("", false)]
[InlineData("exp", true)]
[InlineData("experimental", true)]
[InlineData("--exp", true)]
[InlineData("--experimental", true)]
public void when_parsing_experimental_then_experimental_is_set(string argument, bool expectedValue)
{
var options = VisualStudioOptions.Empty().WithExperimental();

options.Parse(new[] { argument });

Assert.Equal(expectedValue, options.IsExperimental);
}

[Theory]
[InlineData("", default)]
[InlineData("x => x.Prop == 'value'", "x => x.Prop == \"value\"")]
[InlineData("--expr= x => x.Prop == 'value'", "x => x.Prop == \"value\"")]
[InlineData("--expression= x => x.Prop == 'value'", "x => x.Prop == \"value\"")]
[InlineData("/expr: x => x.Prop == 'value'", "x => x.Prop == \"value\"")]
[InlineData("/expression: x => x.Prop == 'value'", "x => x.Prop == \"value\"")]
public void when_parsing_expression_then_exppression_is_set(string argument, string expectedValue)
{
var options = new VisualStudioOptions();
var options = VisualStudioOptions.Empty().WithExpression();

options.Parse(new[] { nicknameArgument });
options.Parse(new[] { argument });

Assert.Equal(expectedNickname, options.Nickname);
Assert.Equal(expectedValue, options.Expression);
}

[Theory]
[InlineData("", false)]
[InlineData("all", true)]
[InlineData("All", true)]
[InlineData("--all", true)]
public void when_parsing_all_argument_then_all_is_set(string argument, bool expectedValue)
{
var options = VisualStudioOptions.Empty().WithSelectAll();

options.Parse(new[] { argument });

Assert.Equal(expectedValue, options.All);
}

[Theory]
[InlineData(Sku.Enterprise, Channel.Preview, "x => x.InstanceId == \"123\"", "Enterprise", "Preview", "x => x.InstanceId == '123'")]
[InlineData(Sku.Enterprise, Channel.Preview, "x => x.InstanceId == \"123\"", "/sku:Enterprise", "--preview", "x => x.InstanceId == '123'")]
public void when_parsing_with_default_options_then_sku_and_channel_and_expression_are_set(Sku expectedSku, Channel expectedChannel, string expectedExpression, params string[] arguments)
{
var options = VisualStudioOptions.Default();

options.Parse(arguments);

Assert.Equal(expectedSku, options.Sku);
Assert.Equal(expectedChannel, options.Channel);
Assert.Equal(expectedExpression, options.Expression);
}
}
}
17 changes: 5 additions & 12 deletions VisualStudio.Tests/VisualStudioPredicateBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using vswhere;

namespace VisualStudio.Tests
Expand Down Expand Up @@ -65,14 +62,10 @@ public async Task when_evaluating_combined_criterias_then_predicate_matches_conf
Assert.False(predicate(new vswhere.VisualStudioInstance() { InstanceId = "123" }.WithSku(Sku.Professional).WithChannel(Channel.Release)));
}

VisualStudioOptions GetOptions(Sku? sku = null, Channel? channel = null, string expression = null) =>
new VisualStudioOptionsTests(sku, channel, expression);

class VisualStudioOptionsTests : VisualStudioOptions
{
public VisualStudioOptionsTests(Sku? sku, Channel? channel, string expression)
: base(sku, channel, expression)
{ }
}
IOptions GetOptions(Sku? sku = null, Channel? channel = null, string expression = null) =>
new Options(
new SkuOption(sku),
new ChannelOption("test", channel),
new ExpressionOption(expression));
}
}
13 changes: 3 additions & 10 deletions VisualStudio/CommandDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
using Mono.Options;
using vswhere;

namespace VisualStudio
{
abstract class CommandDescriptor
{
protected IOptionSet OptionSet { get; set; }
public IOptions Options { get; protected set; } = VisualStudio.Options.Empty;

public ImmutableArray<string> ExtraArguments { get; private set; } = ImmutableArray.Create<string>();

public virtual void ShowUsage(TextWriter output) =>
OptionSet?.WriteOptionDescriptions(output);
Options?.ShowUsage(output);

public virtual void Parse(IEnumerable<string> args)
{
Expand All @@ -24,17 +22,12 @@ public virtual void Parse(IEnumerable<string> args)
helpOption.Add(Environment.NewLine);
helpOption.Add("?|h|help", "Display this help", h => showHelp = h != null);

var extraArgs = OptionSet.With(helpOption).Parse(args);
var extraArgs = Options.With(helpOption).Parse(args);

if (showHelp)
throw new ShowUsageException(this);

ExtraArguments = ImmutableArray.Create(extraArgs.ToArray());
}

protected virtual VisualStudioOptions VisualStudioOptions { get; }

public virtual Task<Func<VisualStudioInstance, bool>> GetPredicateAsync() =>
new VisualStudioPredicateBuilder().BuildPredicateAsync(VisualStudioOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class ConfigCommand : Command<ConfigCommandDescriptor>
{
readonly WhereService whereService;

public ConfigCommand(ConfigCommandDescriptor descriptor, WhereService whereService) : base(descriptor)
{
public ConfigCommand(ConfigCommandDescriptor descriptor, WhereService whereService) : base(descriptor) =>
this.whereService = whereService;
}

public override async Task ExecuteAsync(TextWriter output)
{
var instances = await whereService.GetAllInstancesAsync(await Descriptor.GetPredicateAsync());
var instances = await whereService.GetAllInstancesAsync(Descriptor.Options);
var instance = new Chooser("open").Choose(instances, output);

if (instance != null)
Expand Down
13 changes: 13 additions & 0 deletions VisualStudio/Commands/ConfigCommandDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace VisualStudio
{
class ConfigCommandDescriptor : CommandDescriptor
{
readonly VisualStudioOptions vsOptions = VisualStudioOptions.Default("open").WithExperimental();

public ConfigCommandDescriptor() => Options = vsOptions;

public bool Experimental => vsOptions.IsExperimental;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace VisualStudio
Expand All @@ -12,10 +9,8 @@ class InstallCommand : Command<InstallCommandDescriptor>
{
readonly InstallerService installerService;

public InstallCommand(InstallCommandDescriptor descriptor, InstallerService installerService) : base(descriptor)
{
public InstallCommand(InstallCommandDescriptor descriptor, InstallerService installerService) : base(descriptor) =>
this.installerService = installerService;
}

public override async Task ExecuteAsync(TextWriter output)
{
Expand Down
21 changes: 21 additions & 0 deletions VisualStudio/Commands/InstallCommandDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;

namespace VisualStudio
{
class InstallCommandDescriptor : CommandDescriptor
{
readonly VisualStudioOptions vsOptions = VisualStudioOptions.Default("install");
readonly WorkloadOptions workloads = new WorkloadOptions("add", "+");

public InstallCommandDescriptor() => Options = vsOptions.With(workloads);

public Channel? Channel => vsOptions.Channel;

public Sku? Sku => vsOptions.Sku;

public string Nickname => vsOptions.Nickname;

public IEnumerable<string> WorkloadArgs => workloads.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override async Task ExecuteAsync(TextWriter output)
{
var devenvProcesses = Process.GetProcessesByName("devenv").ToList();
var targetProcesses =
(from instance in await whereService.GetAllInstancesAsync(await Descriptor.GetPredicateAsync())
(from instance in await whereService.GetAllInstancesAsync(Descriptor.Options)
from devenvProcess in devenvProcesses
where Match(devenvProcess, instance)
select devenvProcess).Distinct().ToList();
Expand Down
15 changes: 15 additions & 0 deletions VisualStudio/Commands/KillCommandDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace VisualStudio
{
class KillCommandDescriptor : CommandDescriptor
{
readonly VisualStudioOptions vsOptions = VisualStudioOptions.Default("kill").WithExperimental().WithSelectAll();

public KillCommandDescriptor() => Options = vsOptions;

public bool IsExperimental => vsOptions.IsExperimental;

public bool KillAll => vsOptions.All;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class LogCommand : Command<LogCommandDescriptor>
{
readonly WhereService whereService;

public LogCommand(LogCommandDescriptor descriptor, WhereService whereService) : base(descriptor)
{
public LogCommand(LogCommandDescriptor descriptor, WhereService whereService) : base(descriptor) =>
this.whereService = whereService;
}

public override async Task ExecuteAsync(TextWriter output)
{
var instances = await whereService.GetAllInstancesAsync(await Descriptor.GetPredicateAsync());
var instances = await whereService.GetAllInstancesAsync(Descriptor.Options);
var instance = new Chooser().Choose(instances, output);

if (instance != null)
Expand Down
13 changes: 13 additions & 0 deletions VisualStudio/Commands/LogCommandDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace VisualStudio
{
class LogCommandDescriptor : CommandDescriptor
{
readonly VisualStudioOptions vsOptions = VisualStudioOptions.Default("open").WithExperimental();

public LogCommandDescriptor() => Options = vsOptions;

public bool IsExperimental => vsOptions.IsExperimental;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class ModifyCommand : Command<ModifyCommandDescriptor>
readonly WhereService whereService;
readonly InstallerService installerService;

public ModifyCommand(ModifyCommandDescriptor descriptor, WhereService whereService, InstallerService installerService) : base(descriptor)
public ModifyCommand(ModifyCommandDescriptor descriptor, WhereService whereService, InstallerService installerService)
: base(descriptor)
{
this.whereService = whereService;
this.installerService = installerService;
}

public override async Task ExecuteAsync(TextWriter output)
{
var instances = await whereService.GetAllInstancesAsync(await Descriptor.GetPredicateAsync());
var instances = await whereService.GetAllInstancesAsync(Descriptor.Options);

var instance = new Chooser().Choose(instances, output);

Expand Down
Loading