From 9ea27a64e88269e65686cf57bf8541f10f8d592c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 9 Mar 2024 20:35:21 +0100 Subject: [PATCH] Modified template and improved template creation --- .../.idea.[ProjectName]/.idea/workspace.xml | 17 +++ ...ateProject.csproj => [ProjectName].csproj} | 0 ...{TemplateProject.sln => [ProjectName].sln} | 0 GlitchyEditor/src/EditorLayer.bf | 120 ++++++++++-------- 4 files changed, 81 insertions(+), 56 deletions(-) create mode 100644 GlitchyEditor/resources/ProjectTemplate/.idea/.idea.[ProjectName]/.idea/workspace.xml rename GlitchyEditor/resources/ProjectTemplate/{TemplateProject.csproj => [ProjectName].csproj} (100%) rename GlitchyEditor/resources/ProjectTemplate/{TemplateProject.sln => [ProjectName].sln} (100%) diff --git a/GlitchyEditor/resources/ProjectTemplate/.idea/.idea.[ProjectName]/.idea/workspace.xml b/GlitchyEditor/resources/ProjectTemplate/.idea/.idea.[ProjectName]/.idea/workspace.xml new file mode 100644 index 0000000..56732b6 --- /dev/null +++ b/GlitchyEditor/resources/ProjectTemplate/.idea/.idea.[ProjectName]/.idea/workspace.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + diff --git a/GlitchyEditor/resources/ProjectTemplate/TemplateProject.csproj b/GlitchyEditor/resources/ProjectTemplate/[ProjectName].csproj similarity index 100% rename from GlitchyEditor/resources/ProjectTemplate/TemplateProject.csproj rename to GlitchyEditor/resources/ProjectTemplate/[ProjectName].csproj diff --git a/GlitchyEditor/resources/ProjectTemplate/TemplateProject.sln b/GlitchyEditor/resources/ProjectTemplate/[ProjectName].sln similarity index 100% rename from GlitchyEditor/resources/ProjectTemplate/TemplateProject.sln rename to GlitchyEditor/resources/ProjectTemplate/[ProjectName].sln diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index 2df5bd8..990ce50 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -628,23 +628,78 @@ namespace GlitchyEditor return .Ok; } - + + const String ProjectNamePlaceholder = "[ProjectName]"; + const String CoreLibPathPlaceholder = "[CoreLibPath]"; + /// Initializes the given project with the template. private static Result InitProject(Project project) { Try!(CopyTemplate(project)); - // Rename files within project - Try!(MoveFile(project, "TemplateProject.sln", scope $"{project.Name}.sln")); - Try!(MoveFile(project, "TemplateProject.csproj", scope $"{project.Name}.csproj")); - - Try!(FixupTemplateFile(project, scope $"{project.Name}.sln")); - Try!(FixupTemplateFile(project, scope $"{project.Name}.csproj")); - Try!(FixupTemplateFile(project, scope $"Properties/AssemblyInfo.cs")); + Try!(RenameAndFixupFiles(project.WorkspacePath, project)); return .Ok; } - + + /// Moves files so that [ProjectName] in their paths will be replaced by the actual project name + private static Result RenameAndFixupFiles(StringView directoryPath, Project project) + { + String fullPath = scope .(256); + String modifiedFileName = scope String(256); + + for (FileFindEntry entry in Directory.Enumerate(scope $"{directoryPath}/*", .Files | .Directories)) + { + entry.GetFilePath(fullPath..Clear()); + + if (fullPath.Contains(ProjectNamePlaceholder)) + { + modifiedFileName.Set(fullPath); + modifiedFileName.Replace(ProjectNamePlaceholder, project.Name); + + if (entry.IsDirectory) + Try!(Directory.Move(fullPath, modifiedFileName)); + else + Try!(File.Move(fullPath, modifiedFileName)); + + Swap!(fullPath, modifiedFileName); + } + + if (entry.IsDirectory) + { + Try!(RenameAndFixupFiles(fullPath, project)); + } + else + { + String fileContent = scope String(); + if (File.ReadAllText(fullPath, fileContent, true) case .Err) + { + Log.EngineLogger.Error($"FixupFile: Failed to read file {fullPath}."); + return .Err; + } + + if (!fileContent.Contains(ProjectNamePlaceholder) && !fileContent.Contains(CoreLibPathPlaceholder)) + continue; + + fileContent.Replace(ProjectNamePlaceholder, project.Name); + + // TODO: This is pretty bad, we don't really want to hard code the script core path like that! + String scriptCorePath = scope .(); + Directory.GetCurrentDirectory(scriptCorePath); + scriptCorePath.Append("/Resources/Scripts/ScriptCore.dll"); + fileContent.Replace(CoreLibPathPlaceholder, scriptCorePath); + + if (File.WriteAllText(fullPath, fileContent, false) case .Err) + { + Log.EngineLogger.Error($"FixupFile: Failed to write file {fullPath}."); + return .Err; + } + } + } + + return .Ok; + } + /// Copies the template files into the given project. private static Result CopyTemplate(Project project) { @@ -660,53 +715,6 @@ namespace GlitchyEditor return .Ok; } - - /// Renames a file within the project. - private static Result MoveFile(Project project, StringView fromName, StringView toName) - { - String fromPath = scope String(); - project.PathInProject(fromPath, fromName); - - String toPath = scope String(); - project.PathInProject(toPath, toName); - - if (File.Move(fromPath, toPath) case .Err(let error)) - { - Log.EngineLogger.Error($"Failed to move file from {fromPath} to {toPath}. ({error})"); - return .Err; - } - - return .Ok; - } - - /// Fixups a template file by replacing placeholders with actual values. - private static Result FixupTemplateFile(Project project, StringView fileName) - { - String fileTargetPath = scope String(); - project.PathInProject(fileTargetPath, fileName); - - String fileContent = scope String(); - if (File.ReadAllText(fileTargetPath, fileContent, true) case .Err) - { - Log.EngineLogger.Error($"FixupFile: Failed to read file {fileTargetPath}."); - return .Err; - } - - fileContent.Replace("[ProjectName]", project.Name); - - String scriptCorePath = scope .(); - Directory.GetCurrentDirectory(scriptCorePath); - scriptCorePath.Append("/Resources/Scripts/ScriptCore.dll"); - fileContent.Replace("[CoreLibPath]", scriptCorePath); - - if (File.WriteAllText(fileTargetPath, fileContent, false) case .Err) - { - Log.EngineLogger.Error($"FixupFile: Failed to write file {fileTargetPath}."); - return .Err; - } - - return .Ok; - } /// If set to true, the popup for creating a new project will be shown. private bool _openCreateProjectModal;