From fa78ce4c8c0779964d6be0d7f67df48ac6f19f24 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Sun, 3 Aug 2025 02:31:16 +1000 Subject: [PATCH] run project from context menu --- .../Features/Run/RunService.cs | 45 +++++++++++++++++++ .../Components/SolutionExplorer.razor | 18 ++++++-- src/SharpIDE.Photino/Program.cs | 2 + 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/SharpIDE.Application/Features/Run/RunService.cs diff --git a/src/SharpIDE.Application/Features/Run/RunService.cs b/src/SharpIDE.Application/Features/Run/RunService.cs new file mode 100644 index 0000000..9baa7f8 --- /dev/null +++ b/src/SharpIDE.Application/Features/Run/RunService.cs @@ -0,0 +1,45 @@ +using System.Diagnostics; +using Ardalis.GuardClauses; +using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; + +namespace SharpIDE.Application.Features.Run; + +public class RunService +{ + // TODO: optimise this Copilot junk + public async Task RunProject(SharpIdeProjectModel project) + { + Guard.Against.Null(project, nameof(project)); + Guard.Against.NullOrWhiteSpace(project.FilePath, nameof(project.FilePath), "Project file path cannot be null or empty."); + + var processStartInfo = new ProcessStartInfo + { + FileName = "dotnet", + Arguments = $"run --project \"{project.FilePath}\"", + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + + using var process = new Process(); + process.StartInfo = processStartInfo; + + process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data); + process.ErrorDataReceived += (sender, e) => Console.Error.WriteLine(e.Data); + + process.Start(); + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + await process.WaitForExitAsync(); + + if (process.ExitCode != 0) + { + throw new InvalidOperationException($"Project run failed with exit code {process.ExitCode}."); + } + + Console.WriteLine("Project ran successfully."); + } +} diff --git a/src/SharpIDE.Photino/Components/SolutionExplorer.razor b/src/SharpIDE.Photino/Components/SolutionExplorer.razor index 42f7a65..a3dba48 100644 --- a/src/SharpIDE.Photino/Components/SolutionExplorer.razor +++ b/src/SharpIDE.Photino/Components/SolutionExplorer.razor @@ -1,6 +1,10 @@ -@using SharpIDE.Application.Features.SolutionDiscovery +@using Ardalis.GuardClauses +@using SharpIDE.Application.Features.Run +@using SharpIDE.Application.Features.SolutionDiscovery @using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence +@inject RunService RunService + @if (SolutionModel is null) { return; @@ -28,7 +32,7 @@ - + @@ -46,6 +50,7 @@ public EventCallback SelectedFileChanged { get; set; } private MudMenu _contextMenuRef = null!; + private SharpIdeProjectModel? _contextMenuProject; private async Task InvokeSelectedFileChanged(SharpIdeFile file) { @@ -55,10 +60,17 @@ private async Task OpenProjectContextMenu(MouseEventArgs args, SharpIdeProjectModel project) { - Console.WriteLine($"Opening context menu for project: {project.Name}"); + _contextMenuProject = null; + _contextMenuProject = project; await _contextMenuRef.OpenMenuAsync(args); } + private async Task RunProject() + { + Guard.Against.Null(_contextMenuProject, nameof(_contextMenuProject)); + await RunService.RunProject(_contextMenuProject); + } + private RenderFragment GetSolutionFolderFragment(SharpIdeSolutionFolder slnFolder) => @ diff --git a/src/SharpIDE.Photino/Program.cs b/src/SharpIDE.Photino/Program.cs index d8fde98..af35773 100644 --- a/src/SharpIDE.Photino/Program.cs +++ b/src/SharpIDE.Photino/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using MudBlazor.Services; using Photino.Blazor; using SharpIDE.Application.Features.Build; +using SharpIDE.Application.Features.Run; using SharpIDE.Photino.Models; using SharpIDE.Photino.Services; @@ -21,6 +22,7 @@ public class Program appBuilder.Services.AddSingleton(); appBuilder.Services.AddSingleton(); appBuilder.Services.AddSingleton(); + appBuilder.Services.AddSingleton(); appBuilder.RootComponents.Add("app");