Ejemplo n.º 1
0
        /// <summary>
        /// Checks if a type is marked with an ExportTs... attribute
        /// </summary>
        /// <param name="type"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static bool HasExportAttribute(this Type type, IMetadataReader reader)
        {
            Requires.NotNull(type, nameof(type));
            Requires.NotNull(reader, nameof(reader));

            return(reader.GetAttribute <ExportTsClassAttribute>(type) != null ||
                   reader.GetAttribute <ExportTsInterfaceAttribute>(type) != null ||
                   reader.GetAttribute <ExportTsEnumAttribute>(type) != null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes members marked with TsIgnore attribute
        /// </summary>
        /// <param name="memberInfos"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static IEnumerable <T> WithoutTsIgnore <T>(this IEnumerable <T> memberInfos, IMetadataReader reader) where T : MemberInfo
        {
            Requires.NotNull(memberInfos, nameof(memberInfos));
            Requires.NotNull(reader, nameof(reader));

            return(memberInfos.Where(i => reader.GetAttribute <TsIgnoreAttribute>(i) == null));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the base type dependency for a type, if the base type exists
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private IEnumerable <TypeDependencyInfo> GetBaseTypeDependency(Type type)
        {
            if (_metadataReader.GetAttribute <TsIgnoreBaseAttribute>(type) != null)
            {
                return(Enumerable.Empty <TypeDependencyInfo>());
            }

            Type baseType = _typeService.GetBaseType(type);

            if (baseType == null)
            {
                return(Enumerable.Empty <TypeDependencyInfo>());
            }

            return(GetFlatTypeDependencies(baseType, null, true));
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public bool IsTsClass(Type type)
        {
            Requires.NotNull(type, nameof(type));
            TypeInfo typeInfo = type.GetTypeInfo();

            if (!typeInfo.IsClass)
            {
                return(false);
            }

            var exportAttribute = _metadataReader.GetAttribute <ExportAttribute>(type);

            return(exportAttribute == null || exportAttribute is ExportTsClassAttribute);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns TypeScript imports source code related to type dependencies
        /// </summary>
        /// <param name="type"></param>
        /// <param name="outputDir"></param>
        /// <param name="fileNameConverters"></param>
        /// <param name="typeNameConverters"></param>
        /// <returns></returns>
        private string GetTypeDependencyImportsText(Type type, string outputDir, TypeNameConverterCollection fileNameConverters, TypeNameConverterCollection typeNameConverters)
        {
            if (!string.IsNullOrEmpty(outputDir) && !outputDir.EndsWith("/") && !outputDir.EndsWith("\\"))
            {
                outputDir += "\\";
            }
            var result = "";
            IEnumerable <TypeDependencyInfo> typeDependencies = _typeDependencyService.GetTypeDependencies(type);

            // exclude base type dependency if TsCustomBaseAttribute is specified (it will be added in custom imports)
            if (_metadataReader.GetAttribute <TsCustomBaseAttribute>(type) != null)
            {
                typeDependencies = typeDependencies.Where(td => !td.IsBase);
            }

            foreach (TypeDependencyInfo typeDependencyInfo in typeDependencies)
            {
                Type typeDependency = typeDependencyInfo.Type;

                string dependencyOutputDir = GetTypeDependencyOutputDir(typeDependencyInfo, outputDir);

                // get path diff
                string pathDiff = FileSystemUtils.GetPathDiff(outputDir, dependencyOutputDir);
                pathDiff = pathDiff.StartsWith("..\\") || pathDiff.StartsWith("../") ? pathDiff : $"./{pathDiff}";

                // get type & file name
                string typeDependencyName = typeDependency.Name.RemoveTypeArity();
                string fileName           = fileNameConverters.Convert(typeDependencyName, typeDependency);

                // get file path
                string dependencyPath = Path.Combine(pathDiff, fileName);
                dependencyPath = dependencyPath.Replace('\\', '/');

                string typeName = typeNameConverters.Convert(typeDependencyName, typeDependency);
                result += _templateService.FillImportTemplate(typeName, "", dependencyPath);
            }

            return(result);
        }