editor project

yes
This commit is contained in:
max 2024-07-21 14:03:40 +02:00
parent 6e41c2579c
commit 36a134170a
5 changed files with 95 additions and 197 deletions

View File

@ -53,6 +53,7 @@ public static bool Compile(string projectFilePath, string configuration = "Debug
} }
else else
{ {
Console.WriteLine($"ERROR: Platform not supported!");
return false; return false;
} }

View File

@ -9,8 +9,6 @@ public class Project
public static bool Create(string path, string name, out Project project) public static bool Create(string path, string name, out Project project)
{ {
project = null;
// Create project file. // Create project file.
project = new Project project = new Project
{ {

View File

@ -23,6 +23,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Nerfed.Compiler\Nerfed.Compiler.csproj" />
<ProjectReference Include="..\Nerfed.Runtime\Nerfed.Runtime.csproj" /> <ProjectReference Include="..\Nerfed.Runtime\Nerfed.Runtime.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,53 +1,25 @@
using Nerfed.Runtime; using Nerfed.Runtime;
using System.Reflection;
using System.Text.Json;
namespace Nerfed.Editor.Project; namespace Nerfed.Editor.Project;
public class ProjectConfig internal static class EditorProject
{ {
public string Name { get; set; } internal static Compiler.Project Project { get; private set; }
public uint Version { get; set; } internal static string ProjectFilePath { get; private set; }
public string RuntimeProject { get; set; } internal static string ProjectSolutionFilePath { get; private set; }
} internal static string ProjectPath { get; private set; }
internal static string ProjectContentPath { get; private set; }
internal static string ProjectTempPath { get; private set; }
internal static class Project internal static bool Create(string projectFilePath, string projectName)
{ {
private const string projectFileName = ".project"; Close();
internal static ProjectConfig LoadedConfig { get; private set; } if (!Compiler.Project.Create(projectFilePath, projectName, out Compiler.Project project))
internal static string ProjectDirectory { get; private set; }
internal static string ProjectContentDirectory { get; private set; }
internal static string ProjectTempDirectory { get; private set; }
internal static bool Create(string path, string projectName)
{ {
// Check if folder is empty.
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if(Directory.GetFiles(path).Length != 0)
{
Log.Error($"Directory {path} is not empty!");
return false; return false;
} }
// Create project file.
ProjectConfig projectConfig = new ProjectConfig
{
Name = projectName,
Version = /*Nerfed.Runtime.Verion*/ 1,
};
// Create solution.
GenerateSolution(path, projectConfig);
string projectFilePath = Path.Combine(path, projectFileName);
SaveProject(projectFilePath, projectConfig);
// Open the project.
Open(projectFilePath); Open(projectFilePath);
Log.Info($"Succesfully created project."); Log.Info($"Succesfully created project.");
@ -56,180 +28,80 @@ internal static bool Create(string path, string projectName)
internal static bool Open(string path) internal static bool Open(string path)
{ {
string projectFilePath = path; Close();
if (!File.Exists(projectFilePath)) { if(!Compiler.Project.Open(path, out Compiler.Project project))
projectFilePath = Path.Combine(path, projectFileName);
if (!File.Exists(projectFilePath))
{ {
Log.Error($"Project file not found at {path} or {projectFilePath}");
return false;
}
}
// Load from file.
string jsonString = File.ReadAllText(projectFilePath);
ProjectConfig projectConfig = JsonSerializer.Deserialize<ProjectConfig>(jsonString);
// Check version.
if(projectConfig.Version != /*Nerfed.Runtime.Verion*/ 1)
{
Log.Error($"Project runtime version does not match editor runtime verion!");
return false; return false;
} }
LoadedConfig = projectConfig; Project = project;
SetupDefaultFolders(Path.GetDirectoryName(projectFilePath)); ProjectFilePath = path;
Log.Info($"Opened project: {projectConfig.Name}"); ProjectPath = Path.GetDirectoryName(path);
SetupDefaultFolders();
Compile();
Log.Info($"Opened project: {project.Name}");
return true; return true;
} }
internal static bool SaveProject(string path, ProjectConfig projectConfig) internal static void Close()
{ {
JsonSerializerOptions options = new JsonSerializerOptions(JsonSerializerOptions.Default) Project = null;
{ ProjectFilePath = string.Empty;
WriteIndented = true, ProjectSolutionFilePath = string.Empty;
}; ProjectPath = string.Empty;
ProjectContentPath = string.Empty;
string jsonString = JsonSerializer.Serialize(projectConfig, options); ProjectTempPath = string.Empty;
File.WriteAllText(path, jsonString);
return true;
} }
// Compile the project using the builder. internal static bool Save()
internal static bool CompileProject() {
if(Project == null)
{ {
//GenerateSolution();
//dotnet build <sln>
return false; return false;
} }
private static void GenerateSolution(string path, ProjectConfig projectConfig) return Compiler.Project.Save(Project, ProjectFilePath);
{
GenerateCSProjectFiles(path, projectConfig);
Log.Info("Generating solution file");
string solution =
$@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35013.160
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{projectConfig.Name}.Runtime"", ""{projectConfig.Name}.Runtime.csproj"", ""{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Test|x64 = Test|x64
Release|x64 = Release|x64
Debug|x64 = Debug|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}.Test|x64.ActiveCfg = Test|x64
{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}.Test|x64.Build.0 = Test|x64
{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}.Release|x64.ActiveCfg = Release|x64
{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}.Release|x64.Build.0 = Release|x64
{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}.Debug|x64.ActiveCfg = Debug|x64
{{B04FAEF8-7D91-43A9-81C5-4BD3217B0C23}}.Debug|x64.Build.0 = Debug|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {{117DD74B-A4DC-48CE-B782-C7AE565A9270}}
EndGlobalSection
EndGlobal
";
string solutionName = projectConfig.Name + ".sln";
string filePath = Path.Combine(path, solutionName);
File.WriteAllText(filePath, solution);
} }
// Inspirered by https://github.com/ProwlEngine/Prowl/blob/main/Prowl.Editor/Project.cs internal static void Compile()
// Maybe the builder should do this?
// When compiling the 'runtimeAssembly.Location' needs to be updated to where ever the one is that is being used.
private static void GenerateCSProjectFiles(string path, ProjectConfig projectConfig)
{ {
Log.Info("Generating CS project files"); if(Project == null)
{
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); return;
Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!");
string propertyGroup =
$@"
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<IsPackable>false</IsPackable>
<Configurations>Debug;Test;Release</Configurations>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|x64' "">
<DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Test|x64' "">
<DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Release|x64' "">
<DefineConstants>TRACE;LOG_ERROR</DefineConstants>
<Optimize>true</Optimize>
</PropertyGroup>
";
string runtimeProject =
$@"
<Project Sdk=""Microsoft.NET.Sdk"">
{propertyGroup}
<ItemGroup>
<Compile Include=""/Content/Scripts/Runtime/**/*.cs""/>
</ItemGroup>
<ItemGroup>
<Reference Include=""Nerfed.Runtime"">
<HintPath>{runtimeAssembly.Location}</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
</Project>
";
string runtimeProjectName = projectConfig.Name + ".Runtime.csproj";
string filePath = Path.Combine(path, runtimeProjectName);
File.WriteAllText(filePath, runtimeProject);
projectConfig.RuntimeProject = runtimeProjectName;
Log.Info($"Generated runtime at {filePath}");
// TODO: Editor project.
} }
private static void SetupDefaultFolders(string path) Nerfed.Compiler.Compiler.Compile(ProjectFilePath, "Debug");
{ }
ProjectDirectory = path;
string contentPath = Path.Combine(path, "Content"); internal static void GenerateSolution()
{
if(Project == null)
{
return;
}
Nerfed.Compiler.Compiler.GenerateSolution(ProjectPath, Project, out string solutionFilePath);
ProjectSolutionFilePath = solutionFilePath;
}
private static void SetupDefaultFolders()
{
if (Project == null || ProjectPath == null)
{
return;
}
string contentPath = Path.Combine(ProjectPath, "Content");
if (!Directory.Exists(contentPath)) if (!Directory.Exists(contentPath))
{ {
Directory.CreateDirectory(contentPath); Directory.CreateDirectory(contentPath);
} }
ProjectContentDirectory = contentPath; ProjectContentPath = contentPath;
string scriptsPath = Path.Combine(ProjectContentDirectory, "Scripts"); string scriptsPath = Path.Combine(ProjectContentPath, "Scripts");
if (!Directory.Exists(scriptsPath)) if (!Directory.Exists(scriptsPath))
{ {
Directory.CreateDirectory(scriptsPath); Directory.CreateDirectory(scriptsPath);
@ -240,11 +112,11 @@ private static void SetupDefaultFolders(string path)
Directory.CreateDirectory(scriptsRuntimePath); Directory.CreateDirectory(scriptsRuntimePath);
} }
string tempPath = Path.Combine(path, "Temp"); string tempPath = Path.Combine(ProjectPath, "Temp");
if (!Directory.Exists(tempPath)) if (!Directory.Exists(tempPath))
{ {
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
} }
ProjectTempDirectory = tempPath; ProjectTempPath = tempPath;
} }
} }

View File

@ -4,38 +4,64 @@ namespace Nerfed.Editor.Project
{ {
internal static class ProjectGui internal static class ProjectGui
{ {
private static string projectDirectory = string.Empty; private static string projectPath = string.Empty;
private static string projectName = string.Empty; private static string projectName = string.Empty;
private static string projectFilePath = string.Empty;
internal static void OnGui() internal static void OnGui()
{ {
ImGui.Begin("Project"); ImGui.Begin("Project");
ImGui.BeginGroup();
ImGui.InputText("Project Directory", ref projectDirectory, 512); ImGui.InputText("Project Path", ref projectPath, 512);
ImGui.InputText("Project Name", ref projectName, 512); ImGui.InputText("Project Name", ref projectName, 512);
string newProjectFilePath = Path.Combine(projectPath, ".project");
ImGui.Text(newProjectFilePath);
if (ImGui.Button("Create Project")) if (ImGui.Button("Create Project"))
{ {
Project.Create(projectDirectory, projectName); EditorProject.Create(newProjectFilePath, projectName);
} }
ImGui.EndGroup();
ImGui.BeginGroup();
ImGui.InputText("Project File Path", ref projectFilePath, 512);
if (ImGui.Button("Open Project")) if (ImGui.Button("Open Project"))
{ {
Project.Open(projectDirectory); EditorProject.Open(projectFilePath);
} }
ImGui.Text("Loaded project: "); ImGui.Text("Loaded project: ");
if(Project.LoadedConfig != null) if(EditorProject.Project != null)
{ {
ImGui.Text(Project.LoadedConfig.Name); ImGui.Text(EditorProject.Project.Name);
ImGui.Text(Project.LoadedConfig.Version.ToString()); ImGui.Text(EditorProject.ProjectFilePath);
ImGui.Text(Project.LoadedConfig.RuntimeProject); ImGui.Text(EditorProject.ProjectSolutionFilePath);
ImGui.Text(EditorProject.ProjectPath);
ImGui.Text(EditorProject.ProjectContentPath);
ImGui.Text(EditorProject.ProjectTempPath);
} }
else else
{ {
ImGui.Text("None"); ImGui.Text("None");
} }
ImGui.EndGroup();
ImGui.BeginGroup();
if (ImGui.Button("Generate Solution"))
{
EditorProject.GenerateSolution();
}
if (ImGui.Button("Compile"))
{
EditorProject.Compile();
}
ImGui.EndGroup();
ImGui.End(); ImGui.End();
} }
} }