Skip to content

Commit

Permalink
CodeGenerator takes arrays of IComponentCodeGenerator and IPoolCodeGe…
Browse files Browse the repository at this point in the history
…nerator to generate
  • Loading branch information
sschmid committed Jun 2, 2015
1 parent 1499f8c commit 1b621ba
Show file tree
Hide file tree
Showing 50 changed files with 441 additions and 295 deletions.
34 changes: 14 additions & 20 deletions Entitas.CodeGenerator/Entitas.CodeGenerator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ namespace Entitas.CodeGenerator {
public static class CodeGenerator {
public const string componentSuffix = "Component";

public static void Generate(Type[] types, string[] poolNames, string dir) {
public static void Generate(Type[] types, string[] poolNames, string dir,
IComponentCodeGenerator[] componentCodeGenerators, IPoolCodeGenerator[] poolCodeGenerators) {

if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}

CleanDir(dir);
var components = GetComponents(types);
generateIndicesLookup(dir, components);
generateComponentExtensions(dir, components);
generatePoolAttributes(dir, poolNames);

foreach (var generator in componentCodeGenerators) {
var files = generator.Generate(components);
writeFiles(dir, files);
}

foreach (var generator in poolCodeGenerators) {
var files = generator.Generate(poolNames);
writeFiles(dir, files);
}
}

public static void CleanDir(string dir) {
Expand All @@ -37,24 +46,9 @@ public static Type[]GetComponents(Type[] types) {
.ToArray();
}

static void generateIndicesLookup(string dir, Type[] components) {
var files = new IndicesLookupGenerator().Generate(components);
writeFiles(dir, files);
}

static void generateComponentExtensions(string dir, Type[] components) {
var files = new ComponentExtensionsGenerator().Generate(components);
writeFiles(dir, files);
}

static void generatePoolAttributes(string dir, string[] poolNames) {
var files = new PoolAttributeGenerator().Generate(poolNames);
writeFiles(dir, files);
}

static void writeFiles(string dir, CodeGenFile[] files) {
foreach (var file in files) {
File.WriteAllText(file.fileName + ".cs", file.fileContent);
File.WriteAllText(dir + file.fileName + ".cs", file.fileContent);
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion Entitas.CodeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
public class Program {
public static void Main(string[] args) {
var assembly = Assembly.GetAssembly(typeof(CodeGenerator));
CodeGenerator.Generate(assembly.GetTypes(), new string[0], "Generated/");

var componentCodeGenerators = new IComponentCodeGenerator[] {
new IndicesLookupGenerator(),
new ComponentExtensionsGenerator()
};

var poolCodeGenerators = new IPoolCodeGenerator[] {
new PoolAttributeGenerator()
};

CodeGenerator.Generate(assembly.GetTypes(), new string[0], "Generated/",
componentCodeGenerators, poolCodeGenerators);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using Entitas;
using Entitas.CodeGenerator;
using UnityEditor;

namespace Entitas.Unity.CodeGenerator {
Expand All @@ -10,7 +11,19 @@ public static class CodeGeneratorEditor {
public static void Generate() {
var types = Assembly.GetAssembly(typeof(Entity)).GetTypes();
var config = new CodeGeneratorConfig(EntitasPreferencesEditor.LoadConfig());
Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath);

var componentCodeGenerators = new IComponentCodeGenerator [] {
new IndicesLookupGenerator(),
new ComponentExtensionsGenerator()
};

var poolCodeGenerators = new IPoolCodeGenerator [] {
new PoolAttributeGenerator()
};

Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath,
componentCodeGenerators, poolCodeGenerators);

AssetDatabase.Refresh();
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ namespace Entitas.CodeGenerator {
public static class CodeGenerator {
public const string componentSuffix = "Component";

public static void Generate(Type[] types, string[] poolNames, string dir) {
public static void Generate(Type[] types, string[] poolNames, string dir,
IComponentCodeGenerator[] componentCodeGenerators, IPoolCodeGenerator[] poolCodeGenerators) {

if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}

CleanDir(dir);
var components = GetComponents(types);
generateIndicesLookup(dir, components);
generateComponentExtensions(dir, components);
generatePoolAttributes(dir, poolNames);

foreach (var generator in componentCodeGenerators) {
var files = generator.Generate(components);
writeFiles(dir, files);
}

foreach (var generator in poolCodeGenerators) {
var files = generator.Generate(poolNames);
writeFiles(dir, files);
}
}

public static void CleanDir(string dir) {
Expand All @@ -37,24 +46,9 @@ public static Type[]GetComponents(Type[] types) {
.ToArray();
}

static void generateIndicesLookup(string dir, Type[] components) {
var files = new IndicesLookupGenerator().Generate(components);
writeFiles(dir, files);
}

static void generateComponentExtensions(string dir, Type[] components) {
var files = new ComponentExtensionsGenerator().Generate(components);
writeFiles(dir, files);
}

static void generatePoolAttributes(string dir, string[] poolNames) {
var files = new PoolAttributeGenerator().Generate(poolNames);
writeFiles(dir, files);
}

static void writeFiles(string dir, CodeGenFile[] files) {
foreach (var file in files) {
File.WriteAllText(file.fileName + ".cs", file.fileContent);
File.WriteAllText(dir + file.fileName + ".cs", file.fileContent);
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public bool isTest {
}
}
}

public Entity IsTest(bool value) {
isTest = value;
return this;
}
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Entitas.Unity.CodeGenerator/Entitas.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Entitas.CodeGenerator.GeneratedFolderPath = Assets/Tests/Generated/
Entitas.CodeGenerator.Pools = Test
Entitas.Unity.CodeGenerator.GeneratedFolderPath = Assets/Generated/
Entitas.Unity.CodeGenerator.GeneratedFolderPath = Assets/Tests/Generated/
Entitas.Unity.CodeGenerator.Pools =

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ namespace Entitas.CodeGenerator {
public static class CodeGenerator {
public const string componentSuffix = "Component";

public static void Generate(Type[] types, string[] poolNames, string dir) {
public static void Generate(Type[] types, string[] poolNames, string dir,
IComponentCodeGenerator[] componentCodeGenerators, IPoolCodeGenerator[] poolCodeGenerators) {

if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}

CleanDir(dir);
var components = GetComponents(types);
generateIndicesLookup(dir, components);
generateComponentExtensions(dir, components);
generatePoolAttributes(dir, poolNames);

foreach (var generator in componentCodeGenerators) {
var files = generator.Generate(components);
writeFiles(dir, files);
}

foreach (var generator in poolCodeGenerators) {
var files = generator.Generate(poolNames);
writeFiles(dir, files);
}
}

public static void CleanDir(string dir) {
Expand All @@ -37,24 +46,9 @@ public static Type[]GetComponents(Type[] types) {
.ToArray();
}

static void generateIndicesLookup(string dir, Type[] components) {
var files = new IndicesLookupGenerator().Generate(components);
writeFiles(dir, files);
}

static void generateComponentExtensions(string dir, Type[] components) {
var files = new ComponentExtensionsGenerator().Generate(components);
writeFiles(dir, files);
}

static void generatePoolAttributes(string dir, string[] poolNames) {
var files = new PoolAttributeGenerator().Generate(poolNames);
writeFiles(dir, files);
}

static void writeFiles(string dir, CodeGenFile[] files) {
foreach (var file in files) {
File.WriteAllText(file.fileName + ".cs", file.fileContent);
File.WriteAllText(dir + file.fileName + ".cs", file.fileContent);
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using Entitas;
using Entitas.CodeGenerator;
using UnityEditor;

namespace Entitas.Unity.CodeGenerator {
Expand All @@ -10,7 +11,19 @@ public static class CodeGeneratorEditor {
public static void Generate() {
var types = Assembly.GetAssembly(typeof(Entity)).GetTypes();
var config = new CodeGeneratorConfig(EntitasPreferencesEditor.LoadConfig());
Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath);

var componentCodeGenerators = new IComponentCodeGenerator [] {
new IndicesLookupGenerator(),
new ComponentExtensionsGenerator()
};

var poolCodeGenerators = new IPoolCodeGenerator [] {
new PoolAttributeGenerator()
};

Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath,
componentCodeGenerators, poolCodeGenerators);

AssetDatabase.Refresh();
}
}
Expand Down
Loading

0 comments on commit 1b621ba

Please sign in to comment.