Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/Aspire.Cli/Aspire.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<PackageReference Include="Spectre.Console" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="StreamJsonRpc" VersionOverride="$(StreamJsonRpcPackageVersionForCli)" />
Expand Down
101 changes: 101 additions & 0 deletions src/Aspire.Cli/Commands/LsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using System.Text.Json;
using Aspire.Cli.Configuration;
using Aspire.Cli.Interaction;
using Aspire.Cli.Projects;
using Aspire.Cli.Resources;
using Aspire.Cli.Telemetry;
using Aspire.Cli.Utils;
using Spectre.Console;

namespace Aspire.Cli.Commands;

internal sealed class LsCommand : BaseCommand
{
internal override HelpGroup HelpGroup => HelpGroup.AppCommands;

private readonly IInteractionService _interactionService;
private readonly IProjectLocator _projectLocator;
private readonly CliExecutionContext _executionContext;

private static readonly Option<OutputFormat> s_formatOption = new("--format")
{
Description = SharedCommandStrings.LsFormatOptionDescription
};

public LsCommand(
IInteractionService interactionService,
IProjectLocator projectLocator,
IFeatures features,
ICliUpdateNotifier updateNotifier,
CliExecutionContext executionContext,
AspireCliTelemetry telemetry)
: base("ls", SharedCommandStrings.LsCommandDescription, features, updateNotifier, executionContext, interactionService, telemetry)
{
_interactionService = interactionService;
_projectLocator = projectLocator;
_executionContext = executionContext;

Options.Add(s_formatOption);
}

protected override async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
using var activity = Telemetry.StartDiagnosticActivity(Name);

var format = parseResult.GetValue(s_formatOption);
var appHosts = await _projectLocator.FindAppHostProjectsAsync(_executionContext.WorkingDirectory, cancellationToken).ConfigureAwait(false);
var appHostInfos = appHosts.Select(a => new CandidateAppHostDisplayInfo
{
RelativePath = System.IO.Path.GetRelativePath(_executionContext.WorkingDirectory.FullName, a.AppHostFile.FullName),
Path = a.AppHostFile.FullName,
Language = a.Language
}).ToList();

if (format == OutputFormat.Json)
{
var json = JsonSerializer.Serialize(appHostInfos, JsonSourceGenerationContext.RelaxedEscaping.ListCandidateAppHostDisplayInfo);
_interactionService.DisplayRawText(json, ConsoleOutput.Standard);
}
else if (appHostInfos.Count == 0)
{
_interactionService.DisplayMessage(KnownEmojis.Information, SharedCommandStrings.LsNoCandidateAppHostsFound);
}
else
{
DisplayTable(appHostInfos);
}

return ExitCodeConstants.Success;
}

private void DisplayTable(List<CandidateAppHostDisplayInfo> appHosts)
{
var table = new Table();
table.AddBoldColumn(SharedCommandStrings.HeaderRelativePath);
table.AddBoldColumn(SharedCommandStrings.HeaderPath);
table.AddBoldColumn(SharedCommandStrings.HeaderLanguage);

foreach (var appHost in appHosts)
{
table.AddRow(
Markup.Escape(appHost.RelativePath),
Markup.Escape(appHost.Path),
Markup.Escape(appHost.Language));
}

_interactionService.DisplayRenderable(table);
}
}

internal sealed class CandidateAppHostDisplayInfo
{
public required string RelativePath { get; init; }

public required string Path { get; init; }

public required string Language { get; init; }
}
2 changes: 2 additions & 0 deletions src/Aspire.Cli/Commands/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public RootCommand(
StopCommand stopCommand,
StartCommand startCommand,
WaitCommand waitCommand,
LsCommand lsCommand,
ResourceCommand commandCommand,
PsCommand psCommand,
DescribeCommand describeCommand,
Expand Down Expand Up @@ -205,6 +206,7 @@ public RootCommand(
Subcommands.Add(stopCommand);
Subcommands.Add(startCommand);
Subcommands.Add(waitCommand);
Subcommands.Add(lsCommand);
Subcommands.Add(commandCommand);
Subcommands.Add(psCommand);
Subcommands.Add(describeCommand);
Expand Down
7 changes: 6 additions & 1 deletion src/Aspire.Cli/DotNet/DotNetCliRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ private async Task StartBackchannelAsync(IProcessExecution? execution, string so
cliArgsList.Add(projectFile.FullName);

string[] cliArgs = [.. cliArgsList];
var env = new Dictionary<string, string>
{
[KnownConfigNames.DotnetCliTelemetryOptOut] = "1",
[KnownConfigNames.DotnetCliWorkloadUpdateNotifyDisable] = "1"
};

var existingStandardOutputCallback = options.StandardOutputCallback;
var existingStandardErrorCallback = options.StandardErrorCallback;
Expand All @@ -440,7 +445,7 @@ private async Task StartBackchannelAsync(IProcessExecution? execution, string so

var exitCode = await ExecuteAsync(
args: cliArgs,
env: null,
env: env,
projectFile: projectFile,
workingDirectory: projectFile.Directory!,
backchannelCompletionSource: null,
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Cli/JsonSourceGenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace Aspire.Cli;
[JsonSerializable(typeof(ApiListItem[]))]
[JsonSerializable(typeof(ApiSearchResult[]))]
[JsonSerializable(typeof(ApiContent))]
[JsonSerializable(typeof(List<CandidateAppHostDisplayInfo>))]
internal partial class JsonSourceGenerationContext : JsonSerializerContext
{
private static JsonSourceGenerationContext? s_relaxedEscaping;
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ internal static async Task<IHost> BuildApplicationAsync(string[] args, CliStartu
builder.Services.AddTransient<StopCommand>();
builder.Services.AddTransient<StartCommand>();
builder.Services.AddTransient<WaitCommand>();
builder.Services.AddTransient<LsCommand>();
builder.Services.AddTransient<ResourceCommand>();
builder.Services.AddTransient<PsCommand>();
builder.Services.AddTransient<DescribeCommand>();
Expand Down
Loading
Loading