/// <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)); }
/// <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)); }
/// <summary> /// Gets the name of the file that the code fragment should be written to. /// </summary> /// <param name="codeFragmentId">ID of the code fragment that should be written.</param> /// <returns>The name of the file that the code fragment should be written to.</returns> private string GetFileNameFor(CodeFragmentId codeFragmentId) { var parts = codeFragmentId.Name.Split(CodeFragmentIdSeparator); var directories = new List <string> { _directory }; var fileName = $"{parts.Last()}.ts"; if (_mimicNamespacesWithSubdirectories) { directories.AddRange(parts.Take(parts.Length - 1)); } return(Path.Combine(directories.Concat(new[] { fileName }).ToArray())); }
/// <summary> /// Gets an import statement that adds a reference to another code fragment. /// </summary> /// <param name="self">The current code fragment that should reference another.</param> /// <param name="codeFragmentId">The other code fragment that should be imported.</param> /// <returns>The import statement that adds a reference to the other code fragment.</returns> private string GetImportFor(CodeFragmentId self, CodeFragmentId codeFragmentId) { var parts = codeFragmentId.Name.Split(CodeFragmentIdSeparator); var subdirectory = string.Empty; if (_mimicNamespacesWithSubdirectories) { // Take own nesting into account; walking up those directories first. var selfDepth = self.Name.Split(CodeFragmentIdSeparator).Length - 1; var up = string.Join(string.Empty, Enumerable.Repeat("../", selfDepth)); subdirectory = up + string.Join(string.Empty, parts.Take(parts.Length - 1).Select(p => $"{p}/")); } return($"import {{ {codeFragmentId.ExportedName} }} from './{subdirectory}{ parts.Last() }';"); }
/// <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)); }
/// <summary> /// Is overridden to define how the type reference is translated by this strategy. /// </summary> /// <param name="referencedType">Type reference that should be tranlated.</param> /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param> /// <returns>Result of the translation.</returns> protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator) { var translatedTypeArguments = referencedType.GetGenericArguments().Select(translator.Translate); var referencedTypeName = referencedType.GetNameWithoutGenericTypeParameters() + $"<{ string.Join(", ", translatedTypeArguments.Select(x => x.ReferencedTypeName)) }>"; return(new TypeReferenceTranslationResult(referencedTypeName, CodeDependencies.FromCodeFragments(new[] { CodeFragmentId.ForClrType(referencedType.GetGenericTypeDefinition()) }) .Merge(translatedTypeArguments.Aggregate(CodeDependencies.Empty, (x, n) => x.Merge(n.Dependencies))))); }
/// <summary> /// Is overridden to define how the type reference is translated by this strategy. /// </summary> /// <param name="referencedType">Type reference that should be tranlated.</param> /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param> /// <returns>Result of the translation.</returns> public TypeReferenceTranslationResult Translate(Type referencedType) => new TypeReferenceTranslationResult( referencedType.Name, CodeDependencies.FromCodeFragments(new[] { CodeFragmentId.ForClrType(referencedType) }));