Modified template and improved template creation

This commit is contained in:
Simon Lübeß
2024-03-09 20:35:21 +01:00
parent 4e9a6b54f3
commit 9ea27a64e8
4 changed files with 81 additions and 56 deletions
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunManager" selected=".NET Launch Settings Profile.ScriptCoreGenerator: Debug Code Generator">
<configuration name="Attach to Glitchy Engine" type="ConnectRemote" factoryName="Mono Remote" show_console_on_std_err="false" show_console_on_std_out="false" port="2550" address="localhost">
<option name="allowRunningInParallel" value="false" />
<option name="listenPortForConnections" value="false" />
<option name="projectPathOnTarget" />
<option name="selectedOptions">
<list />
</option>
<method v="2" />
</configuration>
<list>
<item itemvalue="Mono Remote.Attach to Glitchy Engine" />
</list>
</component>
</project>
+64 -56
View File
@@ -628,23 +628,78 @@ namespace GlitchyEditor
return .Ok; return .Ok;
} }
const String ProjectNamePlaceholder = "[ProjectName]";
const String CoreLibPathPlaceholder = "[CoreLibPath]";
/// Initializes the given project with the template. /// Initializes the given project with the template.
private static Result<void> InitProject(Project project) private static Result<void> InitProject(Project project)
{ {
Try!(CopyTemplate(project)); Try!(CopyTemplate(project));
// Rename files within project Try!(RenameAndFixupFiles(project.WorkspacePath, 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"));
return .Ok; return .Ok;
} }
/// Moves files so that [ProjectName] in their paths will be replaced by the actual project name
private static Result<void> 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. /// Copies the template files into the given project.
private static Result<void> CopyTemplate(Project project) private static Result<void> CopyTemplate(Project project)
{ {
@@ -660,53 +715,6 @@ namespace GlitchyEditor
return .Ok; return .Ok;
} }
/// Renames a file within the project.
private static Result<void> 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<void> 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. /// If set to true, the popup for creating a new project will be shown.
private bool _openCreateProjectModal; private bool _openCreateProjectModal;