Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
SourceFileGenerator.cs
1using System.Linq;
2using System;
3using System.IO;
4using Serilog;
5using Nuke.Common.IO;
6using Nuke.Common.Utilities.Collections;
7using Nuke.Common;
8
10{
11 public record SourceFileModel(
12 string Name, string? Copyright,
13 UnrealProject Project,
14 UnrealPlugin Plugin,
15 UnrealModule Module,
16 string CppIncludePath
17 ) : CommonModelBase(Name, Copyright);
18
20 {
21 protected SourceFileModel? Model;
22 public abstract string TemplateSubfolder { get; }
23
24 public virtual void Generate(AbsolutePath templatesPath, AbsolutePath currentFolder, string name)
25 {
26 CheckInsideSourceFolder(currentFolder);
27
28 Model = GetModelFromArguments(currentFolder, name) with {
29 CppIncludePath = Path.GetRelativePath(Model!.Module.Folder, currentFolder)
30 .Replace("\\", "/")
31 .Replace("public", "", true, null)
32 .Replace("private", "", true, null)
33 .Trim('.').Trim('/')
34 };
35
36 if(!(templatesPath / TemplateSubfolder).DirectoryExists())
37 templatesPath = DefaultTemplateFolder;
38
39 var sourceTemplateDir = templatesPath / TemplateSubfolder;
40 var cppDstDir = currentFolder.ToString().Replace("public", "private", true, null);
41
42 if(Model.Plugin != null)
43 Log.Debug($"Plugin: {Model.Plugin.Folder}");
44
45 Log.Debug($"Module: {Model.Module.Folder}");
46
47 Directory.EnumerateFiles(sourceTemplateDir, "*.sbncpp").ForEach(f =>
49 sourceTemplateDir,
50 (RelativePath) Path.GetFileName(f),
51 (AbsolutePath) cppDstDir,
52 Model
53 )
54 );
55
56 var hDstDir = currentFolder.ToString().Replace("private", "public", true, null);
57 Directory.EnumerateFiles(sourceTemplateDir, "*.sbnh").ForEach(f =>
59 sourceTemplateDir,
60 (RelativePath) Path.GetFileName(f),
61 (AbsolutePath) hDstDir,
62 Model
63 )
64 );
65 }
66
67 public virtual SourceFileModel GetModelFromArguments(AbsolutePath currentFolder, string name)
68 {
69 var project = new UnrealProject(currentFolder);
70 return new(
71 Name: name,
72 Copyright: Unreal.ReadCopyrightFromProject(project.Folder!),
73 Project: project,
74 Plugin: new UnrealPlugin(currentFolder),
75 Module: new UnrealModule(
76 currentFolder,
77 $"{currentFolder} is not inside a Module. Unreal Engine requires all source files to be contained inside a Module in a Source folder."
78 ),
79 CppIncludePath: ""
80 );
81 }
82
83 protected void CheckInsideSourceFolder(AbsolutePath currentFolder)
84 {
85 if(!currentFolder.ToString().Contains("Source", StringComparison.InvariantCultureIgnoreCase))
86 {
87 throw new InvalidOperationException(
88 $"{currentFolder} doesn't contain a Source folder. Unreal Engine requires all source files to be contained inside a Module in a Source folder."
89 );
90 }
91 }
92 }
93}
static void RenderFile(AbsolutePath templateRoot, RelativePath source, AbsolutePath destinationFolder, object model)
Render a file from a scriban template.
A collection of utilities around basic functions regarding the environment of the Engine we're workin...
Definition Unreal.cs:24
static string ReadCopyrightFromProject(AbsolutePath projectFolder)
Read copyright info from the project's DefaultGame.ini
Definition Unreal.cs:321