private void GenerateEditor(ExternalType type, string name) { cw.Indented("public partial class "); sb.Append(name); bool isLookupEditor = HasBaseType(type, "Serenity.LookupEditorBase`1") || HasBaseType(type, "Serenity.LookupEditorBase"); bool isServiceLookupEditor = HasBaseType(type, "Serenity.ServiceLookupEditorBase`1") || HasBaseType(type, "Serenity.ServiceLookupEditorBase"); sb.Append(" : "); sb.AppendLine(isLookupEditor ? "LookupEditorBaseAttribute" : (isServiceLookupEditor ? "ServiceLookupEditorBaseAttribute" : "CustomEditorAttribute")); cw.InBrace(delegate { cw.Indented("public const string Key = \""); sb.Append(type.FullName); sb.AppendLine("\";"); sb.AppendLine(); cw.Indented("public "); sb.Append(name); sb.AppendLine("()"); cw.IndentedLine(" : base(Key)"); cw.IndentedLine("{"); cw.IndentedLine("}"); GenerateOptionMembers(type, skip: isLookupEditor?lookupEditorBaseOptions: (isServiceLookupEditor ? serviceLookupEditorBaseOptions : null), isWidget: true); }); }
private bool IsEditorType(ExternalType type) { if (type.IsAbstract) { return(false); } if (type.GenericParameters.Count > 0) { return(false); } if (!HasBaseType(type, "Serenity.Widget") && !HasBaseType(type, "Serenity.Widget<any>")) { return(false); } if (type.AssemblyName != null && type.AssemblyName.StartsWith("Serenity.", StringComparison.Ordinal)) { return(false); } return (GetAttribute(type, "Serenity.EditorAttribute", inherited: true) != null || GetAttribute(type, "Serenity.ElementAttribute", inherited: true) != null || GetAttribute(type, "Serenity.Decorators.registerEditor", inherited: true) != null || GetAttribute(type, "Serenity.Decorators.editor", inherited: true) != null || GetAttribute(type, "Serenity.Decorators.element", inherited: true) != null || HasBaseType(type, "Serenity.Extensions.GridEditorBase") || HasBaseType(type, "Serenity.LookupEditorBase")); }
private SortedDictionary <string, ExternalMember> GetOptionMembers(ExternalType type, bool isWidget) { var result = new SortedDictionary <string, ExternalMember>(); var constructor = type.Methods.FirstOrDefault(x => x.IsConstructor && x.Arguments.Count == (isWidget ? 2 : 1)); if (constructor != null) { if (!isWidget || (constructor.Arguments[0].Type == "jQueryApi.jQueryObject" || constructor.Arguments[0].Type == "JQuery")) { var optionsType = GetScriptType(constructor.Arguments[isWidget ? 1 : 0].Type); AddOptionMembers(result, optionsType, isOptions: true); } } int loop = 0; do { if (type.Namespace.StartsWith("System")) { break; } AddOptionMembers(result, type, isOptions: false); }while ((type = GetBaseType(type)) != null && loop++ < 100); return(result); }
protected ExternalAttribute GetAttribute(ExternalType type, string attributeName, bool inherited) { var attr = type.Attributes.FirstOrDefault(x => x.Type == attributeName); if (attr != null) { return(attr); } if (!inherited) { return(null); } int loop = 0; while ((type = GetBaseType(type)) != null) { attr = type.Attributes.FirstOrDefault(x => x.Type == attributeName); if (attr != null) { return(attr); } if (loop++ > 100) { return(null); } } ; return(null); }
private void GenerateType(ExternalType type) { bool isEditorType = IsEditorType(type); bool isFormatterType = IsFormatterType(type); if (!isEditorType && !isFormatterType) return; AppendUsings(new string[] { "Serenity", "Serenity.ComponentModel", "System", "System.Collections", "System.Collections.Generic", "System.ComponentModel" }); sb.AppendLine(); var ns = GetNamespace(type.Namespace); string name = type.Name + "Attribute"; cw.Indented("namespace "); sb.AppendLine(ns); cw.InBrace(delegate { if (isEditorType) GenerateEditor(type, name); else if (isFormatterType) GenerateFormatter(type, name); }); AddFile(RemoveRootNamespace(ns, name) + ".cs"); }
public void AddSSType(ExternalType type) { type.Origin = ExternalTypeOrigin.SS; var oldFullName = type.FullName; ssByClassName[oldFullName] = type; var ignoreNS = type.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.IgnoreNamespaceAttribute"); if (ignoreNS != null) type.Namespace = ""; var scriptNS = type.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.ScriptNamespaceAttribute"); if (scriptNS != null) type.Namespace = scriptNS.Arguments[0].Value as string; var scriptName = type.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.ScriptNameAttribute"); if (scriptName != null) type.Name = scriptName.Arguments[0].Value as string; ExternalType overriding; if (ssByScriptName.TryGetValue(type.FullName, out overriding) && type.GenericParameters.Count <= overriding.GenericParameters.Count) { return; } ssByScriptName[type.FullName] = type; }
protected virtual string ShortenNamespace(ExternalType type, string codeNamespace) { string ns = type.Namespace ?? ""; if ((codeNamespace != null && (ns == codeNamespace)) || (codeNamespace != null && codeNamespace.StartsWith((ns + ".")))) { return(""); } if (IsUsingNamespace(ns)) { return(""); } if (codeNamespace != null) { var idx = codeNamespace.IndexOf('.'); if (idx >= 0 && ns.StartsWith(codeNamespace.Substring(0, idx + 1))) { return(ns.Substring(idx + 1)); } } return(ns); }
private void AddOptionMembers(SortedDictionary<string, ExternalMember> dict, ExternalType type, bool isOptions) { List<ExternalMember> members = new List<ExternalMember>(); members.AddRange(type.Properties); if (type.Origin == ExternalTypeOrigin.TS) { members.AddRange(type.Fields); members.AddRange(type.Methods.Where(x => x.Arguments.Count == 1)); } foreach (var member in members) { if (dict.ContainsKey(member.Name)) continue; if (type.Origin == ExternalTypeOrigin.SS) { if (member.Attributes.Any(x => x.Type == "System.ComponentModel.HiddenAttribute")) continue; var prop = member as ExternalProperty; if (prop == null) continue; if (string.IsNullOrEmpty(prop.GetMethod) && string.IsNullOrEmpty(prop.SetMethod)) return; var getMethod = type.Methods.FirstOrDefault(x => x.Name == prop.GetMethod); var setMethod = type.Methods.FirstOrDefault(x => x.Name == prop.SetMethod); if (getMethod == null || setMethod == null || getMethod.IsProtected || setMethod.IsProtected) continue; } else if (type.Origin == ExternalTypeOrigin.TS) { } if (member.Type.StartsWith("System.Func`") || member.Type.StartsWith("System.Action`") || member.Type == "System.Delegate" || member.Type.Contains("System.TypeOption") || member.Type == "Function") continue; if (!isOptions && !member.Attributes.Any(x => x.Type == "System.ComponentModel.DisplayNameAttribute" || x.Type == "Serenity.OptionAttribute" || x.Type == "Serenity.Decorators.option" || x.Type == "Serenity.Decorators.displayName")) continue; dict[member.Name] = member; } }
protected string GetBaseTypeName(ExternalType type) { if (type.BaseType == null) { return(null); } var baseType = type.BaseType; SplitGenericArguments(ref baseType); return(baseType); }
private void SSDeclarationProperty(ExternalType type, ExternalProperty prop, string codeNamespace, bool isStaticClass, bool isSerializable, bool preserveMemberCase) { if (string.IsNullOrEmpty(prop.GetMethod) && string.IsNullOrEmpty(prop.SetMethod)) { return; } var propName = GetPropertyScriptName(prop, preserveMemberCase); if (isSerializable || prop.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.IntrinsicPropertyAttribute") != null) { if (isStaticClass && prop.IsStatic) { cw.Indented("let "); sb.Append(propName); } else if (prop.IsStatic) { cw.Indented("static "); sb.Append(propName); } else { cw.Indented(propName); } sb.Append(": "); SSTypeNameToTS(prop.Type, codeNamespace); sb.AppendLine(";"); } else { var getMethod = type.Methods.FirstOrDefault(x => x.Name == prop.GetMethod); if (getMethod != null) { getMethod.Name = "get_" + propName; SSDeclarationMethodInternal(getMethod, codeNamespace, isStaticClass, preserveMemberCase); } var setMethod = type.Methods.FirstOrDefault(x => x.Name == prop.SetMethod); if (setMethod != null) { setMethod.Name = "set_" + propName; SSDeclarationMethodInternal(setMethod, codeNamespace, isStaticClass, preserveMemberCase); } } }
private bool IsFormatterType(ExternalType type) { if (type.IsAbstract) return false; if (type.GenericParameters.Count > 0) return false; if (type.AssemblyName != null && type.AssemblyName.StartsWith("Serenity.")) return false; return type.Interfaces.Any(x => x == "Serenity.ISlickFormatter" || x == "Slick.Formatter"); }
private static bool IsFormatterType(ExternalType type) { if (type.IsAbstract) { return(false); } if (type.GenericParameters.Count > 0) { return(false); } return(type.Interfaces.Any(x => x == "Serenity.ISlickFormatter" || x == "Slick.Formatter")); }
private void SSDeclarationBaseTypeReference(ExternalType type, string baseType, string codeNamespace) { if (string.IsNullOrEmpty(baseType)) { return; } if (baseType == "System.Object") { return; } sb.Append(" extends "); SSTypeNameToTS(baseType, codeNamespace, "Object", type.GenericParameters.Select(x => x.Name).ToArray()); }
private void SSDeclarationField(ExternalType type, ExternalMember field, string codeNamespace, bool isStaticClass, bool isSerializable, bool preserveMemberCase) { string fieldName = field.Name; var scriptNameAttr = field.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.ScriptNameAttribute"); if (scriptNameAttr != null) { fieldName = scriptNameAttr.Arguments[0].Value as string; } else if (!preserveMemberCase && !field.Attributes.Any(x => x.Type == "System.Runtime.CompilerServices.PreserveCaseAttribute")) { if (fieldName == "ID") { fieldName = "id"; } else { fieldName = fieldName.Substring(0, 1).ToLowerInvariant() + fieldName.Substring(1); } } if (isStaticClass && field.IsStatic) { cw.Indented("let "); sb.Append(fieldName); } else if (field.IsStatic) { cw.Indented("static "); sb.Append(fieldName); } else { cw.Indented(fieldName); } sb.Append(": "); SSTypeNameToTS(field.Type, codeNamespace); sb.AppendLine(";"); }
protected string ShortenFullName(ExternalType type, string codeNamespace) { if (type.FullName == "Serenity.Widget") { return("Serenity.Widget<any>"); } var ns = ShortenNamespace(type, codeNamespace); if (!string.IsNullOrEmpty(ns)) { return(ns + "." + type.Name); } else { return(type.Name); } }
protected bool HasBaseType(ExternalType type, string typeName) { int loop = 0; while (type != null) { if (loop++ > 100) { break; } var baseTypeName = GetBaseTypeName(type); if (baseTypeName == typeName) { return(true); } var ns = type.Namespace; type = GetScriptType(baseTypeName); if (type == null && ns != null) { var nsParts = ns.Split('.'); for (var i = nsParts.Length; i > 0; i--) { var prefixed = string.Join('.', nsParts.Take(i)) + '.' + baseTypeName; if (prefixed == typeName) { return(true); } type = GetScriptType(prefixed); if (type != null) { break; } } } } ; return(false); }
public void AddSSType(ExternalType type) { type.Origin = ExternalTypeOrigin.SS; var oldFullName = type.FullName; ssByClassName[oldFullName] = type; var ignoreNS = type.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.IgnoreNamespaceAttribute"); if (ignoreNS != null) { type.Namespace = ""; } var scriptNS = type.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.ScriptNamespaceAttribute"); if (scriptNS != null) { type.Namespace = scriptNS.Arguments[0].Value as string; } var scriptName = type.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.ScriptNameAttribute"); if (scriptName != null) { type.Name = scriptName.Arguments[0].Value as string; } ExternalType overriding; if (ssByScriptName.TryGetValue(type.FullName, out overriding) && type.GenericParameters.Count <= overriding.GenericParameters.Count) { return; } ssByScriptName[type.FullName] = type; }
private void GenerateType(ExternalType type) { bool isEditorType = IsEditorType(type); bool isFormatterType = IsFormatterType(type); if (!isEditorType && !isFormatterType) { return; } AppendUsings(new string[] { "Serenity", "Serenity.ComponentModel", "System", "System.Collections", "System.Collections.Generic", "System.ComponentModel" }); sb.AppendLine(); var ns = GetNamespace(type.Namespace); string name = type.Name + "Attribute"; cw.Indented("namespace "); sb.AppendLine(ns); cw.InBrace(delegate { if (isEditorType) { GenerateEditor(type, name); } else if (isFormatterType) { GenerateFormatter(type, name); } }); AddFile(RemoveRootNamespace(ns, name) + ".cs"); }
private static bool IsFormatterType(ExternalType type) { if (type.IsAbstract) { return(false); } if (type.GenericParameters.Count > 0) { return(false); } if (type.AssemblyName != null && type.AssemblyName.StartsWith("Serenity.", StringComparison.Ordinal)) { return(false); } return(type.Interfaces.Any(x => x == "Serenity.ISlickFormatter" || x == "Slick.Formatter")); }
private void GenerateFormatter(ExternalType type, string name) { cw.Indented("public partial class "); sb.Append(name); sb.AppendLine(" : CustomFormatterAttribute"); cw.InBrace(delegate { cw.Indented("public const string Key = \""); sb.Append(type.FullName); sb.AppendLine("\";"); sb.AppendLine(); cw.Indented("public "); sb.Append(name); sb.AppendLine("()"); cw.IndentedLine(" : base(Key)"); cw.IndentedLine("{"); cw.IndentedLine("}"); GenerateOptionMembers(type, skip: null, isWidget: false); }); }
private void GenerateFormatter(ExternalType type, string name) { cw.Indented("public partial class "); sb.Append(name); sb.AppendLine(" : CustomFormatterAttribute"); cw.InBrace(delegate { cw.Indented("public const string Key = \""); sb.Append(type.FullName); sb.AppendLine("\";"); sb.AppendLine(); cw.Indented("public "); sb.Append(name); sb.AppendLine("()"); cw.IndentedLine(" : base(Key)"); cw.IndentedLine("{"); cw.IndentedLine("}"); GenerateOptionMembers(type, skip: null, isWidget: false); }); }
protected bool HasBaseType(ExternalType type, string typeName) { int loop = 0; while (type != null) { if (loop++ > 100) { break; } var baseTypeName = GetBaseTypeName(type); if (baseTypeName == typeName) { return(true); } type = GetScriptType(baseTypeName); } ; return(false); }
private void GenerateForm(TypeDefinition type, CustomAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); var identifier = type.Name; if (identifier.EndsWith(requestSuffix, StringComparison.Ordinal) && CecilUtils.IsSubclassOf(type, "Serenity.Services", "ServiceRequest")) { identifier = identifier.Substring(0, identifier.Length - requestSuffix.Length) + "Form"; fileIdentifier = identifier; } cw.Indented("export interface "); sb.Append(identifier); var propertyNames = new List <string>(); var propertyTypes = new List <string>(); TypeDefinition basedOnRow = null; var basedOnRowAttr = CecilUtils.GetAttr(type, "Serenity.ComponentModel", "BasedOnRowAttribute"); if (basedOnRowAttr != null && basedOnRowAttr.ConstructorArguments.Count > 0 && basedOnRowAttr.ConstructorArguments[0].Type.FullName == "System.Type") { basedOnRow = (basedOnRowAttr.ConstructorArguments[0].Value as TypeReference).Resolve(); } var rowAnnotations = basedOnRow != null?GetAnnotationTypesFor(basedOnRow) : null; ILookup <string, PropertyDefinition> basedOnByName = null; if (basedOnRowAttr != null) { basedOnByName = basedOnRow.Properties.Where(x => CecilUtils.IsPublicInstanceProperty(x)) .ToLookup(x => x.Name); } cw.InBrace(delegate { foreach (var item in type.Properties) { if (!CecilUtils.IsPublicInstanceProperty(item)) { continue; } PropertyDefinition basedOnField = null; if (basedOnByName != null) { basedOnField = basedOnByName[item.Name].FirstOrDefault(); } if (CecilUtils.FindAttr(item.CustomAttributes, "Serenity.ComponentModel", "IgnoreAttribute") != null) { continue; } if (basedOnField != null) { if (CecilUtils.FindAttr(basedOnField.CustomAttributes, "Serenity.ComponentModel", "IgnoreAttribute") != null) { continue; } bool ignored = false; foreach (var annotationType in rowAnnotations) { if (annotationType.PropertyByName.TryGetValue(item.Name, out PropertyDefinition annotation) && CecilUtils.FindAttr(annotation.CustomAttributes, "Serenity.ComponentModel", "IgnoreAttribute") != null) { ignored = true; break; } } if (ignored) { continue; } } var editorTypeAttr = CecilUtils.FindAttr(item.CustomAttributes, "Serenity.ComponentModel", "EditorTypeAttribute"); if (editorTypeAttr == null && basedOnField != null) { editorTypeAttr = CecilUtils.FindAttr(basedOnField.CustomAttributes, "Serenity.ComponentModel", "EditorTypeAttribute"); } if (editorTypeAttr == null && basedOnRow != null) { foreach (var annotationType in rowAnnotations) { if (!annotationType.PropertyByName.TryGetValue(item.Name, out PropertyDefinition annotation)) { continue; } editorTypeAttr = CecilUtils.FindAttr(annotation.CustomAttributes, "Serenity.ComponentModel", "EditorTypeAttribute"); if (editorTypeAttr != null) { break; } } } var editorType = GetEditorTypeKeyFrom(item.PropertyType, basedOnField?.PropertyType, editorTypeAttr); ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullName = ShortenFullName(scriptType, codeNamespace); var shortName = fullName; if (fullName.StartsWith("Serenity.", StringComparison.Ordinal)) { shortName = "s." + fullName["Serenity.".Length..];
private void GenerateForm(TypeDefinition type, CustomAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); var identifier = type.Name; if (identifier.EndsWith(requestSuffix, StringComparison.Ordinal) && CecilUtils.IsSubclassOf(type, "Serenity.Services", "ServiceRequest")) { identifier = identifier.Substring(0, identifier.Length - requestSuffix.Length) + "Form"; this.fileIdentifier = identifier; } cw.Indented("export interface "); sb.Append(identifier); var propertyNames = new List <string>(); var propertyTypes = new List <string>(); TypeDefinition basedOnRow = null; var basedOnRowAttr = CecilUtils.GetAttr(type, "Serenity.ComponentModel", "BasedOnRowAttribute"); if (basedOnRowAttr != null && basedOnRowAttr.ConstructorArguments.Count > 0 && basedOnRowAttr.ConstructorArguments[0].Type.FullName == "System.Type") { basedOnRow = (basedOnRowAttr.ConstructorArguments[0].Value as TypeReference).Resolve(); } var rowAnnotations = basedOnRow != null?GetAnnotationTypesFor(basedOnRow) : null; ILookup <string, PropertyDefinition> basedOnByName = null; if (basedOnRowAttr != null) { basedOnByName = basedOnRow.Properties.Where(x => CecilUtils.IsPublicInstanceProperty(x)) .ToLookup(x => x.Name); } cw.InBrace(delegate { foreach (var item in type.Properties) { if (!CecilUtils.IsPublicInstanceProperty(item)) { continue; } PropertyDefinition basedOnField = null; if (basedOnByName != null) { basedOnField = basedOnByName[item.Name].FirstOrDefault(); } if (CecilUtils.FindAttr(item.CustomAttributes, "Serenity.ComponentModel", "IgnoreAttribute") != null) { continue; } if (basedOnField != null) { if (CecilUtils.FindAttr(basedOnField.CustomAttributes, "Serenity.ComponentModel", "IgnoreAttribute") != null) { continue; } bool ignored = false; foreach (var annotationType in rowAnnotations) { PropertyDefinition annotation; if (annotationType.PropertyByName.TryGetValue(item.Name, out annotation) && CecilUtils.FindAttr(annotation.CustomAttributes, "Serenity.ComponentModel", "IgnoreAttribute") != null) { ignored = true; break; } } if (ignored) { continue; } } var editorTypeAttr = CecilUtils.FindAttr(item.CustomAttributes, "Serenity.ComponentModel", "EditorTypeAttribute"); if (editorTypeAttr == null && basedOnField != null) { editorTypeAttr = CecilUtils.FindAttr(basedOnField.CustomAttributes, "Serenity.ComponentModel", "EditorTypeAttribute"); } if (editorTypeAttr == null && basedOnRow != null) { foreach (var annotationType in rowAnnotations) { PropertyDefinition annotation; if (!annotationType.PropertyByName.TryGetValue(item.Name, out annotation)) { continue; } editorTypeAttr = CecilUtils.FindAttr(annotation.CustomAttributes, "Serenity.ComponentModel", "EditorTypeAttribute"); if (editorTypeAttr != null) { break; } } } var editorType = GetEditorTypeKeyFrom(item.PropertyType, basedOnField != null ? basedOnField.PropertyType : (TypeReference)null, editorTypeAttr); ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullName = ShortenFullName(scriptType, codeNamespace); var shortName = fullName; if (fullName.StartsWith("Serenity.", StringComparison.Ordinal)) { shortName = "s." + fullName.Substring("Serenity.".Length); } propertyNames.Add(item.Name); propertyTypes.Add(shortName); cw.Indented(item.Name); sb.Append(": "); sb.Append(fullName); sb.AppendLine(";"); } }); sb.AppendLine(); cw.Indented("export class "); sb.Append(identifier); sb.Append(" extends Serenity.PrefixedContext"); cw.InBrace(delegate { cw.Indented("static formKey = '"); sb.Append(formScriptAttribute.ConstructorArguments[0].Value as string); sb.AppendLine("';"); if (propertyNames.Count > 0) { cw.IndentedLine("private static init: boolean;"); sb.AppendLine(); cw.Indented("constructor(prefix: string)"); cw.InBrace(delegate { cw.IndentedLine("super(prefix);"); sb.AppendLine(); cw.Indented("if (!"); sb.Append(identifier); sb.Append(".init) "); cw.InBrace(delegate { cw.Indented(identifier); sb.AppendLine(".init = true;"); sb.AppendLine(); cw.IndentedLine("var s = Serenity;"); var typeNumber = new Dictionary <string, int>(); foreach (var s in propertyTypes) { if (!typeNumber.ContainsKey(s)) { cw.Indented("var w"); sb.Append(typeNumber.Count); sb.Append(" = "); sb.Append(s); sb.AppendLine(";"); typeNumber[s] = typeNumber.Count; } } sb.AppendLine(); cw.Indented("Q.initFormType("); sb.Append(identifier); sb.AppendLine(", ["); cw.Block(delegate { for (var i = 0; i < propertyNames.Count; i++) { if (i > 0) { sb.AppendLine(","); } cw.Indented("'"); sb.Append(propertyNames[i]); sb.Append("', w"); sb.Append(typeNumber[propertyTypes[i]]); sb.Append(""); } sb.AppendLine(); }); cw.IndentedLine("]);"); }); }); } }); generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + identifier); }
protected string ShortenFullName(ExternalType type, string codeNamespace) { if (type.FullName == "Serenity.Widget") return "Serenity.Widget<any>"; var ns = ShortenNamespace(type, codeNamespace); if (!string.IsNullOrEmpty(ns)) return ns + "." + type.Name; else return type.Name; }
private void SSDeclarationField(ExternalType type, ExternalMember field, string codeNamespace, bool isStaticClass, bool isSerializable, bool preserveMemberCase) { string fieldName = field.Name; var scriptNameAttr = field.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.ScriptNameAttribute"); if (scriptNameAttr != null) fieldName = scriptNameAttr.Arguments[0].Value as string; else if (!preserveMemberCase && !field.Attributes.Any(x => x.Type == "System.Runtime.CompilerServices.PreserveCaseAttribute")) { if (fieldName == "ID") fieldName = "id"; else fieldName = fieldName.Substring(0, 1).ToLowerInvariant() + fieldName.Substring(1); } if (isStaticClass && field.IsStatic) { cw.Indented("let "); sb.Append(fieldName); } else if (field.IsStatic) { cw.Indented("static "); sb.Append(fieldName); } else { cw.Indented(fieldName); } sb.Append(": "); SSTypeNameToTS(field.Type, codeNamespace); sb.AppendLine(";"); }
private void SSDeclarationBaseTypeReference(ExternalType type, string baseType, string codeNamespace) { if (string.IsNullOrEmpty(baseType)) return; if (baseType == "System.Object") return; sb.Append(" extends "); SSTypeNameToTS(baseType, codeNamespace, "Object", type.GenericParameters.Select(x => x.Name).ToArray()); }
public void AddTSType(ExternalType type) { tsTypes[type.FullName] = type; }
private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); cw.Indented("public partial class "); var generatedName = MakeFriendlyName(type, codeNamespace); generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + generatedName); sb.AppendLine(" : PrefixedContext"); cw.InBrace(delegate { cw.Indented("[InlineConstant] public const string FormKey = \""); sb.Append(formScriptAttribute.Key); sb.AppendLine("\";"); sb.AppendLine(); cw.Indented("public "); sb.Append(generatedName); sb.AppendLine("(string idPrefix) : base(idPrefix) {}"); sb.AppendLine(); foreach (var item in PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType ?? "String"; ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullTypeName = scriptType.FullName; if (type.FullName == "Serenity.Widget") { fullTypeName = "Serenity.Widget<any>"; } var shortTypeName = ShortenFullName(scriptType, codeNamespace); cw.Indented("public "); sb.Append(shortTypeName); sb.Append(" "); sb.Append(item.Name); sb.Append(" { "); sb.Append("[InlineCode(\"{this}.w('"); sb.Append(item.Name); sb.Append("', "); sb.Append(fullTypeName); sb.AppendLine(")\")] get; private set; }"); } }); }
public void AddTSType(ExternalType type) { type.Origin = ExternalTypeOrigin.TS; tsTypes[type.FullName] = type; }
public void AddTSType(ExternalType type) { type.Origin = ExternalTypeOrigin.TS; tsTypes[type.FullName] = type; }
private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); cw.Indented("export class "); var generatedName = MakeFriendlyName(type, codeNamespace); generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + generatedName); sb.Append(" extends Serenity.PrefixedContext"); cw.InBrace(delegate { cw.Indented("static formKey = '"); sb.Append(formScriptAttribute.Key); sb.AppendLine("';"); sb.AppendLine(); }); sb.AppendLine(); cw.Indented("export interface "); MakeFriendlyName(type, codeNamespace); StringBuilder initializer = new StringBuilder("["); cw.InBrace(delegate { int j = 0; foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType ?? "String"; ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullName = ShortenFullName(scriptType, codeNamespace); if (j++ > 0) { initializer.Append(", "); } initializer.Append("['"); initializer.Append(item.Name); initializer.Append("', "); initializer.Append(fullName); initializer.Append("]"); cw.Indented(item.Name); sb.Append("(): "); sb.Append(fullName); sb.AppendLine(";"); } }); initializer.Append("].forEach(x => "); MakeFriendlyName(type, codeNamespace, initializer); initializer.Append(".prototype[<string>x[0]] = function() { return this.w(x[0], x[1]); });"); sb.AppendLine(); cw.IndentedLine(initializer.ToString()); }
protected virtual string ShortenNamespace(ExternalType type, string codeNamespace) { string ns = type.Namespace ?? ""; if ((codeNamespace != null && (ns == codeNamespace)) || (codeNamespace != null && codeNamespace.StartsWith((ns + ".")))) { return ""; } if (IsUsingNamespace(ns)) return ""; if (codeNamespace != null) { var idx = codeNamespace.IndexOf('.'); if (idx >= 0 && ns.StartsWith(codeNamespace.Substring(0, idx + 1))) return ns.Substring(idx + 1); } return ns; }
protected ExternalType GetBaseType(ExternalType type) { return(GetScriptType(GetBaseTypeName(type))); }
protected ExternalType GetBaseType(ExternalType type) { return GetScriptType(GetBaseTypeName(type)); }
private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); cw.Indented("export class "); var identifier = type.Name; if (identifier.EndsWith(requestSuffix) && type.IsSubclassOf(typeof(ServiceRequest))) { identifier = identifier.Substring(0, identifier.Length - requestSuffix.Length) + "Form"; this.fileIdentifier = identifier; } sb.Append(identifier); generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + identifier); sb.Append(" extends Serenity.PrefixedContext"); cw.InBrace(delegate { cw.Indented("static formKey = '"); sb.Append(formScriptAttribute.Key); sb.AppendLine("';"); sb.AppendLine(); }); sb.AppendLine(); cw.Indented("export interface "); sb.Append(identifier); StringBuilder initializer = new StringBuilder("["); cw.InBrace(delegate { int j = 0; foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType ?? "String"; ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullName = ShortenFullName(scriptType, codeNamespace); if (j++ > 0) { initializer.Append(", "); } initializer.Append("['"); initializer.Append(item.Name); initializer.Append("', () => "); initializer.Append(fullName); initializer.Append("]"); cw.Indented(item.Name); sb.Append(": "); sb.Append(fullName); sb.AppendLine(";"); } }); initializer.Append("].forEach(x => Object.defineProperty("); initializer.Append(identifier); initializer.Append(".prototype, <string>x[0], { get: function () { return this.w(x[0], (x[1] as any)()); }, enumerable: true, configurable: true }));"); sb.AppendLine(); cw.IndentedLine(initializer.ToString()); }
protected ExternalAttribute GetAttribute(ExternalType type, string attributeName, bool inherited) { var attr = type.Attributes.FirstOrDefault(x => x.Type == attributeName); if (attr != null) return attr; if (!inherited) return null; int loop = 0; while ((type = GetBaseType(type)) != null) { attr = type.Attributes.FirstOrDefault(x => x.Type == attributeName); if (attr != null) return attr; if (loop++ > 100) return null; }; return null; }
private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); var identifier = type.Name; if (identifier.EndsWith(requestSuffix) && type.IsSubclassOf(typeof(ServiceRequest))) { identifier = identifier.Substring(0, identifier.Length - requestSuffix.Length) + "Form"; this.fileIdentifier = identifier; } cw.Indented("export interface "); sb.Append(identifier); var propertyNames = new List <string>(); var propertyTypes = new List <string>(); cw.InBrace(delegate { foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType ?? "String"; ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullName = ShortenFullName(scriptType, codeNamespace); var shortName = fullName; if (fullName.StartsWith("Serenity.")) { shortName = "s." + fullName.Substring("Serenity.".Length); } propertyNames.Add(item.Name); propertyTypes.Add(shortName); cw.Indented(item.Name); sb.Append(": "); sb.Append(fullName); sb.AppendLine(";"); } }); sb.AppendLine(); cw.Indented("export class "); sb.Append(identifier); sb.Append(" extends Serenity.PrefixedContext"); cw.InBrace(delegate { cw.Indented("static formKey = '"); sb.Append(formScriptAttribute.Key); sb.AppendLine("';"); if (propertyNames.Count > 0) { cw.IndentedLine("private static init: boolean;"); sb.AppendLine(); cw.Indented("constructor(prefix: string)"); cw.InBrace(delegate { cw.IndentedLine("super(prefix);"); sb.AppendLine(); cw.Indented("if (!"); sb.Append(identifier); sb.Append(".init) "); cw.InBrace(delegate { cw.Indented(identifier); sb.AppendLine(".init = true;"); sb.AppendLine(); cw.IndentedLine("var s = Serenity;"); var typeNumber = new Dictionary <string, int>(); foreach (var s in propertyTypes) { if (!typeNumber.ContainsKey(s)) { cw.Indented("var w"); sb.Append(typeNumber.Count); sb.Append(" = "); sb.Append(s); sb.AppendLine(";"); typeNumber[s] = typeNumber.Count; } } sb.AppendLine(); cw.Indented("Q.initFormType("); sb.Append(identifier); sb.AppendLine(", ["); cw.Block(delegate { for (var i = 0; i < propertyNames.Count; i++) { if (i > 0) { sb.AppendLine(","); } cw.Indented("'"); sb.Append(propertyNames[i]); sb.Append("', w"); sb.Append(typeNumber[propertyTypes[i]]); sb.Append(""); } sb.AppendLine(); }); cw.IndentedLine("]);"); }); }); } }); generatedTypes.Add((codeNamespace.IsEmptyOrNull() ? "" : codeNamespace + ".") + identifier); }
protected bool HasBaseType(ExternalType type, string typeName) { int loop = 0; while (type != null) { if (loop++ > 100) break; var baseTypeName = GetBaseTypeName(type); if (baseTypeName == typeName) return true; type = GetScriptType(baseTypeName); }; return false; }
private void SSDeclarationProperty(ExternalType type, ExternalProperty prop, string codeNamespace, bool isStaticClass, bool isSerializable, bool preserveMemberCase) { if (string.IsNullOrEmpty(prop.GetMethod) && string.IsNullOrEmpty(prop.SetMethod)) return; var propName = GetPropertyScriptName(prop, preserveMemberCase); if (isSerializable || prop.Attributes.FirstOrDefault(x => x.Type == "System.Runtime.CompilerServices.IntrinsicPropertyAttribute") != null) { if (isStaticClass && prop.IsStatic) { cw.Indented("let "); sb.Append(propName); } else if (prop.IsStatic) { cw.Indented("static "); sb.Append(propName); } else { cw.Indented(propName); } sb.Append(": "); SSTypeNameToTS(prop.Type, codeNamespace); sb.AppendLine(";"); } else { var getMethod = type.Methods.FirstOrDefault(x => x.Name == prop.GetMethod); if (getMethod != null) { getMethod.Name = "get_" + propName; SSDeclarationMethodInternal(getMethod, codeNamespace, isStaticClass, preserveMemberCase); } var setMethod = type.Methods.FirstOrDefault(x => x.Name == prop.SetMethod); if (setMethod != null) { setMethod.Name = "set_" + propName; SSDeclarationMethodInternal(setMethod, codeNamespace, isStaticClass, preserveMemberCase); } } }
protected string GetBaseTypeName(ExternalType type) { if (type.BaseType == null) return null; var baseType = type.BaseType; SplitGenericArguments(ref baseType); return baseType; }
private SortedDictionary<string, ExternalMember> GetOptionMembers(ExternalType type, bool isWidget) { var result = new SortedDictionary<string, ExternalMember>(); var constructor = type.Methods.FirstOrDefault(x => x.IsConstructor && x.Arguments.Count == (isWidget ? 2 : 1)); if (constructor != null) { if (!isWidget || (constructor.Arguments[0].Type == "jQueryApi.jQueryObject" || constructor.Arguments[0].Type == "JQuery")) { var optionsType = GetScriptType(constructor.Arguments[isWidget ? 1 : 0].Type); AddOptionMembers(result, optionsType, isOptions: true); } } int loop = 0; do { if (type.Namespace.StartsWith("System")) break; AddOptionMembers(result, type, isOptions: false); } while ((type = GetBaseType(type)) != null && loop++ < 100); return result; }
private void AddOptionMembers(SortedDictionary <string, ExternalMember> dict, ExternalType type, bool isOptions) { List <ExternalMember> members = new List <ExternalMember>(); members.AddRange(type.Properties); if (type.Origin == ExternalTypeOrigin.TS) { members.AddRange(type.Fields); members.AddRange(type.Methods.Where(x => x.Arguments.Count == 1)); } foreach (var member in members) { if (dict.ContainsKey(member.Name)) { continue; } if (type.Origin == ExternalTypeOrigin.SS) { if (member.Attributes.Any(x => x.Type == "System.ComponentModel.HiddenAttribute")) { continue; } var prop = member as ExternalProperty; if (prop == null) { continue; } if (string.IsNullOrEmpty(prop.GetMethod) && string.IsNullOrEmpty(prop.SetMethod)) { return; } var getMethod = type.Methods.FirstOrDefault(x => x.Name == prop.GetMethod); var setMethod = type.Methods.FirstOrDefault(x => x.Name == prop.SetMethod); if (getMethod == null || setMethod == null || getMethod.IsProtected || setMethod.IsProtected) { continue; } } else if (type.Origin == ExternalTypeOrigin.TS) { } if (member.Type.StartsWith("System.Func`") || member.Type.StartsWith("System.Action`") || member.Type == "System.Delegate" || member.Type.Contains("System.TypeOption") || member.Type == "Function") { continue; } if (!isOptions && !member.Attributes.Any(x => x.Type == "System.ComponentModel.DisplayNameAttribute" || x.Type == "Serenity.OptionAttribute" || x.Type == "Serenity.Decorators.option" || x.Type == "Serenity.Decorators.displayName")) { continue; } dict[member.Name] = member; } }
private void GenerateOptionMembers(ExternalType type, HashSet<string> skip, bool isWidget) { bool preserveMemberCase = type.Attributes.Any(x => x.Type == "System.Runtime.CompilerServices.PreserveMemberCaseAttribute"); var options = GetOptionMembers(type, isWidget); foreach (var option in options.Values) { if (skip != null && skip.Contains(option.Name)) continue; var typeName = FormatterTypeGenerator.GetOptionTypeName(option.Type); sb.AppendLine(); cw.Indented("public "); sb.Append(typeName); sb.Append(" "); string jsName = option.Name; string optionName = option.Name; var prop = option as ExternalProperty; if (prop != null) jsName = GetPropertyScriptName(prop, preserveMemberCase); else if (type.Origin == ExternalTypeOrigin.TS) { var emo = option as ExternalMethod; if (emo != null && emo.Arguments.Count == 1) { if (jsName.StartsWith("set_")) { jsName = jsName.Substring(4); optionName = optionName.Substring(4); } typeName = FormatterTypeGenerator.GetOptionTypeName(emo.Arguments[0].Type); } } if (Char.IsLower(optionName[0])) { if (optionName == "id") optionName = "ID"; else optionName = Char.ToUpperInvariant(optionName[0]) + optionName.Substring(1); } sb.AppendLine(optionName); cw.InBrace(() => { cw.Indented("get { return GetOption<"); sb.Append(typeName); sb.Append(">(\""); sb.Append(jsName); sb.AppendLine("\"); }"); cw.Indented("set { SetOption(\""); sb.Append(jsName); sb.AppendLine("\", value); }"); }); } }
private void GenerateOptionMembers(ExternalType type, HashSet <string> skip, bool isWidget) { bool preserveMemberCase = type.Attributes.Any(x => x.Type == "System.Runtime.CompilerServices.PreserveMemberCaseAttribute"); var options = GetOptionMembers(type, isWidget); foreach (var option in options.Values) { if (skip != null && skip.Contains(option.Name)) { continue; } var typeName = GetOptionTypeName(option.Type); sb.AppendLine(); cw.Indented("public "); sb.Append(typeName); sb.Append(" "); string jsName = option.Name; string optionName = option.Name; var prop = option as ExternalProperty; if (prop != null) { jsName = GetPropertyScriptName(prop, preserveMemberCase); } else if (type.Origin == ExternalTypeOrigin.TS) { var emo = option as ExternalMethod; if (emo != null && emo.Arguments.Count == 1) { if (jsName.StartsWith("set_")) { jsName = jsName.Substring(4); optionName = optionName.Substring(4); } typeName = GetOptionTypeName(emo.Arguments[0].Type); } } if (Char.IsLower(optionName[0])) { if (optionName == "id") { optionName = "ID"; } else { optionName = Char.ToUpperInvariant(optionName[0]) + optionName.Substring(1); } } sb.AppendLine(optionName); cw.InBrace(() => { cw.Indented("get { return GetOption<"); sb.Append(typeName); sb.Append(">(\""); sb.Append(jsName); sb.AppendLine("\"); }"); cw.Indented("set { SetOption(\""); sb.Append(jsName); sb.AppendLine("\", value); }"); }); } }
private void GenerateForm(Type type, FormScriptAttribute formScriptAttribute) { var codeNamespace = GetNamespace(type); cw.Indented("public partial class "); var identifier = type.Name; if (identifier.EndsWith(requestSuffix) && type.IsSubclassOf(typeof(ServiceRequest))) { identifier = identifier.Substring(0, identifier.Length - requestSuffix.Length) + "Form"; this.fileIdentifier = identifier; } sb.Append(identifier); sb.AppendLine(" : PrefixedContext"); cw.InBrace(delegate { cw.Indented("[InlineConstant] public const string FormKey = \""); sb.Append(formScriptAttribute.Key); sb.AppendLine("\";"); sb.AppendLine(); cw.Indented("public "); sb.Append(identifier); sb.AppendLine("(string idPrefix) : base(idPrefix) {}"); sb.AppendLine(); foreach (var item in PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType ?? "String"; ExternalType scriptType = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if ((scriptType = (GetScriptType(wn) ?? GetScriptType(wn + "Editor"))) != null) { break; } } if (scriptType == null && (scriptType = (GetScriptType(editorType) ?? GetScriptType(editorType + "Editor"))) == null) { continue; } var fullTypeName = scriptType.FullName; if (type.FullName == "Serenity.Widget") { fullTypeName = "Serenity.Widget<any>"; } var shortTypeName = ShortenFullName(scriptType, codeNamespace); cw.Indented("public "); sb.Append(shortTypeName); sb.Append(" "); sb.Append(item.Name); sb.Append(" { "); sb.Append("[InlineCode(\"{this}.w('"); sb.Append(item.Name); sb.Append("', "); sb.Append(fullTypeName); sb.AppendLine(")\")] get; private set; }"); } }); }