Skip to content

Commit e99c0b8

Browse files
committed
Added IConfigurable to be able to use additional config keys
1 parent 4bdd5d9 commit e99c0b8

File tree

11 files changed

+227
-41
lines changed

11 files changed

+227
-41
lines changed

Addons/Entitas.CodeGeneration.CodeGenerator.CLI/Program.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static void fixConfig() {
170170
var fileContent = File.ReadAllText(Preferences.configPath);
171171
var properties = new Properties(fileContent);
172172

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

210210
_logger.Debug(config.ToString());
211211

212-
foreach(var key in getUnusedKeys(properties)) {
212+
Type[] types = null;
213+
string[] configurableKeys = null;
214+
215+
try {
216+
types = CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies();
217+
configurableKeys = getConfigurableKeys(
218+
CodeGeneratorUtil.GetUsed<ICodeGeneratorDataProvider>(types, config.dataProviders),
219+
CodeGeneratorUtil.GetUsed<ICodeGenerator>(types, config.codeGenerators),
220+
CodeGeneratorUtil.GetUsed<ICodeGenFilePostProcessor>(types, config.postProcessors)
221+
);
222+
} catch(Exception ex) {
223+
foreach(var key in getUnusedKeys(properties)) {
224+
_logger.Info("Unused key: " + key);
225+
}
226+
227+
foreach(var key in getMissingKeys(properties)) {
228+
_logger.Warn("Missing key: " + key);
229+
}
230+
231+
throw ex;
232+
}
233+
234+
foreach(var key in getUnusedKeys(properties).Where(key => !configurableKeys.Contains(key))) {
213235
_logger.Info("Unused key: " + key);
214236
}
215237

216-
foreach(var key in getMissingKey(properties)) {
238+
foreach(var key in getMissingKeys(properties)) {
217239
_logger.Warn("Missing key: " + key);
218240
}
219241

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

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

240-
static string[] getMissingKey(Properties properties) {
264+
static string[] getMissingKeys(Properties properties) {
241265
return CodeGeneratorConfig.keys
242266
.Where(key => !properties.HasKey(key))
243267
.ToArray();
244268
}
245269

270+
static string[] getConfigurableKeys(ICodeGeneratorDataProvider[] dataProviders, ICodeGenerator[] codeGenerators, ICodeGenFilePostProcessor[] postProcessors) {
271+
return dataProviders.OfType<IConfigurable>()
272+
.Concat(codeGenerators.OfType<IConfigurable>())
273+
.Concat(postProcessors.OfType<IConfigurable>())
274+
.Select(instance => instance.defaultProperties)
275+
.SelectMany(props => props.Keys)
276+
.Distinct()
277+
.ToArray();
278+
}
279+
246280
static void scanDlls() {
247281
if(File.Exists(Preferences.configPath)) {
248282
printTypes(CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies());

Addons/Entitas.CodeGeneration.CodeGenerator/Entitas.CodeGeneration.CodeGenerator/CodeGenerator/CodeGeneratorUtil.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using Entitas.Utils;
44

@@ -84,5 +84,11 @@ public static string[] GetUnavailable<T>(Type[] types, string[] enabledTypeNames
8484
.Where(typeName => !typeNames.Contains(typeName))
8585
.ToArray();
8686
}
87+
88+
public static T[] GetUsed<T>(Type[] types, string[] enabledTypeNames) where T : ICodeGeneratorInterface {
89+
return GetOrderedInstances<T>(types)
90+
.Where(instance => enabledTypeNames.Contains(instance.GetType().ToCompilableString()))
91+
.ToArray();
92+
}
8793
}
8894
}

Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentContextGenerator.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34
using Entitas.Utils;
45

56
namespace Entitas.CodeGeneration.Plugins {
67

7-
public class ComponentContextGenerator : ICodeGenerator {
8+
public class ComponentContextGenerator : ICodeGenerator, IConfigurable {
89

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

15+
const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";
16+
17+
public Dictionary<string, string> defaultProperties {
18+
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
19+
}
20+
21+
bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }
22+
23+
Dictionary<string, string> properties {
24+
get {
25+
if(_properties == null) {
26+
_properties = defaultProperties;
27+
}
28+
29+
return _properties;
30+
}
31+
}
32+
33+
Dictionary<string, string> _properties;
34+
1435
const string STANDARD_COMPONENT_TEMPLATE =
1536
@"public partial class ${ContextName}Context {
1637
@@ -70,7 +91,11 @@ public bool ${prefixedComponentName} {
7091
}
7192
";
7293

73-
public CodeGenFile[] Generate(CodeGeneratorData[] data) {
94+
public void Configure(Dictionary<string, string> properties) {
95+
_properties = properties;
96+
}
97+
98+
public CodeGenFile[] Generate(CodeGeneratorData[] data) {
7499
return data
75100
.OfType<ComponentData>()
76101
.Where(d => d.ShouldGenerateMethods())
@@ -86,8 +111,8 @@ CodeGenFile[] generateExtensions(ComponentData data) {
86111
}
87112

88113
CodeGenFile generateExtension(string contextName, ComponentData data) {
89-
var memberData = data.GetMemberData();
90-
var componentName = data.GetFullTypeName().ToComponentName();
114+
var memberData = data.GetMemberData();
115+
var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces);
91116
var template = memberData.Length == 0
92117
? FLAG_COMPONENT_TEMPLATE
93118
: STANDARD_COMPONENT_TEMPLATE;

Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentEntityGenerator.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1+
using System.Collections.Generic;
12
using System.IO;
23
using System.Linq;
34
using Entitas.Utils;
45

56
namespace Entitas.CodeGeneration.Plugins {
67

7-
public class ComponentEntityGenerator : ICodeGenerator {
8+
public class ComponentEntityGenerator : ICodeGenerator, IConfigurable {
89

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

15+
const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";
16+
17+
public Dictionary<string, string> defaultProperties {
18+
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
19+
}
20+
21+
bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }
22+
23+
Dictionary<string, string> properties {
24+
get {
25+
if(_properties == null) {
26+
_properties = defaultProperties;
27+
}
28+
29+
return _properties;
30+
}
31+
}
32+
33+
Dictionary<string, string> _properties;
34+
1435
const string STANDARD_COMPONENT_TEMPLATE =
1536
@"public partial class ${ContextName}Entity {
1637
@@ -63,7 +84,11 @@ public bool ${prefixedName} {
6384
}
6485
";
6586

66-
public CodeGenFile[] Generate(CodeGeneratorData[] data) {
87+
public void Configure(Dictionary<string, string> properties) {
88+
_properties = properties;
89+
}
90+
91+
public CodeGenFile[] Generate(CodeGeneratorData[] data) {
6792
return data
6893
.OfType<ComponentData>()
6994
.Where(d => d.ShouldGenerateMethods())
@@ -77,8 +102,8 @@ CodeGenFile[] generateExtensions(ComponentData data) {
77102
.ToArray();
78103
}
79104

80-
CodeGenFile generateExtension(string contextName, ComponentData data) {
81-
var componentName = data.GetFullTypeName().ToComponentName();
105+
CodeGenFile generateExtension(string contextName, ComponentData data) {
106+
var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces);
82107
var index = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + "." + componentName;
83108
var memberData = data.GetMemberData();
84109
var template = memberData.Length == 0

Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentMatcherGenerator.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34

45
namespace Entitas.CodeGeneration.Plugins {
56

6-
public class ComponentMatcherGenerator : ICodeGenerator {
7+
public class ComponentMatcherGenerator : ICodeGenerator, IConfigurable {
78

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

14+
const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";
15+
16+
public Dictionary<string, string> defaultProperties {
17+
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
18+
}
19+
20+
bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }
21+
22+
Dictionary<string, string> properties {
23+
get {
24+
if(_properties == null) {
25+
_properties = defaultProperties;
26+
}
27+
28+
return _properties;
29+
}
30+
}
31+
32+
Dictionary<string, string> _properties;
33+
1334
const string STANDARD_COMPONENT_TEMPLATE =
1435
@"public sealed partial class ${ContextName}Matcher {
1536
@@ -29,6 +50,10 @@ public static Entitas.IMatcher<${ContextName}Entity> ${ComponentName} {
2950
}
3051
";
3152

53+
public void Configure(Dictionary<string, string> properties) {
54+
_properties = properties;
55+
}
56+
3257
public CodeGenFile[] Generate(CodeGeneratorData[] data) {
3358
return data
3459
.OfType<ComponentData>()
@@ -44,7 +69,7 @@ CodeGenFile[] generateMatcher(ComponentData data) {
4469
}
4570

4671
CodeGenFile generateMatcher(string contextName, ComponentData data) {
47-
var componentName = data.GetFullTypeName().ToComponentName();
72+
var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces);
4873
var index = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + "." + componentName;
4974
var componentNames = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + ".componentNames";
5075

Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentsLookupGenerator.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using System.IO;
44

55
namespace Entitas.CodeGeneration.Plugins {
66

7-
public class ComponentsLookupGenerator : ICodeGenerator {
7+
public class ComponentsLookupGenerator : ICodeGenerator, IConfigurable {
88

99
public string name { get { return "Components Lookup"; } }
1010
public int priority { get { return 0; } }
1111
public bool isEnabledByDefault { get { return true; } }
1212
public bool runInDryMode { get { return true; } }
1313

14+
const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces";
15+
16+
public Dictionary<string, string> defaultProperties {
17+
get { return new Dictionary<string, string> { { IGNORE_NAMESPACES_KEY, "false" } }; }
18+
}
19+
20+
bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } }
21+
22+
Dictionary<string, string> properties {
23+
get {
24+
if(_properties == null) {
25+
_properties = defaultProperties;
26+
}
27+
28+
return _properties;
29+
}
30+
}
31+
32+
Dictionary<string, string> _properties;
33+
1434
public const string COMPONENTS_LOOKUP = "ComponentsLookup";
1535

1636
const string COMPONENTS_LOOKUP_TEMPLATE =
@@ -42,6 +62,10 @@ public class ComponentsLookupGenerator : ICodeGenerator {
4262
const string COMPONENT_TYPES_TEMPLATE =
4363
@" typeof(${ComponentType})";
4464

65+
public void Configure(Dictionary<string, string> properties) {
66+
_properties = properties;
67+
}
68+
4569
public CodeGenFile[] Generate(CodeGeneratorData[] data) {
4670
var files = new List<CodeGenFile>();
4771

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

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

115139
var componentNames = string.Join(",\n", data
116140
.Select(d => COMPONENT_NAMES_TEMPLATE
117-
.Replace("${ComponentName}", d.GetFullTypeName().ToComponentName())
141+
.Replace("${ComponentName}", d.GetFullTypeName().ToComponentName(ignoreNamespaces))
118142
).ToArray());
119143

120144
var componentTypes = string.Join(",\n", data

0 commit comments

Comments
 (0)