Beispiel #1
0
        private IEnumerable <string> GenerateBarrel(BarrelSpec barrelSpec)
        {
            string directory = Path.Combine(Options.BaseOutputDirectory?.EnsurePostfix("/") ?? "", barrelSpec.Directory);

            var fileName = "index";

            if (!string.IsNullOrWhiteSpace(Options.TypeScriptFileExtension))
            {
                fileName += $".{Options.TypeScriptFileExtension}";
            }
            string filePath = Path.Combine(directory.EnsurePostfix("/"), fileName);

            var entries = new List <string>();

            if (barrelSpec.BarrelScope.HasFlag(BarrelScope.Files))
            {
                entries.AddRange(_fileSystem.GetDirectoryFiles(directory)
                                 .Where(x => Path.GetFileName(x) != fileName && x.EndsWith($".{Options.TypeScriptFileExtension}"))
                                 .Select(Path.GetFileNameWithoutExtension));
            }

            if (barrelSpec.BarrelScope.HasFlag(BarrelScope.Directories))
            {
                entries.AddRange(
                    _fileSystem.GetDirectoryDirectories(directory)
                    .Select(dir => dir.Replace("\\", "/").Split('/').Last())
                    );
            }

            string indexExportsContent = entries.Aggregate("", (acc, entry) => acc += _templateService.FillIndexExportTemplate(entry));
            string content             = _templateService.FillIndexTemplate(indexExportsContent);

            FileContentGenerated?.Invoke(this, new FileContentGeneratedArgs(null, filePath, content));
            return(new[] { Path.Combine(barrelSpec.Directory.EnsurePostfix("/"), fileName) });
        }
Beispiel #2
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());
        }
Beispiel #3
0
        /// <summary>
        /// Generates a TypeScript enum file from a class type
        /// </summary>
        /// <param name="type"></param>
        /// <param name="enumAttribute"></param>
        /// <returns>Generated TypeScript file paths (relative to the Options.BaseOutputDirectory)</returns>
        private IEnumerable <string> GenerateEnum(Type type, ExportTsEnumAttribute enumAttribute)
        {
            string valuesText = GetEnumMembersText(type);

            // create TypeScript source code for the enum

            string tsEnumName       = _typeService.GetTsTypeName(type, true);
            string filePath         = GetFilePath(type, enumAttribute.OutputDir);
            string filePathRelative = GetRelativeFilePath(type, enumAttribute.OutputDir);

            string enumText = _typeService.UseDefaultExport(type) ?
                              _templateService.FillEnumDefaultExportTemplate("", tsEnumName, valuesText, enumAttribute.IsConst, Options.FileHeading) :
                              _templateService.FillEnumTemplate("", tsEnumName, valuesText, enumAttribute.IsConst, Options.FileHeading);

            // write TypeScript file

            FileContentGenerated?.Invoke(this, new FileContentGeneratedArgs(type, filePath, enumText));
            return(new[] { filePathRelative });
        }
Beispiel #4
0
        /// <summary>
        /// DEPRECATED, will be removed in the future.
        /// Generates an `index.ts` file which exports all types within the generated files
        /// </summary>
        /// <param name="generatedFiles"></param>
        /// <returns>Generated TypeScript file paths (relative to the Options.BaseOutputDirectory)</returns>
        private IEnumerable <string> GenerateIndexFile(IEnumerable <string> generatedFiles)
        {
            var typeScriptFileExtension = "";

            if (!string.IsNullOrEmpty(Options.TypeScriptFileExtension))
            {
                typeScriptFileExtension = "." + Options.TypeScriptFileExtension;
            }

            string exports = generatedFiles.Aggregate("", (prevExports, file) =>
            {
                string fileNameWithoutExt = file.Remove(file.Length - typeScriptFileExtension.Length).Replace("\\", "/");
                return(prevExports + _templateService.FillIndexExportTemplate(fileNameWithoutExt));
            });
            string content = _templateService.FillIndexTemplate(exports);

            string filename = "index" + typeScriptFileExtension;

            FileContentGenerated?.Invoke(this, new FileContentGeneratedArgs(null, Path.Combine(Options.BaseOutputDirectory, filename), content));

            return(new[] { filename });
        }