-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
460 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/BuiltInTools/dotnet-watch/Aspire/AspireServerService.Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.DotNet.Watcher; | ||
|
||
namespace Microsoft.WebTools.AspireServer; | ||
|
||
internal partial class AspireServerService : IRuntimeProcessLauncher | ||
{ | ||
public bool SupportsPartialRestart => false; | ||
|
||
public async ValueTask<IEnumerable<(string name, string value)>> GetEnvironmentVariablesAsync(CancellationToken cancelationToken) | ||
{ | ||
var environment = await GetServerConnectionEnvironmentAsync(cancelationToken).ConfigureAwait(false); | ||
return environment.Select(kvp => (kvp.Key, kvp.Value)); | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
src/BuiltInTools/dotnet-watch/Aspire/AspireServiceFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Microsoft.Build.Graph; | ||
using Microsoft.DotNet.Watcher.Tools; | ||
using Microsoft.Extensions.Tools.Internal; | ||
using Microsoft.WebTools.AspireServer; | ||
using Microsoft.WebTools.AspireServer.Contracts; | ||
|
||
namespace Microsoft.DotNet.Watcher; | ||
|
||
internal class AspireServiceFactory : IRuntimeProcessLauncherFactory | ||
{ | ||
private sealed class ServerEvents(ProjectLauncher projectLauncher, IReadOnlyList<(string name, string value)> buildProperties) : IAspireServerEvents | ||
{ | ||
/// <summary> | ||
/// Lock to access: | ||
/// <see cref="_sessions"/> | ||
/// <see cref="_sessionIdDispenser"/> | ||
/// </summary> | ||
private readonly object _guard = new(); | ||
|
||
private readonly Dictionary<string, RunningProject> _sessions = []; | ||
private int _sessionIdDispenser; | ||
|
||
private IReporter Reporter | ||
=> projectLauncher.Reporter; | ||
|
||
/// <summary> | ||
/// Implements https://github.com/dotnet/aspire/blob/445d2fc8a6a0b7ce3d8cc42def4d37b02709043b/docs/specs/IDE-execution.md#create-session-request. | ||
/// </summary> | ||
public async ValueTask<string> StartProjectAsync(string dcpId, ProjectLaunchRequest projectLaunchInfo, CancellationToken cancellationToken) | ||
{ | ||
Reporter.Verbose($"Starting project: {projectLaunchInfo.ProjectPath}", MessageEmoji); | ||
|
||
var projectOptions = GetProjectOptions(projectLaunchInfo); | ||
|
||
var processTerminationSource = new CancellationTokenSource(); | ||
|
||
var runningProject = await projectLauncher.TryLaunchProcessAsync(projectOptions, processTerminationSource, build: false, cancellationToken); | ||
if (runningProject == null) | ||
{ | ||
// detailed error already reported: | ||
throw new ApplicationException($"Failed to launch project '{projectLaunchInfo.ProjectPath}'."); | ||
} | ||
|
||
string sessionId; | ||
lock (_guard) | ||
{ | ||
sessionId = _sessionIdDispenser++.ToString(CultureInfo.InvariantCulture); | ||
_sessions.Add(sessionId, runningProject); | ||
} | ||
|
||
Reporter.Verbose($"Session started: {sessionId}"); | ||
return sessionId; | ||
} | ||
|
||
/// <summary> | ||
/// Implements https://github.com/dotnet/aspire/blob/445d2fc8a6a0b7ce3d8cc42def4d37b02709043b/docs/specs/IDE-execution.md#stop-session-request. | ||
/// </summary> | ||
public async ValueTask<bool> StopSessionAsync(string dcpId, string sessionId, CancellationToken cancellationToken) | ||
{ | ||
Reporter.Verbose($"Stop Session {sessionId}", MessageEmoji); | ||
|
||
RunningProject? runningProject; | ||
lock (_guard) | ||
{ | ||
if (!_sessions.TryGetValue(sessionId, out runningProject)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
_ = await projectLauncher.TerminateProcessesAsync([runningProject.ProjectNode.ProjectInstance.FullPath], cancellationToken); | ||
return true; | ||
} | ||
|
||
private ProjectOptions GetProjectOptions(ProjectLaunchRequest projectLaunchInfo) | ||
{ | ||
var arguments = new List<string> | ||
{ | ||
"--project", | ||
projectLaunchInfo.ProjectPath, | ||
// TODO: https://github.com/dotnet/sdk/issues/43946 | ||
// Need to suppress launch profile for now, otherwise it would override the port set via env variable. | ||
"--no-launch-profile", | ||
}; | ||
|
||
//if (projectLaunchInfo.DisableLaunchProfile) | ||
//{ | ||
// arguments.Add("--no-launch-profile"); | ||
//} | ||
//else if (!string.IsNullOrEmpty(projectLaunchInfo.LaunchProfile)) | ||
//{ | ||
// arguments.Add("--launch-profile"); | ||
// arguments.Add(projectLaunchInfo.LaunchProfile); | ||
//} | ||
|
||
if (projectLaunchInfo.Arguments != null) | ||
{ | ||
arguments.AddRange(projectLaunchInfo.Arguments); | ||
} | ||
|
||
return new() | ||
{ | ||
IsRootProject = false, | ||
ProjectPath = projectLaunchInfo.ProjectPath, | ||
WorkingDirectory = projectLauncher.EnvironmentOptions.WorkingDirectory, // TODO: Should DCP protocol specify? | ||
BuildProperties = buildProperties, // TODO: Should DCP protocol specify? | ||
Command = "run", | ||
CommandArguments = arguments, | ||
LaunchEnvironmentVariables = projectLaunchInfo.Environment?.Select(kvp => (kvp.Key, kvp.Value)).ToArray() ?? [], | ||
LaunchProfileName = projectLaunchInfo.LaunchProfile, | ||
NoLaunchProfile = projectLaunchInfo.DisableLaunchProfile, | ||
TargetFramework = null, // TODO: Should DCP protocol specify? | ||
}; | ||
} | ||
} | ||
|
||
public const string MessageEmoji = "⭐"; | ||
|
||
public static readonly AspireServiceFactory Instance = new(); | ||
public const string AppHostProjectCapability = "Aspire"; | ||
|
||
public IRuntimeProcessLauncher? TryCreate(ProjectGraphNode projectNode, ProjectLauncher projectLauncher, IReadOnlyList<(string name, string value)> buildProperties) | ||
{ | ||
if (!projectNode.GetCapabilities().Contains(AppHostProjectCapability)) | ||
{ | ||
return null; | ||
} | ||
|
||
// TODO: implement notifications: | ||
// 1) Process restarted notification | ||
// 2) Session terminated notification | ||
return new AspireServerService(new ServerEvents(projectLauncher, buildProperties), displayName: ".NET Watch Aspire Server", m => projectLauncher.Reporter.Verbose(m, MessageEmoji)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
test/TestAssets/TestProjects/WatchAspire/WatchAspire.ApiService/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add service defaults & Aspire components. | ||
builder.AddServiceDefaults(); | ||
|
||
// Add services to the container. | ||
builder.Services.AddProblemDetails(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
app.UseExceptionHandler(); | ||
|
||
var summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
app.MapGet("/weatherforecast", () => | ||
{ | ||
var forecast = Enumerable.Range(1, 5).Select(index => | ||
new WeatherForecast | ||
( | ||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
Random.Shared.Next(-20, 55), | ||
summaries[Random.Shared.Next(summaries.Length)] | ||
)) | ||
.ToArray(); | ||
return forecast; | ||
}); | ||
|
||
app.MapDefaultEndpoints(); | ||
|
||
app.Run(); | ||
|
||
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | ||
{ | ||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
} |
15 changes: 15 additions & 0 deletions
15
...TestAssets/TestProjects/WatchAspire/WatchAspire.ApiService/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "http://localhost:5303", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../TestAssets/TestProjects/WatchAspire/WatchAspire.ApiService/WatchAspire.ApiService.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>$(CurrentTargetFramework)</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WatchAspire.ServiceDefaults\WatchAspire.ServiceDefaults.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
test/TestAssets/TestProjects/WatchAspire/WatchAspire.ApiService/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/TestAssets/TestProjects/WatchAspire/WatchAspire.ApiService/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
5 changes: 5 additions & 0 deletions
5
test/TestAssets/TestProjects/WatchAspire/WatchAspire.AppHost/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
builder.AddProject<Projects.WatchAspire_ApiService>("apiservice"); | ||
|
||
builder.Build().Run(); |
30 changes: 30 additions & 0 deletions
30
test/TestAssets/TestProjects/WatchAspire/WatchAspire.AppHost/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "https://localhost:17211;http://localhost:15137", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21185", | ||
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22024" | ||
} | ||
}, | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:15137", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"DOTNET_ENVIRONMENT": "Development", | ||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19235", | ||
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20033", | ||
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true" | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/TestAssets/TestProjects/WatchAspire/WatchAspire.AppHost/WatchAspire.AppHost.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>$(CurrentTargetFramework)</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsAspireHost>true</IsAspireHost> | ||
<UserSecretsId>ad800ccc-954c-40cc-920b-2e09fc9eee7a</UserSecretsId> | ||
<!-- workaround --> | ||
<AspireHostingSDKVersion>9.0.0</AspireHostingSDKVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WatchAspire.ApiService\WatchAspire.ApiService.csproj" Watch="true" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting.AppHost" Version="$(MicrosoftNETSdkAspireManifest80100PackageVersion)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
test/TestAssets/TestProjects/WatchAspire/WatchAspire.AppHost/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
Oops, something went wrong.