From 356a5350858cead79740834b0b2bb68074871c92 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Sun, 24 Aug 2025 13:16:02 +1000 Subject: [PATCH] use dotnet .dll and read launch settings --- .../Features/Evaluation/ProjectEvaluation.cs | 11 ++- .../Features/Run/LaunchSettingsParser.cs | 68 +++++++++++++++++++ .../Features/Run/RunService.cs | 16 ++++- .../SharpIDE.Application.csproj | 2 +- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/SharpIDE.Application/Features/Run/LaunchSettingsParser.cs diff --git a/src/SharpIDE.Application/Features/Evaluation/ProjectEvaluation.cs b/src/SharpIDE.Application/Features/Evaluation/ProjectEvaluation.cs index 0f034fc..90cf158 100644 --- a/src/SharpIDE.Application/Features/Evaluation/ProjectEvaluation.cs +++ b/src/SharpIDE.Application/Features/Evaluation/ProjectEvaluation.cs @@ -1,5 +1,6 @@ using Ardalis.GuardClauses; using Microsoft.Build.Evaluation; +using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; namespace SharpIDE.Application.Features.Evaluation; @@ -13,7 +14,15 @@ public static class ProjectEvaluation await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding); var project = _projectCollection.LoadProject(projectFilePath); - Console.WriteLine($"ProjectEvaluation: loaded {project.FullPath}"); + //Console.WriteLine($"ProjectEvaluation: loaded {project.FullPath}"); return project; } + + public static string? GetOutputDllFullPath(SharpIdeProjectModel projectModel) + { + var project = _projectCollection.GetLoadedProjects(projectModel.FilePath).Single(); + var targetPath = project.GetPropertyValue("TargetPath"); + Guard.Against.NullOrWhiteSpace(targetPath, nameof(targetPath)); + return targetPath; + } } diff --git a/src/SharpIDE.Application/Features/Run/LaunchSettingsParser.cs b/src/SharpIDE.Application/Features/Run/LaunchSettingsParser.cs new file mode 100644 index 0000000..4d7d547 --- /dev/null +++ b/src/SharpIDE.Application/Features/Run/LaunchSettingsParser.cs @@ -0,0 +1,68 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; + +namespace SharpIDE.Application.Features.Run; + +public static class LaunchSettingsParser +{ + private static readonly JsonSerializerOptions _jsonSerializerOptions = new() + { + PropertyNameCaseInsensitive = true, + ReadCommentHandling = JsonCommentHandling.Skip + }; + + public static async Task> GetLaunchSettingsProfiles(SharpIdeProjectModel projectModel) + { + var launchSettingsFilePath = Path.Combine(Path.GetDirectoryName(projectModel.FilePath)!, "Properties", "launchSettings.json"); + var launchSettingsFile = new FileInfo(launchSettingsFilePath); + if (launchSettingsFile.Exists is false) + { + return []; + } + + await using var stream = launchSettingsFile.OpenRead(); + var launchSettings = await JsonSerializer.DeserializeAsync(stream, _jsonSerializerOptions); + if (launchSettings is null) return []; + + var result = launchSettings.Profiles.Select(s => new ProjectLaunchSettingsModel + { + LaunchProfileName = s.Key, + CommandLineArgs = s.Value.CommandName, + DotNetRunMessages = s.Value.DotnetRunMessages, + LaunchBrowser = s.Value.LaunchBrowser, + LaunchUrl = s.Value.LaunchUrl, + ApplicationUrl = s.Value.ApplicationUrl, + EnvironmentVariables = s.Value.EnvironmentVariables + }).ToList(); + return result; + } +} + +public class ProjectLaunchSettingsModel +{ + public required string? LaunchProfileName { get; set; } + public required string? CommandLineArgs { get; set; } + public required bool LaunchBrowser { get; set; } + public required string? LaunchUrl { get; set; } + public required string? ApplicationUrl { get; set; } + public required bool DotNetRunMessages { get; set; } + public required Dictionary EnvironmentVariables { get; init; } +} + +// Json models +public class LaunchSettings +{ + public required Dictionary Profiles { get; set; } +} + +public class Profile +{ + public string CommandName { get; set; } + public bool DotnetRunMessages { get; set; } + public bool LaunchBrowser { get; set; } + public string LaunchUrl { get; set; } + public string ApplicationUrl { get; set; } + public Dictionary EnvironmentVariables { get; set; } +} + diff --git a/src/SharpIDE.Application/Features/Run/RunService.cs b/src/SharpIDE.Application/Features/Run/RunService.cs index 73a99de..f77d942 100644 --- a/src/SharpIDE.Application/Features/Run/RunService.cs +++ b/src/SharpIDE.Application/Features/Run/RunService.cs @@ -2,6 +2,7 @@ using System.Threading.Channels; using Ardalis.GuardClauses; using AsyncReadProcess; +using SharpIDE.Application.Features.Evaluation; using SharpIDE.Application.Features.Events; using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; @@ -22,16 +23,29 @@ public class RunService if (project.RunningCancellationTokenSource is not null) throw new InvalidOperationException($"Project {project.Name} is already running with a cancellation token source."); project.RunningCancellationTokenSource = new CancellationTokenSource(); + var dllFullPath = ProjectEvaluation.GetOutputDllFullPath(project); + var launchProfiles = await LaunchSettingsParser.GetLaunchSettingsProfiles(project); + var launchProfile = launchProfiles.FirstOrDefault(); try { var processStartInfo = new ProcessStartInfo2 { FileName = "dotnet", - Arguments = $"run --project \"{project.FilePath}\" --no-restore", + WorkingDirectory = Path.GetDirectoryName(project.FilePath), + //Arguments = $"run --project \"{project.FilePath}\" --no-restore", + Arguments = $"\"{dllFullPath}\"", RedirectStandardOutput = true, RedirectStandardError = true, EnvironmentVariables = [] }; + if (launchProfile is not null) + { + foreach (var envVar in launchProfile.EnvironmentVariables) + { + processStartInfo.EnvironmentVariables[envVar.Key] = envVar.Value; + } + if (launchProfile.ApplicationUrl != null) processStartInfo.EnvironmentVariables["ASPNETCORE_URLS"] = launchProfile.ApplicationUrl; + } var process = new Process2 { diff --git a/src/SharpIDE.Application/SharpIDE.Application.csproj b/src/SharpIDE.Application/SharpIDE.Application.csproj index c976ad9..39baa2d 100644 --- a/src/SharpIDE.Application/SharpIDE.Application.csproj +++ b/src/SharpIDE.Application/SharpIDE.Application.csproj @@ -16,7 +16,7 @@ - +