Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
add code generators
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 committed Jun 1, 2020
1 parent e66495f commit 3b7c48d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Linq;
using Improbable.Gdk.CodeGeneration.FileHandling;
using Improbable.Gdk.CodeGeneration.Jobs;
using Improbable.Gdk.CodeGeneration.Model.Details;

namespace Improbable.Gdk.CodeGenerator
{
public class ComponentVisualElementJob : CodegenJob
{
private const string DebugAsmdefFileName = "Improbable.Gdk.Generated.Debug.asmdef";
private readonly string relativeOutputPath = Path.Combine("improbable", "debugextensions");

public ComponentVisualElementJob(CodegenJobOptions options, IFileSystem fileSystem, DetailsStore detailsStore) : base(options.AsEditor(), fileSystem, detailsStore)
{
const string jobName = nameof(ComponentVisualElementJob);
Logger.Trace($"Initialising {jobName}.");

Logger.Trace($"Adding job target for {DebugAsmdefFileName}");
AddJobTarget(Path.Combine(relativeOutputPath, DebugAsmdefFileName), () => DebugAssemblyGenerator.Generate());

var componentsToGenerate = detailsStore.Components.Values.ToList();
AddGenerators(relativeOutputPath, componentsToGenerate, component => ($"{component.Name}Renderer.cs", ComponentVisualElementGenerator.Generate));
Logger.Trace($"Added job targets for {componentsToGenerate.Count} components");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Improbable.Gdk.CodeGeneration.CodeWriter;
using Improbable.Gdk.CodeGeneration.CodeWriter.Scopes;
using Improbable.Gdk.CodeGeneration.Model.Details;

namespace Improbable.Gdk.CodeGenerator
{
public static class ComponentVisualElementGenerator
{
public static CodeWriter Generate(UnityComponentDetails details)
{
return CodeWriter.Populate(cgw =>
{
cgw.UsingDirectives(
"Unity.Entities",
"UnityEngine.UIElements",
"Improbable.Gdk.Debug.WorkerInspector.Codegen"
);

cgw.Namespace(details.Namespace, ns =>
{
ns.Type($"public class {details.Name}Renderer : ComponentVisualElement", type =>
{
type.Line($"public override ComponentType ComponentType {{ get; }} = ComponentType.ReadOnly<{details.Name}.Component>();");

GenerateConstructor(type, details);
GenerateUpdateMethod(type, details);
});
});
});
}

private static void GenerateConstructor(TypeBlock typeBlock, UnityComponentDetails details)
{
typeBlock.Method($"public {details.Name}Renderer() : base()", mb =>
{
mb.Line($"ComponentFoldout.text = \"{details.Name}\";");
mb.Line($"AuthoritativeToggle.SetEnabled(false);");
});
}

private static void GenerateUpdateMethod(TypeBlock typeBlock, UnityComponentDetails details)
{
typeBlock.Method("public override void Update(EntityManager manager, Entity entity)", mb =>
{
mb.Line($"AuthoritativeToggle.value = manager.HasComponent<{details.Name}.HasAuthority>(entity);");
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Improbable.Gdk.CodeGenerator
{
public static class DebugAssemblyGenerator
{
public static string Generate()
{
return @"{
""name"": ""Improbable.Gdk.Generated.Debug"",
""references"": [
""Improbable.Gdk.Generated"",
""Improbable.Gdk.Debug.WorkerInspector.Codegen"",
""Unity.Entities""
],
""includePlatforms"": [
""Editor""
],
""excludePlatforms"": [],
""allowUnsafeCode"": false,
""overrideReferences"": false,
""precompiledReferences"": [],
""autoReferenced"": true,
""defineConstraints"": [],
""versionDefines"": []
}
";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ protected void AddGenerators<TDetails>(IEnumerable<TDetails> details, params Gen
}));
}

protected void AddGenerators<TDetails>(string relativeOutputDir, IEnumerable<TDetails> details, params GeneratorSetupDelegate<TDetails, CodeWriter.CodeWriter>[] generatorSetupDelegates)
where TDetails : GeneratorInputDetails
{
jobTargets.AddRange(details.SelectMany(detail =>
{
return generatorSetupDelegates.Select(generatorSetup =>
{
var (filePath, generate) = generatorSetup(detail);
return new JobTarget(Path.Combine(OutputDirectory, relativeOutputDir, detail.NamespacePath, filePath), () => generate(detail));
});
}));
}

protected void AddGenerators<TDetails>(string relativeOutputDir, IEnumerable<TDetails> details, params GeneratorSetupDelegate<TDetails, string>[] generatorSetupDelegates)
where TDetails : GeneratorInputDetails
{
jobTargets.AddRange(details.SelectMany(detail =>
{
return generatorSetupDelegates.Select(generatorSetup =>
{
var (filePath, generate) = generatorSetup(detail);
return new JobTarget(Path.Combine(OutputDirectory, relativeOutputDir, detail.NamespacePath, filePath), () => generate(detail));
});
}));
}

public void Clean()
{
var numRemovedDirectories = 0;
Expand Down

0 comments on commit 3b7c48d

Please sign in to comment.