Exemplo n.º 1
0
        private IEnumerable <string> GenerateClassOrInterface(Type type, ExportTsClassAttribute classAttribute, ExportTsInterfaceAttribute interfaceAttribute)
        {
            string outputDir = classAttribute != null ? classAttribute.OutputDir : interfaceAttribute.OutputDir;
            IEnumerable <string> dependenciesGenerationResult = GenerateTypeDependencies(type, outputDir);

            // get text for sections

            var tsCustomBaseAttribute = _metadataReaderFactory.GetInstance().GetAttribute <TsCustomBaseAttribute>(type);
            var extendsText           = "";


            if (tsCustomBaseAttribute != null)
            {
                extendsText = string.IsNullOrEmpty(tsCustomBaseAttribute.Base) ? "" : _templateService.GetExtendsText(tsCustomBaseAttribute.Base);
            }
            else if (type.IsInterface)
            {
                // this is an interface, generate extends for an interface.
                extendsText = _tsContentGenerator.GetExtendsForInterfacesText(type);
            }
            else if (_metadataReaderFactory.GetInstance().GetAttribute <TsIgnoreBaseAttribute>(type) == null)
            {
                extendsText = _tsContentGenerator.GetExtendsText(type);
            }

            string implementsText = _tsContentGenerator.GetImplementsText(type);

            string importsText    = _tsContentGenerator.GetImportsText(type, outputDir);
            string propertiesText = classAttribute != null?GetClassPropertiesText(type) : GetInterfacePropertiesText(type);

            // generate the file content

            string tsTypeName          = _typeService.GetTsTypeName(type, true);
            string tsTypeNameFirstPart = tsTypeName.RemoveTypeGenericComponent();
            string filePath            = GetFilePath(type, outputDir);
            string filePathRelative    = GetRelativeFilePath(type, outputDir);
            string customHead          = _tsContentGenerator.GetCustomHead(filePath);
            string customBody          = _tsContentGenerator.GetCustomBody(filePath, Options.TabLength);

            string content;

            if (classAttribute != null)
            {
                content = _typeService.UseDefaultExport(type) ?
                          _templateService.FillClassDefaultExportTemplate(importsText, tsTypeName, tsTypeNameFirstPart, extendsText, implementsText, propertiesText, customHead, customBody, Options.FileHeading) :
                          _templateService.FillClassTemplate(importsText, tsTypeName, extendsText, implementsText, propertiesText, customHead, customBody, Options.FileHeading);
            }
            else
            {
                content = _typeService.UseDefaultExport(type) ?
                          _templateService.FillInterfaceDefaultExportTemplate(importsText, tsTypeName, tsTypeNameFirstPart, extendsText, propertiesText, customHead, customBody, Options.FileHeading) :
                          _templateService.FillInterfaceTemplate(importsText, tsTypeName, extendsText, propertiesText, customHead, customBody, Options.FileHeading);
            }

            // write TypeScript file

            FileContentGenerated?.Invoke(this, new FileContentGeneratedArgs(type, filePath, content));
            return(new[] { filePathRelative }.Concat(dependenciesGenerationResult).ToList());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Generates a TypeScript class file from a class type
 /// </summary>
 /// <param name="type"></param>
 /// <param name="classAttribute"></param>
 /// <returns>Generated TypeScript file paths (relative to the Options.BaseOutputDirectory)</returns>
 private IEnumerable <string> GenerateClass(Type type, ExportTsClassAttribute classAttribute)
 {
     return(GenerateClassOrInterface(type, classAttribute, null));
 }