private string GenerateEnumAttributeMap(Type type, ITemplatingEngine templatingEngine, string mapName, string mapValueTemplate) { return(templatingEngine.UseTemplate("EnumAttributeMap", new Dictionary <string, string> { { "EnumName", type.Name }, { "MapName", mapName }, { "MapItems", string.Join($",{Environment.NewLine}", from memberName in Enum.GetNames(type) select templatingEngine.UseTemplate("EnumAttributeMapItem", new Dictionary <string, string> { { "EnumName", type.Name }, { "EnumMember", memberName }, { "MappedValue", mapValueTemplate.FormatWith(type.GetMember(memberName).SingleOrDefault().CreateFormattingContext()) } })).AddIndentation() } })); }
/// <summary> /// Is overridden to define how the type definition is translated by this strategy. /// </summary> /// <param name="type">Type definition that should be tranlated.</param> /// <param name="templatingEngine">Templating engine that can be used to fill predefined code snippets.</param> /// <returns>Result of the translation.</returns> protected override CodeFragment Translate(Type type, ITemplatingEngine templatingEngine) { var decorators = DecoratorTranslator.GenerateDecorators(type); var properties = GeneratePropertyDefinitions(type, templatingEngine); var declaration = GenerateClassDeclaration(type); var deps = declaration.dependencies.Merge(properties.dependencies).Merge(decorators.Dependencies); var code = templatingEngine.UseTemplate("ClassDefinition", new Dictionary <string, string> { { "Decorators", decorators.DecoratorCode }, { "ClassName", type.GetNameWithGenericTypeParameters() }, { "ClassDeclaration", declaration.code }, { "Documentation", GenerateDocumentationComment(type) }, { "Properties", properties.code.AddIndentation() }, { "Dependencies", GenerateDependencyInitialization(deps, templatingEngine) }, { "ConstructorCode", declaration.isDerived ? $"{ Environment.NewLine }super();".AddIndentation(2) : string.Empty } }); return(new CodeFragment( CodeFragmentId.ForClrType(type), code, deps)); }
private (string code, CodeDependencies dependencies) GeneratePropertyDefinitions(Type type, ITemplatingEngine templatingEngine) { var propertyCodeSnippets = new List <string>(); var deps = CodeDependencies.Empty; foreach (var property in GetPropertiesForTranspilation(type)) { Logger.WriteInformation($"Translating property {property.Name} on type {type}."); var typeReferenceTranslation = TypeReferenceTranslator.Translate(property.PropertyType); var decorators = DecoratorTranslator.GenerateDecorators(property); deps = deps .Merge(typeReferenceTranslation.Dependencies) .Merge(decorators.Dependencies); propertyCodeSnippets.Add(templatingEngine.UseTemplate("ClassPropertyDefinition", new Dictionary <string, string> { { "Decorators", decorators.DecoratorCode }, { "PropertyName", GetTypeScriptPropertyName(property) }, { "Documentation", GenerateDocumentationComment(property) }, { "PropertyType", typeReferenceTranslation.ReferencedTypeName }, { "Assignment", _defaultValueProvider.Assignment(property) } })); } var properties = string.Join(Environment.NewLine, propertyCodeSnippets); return(properties, deps); }
private string GenerateEnumValues(Type type, ITemplatingEngine templatingEngine) { return(string.Join("," + Environment.NewLine, Enum.GetNames(type).Select(name => templatingEngine.UseTemplate("EnumValue", new Dictionary <string, string> { { "EnumValueName", name }, { "Documentation", GenerateDocumentationComment(type.GetMember(name).Single()) }, { "EnumValue", Convert.ChangeType(Enum.Parse(type, name), Enum.GetUnderlyingType(type)).ToString() } })))); }
/// <summary> /// Is overridden to define how the type definition is translated by this strategy. /// </summary> /// <param name="type">Type definition that should be tranlated.</param> /// <param name="templatingEngine">Templating engine that can be used to fill predefined code snippets.</param> /// <returns>Result of the translation.</returns> protected override CodeFragment Translate(Type type, ITemplatingEngine templatingEngine) { var code = templatingEngine.UseTemplate("EnumDefinition", new Dictionary <string, string> { { "EnumName", type.Name }, { "Documentation", GenerateDocumentationComment(type) }, { "EnumValues", GenerateEnumValues(type, templatingEngine).AddIndentation() }, { "EnumAttributeMaps", GenerateEnumAttributeMaps(type, templatingEngine) } }); return(new CodeFragment( CodeFragmentId.ForClrType(type), code, CodeDependencies.Empty)); }
private string GenerateDependencyInitialization(CodeDependencies dependencies, ITemplatingEngine templatingEngine) { if (!Configuration.RuntimeDependencyLoading) { return(string.Empty); } var loadableDependencies = dependencies.CodeFragments .Where(x => x.TryRecreateClrType(out var type) && !type.IsInterface) .Select(x => x.ExportedName).Concat(dependencies.Imports.Select(x => x.Name)); return(templatingEngine.UseTemplate("DependencyInitialization", new Dictionary <string, string> { { "Dependencies", string.Join(", ", loadableDependencies) } })); }
/// <summary> /// Is overridden to define how the type definition is translated by this strategy. /// </summary> /// <param name="type">Type definition that should be tranlated.</param> /// <param name="templatingEngine">Templating engine that can be used to fill predefined code snippets.</param> /// <returns>Result of the translation.</returns> protected override CodeFragment Translate(Type type, ITemplatingEngine templatingEngine) { var properties = GeneratePropertyDefinitions(type, templatingEngine, out var dependencies); var declaration = GenerateClassDeclaration(type); dependencies = dependencies.Merge(declaration.dependencies); var code = templatingEngine.UseTemplate("InterfaceDefinition", new Dictionary <string, string> { { "InterfaceDeclaration", declaration.code }, { "Documentation", GenerateDocumentationComment(type) }, { "Properties", properties.AddIndentation() } }); return(new CodeFragment( CodeFragmentId.ForClrType(type), code, dependencies)); }
private string GeneratePropertyDefinitions(Type type, ITemplatingEngine templatingEngine, out CodeDependencies dependencies) { var propertyCodeSnippets = new List <string>(); var deps = CodeDependencies.Empty; foreach (var property in type.GetProperties()) { Logger.WriteInformation($"Translating property {property.Name} on type {type}."); var typeReferenceTranslation = TypeReferenceTranslator.Translate(property.PropertyType); deps = deps.Merge(typeReferenceTranslation.Dependencies); propertyCodeSnippets.Add(templatingEngine.UseTemplate("InterfacePropertyDefinition", new Dictionary <string, string> { { "PropertyName", GetTypeScriptPropertyName(property) }, { "Documentation", GenerateDocumentationComment(property) }, { "PropertyType", typeReferenceTranslation.ReferencedTypeName } })); } dependencies = deps; return(string.Join(Environment.NewLine, propertyCodeSnippets)); }