Skip to content
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
31 changes: 18 additions & 13 deletions VisualStudio/Commands/WhereCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@ public WhereCommand(WhereCommandDescriptor descriptor, WhereService whereService

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

if (!Descriptor.ShowAll)
instances = new Chooser("show").ChooseMany(instances, output);
Descriptor.WorkloadsArguments.Concat(Descriptor.ExtraArguments))).ToList();

foreach (var instance in instances)
{
var properties = GetProperties(instance);

if (!string.IsNullOrEmpty(Descriptor.Property))
properties = properties.Where(x => x.PropertyName == Descriptor.Property);

output.WriteLine();
output.WriteLine($"{ instance.DisplayName} - Version { instance.Catalog.ProductDisplayVersion}");

foreach (var prop in properties)
output.WriteLine($"{prop.PropertyName}: {prop.PropertyValue}");
if (string.IsNullOrEmpty(Descriptor.Property))
{
output.WriteLine();
output.WriteLine($"{ instance.DisplayName} - Version { instance.Catalog.ProductDisplayVersion}");

foreach (var prop in properties)
output.WriteLine($"{prop.PropertyName}: {prop.PropertyValue}");
}
else
{
Console.WriteLine(
properties
.Where(x => x.PropertyName == Descriptor.Property)
.Select(x => x.PropertyValue)
.FirstOrDefault() ?? string.Empty);
}
}
}

Expand Down
10 changes: 4 additions & 6 deletions VisualStudio/Commands/WhereCommandDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace VisualStudio
{
class WhereCommandDescriptor : CommandDescriptor
{
readonly VisualStudioOptions vsOptions = VisualStudioOptions.Default("show").WithSelectAll();
readonly SelectPropertyOption selectProperty = new SelectPropertyOption();
readonly VisualStudioOptions vsOptions = VisualStudioOptions.Default("show");
readonly SelectPropertyOption propOption = new SelectPropertyOption();
readonly WorkloadOptions workloads = new WorkloadOptions("requires", "--", "-");

readonly WhereService whereService;
Expand All @@ -17,15 +17,13 @@ public WhereCommandDescriptor(WhereService whereService)
Description = "Locates the installed version(s) of Visual Studio that satisfy the requested requirements.";

Options = vsOptions
.With(selectProperty)
.With(propOption)
.With(workloads);

this.whereService = whereService;
}

public string Property { get; private set; }

public bool ShowAll => vsOptions.All;
public string Property => propOption.Value;

public IEnumerable<string> WorkloadsArguments => workloads.Value;

Expand Down
3 changes: 2 additions & 1 deletion VisualStudio/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public struct ErrorCodes
{
public const int ShowUsage = 1;
public const int OptionError = 2;
public const int Other = 3;
public const int WhereError = 3;
public const int Unknown = -1;
}
}
6 changes: 6 additions & 0 deletions VisualStudio/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public async Task<int> RunAsync()

return ErrorCodes.OptionError;
}
catch (WhereException ex)
{
output.WriteLine(ex.Message);

return ErrorCodes.WhereError;
}

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion VisualStudio/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"VisualStudio": {
"commandName": "Project",
"commandLineArgs": "run Enterprise --save=foo"
"commandLineArgs": "where dsadsa"
}
}
}
10 changes: 10 additions & 0 deletions VisualStudio/WhereException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace VisualStudio
{
class WhereException : Exception
{
public WhereException(string message) : base(message)
{ }
}
}
6 changes: 5 additions & 1 deletion VisualStudio/WhereService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public async Task<IEnumerable<VisualStudioInstance>> GetAllInstancesAsync(IOptio
psi.ArgumentList.Add(arg);

var process = Process.Start(psi);
var output = await process.StandardOutput.ReadToEndAsync();

if (process.ExitCode != 0)
throw new WhereException(output);

var instances = JsonSerializer.Deserialize<VisualStudioInstance[]>(
await process.StandardOutput.ReadToEndAsync(),
output,
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Expand Down