run project from context menu
This commit is contained in:
45
src/SharpIDE.Application/Features/Run/RunService.cs
Normal file
45
src/SharpIDE.Application/Features/Run/RunService.cs
Normal file
@@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
@using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence
|
||||||
|
|
||||||
|
@inject RunService RunService
|
||||||
|
|
||||||
@if (SolutionModel is null)
|
@if (SolutionModel is null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -28,7 +32,7 @@
|
|||||||
</MudTreeView>
|
</MudTreeView>
|
||||||
|
|
||||||
<MudMenu PositionAtCursor @ref="_contextMenuRef" Dense="true" Modal="false">
|
<MudMenu PositionAtCursor @ref="_contextMenuRef" Dense="true" Modal="false">
|
||||||
<MudMenuItem Icon="@Icons.Material.Filled.PlayArrow" IconColor="Color.Success" Label="Run" />
|
<MudMenuItem Icon="@Icons.Material.Filled.PlayArrow" IconColor="Color.Success" Label="Run" OnClick="@RunProject" />
|
||||||
<MudMenuItem Label="Build" />
|
<MudMenuItem Label="Build" />
|
||||||
<MudMenuItem Label="Rebuild" />
|
<MudMenuItem Label="Rebuild" />
|
||||||
<MudMenuItem Label="Clean" />
|
<MudMenuItem Label="Clean" />
|
||||||
@@ -46,6 +50,7 @@
|
|||||||
public EventCallback<SharpIdeFile> SelectedFileChanged { get; set; }
|
public EventCallback<SharpIdeFile> SelectedFileChanged { get; set; }
|
||||||
|
|
||||||
private MudMenu _contextMenuRef = null!;
|
private MudMenu _contextMenuRef = null!;
|
||||||
|
private SharpIdeProjectModel? _contextMenuProject;
|
||||||
|
|
||||||
private async Task InvokeSelectedFileChanged(SharpIdeFile file)
|
private async Task InvokeSelectedFileChanged(SharpIdeFile file)
|
||||||
{
|
{
|
||||||
@@ -55,10 +60,17 @@
|
|||||||
|
|
||||||
private async Task OpenProjectContextMenu(MouseEventArgs args, SharpIdeProjectModel project)
|
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);
|
await _contextMenuRef.OpenMenuAsync(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task RunProject()
|
||||||
|
{
|
||||||
|
Guard.Against.Null(_contextMenuProject, nameof(_contextMenuProject));
|
||||||
|
await RunService.RunProject(_contextMenuProject);
|
||||||
|
}
|
||||||
|
|
||||||
private RenderFragment GetSolutionFolderFragment(SharpIdeSolutionFolder slnFolder) =>
|
private RenderFragment GetSolutionFolderFragment(SharpIdeSolutionFolder slnFolder) =>
|
||||||
@<text>
|
@<text>
|
||||||
<MudTreeViewItem T="ISharpIdeNode" TextTypo="Typo.body2" Icon="@Icons.Material.Filled.Folder" IconColor="Color.Primary" Value="@slnFolder" Text="@slnFolder.Name" @bind-Expanded="@slnFolder.Expanded">
|
<MudTreeViewItem T="ISharpIdeNode" TextTypo="Typo.body2" Icon="@Icons.Material.Filled.Folder" IconColor="Color.Primary" Value="@slnFolder" Text="@slnFolder.Name" @bind-Expanded="@slnFolder.Expanded">
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using MudBlazor.Services;
|
using MudBlazor.Services;
|
||||||
using Photino.Blazor;
|
using Photino.Blazor;
|
||||||
using SharpIDE.Application.Features.Build;
|
using SharpIDE.Application.Features.Build;
|
||||||
|
using SharpIDE.Application.Features.Run;
|
||||||
using SharpIDE.Photino.Models;
|
using SharpIDE.Photino.Models;
|
||||||
using SharpIDE.Photino.Services;
|
using SharpIDE.Photino.Services;
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ public class Program
|
|||||||
appBuilder.Services.AddSingleton<RefreshOpenFileService>();
|
appBuilder.Services.AddSingleton<RefreshOpenFileService>();
|
||||||
appBuilder.Services.AddSingleton<AppState>();
|
appBuilder.Services.AddSingleton<AppState>();
|
||||||
appBuilder.Services.AddSingleton<BuildService>();
|
appBuilder.Services.AddSingleton<BuildService>();
|
||||||
|
appBuilder.Services.AddSingleton<RunService>();
|
||||||
|
|
||||||
appBuilder.RootComponents.Add<App>("app");
|
appBuilder.RootComponents.Add<App>("app");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user