public TypeScriptInterface Build( CodeInterface codeInterface, TypeContext typeContext) { TypeScriptInterface result = null; string moduleName = this.settings.GetModuleNameFromNamespace(codeInterface.Namespace); bool interfaceCreated; result = typeContext.GetOrCreateInterface( moduleName, TypeName.ParseDte(codeInterface.FullName), codeInterface.Name, out interfaceCreated); result.IsClass = this.settings.CreateClasses; this.PopulateBases( codeInterface.Bases, result, typeContext); this.PopulateMembers( codeInterface.Members, result, typeContext); return(result); }
private string GetMethodCall( string toObjectName, string fromObjectName, string fromFieldName, TypeScriptInterface interfaceType, TypeScriptMethod copyMethod) { string result; if (this.ToContainingType) { TypeName outputName = this.TypeContext.ResolveOutputTypeName(interfaceType); result = String.Format( "({2}.{3}) ? new {0}().{1}({2}.{3}) : undefined", outputName.QualifiedName, copyMethod.Name, fromObjectName, fromFieldName); } else { TypeName outputName = this.TypeContext.ResolveOutputTypeName(copyMethod.Arguments.First().Type); result = String.Format( "{0}.{1}.{2}(new {3}())", fromObjectName, fromFieldName, copyMethod.Name, outputName.QualifiedName); } return(result); }
private void PopulateBases( CodeElements bases, TypeScriptInterface interfaceOutput, TypeContext typeContext) { if (bases != null) { foreach (CodeElement baseElement in bases) { if (baseElement.FullName != typeof(Object).FullName) { interfaceOutput.Bases.Add( typeContext.GetTypeReference( TypeName.ParseDte(baseElement.FullName), interfaceOutput)); } } TypeReference parentType = interfaceOutput.Bases.FirstOrDefault(); if (parentType != null && BuilderHelper.IsValidBaseType(parentType.SourceType)) { interfaceOutput.Parent = parentType; } } }
private bool TryGetMember( TypeScriptInterface interfaceContext, CodeProperty property, TypeContext typeContext, out TypeScriptMember member) { member = null; if (!this.settings.MemberAccessTypes.Contains(property.Access)) { return(false); } var getter = property.Getter; if (getter == null) { return(false); } string name = property.Name; if (name.StartsWith("@")) { name = name.Substring(1); } member = new TypeScriptMember { Name = name, Type = typeContext.GetTypeReference( TypeName.ParseDte(getter.Type.AsFullName), interfaceContext) }; return(true); }
private void PopulateMembers( CodeElements members, TypeScriptInterface interfaceOutput, TypeContext typeContext) { Traversal.TraverseProperties( members, (property) => { TypeScriptMember member; if (TryGetMember( interfaceOutput, property, typeContext, out member)) { interfaceOutput.Fields.Add(member); } }); }
public static TypeScriptInterface CreateInterface(string assemblyFile, string rootType, bool odata = false) { var moduleDefinition = ModuleDefinition.ReadModule(assemblyFile); if (moduleDefinition == null) { throw new ArgumentException(string.Format("The assembly '{0}' could not be loaded.", assemblyFile), "assemblyFile"); } var typeDefintion = moduleDefinition.GetType(rootType); if (typeDefintion == null) { throw new ArgumentException(string.Format("The type '{0}' could not be found.", rootType), "rootType"); } var scriptInterface = new TypeScriptInterface(); scriptInterface.Name = typeDefintion.Name; // get all properties var propertyDefinitions = typeDefintion .Traverse(t => t.BaseType == null ? null : t.BaseType.Resolve()) .SelectMany(t => t.Properties); foreach (var propertyDefinition in propertyDefinitions) { var scriptPropery = new TypeScriptProperty(); scriptPropery.Name = propertyDefinition.Name; var propertyType = propertyDefinition.PropertyType; scriptPropery.Type = propertyType.ToScriptType(odata); scriptPropery.IsNullable = propertyDefinition.PropertyType.IsNullable(); scriptPropery.IsArray = propertyDefinition.PropertyType.IsScriptArray(); scriptInterface.Properties.Add(scriptPropery); } return(scriptInterface); }
private TypeReference GetIndexedType( TypeScriptInterface interfaceContext, CodeClass codeClass, TypeContext typeContext) { TypeReference result = null; if (codeClass.Bases != null) { TypeName baseName; foreach (CodeElement baseClass in codeClass.Bases) { baseName = TypeName.ParseDte(baseClass.FullName); if (baseName.UniversalName == typeof(IEnumerable <>).FullName) { result = typeContext.GetTypeReference( baseName.TypeArguments.First(), interfaceContext); } } } return(result); }
protected override void AppendBody( StringBuilder output, int indent, TypeScriptMethod method) { this.AppendIndentedLine( output, indent, "{"); int bodyIndent = indent + 4; string toObjectName; string fromObjectName; if (this.ToContainingType) { toObjectName = "this"; fromObjectName = method.Arguments.First().Name; } else { toObjectName = method.Arguments.Last().Name; fromObjectName = "this"; } if (this.containingType.Parent != null) { TypeScriptInterface parentType = this.TypeContext.GetInterface( this.containingType.Parent.SourceType); TypeScriptMethod parentCopyMethod = parentType.Methods.FirstOrDefault( this.CopySettings.IsParentCopyMethod); if (parentCopyMethod != null) { this.AppendMethodCall( output, bodyIndent, "super", parentCopyMethod.Name, method.Arguments .Select((currentArgument) => currentArgument.Name) .ToList()); } } foreach (TypeScriptMember field in containingType.Fields) { this.AppendFieldCopy( output, bodyIndent, method, toObjectName, fromObjectName, field); } this.AppendIndentedLine( output, bodyIndent, String.Format( "return {0};", toObjectName)); this.AppendIndentedLine( output, indent, "}"); }
private void AppendFieldValueCopy( StringBuilder output, int indent, string toObjectName, string toFieldName, string fromObjectName, string fromFieldName, TypeScriptMethod parentMethod, TypeReference fieldType) { TypeName resolvedFieldType = this.TypeContext.ResolveOutputTypeName(fieldType); if (!resolvedFieldType.IsArray) { TypeScriptInterface interfaceType = this.TypeContext.GetInterface(fieldType.SourceType); TypeScriptMethod copyMethod = null; if (interfaceType != null && interfaceType.Methods != null) { copyMethod = interfaceType.Methods.FirstOrDefault( this.CopySettings.IsParentCopyMethod); } string rightHandSide; if (copyMethod != null) { rightHandSide = this.GetMethodCall( toObjectName, fromObjectName, fromFieldName, interfaceType, copyMethod); } else { rightHandSide = String.Format( "{0}.{1}", fromObjectName, fromFieldName); } this.AppendIndentedLine( output, indent, String.Format( "{0}.{1} = {2};", toObjectName, toFieldName, rightHandSide)); } else { this.AppendArrayCopy( output, indent, toObjectName, toFieldName, fromObjectName, fromFieldName, parentMethod, fieldType); } }
private bool GenerateInterface(TypeScriptInterface tsInterface, TextWriter output) { return true; }
public TypeScriptInterface Build( CodeClass codeClass, TypeContext typeContext) { TypeScriptInterface result = null; TypeScriptInterfaceAttributeValues attributeValues = this.GetAttributeValues(codeClass); if (attributeValues != null) { string moduleName = attributeValues.Module; if (String.IsNullOrEmpty(moduleName) && codeClass.Namespace != null) { moduleName = codeClass.Namespace.FullName; } bool interfaceCreated; result = typeContext.GetOrCreateInterface( moduleName, TypeName.ParseDte(codeClass.FullName), GetInterfaceName(attributeValues), out interfaceCreated); if (!String.IsNullOrEmpty(attributeValues.Extends)) { result.Parent = typeContext.GetLiteralReference(attributeValues.Extends); } else if (codeClass.Bases.Count > 0) { // Getting the first item directly causes problems in unit tests. Get it from an enumerator. IEnumerator enumerator = codeClass.Bases.GetEnumerator(); enumerator.MoveNext(); TypeName parentTypeName = TypeName.ParseDte( ((CodeElement)enumerator.Current).FullName); if (BuilderHelper.IsValidBaseType(parentTypeName)) { result.Parent = typeContext.GetTypeReference( parentTypeName, result); } } result.IndexedType = this.GetIndexedType( result, codeClass, typeContext); Traversal.TraverseProperties( codeClass.Members, (property) => { TypeScriptMember member; if (TryGetMember( result, property, typeContext, out member)) { result.Fields.Add(member); } }); } return(result); }
private bool TryGetMember(TypeScriptInterface interfaceContext, CodeProperty property, TypeContext typeContext, out TypeScriptMember member) { member = null; if (property.Access != vsCMAccess.vsCMAccessPublic) { return(false); } var getter = property.Getter; if (getter == null) { return(false); } var values = GetMemberValues(property, typeContext); string name; if (values.Name != null) { name = values.Name; } else { name = property.Name; if (name.StartsWith("@")) { name = name.Substring(1); } } TypeReference memberType; if (!string.IsNullOrWhiteSpace(values.Type)) { memberType = typeContext.GetLiteralReference(values.Type); } else { memberType = typeContext.GetTypeReference( TypeName.ParseDte(getter.Type.AsFullName), interfaceContext); } member = new TypeScriptMember { Name = name, //FullName = property.FullName, Optional = values.Optional, Ignore = values.Ignore, Type = memberType }; if (member.Ignore) { return(false); } if (values.CamelCase && values.Name == null) { member.Name = member.Name.Substring(0, 1).ToLowerInvariant() + member.Name.Substring(1); } return(true); }