Skip to content

Commit

Permalink
Added IConfigurable to be able to use additional config keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sschmid committed Apr 10, 2017
1 parent 4bdd5d9 commit e99c0b8
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 41 deletions.
44 changes: 39 additions & 5 deletions Addons/Entitas.CodeGeneration.CodeGenerator.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static void fixConfig() {
var fileContent = File.ReadAllText(Preferences.configPath);
var properties = new Properties(fileContent);

foreach(var key in getMissingKey(properties)) {
foreach(var key in getMissingKeys(properties)) {
_logger.Info("Add missing key: '" + key + "' ? (y / n)");
if(getUserDecision()) {
properties[key] = string.Empty;
Expand Down Expand Up @@ -209,15 +209,39 @@ static void status() {

_logger.Debug(config.ToString());

foreach(var key in getUnusedKeys(properties)) {
Type[] types = null;
string[] configurableKeys = null;

try {
types = CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies();
configurableKeys = getConfigurableKeys(
CodeGeneratorUtil.GetUsed<ICodeGeneratorDataProvider>(types, config.dataProviders),
CodeGeneratorUtil.GetUsed<ICodeGenerator>(types, config.codeGenerators),
CodeGeneratorUtil.GetUsed<ICodeGenFilePostProcessor>(types, config.postProcessors)
);
} catch(Exception ex) {
foreach(var key in getUnusedKeys(properties)) {
_logger.Info("Unused key: " + key);
}

foreach(var key in getMissingKeys(properties)) {
_logger.Warn("Missing key: " + key);
}

throw ex;
}

foreach(var key in getUnusedKeys(properties).Where(key => !configurableKeys.Contains(key))) {
_logger.Info("Unused key: " + key);
}

foreach(var key in getMissingKey(properties)) {
foreach(var key in getMissingKeys(properties)) {
_logger.Warn("Missing key: " + key);
}

var types = CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies();
foreach(var key in configurableKeys.Where(key => !properties.HasKey(key))) {
_logger.Warn("Missing key: " + key);
}

printUnavailable(CodeGeneratorUtil.GetUnavailable<ICodeGeneratorDataProvider>(types, config.dataProviders));
printUnavailable(CodeGeneratorUtil.GetUnavailable<ICodeGenerator>(types, config.codeGenerators));
Expand All @@ -237,12 +261,22 @@ static string[] getUnusedKeys(Properties properties) {
.ToArray();
}

static string[] getMissingKey(Properties properties) {
static string[] getMissingKeys(Properties properties) {
return CodeGeneratorConfig.keys
.Where(key => !properties.HasKey(key))
.ToArray();
}

static string[] getConfigurableKeys(ICodeGeneratorDataProvider[] dataProviders, ICodeGenerator[] codeGenerators, ICodeGenFilePostProcessor[] postProcessors) {
return dataProviders.OfType<IConfigurable>()
.Concat(codeGenerators.OfType<IConfigurable>())
.Concat(postProcessors.OfType<IConfigurable>())
.Select(instance => instance.defaultProperties)
.SelectMany(props => props.Keys)
.Distinct()
.ToArray();
}

static void scanDlls() {
if(File.Exists(Preferences.configPath)) {
printTypes(CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Entitas.Utils;

Expand Down Expand Up @@ -84,5 +84,11 @@ public static string[] GetUnavailable<T>(Type[] types, string[] enabledTypeNames
.Where(typeName => !typeNames.Contains(typeName))
.ToArray();
}

public static T[] GetUsed<T>(Type[] types, string[] enabledTypeNames) where T : ICodeGeneratorInterface {
return GetOrderedInstances<T>(types)
.Where(instance => enabledTypeNames.Contains(instance.GetType().ToCompilableString()))
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Entitas.Utils;

namespace Entitas.CodeGeneration.Plugins {

public class ComponentContextGenerator : ICodeGenerator {
public class ComponentContextGenerator : ICodeGenerator, IConfigurable {

public string name { get { return "Component (Context API)"; } }
public int priority { get { return 0; } }
public bool isEnabledByDefault { get { return true; } }
public bool runInDryMode { get { return true; } }

const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";

public Dictionary<string, string> defaultProperties {
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
}

bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }

Dictionary<string, string> properties {
get {
if(_properties == null) {
_properties = defaultProperties;
}

return _properties;
}
}

Dictionary<string, string> _properties;

const string STANDARD_COMPONENT_TEMPLATE =
@"public partial class ${ContextName}Context {
Expand Down Expand Up @@ -70,7 +91,11 @@ public bool ${prefixedComponentName} {
}
";

public CodeGenFile[] Generate(CodeGeneratorData[] data) {
public void Configure(Dictionary<string, string> properties) {
_properties = properties;
}

public CodeGenFile[] Generate(CodeGeneratorData[] data) {
return data
.OfType<ComponentData>()
.Where(d => d.ShouldGenerateMethods())
Expand All @@ -86,8 +111,8 @@ CodeGenFile[] generateExtensions(ComponentData data) {
}

CodeGenFile generateExtension(string contextName, ComponentData data) {
var memberData = data.GetMemberData();
var componentName = data.GetFullTypeName().ToComponentName();
var memberData = data.GetMemberData();
var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces);
var template = memberData.Length == 0
? FLAG_COMPONENT_TEMPLATE
: STANDARD_COMPONENT_TEMPLATE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Entitas.Utils;

namespace Entitas.CodeGeneration.Plugins {

public class ComponentEntityGenerator : ICodeGenerator {
public class ComponentEntityGenerator : ICodeGenerator, IConfigurable {

public string name { get { return "Component (Entity API)"; } }
public int priority { get { return 0; } }
public bool isEnabledByDefault { get { return true; } }
public bool runInDryMode { get { return true; } }

const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";

public Dictionary<string, string> defaultProperties {
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
}

bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }

Dictionary<string, string> properties {
get {
if(_properties == null) {
_properties = defaultProperties;
}

return _properties;
}
}

Dictionary<string, string> _properties;

const string STANDARD_COMPONENT_TEMPLATE =
@"public partial class ${ContextName}Entity {
Expand Down Expand Up @@ -63,7 +84,11 @@ public bool ${prefixedName} {
}
";

public CodeGenFile[] Generate(CodeGeneratorData[] data) {
public void Configure(Dictionary<string, string> properties) {
_properties = properties;
}

public CodeGenFile[] Generate(CodeGeneratorData[] data) {
return data
.OfType<ComponentData>()
.Where(d => d.ShouldGenerateMethods())
Expand All @@ -77,8 +102,8 @@ CodeGenFile[] generateExtensions(ComponentData data) {
.ToArray();
}

CodeGenFile generateExtension(string contextName, ComponentData data) {
var componentName = data.GetFullTypeName().ToComponentName();
CodeGenFile generateExtension(string contextName, ComponentData data) {
var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces);
var index = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + "." + componentName;
var memberData = data.GetMemberData();
var template = memberData.Length == 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Entitas.CodeGeneration.Plugins {

public class ComponentMatcherGenerator : ICodeGenerator {
public class ComponentMatcherGenerator : ICodeGenerator, IConfigurable {

public string name { get { return "Component (Matcher API)"; } }
public int priority { get { return 0; } }
public bool isEnabledByDefault { get { return true; } }
public bool runInDryMode { get { return true; } }

const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";

public Dictionary<string, string> defaultProperties {
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
}

bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }

Dictionary<string, string> properties {
get {
if(_properties == null) {
_properties = defaultProperties;
}

return _properties;
}
}

Dictionary<string, string> _properties;

const string STANDARD_COMPONENT_TEMPLATE =
@"public sealed partial class ${ContextName}Matcher {
Expand All @@ -29,6 +50,10 @@ public static Entitas.IMatcher<${ContextName}Entity> ${ComponentName} {
}
";

public void Configure(Dictionary<string, string> properties) {
_properties = properties;
}

public CodeGenFile[] Generate(CodeGeneratorData[] data) {
return data
.OfType<ComponentData>()
Expand All @@ -44,7 +69,7 @@ CodeGenFile[] generateMatcher(ComponentData data) {
}

CodeGenFile generateMatcher(string contextName, ComponentData data) {
var componentName = data.GetFullTypeName().ToComponentName();
var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces);
var index = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + "." + componentName;
var componentNames = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + ".componentNames";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace Entitas.CodeGeneration.Plugins {

public class ComponentsLookupGenerator : ICodeGenerator {
public class ComponentsLookupGenerator : ICodeGenerator, IConfigurable {

public string name { get { return "Components Lookup"; } }
public int priority { get { return 0; } }
public bool isEnabledByDefault { get { return true; } }
public bool runInDryMode { get { return true; } }

const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";

public Dictionary<string, string> defaultProperties {
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
}

bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }

Dictionary<string, string> properties {
get {
if(_properties == null) {
_properties = defaultProperties;
}

return _properties;
}
}

Dictionary<string, string> _properties;

public const string COMPONENTS_LOOKUP = "ComponentsLookup";

const string COMPONENTS_LOOKUP_TEMPLATE =
Expand Down Expand Up @@ -42,6 +62,10 @@ public class ComponentsLookupGenerator : ICodeGenerator {
const string COMPONENT_TYPES_TEMPLATE =
@" typeof(${ComponentType})";

public void Configure(Dictionary<string, string> properties) {
_properties = properties;
}

public CodeGenFile[] Generate(CodeGeneratorData[] data) {
var files = new List<CodeGenFile>();

Expand Down Expand Up @@ -105,7 +129,7 @@ CodeGenFile generateComponentsLookupClass(string contextName, ComponentData[] da
var componentConstants = string.Join("\n", data
.Select((d, index) => {
return COMPONENT_CONSTANTS_TEMPLATE
.Replace("${ComponentName}", d.GetFullTypeName().ToComponentName())
.Replace("${ComponentName}", d.GetFullTypeName().ToComponentName(ignoreNamespaces))
.Replace("${Index}", index.ToString());
}).ToArray());

Expand All @@ -114,7 +138,7 @@ CodeGenFile generateComponentsLookupClass(string contextName, ComponentData[] da

var componentNames = string.Join(",\n", data
.Select(d => COMPONENT_NAMES_TEMPLATE
.Replace("${ComponentName}", d.GetFullTypeName().ToComponentName())
.Replace("${ComponentName}", d.GetFullTypeName().ToComponentName(ignoreNamespaces))
).ToArray());

var componentTypes = string.Join(",\n", data
Expand Down
Loading

0 comments on commit e99c0b8

Please sign in to comment.