Skip to content

Commit

Permalink
childprocessservice
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Nov 15, 2023
1 parent bd6de9b commit 2af1f6b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
80 changes: 80 additions & 0 deletions src/OneWare.Core/Services/ChildProcessService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Diagnostics;
using Avalonia.Media;
using OneWare.Shared.Enums;
using OneWare.Shared.Services;

namespace OneWare.Core.Services;

public class ChildProcessService : IChildProcessService
{
private readonly ILogger _logger;
private readonly IActive _active;

public ChildProcessService(ILogger logger, IActive active)
{
_logger = logger;
_active = active;
}

private static ProcessStartInfo GetProcessStartInfo(string workingDirectory, string path, string arguments)
{
return new ProcessStartInfo
{
FileName = path,
Arguments = $"{arguments}",
CreateNoWindow = true,
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
}

public async Task<(bool success, string output)> ExecuteShellAsync(string path, string arguments, string workingDirectory, string status, AppState state = AppState.Loading)
{
var success = true;

_logger.Log($"{Path.GetFileNameWithoutExtension(path)} {arguments}", ConsoleColor.DarkCyan, true, Brushes.CornflowerBlue);

var output = string.Empty;

var startInfo = GetProcessStartInfo(path, workingDirectory, arguments);

using var activeProcess = new Process();
activeProcess.StartInfo = startInfo;
var key = _active.AddState(status, state, activeProcess);

activeProcess.OutputDataReceived += (o, i) =>
{
if (string.IsNullOrEmpty(i.Data)) return;
_logger.Log(i.Data, ConsoleColor.Black, true);
output += i.Data + '\n';
};
activeProcess.ErrorDataReceived += (o, i) =>
{
if (string.IsNullOrEmpty(i.Data)) return;
success = false;
_logger.Warning(i.Data, null, false);
output += i.Data + '\n';
};

try
{
activeProcess.Start();
activeProcess.BeginOutputReadLine();
activeProcess.BeginErrorReadLine();

await Task.Run(() => activeProcess.WaitForExit());
}
catch (Exception e)
{
_logger.Error(e.Message, e);
success = false;
}

if (key.Terminated) success = false;
_active.RemoveState(key);

return (success,output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

<ItemGroup>
<ProjectReference Include="..\OneWare.Shared\OneWare.Shared.csproj" />
<ProjectReference Include="..\OneWare.UniversalFpgaProjectSystem\OneWare.UniversalFpgaProjectSystem.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void OnInitialized(IContainerProvider containerProvider)
var currentPath = Environment.GetEnvironmentVariable("PATH");

//TODO Add all
Environment.SetEnvironmentVariable("PATH", $"{currentPath}{environmentPathSetting}");
Environment.SetEnvironmentVariable("PATH", $"{environmentPathSetting}{currentPath}");
Environment.SetEnvironmentVariable("OPENFPGALOADER_SOJ_DIR", Path.Combine(x, "share", "openFPGALoader"));
Environment.SetEnvironmentVariable("PYTHON_EXECUTABLE", Path.Combine(x, "py3bin", $"python3{PlatformHelper.ExecutableExtension}"));
});
Expand Down
27 changes: 27 additions & 0 deletions src/OneWare.OssCadSuiteIntegration/Yosys/YosysService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using OneWare.Shared.Services;
using OneWare.UniversalFpgaProjectSystem.Models;

namespace OneWare.OssCadSuiteIntegration.Yosys;

public class YosysService
{
private readonly IChildProcessService _childProcessService;

public YosysService(IChildProcessService childProcessService)
{
_childProcessService = childProcessService;
}

public async Task SynthAsync(UniversalFpgaProjectRoot project)
{
var fpga = project.Properties["Fpga"];
var top = project.Properties["TopEntity"];

var verilogFiles = string.Join(" ", project.Files.Where(x => x.Extension == ".v"));
var yosysFlags = string.Empty;

await _childProcessService.ExecuteShellAsync("yosys",
$"yosys -p \"synth_${fpga} -top ${top} -json ${project.FullPath}/synth.json\" ${yosysFlags} ${verilogFiles}",
project.FullPath, "Yosys Synth");
}
}
9 changes: 9 additions & 0 deletions src/OneWare.Shared/Services/IChildProcessService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OneWare.Shared.Enums;

namespace OneWare.Shared.Services;

public interface IChildProcessService
{
public Task<(bool success, string output)> ExecuteShellAsync(string path, string arguments, string workingDirectory,
string status, AppState state = AppState.Loading);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace OneWare.UniversalFpgaProjectSystem.ViewModels;
public class UniversalFpgaProjectCompileViewModel : FlexibleWindowViewModelBase
{
private readonly IWindowService _windowService;
private readonly IProjectExplorerService _projectExplorerService;
private readonly UniversalFpgaProjectRoot _project;

public ObservableCollection<FpgaModelBase> FpgaModels { get; } = new();
Expand All @@ -33,9 +34,10 @@ public bool HideExtensions
set => SetProperty(ref _hideExtensions, value);
}

public UniversalFpgaProjectCompileViewModel(IWindowService windowService, FpgaService fpgaService, NodeProviderService nodeProviderService, UniversalFpgaProjectRoot project)
public UniversalFpgaProjectCompileViewModel(IWindowService windowService, IProjectExplorerService projectExplorerService, FpgaService fpgaService, NodeProviderService nodeProviderService, UniversalFpgaProjectRoot project)
{
_windowService = windowService;
_projectExplorerService = projectExplorerService;
_project = project;

this.WhenValueChanged(x => x.IsDirty).Subscribe(x =>
Expand Down Expand Up @@ -97,7 +99,9 @@ private async Task SafeQuitAsync(FlexibleWindow window)

public void SaveAndClose(FlexibleWindow window)
{
_project.Properties["Fpga"] = SelectedFpga?.Name;
CreatePcf();
_ = _projectExplorerService.SaveProjectAsync(_project);
IsDirty = false;
window.Close();
}
Expand Down

0 comments on commit 2af1f6b

Please sign in to comment.