diff --git a/RobloxCS.Tests/CodeGenerator_Should.cs b/RobloxCS.Tests/CodeGenerator_Should.cs index e175469..26a8823 100644 --- a/RobloxCS.Tests/CodeGenerator_Should.cs +++ b/RobloxCS.Tests/CodeGenerator_Should.cs @@ -280,7 +280,7 @@ private List GetLines(string cleanLua) private string GetCleanLua(string source, int extraLines = 0) { var cleanTree = TranspilerUtility.ParseTree(source); - var transformedTree = TranspilerUtility.TransformTree(cleanTree, [TransformFactory.Main()]); + var transformedTree = TranspilerUtility.TransformTree(cleanTree, [BuiltInTransformers.Main()]); var compiler = TranspilerUtility.GetCompiler([transformedTree]); var memberCollector = new MemberCollector([cleanTree]); var generatedLua = TranspilerUtility.GenerateLua(transformedTree, compiler, memberCollector.Collect()); diff --git a/RobloxCS.Tests/DebugTransformer_Should.cs b/RobloxCS.Tests/DebugTransformer_Should.cs index 5d46b29..7ad189e 100644 --- a/RobloxCS.Tests/DebugTransformer_Should.cs +++ b/RobloxCS.Tests/DebugTransformer_Should.cs @@ -10,7 +10,7 @@ public class DebugTransformer_Should public void FileInfo_PrependsArgument(string input) { var cleanTree = TranspilerUtility.ParseTree(input); - var transformedTree = TranspilerUtility.TransformTree(cleanTree, [TransformFactory.Main(), TransformFactory.Debug()]); + var transformedTree = TranspilerUtility.TransformTree(cleanTree, [BuiltInTransformers.Main(), BuiltInTransformers.Get("Debug")]); var cleanRoot = cleanTree.GetRoot(); var cleanInvocation = cleanRoot.DescendantNodes().OfType().First(); var transformedRoot = transformedTree.GetRoot(); @@ -31,7 +31,7 @@ public void FileInfo_PrependsArgument(string input) public void FileInfo_ConcatenatesLiteral(string input) { var cleanTree = TranspilerUtility.ParseTree(input); - var transformedTree = TranspilerUtility.TransformTree(cleanTree, [TransformFactory.Main(), TransformFactory.Debug()]); + var transformedTree = TranspilerUtility.TransformTree(cleanTree, [BuiltInTransformers.Main(), BuiltInTransformers.Get("Debug")]); var cleanRoot = cleanTree.GetRoot(); var cleanInvocation = cleanRoot.DescendantNodes().OfType().First(); var transformedRoot = transformedTree.GetRoot(); diff --git a/RobloxCS.Tests/MainTransformer_Should.cs b/RobloxCS.Tests/MainTransformer_Should.cs index 50eaa4f..b4ebf54 100644 --- a/RobloxCS.Tests/MainTransformer_Should.cs +++ b/RobloxCS.Tests/MainTransformer_Should.cs @@ -9,7 +9,7 @@ public class MainTransformer_Should public void SafeNavigation_TransformsWhenNotNull(string source) { var cleanTree = TranspilerUtility.ParseTree(source); - var transformedTree = TranspilerUtility.TransformTree(cleanTree, [TransformFactory.Main()]); + var transformedTree = TranspilerUtility.TransformTree(cleanTree, [BuiltInTransformers.Main()]); var cleanRoot = cleanTree.GetRoot(); var cleanTernary = cleanRoot.DescendantNodes().OfType().First(); var transformedRoot = transformedTree.GetRoot(); diff --git a/RobloxCS/ConfigReader.cs b/RobloxCS/ConfigReader.cs index cde8074..debb618 100644 --- a/RobloxCS/ConfigReader.cs +++ b/RobloxCS/ConfigReader.cs @@ -14,7 +14,7 @@ public static ConfigData UnitTestingConfig SourceFolder = "test-src", OutputFolder = "test-dist", RojoProjectName = "UNIT_TESTING", - EnableDebugTransformer = true, + EnabledBuiltInTransformers = ["Debug"], EmitNativeAttributeOnClassOrNamespaceCallbacks = true, CSharpOptions = new CSharpOptions() { diff --git a/RobloxCS/ConfigTemplate.cs b/RobloxCS/ConfigTemplate.cs index 038bd30..d36e232 100644 --- a/RobloxCS/ConfigTemplate.cs +++ b/RobloxCS/ConfigTemplate.cs @@ -21,8 +21,8 @@ public sealed class ConfigData public string SourceFolder { get; set; } public string OutputFolder { get; set; } public string RojoProjectName { get; set; } = "default"; - public bool EnableDebugTransformer { get; set;} = true; public bool EmitNativeAttributeOnClassOrNamespaceCallbacks { get; set; } = true; + public HashSet EnabledBuiltInTransformers { get; set; } = ["Debug"]; public CSharpOptions CSharpOptions { get; set; } public bool IsValid() diff --git a/RobloxCS/README.md b/RobloxCS/README.md index 1d107b4..146d67e 100644 --- a/RobloxCS/README.md +++ b/RobloxCS/README.md @@ -18,7 +18,7 @@ This project includes the compiler and transformers. - Overloaded methods - Full qualification of types/namespaces inside of namespaces - Macro `new T()` with collection types to `{}` -- Test MainTransformer more +- Test `MainTransformer` more ## Will maybe be supported - [Class finalizers (destructors)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers) diff --git a/RobloxCS/Transformers/BuiltInTransformers.cs b/RobloxCS/Transformers/BuiltInTransformers.cs new file mode 100644 index 0000000..73aaf2b --- /dev/null +++ b/RobloxCS/Transformers/BuiltInTransformers.cs @@ -0,0 +1,29 @@ +using Microsoft.CodeAnalysis; + +namespace RobloxCS +{ + using TransformMethod = Func; + + public static partial class BuiltInTransformers + { + public static TransformMethod Main() + { + return (tree, config) => new MainTransformer(tree, config).TransformTree(); + } + + public static TransformMethod Get(string name) + { + return name.ToLower() switch + { + "debug" => (tree, config) => new DebugTransformer(tree, config).TransformTree(), + _ => FailedToGetTransformer(name) + }; + } + + private static TransformMethod FailedToGetTransformer(string name) + { + Logger.Error($"No built-in transformer \"{name}\" exists (roblox-cs.yml)"); + return null!; // hack + } + } +} diff --git a/RobloxCS/Transformers/TransformFactory.cs b/RobloxCS/Transformers/TransformFactory.cs deleted file mode 100644 index 3946998..0000000 --- a/RobloxCS/Transformers/TransformFactory.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.CodeAnalysis; - -namespace RobloxCS -{ - using TransformMethod = Func; - - public static partial class TransformFactory - { - public static TransformMethod Main() - { - return (tree, config) => new MainTransformer(tree, config).TransformTree(); - } - - public static TransformMethod Debug() - { - return (tree, config) => new DebugTransformer(tree, config).TransformTree(); - } - } -} diff --git a/RobloxCS/Transpiler.cs b/RobloxCS/Transpiler.cs index 6ee576b..94f7997 100644 --- a/RobloxCS/Transpiler.cs +++ b/RobloxCS/Transpiler.cs @@ -42,10 +42,11 @@ private void ParseSource() { var fileContents = File.ReadAllText(sourceFile); var tree = TranspilerUtility.ParseTree(fileContents, sourceFile); - HashSet> transformers = [TransformFactory.Main()]; - if (Config.EnableDebugTransformer) + HashSet> transformers = [BuiltInTransformers.Main()]; + + foreach (var transformerName in Config.EnabledBuiltInTransformers) { - transformers.Add(TransformFactory.Debug()); + transformers.Add(BuiltInTransformers.Get(transformerName)); } var transformedTree = TranspilerUtility.TransformTree(tree, transformers, Config);