Skip to content

Commit

Permalink
feat: convert roblox-cs.yml EnableDebugTransformer setting into Enabl…
Browse files Browse the repository at this point in the history
…edBuiltInTransformers
  • Loading branch information
R-unic committed Aug 2, 2024
1 parent 0df8e56 commit 02975ca
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion RobloxCS.Tests/CodeGenerator_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private List<string> 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());
Expand Down
4 changes: 2 additions & 2 deletions RobloxCS.Tests/DebugTransformer_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvocationExpressionSyntax>().First();
var transformedRoot = transformedTree.GetRoot();
Expand All @@ -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<InvocationExpressionSyntax>().First();
var transformedRoot = transformedTree.GetRoot();
Expand Down
2 changes: 1 addition & 1 deletion RobloxCS.Tests/MainTransformer_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConditionalAccessExpressionSyntax>().First();
var transformedRoot = transformedTree.GetRoot();
Expand Down
2 changes: 1 addition & 1 deletion RobloxCS/ConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion RobloxCS/ConfigTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> EnabledBuiltInTransformers { get; set; } = ["Debug"];
public CSharpOptions CSharpOptions { get; set; }

public bool IsValid()
Expand Down
2 changes: 1 addition & 1 deletion RobloxCS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions RobloxCS/Transformers/BuiltInTransformers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.CodeAnalysis;

namespace RobloxCS
{
using TransformMethod = Func<SyntaxTree, ConfigData, SyntaxTree>;

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
}
}
}
19 changes: 0 additions & 19 deletions RobloxCS/Transformers/TransformFactory.cs

This file was deleted.

7 changes: 4 additions & 3 deletions RobloxCS/Transpiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ private void ParseSource()
{
var fileContents = File.ReadAllText(sourceFile);
var tree = TranspilerUtility.ParseTree(fileContents, sourceFile);
HashSet<Func<SyntaxTree, ConfigData, SyntaxTree>> transformers = [TransformFactory.Main()];
if (Config.EnableDebugTransformer)
HashSet<Func<SyntaxTree, ConfigData, SyntaxTree>> 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);
Expand Down

0 comments on commit 02975ca

Please sign in to comment.