Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
LibraryGenerator.cs
1using System.Linq;
2using System;
3using System.IO;
4using Newtonsoft.Json;
5using Newtonsoft.Json.Linq;
6using Nuke.Common.IO;
7using Nuke.Common.Tooling;
8using System.ComponentModel;
9using Nuke.Cola;
10using System.Text.RegularExpressions;
12using System.Collections.Generic;
13using Humanizer;
14using Nuke.Common;
15using Nuke.Common.Utilities;
16
18
19[TypeConverter(typeof(TypeConverter<LibraryType>))]
20[JsonConverter(typeof(EnumerationJsonConverter<LibraryType>))]
21public class LibraryType : Enumeration
22{
23 public static readonly LibraryType Header = new()
24 {
25 Value = nameof(Header),
26 TemplateSubfolder = "HeaderLibrary"
27 };
28
29 public static readonly LibraryType CMake = new()
30 {
31 Value = nameof(CMake),
32 TemplateSubfolder = "CMakeLibrary"
33 };
34
35 public static readonly LibraryType XRepo = new()
36 {
37 Value = nameof(XRepo),
38 TemplateSubfolder = "XRepoLibrary",
39 ParseSpec = XRepoLibrary.ParseSpec
40 };
41
42 public required string TemplateSubfolder { init; get; }
43 public Func<string, LibrarySpec> ParseSpec { get; private set; } = s => new LibrarySpec(s, s);
44}
45
46public record class LibrarySpec(
47 string Spec,
48 string Name,
49 string? Version = null,
50 string? Provider = null,
51 string? Options = null,
52 string? Features = null
53) {
54 public string UnrealName => Name
55 .Replace("-", "_")
56 .Replace(".", "_")
57 .Pascalize();
58}
59
60public record class SuffixRecord(string? Text, string? Us, string? Dot)
61{
62 public SuffixRecord(string? suffix) : this(suffix, suffix.PrependNonEmpty("_"), suffix.PrependNonEmpty(".")) {}
63}
64
65public record class LibraryModel(
66 LibrarySpec Spec,
67 string? Copyright,
68 SuffixRecord Suffix,
69 IEnumerable<UnrealPlatform> Platforms
70) : CommonModelBase(Spec.Name, Copyright);
71
73{
74 protected LibraryModel? Model;
75
76 public void Generate(IUnrealBuild build, AbsolutePath templatesPath, AbsolutePath currentFolder, string fullSpec, LibraryType libraryType, string? suffix)
77 {
78 var project = new UnrealProject(build);
79 var spec = libraryType.ParseSpec(fullSpec);
80 if (spec.Name.IsNullOrWhiteSpace())
81 {
82 throw new ArgumentException($"Library name was empty. Using spec '{fullSpec}'");
83 }
84 Model = new(
85 Spec: spec,
86 Copyright: Unreal.ReadCopyrightFromProject(project.Folder!),
87 Suffix: new(suffix),
88 Platforms: UnrealPlatform.Platforms.Where(p => Unreal.Version(build).IsCompatibleWith(p.Compatibility))
89 );
90
91 if ((currentFolder / (spec.Name + Model.Suffix.Us)).DirectoryExists())
92 {
93 throw new InvalidOperationException($"The library module folder of {spec.Name} already exists in the current folder.");
94 }
95
96 if(!(templatesPath / libraryType.TemplateSubfolder).DirectoryExists())
97 templatesPath = DefaultTemplateFolder;
98
99 var templateDir = templatesPath / libraryType.TemplateSubfolder;
100
101 RenderFolder(templateDir, currentFolder, Model);
102 }
103}
bool IsCompatibleWith(UnrealCompatibility compatibility)
Check if given compatibility mask applies to this version too.
High level representation of common platforms supported by Unreal Engine (NDA ones excluded) and extr...
static readonly List< UnrealPlatform > Platforms
List of all "real" platforms.
A collection of utilities around basic functions regarding the environment of the Engine we're workin...
Definition Unreal.cs:24
static EngineVersion Version(IUnrealBuild build)
Get high-level version of currently used Engine.
static string ReadCopyrightFromProject(AbsolutePath projectFolder)
Read copyright info from the project's DefaultGame.ini
Definition Unreal.cs:338
Base interface for build components which require an UnrealBuild main class.