2024-07-21 04:38:31 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Nerfed.Compiler;
|
|
|
|
|
|
|
|
|
|
public static class Compiler
|
|
|
|
|
{
|
|
|
|
|
public static bool Compile(string projectFilePath, string configuration = "Debug")
|
|
|
|
|
{
|
2024-07-23 22:31:19 +02:00
|
|
|
|
string projectDirectory = Path.GetDirectoryName(projectFilePath);
|
2024-07-21 04:38:31 +02:00
|
|
|
|
|
|
|
|
|
if (!File.Exists(projectFilePath))
|
|
|
|
|
{
|
2024-07-23 22:31:19 +02:00
|
|
|
|
Console.WriteLine($"ERROR: Project file not found at {projectDirectory}.");
|
2024-07-21 04:38:31 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Project.Open(projectFilePath, out Project project))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Check project version, to make sure we can compile it or something...
|
|
|
|
|
|
|
|
|
|
// Generate solution.
|
2024-07-23 22:31:19 +02:00
|
|
|
|
Generator.GenerateSolution(projectDirectory, project, out string solutionFilePath);
|
2024-07-21 04:38:31 +02:00
|
|
|
|
|
|
|
|
|
// Compile solution.
|
|
|
|
|
ProcessStartInfo processInfo = new()
|
|
|
|
|
{
|
|
|
|
|
WorkingDirectory = Path.GetDirectoryName(solutionFilePath),
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
|
|
|
{
|
|
|
|
|
processInfo.FileName = "/bin/bash";
|
|
|
|
|
processInfo.Arguments = $"-c \"dotnet build '{Path.GetFileName(solutionFilePath)}'\"" + (string.IsNullOrWhiteSpace(configuration) ? $" --configuration {configuration}" : "");
|
|
|
|
|
}
|
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
processInfo.FileName = "cmd.exe";
|
|
|
|
|
processInfo.Arguments = $"/c dotnet build \"{Path.GetFileName(solutionFilePath)}\"" + (string.IsNullOrWhiteSpace(configuration) ? $" --configuration {configuration}" : "");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-21 14:03:40 +02:00
|
|
|
|
Console.WriteLine($"ERROR: Platform not supported!");
|
2024-07-21 04:38:31 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Process process = Process.Start(processInfo) ?? throw new Exception();
|
|
|
|
|
process.OutputDataReceived += (sender, dataArgs) => {
|
|
|
|
|
string data = dataArgs.Data;
|
|
|
|
|
|
|
|
|
|
if (data is null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.BeginOutputReadLine();
|
|
|
|
|
process.BeginErrorReadLine();
|
|
|
|
|
process.ErrorDataReceived += (sender, dataArgs) => {
|
|
|
|
|
if (dataArgs.Data is not null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(dataArgs.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
|
|
|
|
|
int exitCode = process.ExitCode;
|
|
|
|
|
process.Close();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-07-23 22:31:19 +02:00
|
|
|
|
}
|