renamed some files and structure

This commit is contained in:
max 2024-07-23 22:31:19 +02:00
parent 50a77d5120
commit b9f5a4c56b
6 changed files with 242 additions and 225 deletions

View File

@ -0,0 +1,64 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Nerfed.Compiler;
public class AssemblyDefinition
{
public string Name { get; set; }
public string Guid { get; set; }
//public bool IsEditor { get; set; }
// Add platform stuff here..?
// Add dll's here..?
// Add dependencies here..?
public static bool Create(string assemblyDefinitionFilePath, string name, out AssemblyDefinition assemblyDefinition)
{
assemblyDefinition = null;
if (File.Exists(assemblyDefinitionFilePath))
{
Console.WriteLine($"ERROR: File already exists!");
return false;
}
// Create project file.
assemblyDefinition = new AssemblyDefinition
{
Name = name,
Guid = System.Guid.NewGuid().ToString("B").ToUpper(),
};
Save(assemblyDefinition, assemblyDefinitionFilePath);
return true;
}
public static bool Save(AssemblyDefinition assemblyDefinition, string assemblyDefinitionFilePath)
{
string jsonString = JsonSerializer.Serialize(assemblyDefinition, AssemblyDefinitionContext.Default.AssemblyDefinition);
File.WriteAllText(assemblyDefinitionFilePath, jsonString);
return true;
}
public static bool Open(string assemblyDefinitionFilePath, out AssemblyDefinition assemblyDefinition)
{
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
if (assemblyDefinition == null)
{
Console.WriteLine($"ERROR: Could not open {typeof(AssemblyDefinition)}.");
return false;
}
return true;
}
}
[JsonSerializable(typeof(AssemblyDefinition))]
public partial class AssemblyDefinitionContext : JsonSerializerContext
{
}

View File

@ -1,64 +0,0 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Nerfed.Compiler;
public class CSProject
{
public string Name { get; set; }
public string Guid { get; set; }
//public bool IsEditor { get; set; }
// Add platform stuff here..?
// Add dll's here..?
// Add dependencies here..?
public static bool Create(string projectFilePath, string name, out CSProject project)
{
project = null;
if (File.Exists(projectFilePath))
{
Console.WriteLine($"ERROR: File already exists!");
return false;
}
// Create project file.
project = new CSProject
{
Name = name,
Guid = System.Guid.NewGuid().ToString("B").ToUpper(),
};
Save(project, projectFilePath);
return true;
}
public static bool Save(CSProject project, string projectFilePath)
{
string jsonString = JsonSerializer.Serialize(project, CSProjectContext.Default.CSProject);
File.WriteAllText(projectFilePath, jsonString);
return true;
}
public static bool Open(string projectFilePath, out CSProject project)
{
string jsonString = File.ReadAllText(projectFilePath);
project = JsonSerializer.Deserialize(jsonString, CSProjectContext.Default.CSProject);
if (project == null)
{
Console.WriteLine($"ERROR: Could not open {typeof(CSProject)}.");
return false;
}
return true;
}
}
[JsonSerializable(typeof(CSProject))]
public partial class CSProjectContext : JsonSerializerContext
{
}

View File

@ -1,23 +1,17 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
namespace Nerfed.Compiler;
public static class Compiler
{
public const string CSProjectFileName = ".csproject";
public const string CSProjFileName = ".csproj";
public static bool Compile(string projectFilePath, string configuration = "Debug")
{
string projectPath = Path.GetDirectoryName(projectFilePath);
string projectDirectory = Path.GetDirectoryName(projectFilePath);
if (!File.Exists(projectFilePath))
{
Console.WriteLine($"ERROR: Project file not found at {projectPath}.");
Console.WriteLine($"ERROR: Project file not found at {projectDirectory}.");
return false;
}
@ -29,7 +23,7 @@ public static bool Compile(string projectFilePath, string configuration = "Debug
// TODO: Check project version, to make sure we can compile it or something...
// Generate solution.
GenerateSolution(projectPath, project, out string solutionFilePath);
Generator.GenerateSolution(projectDirectory, project, out string solutionFilePath);
// Compile solution.
ProcessStartInfo processInfo = new()
@ -85,147 +79,4 @@ public static bool Compile(string projectFilePath, string configuration = "Debug
return true;
}
public static void GenerateSolution(string projectPath, Project project, out string solutionPath)
{
// Clear files.
string[] csProjectFiles = Directory.GetFiles(projectPath, $"*{CSProjFileName}", SearchOption.TopDirectoryOnly);
foreach (string csProjFile in csProjectFiles)
{
File.Delete(csProjFile);
}
// Generate projects.
string[] csProjectFilePaths = Directory.GetFiles(projectPath, CSProjectFileName, SearchOption.AllDirectories);
foreach (string csProjectFilePath in csProjectFilePaths)
{
GenerateCSProject(csProjectFilePath, projectPath);
}
// Generate solution.
string[] csProjPaths = Directory.GetFiles(projectPath, $"*{CSProjFileName}", SearchOption.TopDirectoryOnly);
string[] csProjGuids = new string[csProjPaths.Length];
for (int i = 0; i < csProjPaths.Length; i++)
{
csProjGuids[i] = Guid.NewGuid().ToString("B").ToUpper();
}
StringBuilder solutionContent = new StringBuilder();
// Write the solution file header
solutionContent.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
solutionContent.AppendLine("# Visual Studio Version 17");
solutionContent.AppendLine("VisualStudioVersion = 17.10.35013.160");
solutionContent.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1");
// Add each project to the solution file
for (int i = 0; i < csProjPaths.Length; i++)
{
string csProjPath = csProjPaths[i];
string projectGuid = csProjGuids[i];
string projectName = Path.GetFileNameWithoutExtension(csProjPath);
string projectRelativePath = Path.GetRelativePath(projectPath, csProjPath);
// FAE04EC0-301F-11D3-BF4B-00C04F79EFBC for C# projects.
solutionContent.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{projectName}\", \"{projectRelativePath}\", \"{projectGuid}\"");
solutionContent.AppendLine("EndProject");
}
// Add global sections (these can be extended as needed)
solutionContent.AppendLine("Global");
solutionContent.AppendLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution");
solutionContent.AppendLine(" Test|x64 = Test|x64");
solutionContent.AppendLine(" Release|x64 = Release|x64");
solutionContent.AppendLine(" Debug|x64 = Debug|x64");
solutionContent.AppendLine(" EndGlobalSection");
solutionContent.AppendLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution");
for (int i = 0; i < csProjPaths.Length; i++)
{
string projectGuid = csProjGuids[i];
solutionContent.AppendLine($" {projectGuid}.Test|x64.ActiveCfg = Test|x64");
solutionContent.AppendLine($" {projectGuid}.Test|x64.Build.0 = Test|x64");
solutionContent.AppendLine($" {projectGuid}.Release|x64.ActiveCfg = Release|x64");
solutionContent.AppendLine($" {projectGuid}.Release|x64.Build.0 = Release|x64");
solutionContent.AppendLine($" {projectGuid}.Debug|x64.ActiveCfg = Debug|x64");
solutionContent.AppendLine($" {projectGuid}.Debug|x64.Build.0 = Debug|x64");
}
solutionContent.AppendLine(" EndGlobalSection");
solutionContent.AppendLine(" GlobalSection(SolutionProperties) = preSolution");
solutionContent.AppendLine(" HideSolutionNode = FALSE");
solutionContent.AppendLine(" EndGlobalSection");
solutionContent.AppendLine("EndGlobal");
// Write the solution file content to disk
string solutionName = project.Name + ".sln";
string filePath = Path.Combine(projectPath, solutionName);
File.WriteAllText(filePath, solutionContent.ToString());
solutionPath = filePath;
}
private static void GenerateCSProject(string csProjectFilePath, string projectPath)
{
if (!File.Exists(csProjectFilePath))
{
return;
}
string jsonString = File.ReadAllText(csProjectFilePath);
CSProject csProject = JsonSerializer.Deserialize(jsonString, CSProjectContext.Default.CSProject);
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!");
// TODO: get all dependencies.
// TODO: properly get assemblies.
StringBuilder projectContent = new StringBuilder();
projectContent.AppendLine("<Project Sdk=\"Microsoft.NET.Sdk\">");
projectContent.AppendLine(" <PropertyGroup>");
projectContent.AppendLine(" <TargetFramework>net8.0</TargetFramework>");
projectContent.AppendLine(" <ImplicitUsings>enable</ImplicitUsings>");
projectContent.AppendLine(" <Nullable>disable</Nullable>");
projectContent.AppendLine(" <PublishAot>true</PublishAot>");
projectContent.AppendLine(" <InvariantGlobalization>true</InvariantGlobalization>");
projectContent.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
projectContent.AppendLine(" <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>");
projectContent.AppendLine(" <IsPackable>false</IsPackable>");
projectContent.AppendLine(" <Configurations>Debug;Test;Release</Configurations>");
projectContent.AppendLine(" <Platforms>x64</Platforms>");
projectContent.AppendLine(" </PropertyGroup>");
projectContent.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|x64' \">");
projectContent.AppendLine(" <DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>");
projectContent.AppendLine(" </PropertyGroup>");
projectContent.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Test|x64' \">");
projectContent.AppendLine(" <DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>");
projectContent.AppendLine(" <Optimize>true</Optimize>");
projectContent.AppendLine(" </PropertyGroup>");
projectContent.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|x64' \">");
projectContent.AppendLine(" <DefineConstants>TRACE;LOG_ERROR</DefineConstants>");
projectContent.AppendLine(" <Optimize>true</Optimize>");
projectContent.AppendLine(" </PropertyGroup>");
projectContent.AppendLine(" <ItemGroup>");
projectContent.AppendLine($" <Compile Include=\"{csProjectFilePath}/**/*.cs\"/>");
projectContent.AppendLine(" </ItemGroup>");
projectContent.AppendLine(" <ItemGroup>");
projectContent.AppendLine(" <Reference Include=\"Nerfed.Runtime\">");
projectContent.AppendLine($" <HintPath>{runtimeAssembly.Location}</HintPath>");
projectContent.AppendLine(" <Private>false</Private>");
projectContent.AppendLine(" </Reference>");
projectContent.AppendLine(" </ItemGroup>");
projectContent.AppendLine("</Project>");
string projectName = csProject.Name + ".csproj";
string filePath = Path.Combine(projectPath, projectName);
File.WriteAllText(filePath, projectContent.ToString());
}
}

View File

@ -0,0 +1,161 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
namespace Nerfed.Compiler;
public static class Generator
{
public const string AssemblyDefinitionExtensionName = ".asmdef";
public const string CSProjectExtensionName = ".csproj";
public const string SolutionExtensionName = ".sln";
public static void GenerateSolution(string projectDirectory, Project project, out string solutionFilePath)
{
// Clear files.
ClearCSProjectFiles(projectDirectory);
// Generate projects.
string[] assemblyDefinitionFilePaths = Directory.GetFiles(projectDirectory, AssemblyDefinitionExtensionName, SearchOption.AllDirectories);
foreach (string assemblyDefinitionFilePath in assemblyDefinitionFilePaths)
{
GenerateCSProject(assemblyDefinitionFilePath, projectDirectory, out string csProjectFilePath);
}
// Generate solution.
string[] csProjectPaths = Directory.GetFiles(projectDirectory, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly);
string[] csProjectGuids = new string[csProjectPaths.Length];
for (int i = 0; i < csProjectPaths.Length; i++)
{
csProjectGuids[i] = Guid.NewGuid().ToString("B").ToUpper();
}
StringBuilder content = new StringBuilder();
// Write the solution file header
content.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
content.AppendLine("# Visual Studio Version 17");
content.AppendLine("VisualStudioVersion = 17.10.35013.160");
content.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1");
// Add each project to the solution file
for (int i = 0; i < csProjectPaths.Length; i++)
{
string csProjectPath = csProjectPaths[i];
string csProjectGuid = csProjectGuids[i];
string csProjectName = Path.GetFileNameWithoutExtension(csProjectPath);
string csProjectRelativePath = Path.GetRelativePath(projectDirectory, csProjectPath);
// FAE04EC0-301F-11D3-BF4B-00C04F79EFBC for C# projects.
content.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{csProjectName}\", \"{csProjectRelativePath}\", \"{csProjectGuid}\"");
content.AppendLine("EndProject");
}
// Add global sections (these can be extended as needed)
content.AppendLine("Global");
content.AppendLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution");
content.AppendLine(" Test|x64 = Test|x64");
content.AppendLine(" Release|x64 = Release|x64");
content.AppendLine(" Debug|x64 = Debug|x64");
content.AppendLine(" EndGlobalSection");
content.AppendLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution");
for (int i = 0; i < csProjectPaths.Length; i++)
{
string projectGuid = csProjectGuids[i];
content.AppendLine($" {projectGuid}.Test|x64.ActiveCfg = Test|x64");
content.AppendLine($" {projectGuid}.Test|x64.Build.0 = Test|x64");
content.AppendLine($" {projectGuid}.Release|x64.ActiveCfg = Release|x64");
content.AppendLine($" {projectGuid}.Release|x64.Build.0 = Release|x64");
content.AppendLine($" {projectGuid}.Debug|x64.ActiveCfg = Debug|x64");
content.AppendLine($" {projectGuid}.Debug|x64.Build.0 = Debug|x64");
}
content.AppendLine(" EndGlobalSection");
content.AppendLine(" GlobalSection(SolutionProperties) = preSolution");
content.AppendLine(" HideSolutionNode = FALSE");
content.AppendLine(" EndGlobalSection");
content.AppendLine("EndGlobal");
// Write the solution file content to disk
string solutionName = project.Name + SolutionExtensionName;
solutionFilePath = Path.Combine(projectDirectory, solutionName);
File.WriteAllText(solutionFilePath, content.ToString());
}
private static bool GenerateCSProject(string assemblyDefinitionFilePath, string projectPath, out string csProjectFilePath)
{
if (!File.Exists(assemblyDefinitionFilePath))
{
csProjectFilePath = string.Empty;
return false;
}
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
AssemblyDefinition assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!");
// TODO: get all dependencies.
// TODO: properly get assemblies.
StringBuilder content = new StringBuilder();
content.AppendLine("<Project Sdk=\"Microsoft.NET.Sdk\">");
content.AppendLine(" <PropertyGroup>");
content.AppendLine(" <TargetFramework>net8.0</TargetFramework>");
content.AppendLine(" <ImplicitUsings>enable</ImplicitUsings>");
content.AppendLine(" <Nullable>disable</Nullable>");
content.AppendLine(" <PublishAot>true</PublishAot>");
content.AppendLine(" <InvariantGlobalization>true</InvariantGlobalization>");
content.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
content.AppendLine(" <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>");
content.AppendLine(" <IsPackable>false</IsPackable>");
content.AppendLine(" <Configurations>Debug;Test;Release</Configurations>");
content.AppendLine(" <Platforms>x64</Platforms>");
content.AppendLine(" </PropertyGroup>");
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|x64' \">");
content.AppendLine(" <DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>");
content.AppendLine(" </PropertyGroup>");
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Test|x64' \">");
content.AppendLine(" <DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>");
content.AppendLine(" <Optimize>true</Optimize>");
content.AppendLine(" </PropertyGroup>");
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|x64' \">");
content.AppendLine(" <DefineConstants>TRACE;LOG_ERROR</DefineConstants>");
content.AppendLine(" <Optimize>true</Optimize>");
content.AppendLine(" </PropertyGroup>");
content.AppendLine(" <ItemGroup>");
content.AppendLine($" <Compile Include=\"{assemblyDefinitionFilePath}/**/*.cs\"/>");
content.AppendLine(" </ItemGroup>");
content.AppendLine(" <ItemGroup>");
content.AppendLine(" <Reference Include=\"Nerfed.Runtime\">");
content.AppendLine($" <HintPath>{runtimeAssembly.Location}</HintPath>");
content.AppendLine(" <Private>false</Private>");
content.AppendLine(" </Reference>");
content.AppendLine(" </ItemGroup>");
content.AppendLine("</Project>");
string csProjectName = assemblyDefinition.Name + CSProjectExtensionName;
csProjectFilePath = Path.Combine(projectPath, csProjectName);
File.WriteAllText(csProjectFilePath, content.ToString());
return true;
}
private static void ClearCSProjectFiles(string projectPath)
{
string[] csProjectFiles = Directory.GetFiles(projectPath, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly);
foreach (string csProjectFile in csProjectFiles)
{
File.Delete(csProjectFile);
}
}
}

View File

@ -1,10 +1,15 @@
namespace Nerfed.Compiler
namespace Nerfed.Compiler;
public class Program
{
public class Program
{
internal static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("projectFilePath, configuration (Debug, Test, Release)");
return;
}
Compiler.Compile(args[0], args[1]);
}
}
}

View File

@ -39,7 +39,7 @@ internal static bool Open(string path)
ProjectFilePath = path;
ProjectPath = Path.GetDirectoryName(path);
string projectSolutionFilePath = Path.Combine(ProjectPath, Project.Name + ".sln");
string projectSolutionFilePath = Path.Combine(ProjectPath, Project.Name + Compiler.Generator.SolutionExtensionName);
if (File.Exists(projectSolutionFilePath))
{
ProjectSolutionFilePath = projectSolutionFilePath;
@ -90,7 +90,7 @@ internal static void GenerateSolution()
return;
}
Nerfed.Compiler.Compiler.GenerateSolution(ProjectPath, Project, out string solutionFilePath);
Nerfed.Compiler.Generator.GenerateSolution(ProjectPath, Project, out string solutionFilePath);
ProjectSolutionFilePath = solutionFilePath;
}
@ -119,10 +119,10 @@ private static void SetupDefaultFolders()
}
// Test create csproject.
string gameplayRuntimePath = Path.Combine(scriptsRuntimePath, ".csproject");
string gameplayRuntimePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName);
if (!File.Exists(gameplayRuntimePath))
{
Compiler.CSProject.Create(gameplayRuntimePath, "Gameplay", out Compiler.CSProject project);
Compiler.AssemblyDefinition.Create(gameplayRuntimePath, "Gameplay", out Compiler.AssemblyDefinition project);
}
string tempPath = Path.Combine(ProjectPath, "Temp");