コード例 #1
0
        private TypeScriptModule GetOrCreateModule(
            string name,
            out bool created)
        {
            TypeScriptModule result;

            if (this.modulesByName.TryGetValue(
                    name,
                    out result))
            {
                created = false;
            }
            else
            {
                result = new TypeScriptModule()
                {
                    QualifiedName = name
                };
                this.modulesByName.Add(
                    name,
                    result);
                created = true;
            }
            return(result);
        }
コード例 #2
0
        public IEnumerable <TypeScriptModule> GetAllInterfaces()
        {
            var typeContext  = BuildContext();
            var byModuleName = new Dictionary <string, TypeScriptModule>();
            var tsMap        = new Dictionary <CodeClass, TypeScriptInterface>();

            new ProjectTraverser(this.Project, (ns) =>
            {
                new NamespaceTraverser(ns, (codeClass) =>
                {
                    if (codeClass.Attributes == null || codeClass.Attributes.Count == 0)
                    {
                        return;
                    }

                    CodeAttribute attribute;
                    if (!TryGetAttribute(codeClass.Attributes, InterfaceAttributeFullName, out attribute))
                    {
                        return;
                    }

                    var values = GetInterfaceValues(codeClass, attribute);

                    TypeScriptModule module;
                    if (!byModuleName.TryGetValue(values.Module, out module))
                    {
                        module = new TypeScriptModule {
                            QualifiedName = values.Module
                        };
                        byModuleName.Add(values.Module, module);
                    }

                    BuildInterface(codeClass, typeContext, attribute, tsMap, module);
                });
            });

            var tsInterfaces = tsMap.Values.ToList();

            tsMap.Keys.ToList().ForEach(codeClass =>
            {
                var parent = tsInterfaces.LastOrDefault(intf => codeClass.IsDerivedFrom[intf.FullName] && intf.FullName != codeClass.FullName);
                var tsInterfaceLackingParent = tsMap[codeClass];
                if (
                    parent != null &&
                    (
                        // Parent-classes marked specifically TypeScriptInterface -attribute can always be used as parent.
                        !parent.IsIndirectlyMarkedInterface
                        // Only interfaces marked with CanExtendAllBaseClasses can extend BaseClassInterface.
                        || tsInterfaceLackingParent.CanExtendAllBaseClasses
                    )
                    )
                {
                    tsInterfaceLackingParent.Parent = parent;
                }
            });

            return(byModuleName.Values
                   .OrderBy(m => m.QualifiedName)
                   .ToList());
        }
コード例 #3
0
ファイル: CodeTraverser.cs プロジェクト: yogi1916/t4ts
        public IEnumerable <TypeScriptModule> GetAllInterfaces()
        {
            var typeContext  = BuildContext();
            var byModuleName = new Dictionary <string, TypeScriptModule>();
            var tsMap        = new Dictionary <CodeClass, TypeScriptInterface>();

            new SolutionTraverser(this.Solution, (ns) =>
            {
                new NamespaceTraverser(ns, (codeClass) =>
                {
                    InterfaceType interfaceType;
                    if (!typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
                    {
                        return;
                    }

                    var values = interfaceType.AttributeValues;

                    TypeScriptModule module;
                    if (!byModuleName.TryGetValue(values.Module, out module))
                    {
                        module = new TypeScriptModule {
                            QualifiedName = values.Module
                        };
                        byModuleName.Add(values.Module, module);
                    }

                    var tsInterface = BuildInterface(codeClass, values, typeContext);
                    tsMap.Add(codeClass, tsInterface);
                    tsInterface.Module = module;
                    module.Interfaces.Add(tsInterface);
                });
            });

            var tsInterfaces = tsMap.Values.ToList();

            tsMap.Keys.ToList().ForEach(codeClass =>
            {
                CodeElements baseClasses = codeClass.Bases;
                if (baseClasses != null && baseClasses.Count > 0)
                {
                    CodeElement baseClass = baseClasses.Item(1);
                    if (baseClass != null)
                    {
                        ///since this is traversing project files, a class's base class can be defined in multiple files, if that is a partial class.
                        ///SingleOrDefault fails if it finds multiple files ==> That's why use FirstOrDefault
                        var parent = tsInterfaces.FirstOrDefault(intf => intf.FullName == baseClass.FullName);
                        if (parent != null)
                        {
                            tsMap[codeClass].Parent = parent;
                        }
                    }
                }
            });

            return(byModuleName.Values
                   .OrderBy(m => m.QualifiedName)
                   .ToList());
        }
コード例 #4
0
ファイル: CodeTraverser.cs プロジェクト: luisvsilva/t4ts
        public IEnumerable<TypeScriptModule> GetAllInterfaces()
        {
            var typeContext = BuildContext();
            var byModuleName = new Dictionary<string, TypeScriptModule>();
            var tsMap = new Dictionary<CodeClass, TypeScriptInterface>();

            new SolutionTraverser(this.Solution, (ns) =>
            {
                new NamespaceTraverser(ns, (codeClass) =>
                {
                    InterfaceType interfaceType;
                    if (!typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
                        return;

                    var values = interfaceType.AttributeValues;
                    
                    TypeScriptModule module;
                    if (!byModuleName.TryGetValue(values.Module, out module))
                    {
                        module = new TypeScriptModule { QualifiedName = values.Module };
                        byModuleName.Add(values.Module, module);
                    }

                    var tsInterface = BuildInterface(codeClass, values, typeContext);
                    tsMap.Add(codeClass, tsInterface);
                    tsInterface.Module = module;
                    module.Interfaces.Add(tsInterface);
                });
            });

            var tsInterfaces = tsMap.Values.ToList();
            tsMap.Keys.ToList().ForEach(codeClass =>
            {
                CodeElements baseClasses = codeClass.Bases;
                if (baseClasses != null && baseClasses.Count > 0)
                {
                    CodeElement baseClass = baseClasses.Item(1);
                    if (baseClass != null)
                    {
                        ///since this is traversing project files, a class's base class can be defined in multiple files, if that is a partial class.
                        ///SingleOrDefault fails if it finds multiple files ==> That's why use FirstOrDefault
                        var parent = tsInterfaces.FirstOrDefault(intf => intf.FullName == baseClass.FullName);
                        if (parent != null)
                        {
                            tsMap[codeClass].Parent = parent;
                        }
                    }
                }
            });

            return byModuleName.Values
                .OrderBy(m => m.QualifiedName)
                .ToList();
        }
コード例 #5
0
        public TypeScriptEnum GetOrCreateEnum(
            string moduleName,
            TypeName sourceType,
            string outputName,
            out bool created)
        {
            TypeScriptEnum result;

            if (!this.sourceToEnumMap.TryGetValue(
                    sourceType.UniversalName,
                    out result))
            {
                result = new TypeScriptEnum(
                    sourceType,
                    this.GetTypeArgumentReferences(
                        sourceType.TypeArguments,
                        contextTypeReference: null),
                    contextTypeReference: null);

                this.sourceToEnumMap.Add(
                    sourceType.UniversalName,
                    result);

                this.sourceNamesToOutputTypeMap.Add(
                    sourceType.UniversalName,
                    this.GenerateOutputName(
                        moduleName,
                        outputName,
                        (sourceType.TypeArguments != null)
                            ? sourceType.TypeArguments.Count
                            : 0));

                created = true;
            }
            else
            {
                created = false;
            }

            bool             moduleCreated;
            TypeScriptModule module = this.GetOrCreateModule(
                moduleName,
                out moduleCreated);

            module.Enums.Add(result);

            return(result);
        }
コード例 #6
0
        private void ProcessCodeEnum(TypeContext typeContext, IDictionary <CodeEnum, TypeScriptEnum> tsEnumMap,
                                     IDictionary <string, TypeScriptModule> byModuleName, CodeEnum codeEnum)
        {
            EnumType enumType;

            if (typeContext.TryGetEnumType(codeEnum.FullName, out enumType))
            {
                TypeScriptEnumAttributeValues values = enumType.AttributeValues;

                TypeScriptModule module;
                if (!byModuleName.TryGetValue(values.Module, out module))
                {
                    module = new TypeScriptModule {
                        QualifiedName = values.Module
                    };
                    byModuleName.Add(values.Module, module);
                }

                TypeScriptEnum tsEnum = BuildEnum(codeEnum, values, typeContext);
                tsEnumMap.Add(codeEnum, tsEnum);
                tsEnum.Module = module;
                module.Enums.Add(tsEnum);
            }
        }
コード例 #7
0
        private void ProcessCodeClass(TypeContext typeContext, IDictionary <CodeClass, TypeScriptInterface> tsMap,
                                      IDictionary <string, TypeScriptModule> byModuleName, CodeClass codeClass)
        {
            InterfaceType interfaceType;

            if (typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
            {
                TypeScriptInterfaceAttributeValues values = interfaceType.AttributeValues;

                TypeScriptModule module;
                if (!byModuleName.TryGetValue(values.Module, out module))
                {
                    module = new TypeScriptModule {
                        QualifiedName = values.Module
                    };
                    byModuleName.Add(values.Module, module);
                }

                TypeScriptInterface tsInterface = BuildInterface(codeClass, values, typeContext);
                tsMap.Add(codeClass, tsInterface);
                tsInterface.Module = module;
                module.Interfaces.Add(tsInterface);
            }
        }
コード例 #8
0
        /// <summary>
        /// A recursively used method which builds interfaces out of CodeClasses *and* their base-class -CodeClasses
        /// (if instructed with [TypeScriptInterface(CreateTypeScriptInterfacesAlsoForBaseClasses = true)]).
        /// </summary>
        /// <param name="codeClass">The CodeClass for which to create a TypeScriptInterface.</param>
        /// <param name="typeContext"></param>
        /// <param name="originalTypeScriptInterfaceAttribute"></param>
        /// <param name="tsMap">The tsMap in which the created TypeScriptInterface will be stored.</param>
        /// <param name="module">The module in which the created TypeScriptInterface will be stored.</param>
        /// <param name="indirectlyMarkedInterface">A marker to indicate that a CodeClass with [TypeScriptInterface(CreateTypeScriptInterfacesAlsoForBaseClasses = true)] has been hit somewhere in previous recursion.</param>
        private void BuildInterface(
            CodeClass codeClass,
            TypeContext typeContext,
            CodeAttribute originalTypeScriptInterfaceAttribute,
            Dictionary <CodeClass, TypeScriptInterface> tsMap,
            TypeScriptModule module,
            bool indirectlyMarkedInterface = false
            )
        {
            var interfaceAlreadyExists = tsMap.ContainsKey(codeClass) || tsMap.Keys.Any(x => x.FullName == codeClass.FullName);
            // There's no need to display the properties of System.Object, ie. the grand-base-class of all classes.
            var codeClassIsIrrelevant = codeClass.FullName == typeof(Object).FullName;

            if (interfaceAlreadyExists || codeClassIsIrrelevant)
            {
                return;
            }

            CodeAttribute specificTypeScriptInterfaceAttribute;
            var           directlyMarkedInterface = TryGetAttribute(
                codeClass.Attributes,
                InterfaceAttributeFullName,
                out specificTypeScriptInterfaceAttribute
                );

            TypeScriptInterfaceAttributeValues typeScriptInterfaceAttributeValues;

            if (directlyMarkedInterface)
            {
                typeScriptInterfaceAttributeValues = GetInterfaceValues(codeClass, specificTypeScriptInterfaceAttribute);
                // This will set all the baseClasses of this class to be indirectlyMarked.
                indirectlyMarkedInterface =
                    indirectlyMarkedInterface ||
                    typeScriptInterfaceAttributeValues.CreateTypeScriptInterfacesAlsoForBaseClasses;
            }
            else
            {
                // If no specific attribute was available, use the originalTypeScriptInterfaceAttribute as source for typeScriptInterfaceAttributeValues.
                typeScriptInterfaceAttributeValues = GetInterfaceValues(codeClass, originalTypeScriptInterfaceAttribute);
                // Make sure an overridden name from originalTypeScriptInterfaceAttribute never overrides the name of "baseClass-interface", ie. a codeClass without a TypeScriptInterfaceAttribute of it's own.
                typeScriptInterfaceAttributeValues.Name       = codeClass.Name;
                typeScriptInterfaceAttributeValues.NamePrefix = Settings.DefaultInterfaceNamePrefix ?? string.Empty;
            }

            // Only indirectlyMarkedInterfaces build also their base-class -interfaces.
            if (indirectlyMarkedInterface)
            {
                // First, create interfaces for the baseCodeClasses.
                foreach (CodeClass baseClassCodeClass in codeClass.Bases)
                {
                    BuildInterface(
                        baseClassCodeClass,
                        typeContext,
                        originalTypeScriptInterfaceAttribute,
                        tsMap,
                        module,
                        true
                        );
                }
            }

            // Second, create interfaces for the CodeClass itself.
            var tsInterface = BuildInterface(
                codeClass,
                typeScriptInterfaceAttributeValues,
                typeContext,
                directlyMarkedInterface,
                indirectlyMarkedInterface
                );

            tsMap.Add(codeClass, tsInterface);
            tsInterface.Module = module;
            module.Interfaces.Add(tsInterface);
        }
コード例 #9
0
ファイル: CodeGenerator.cs プロジェクト: Lugoues/t4ts
        public IEnumerable<TypeScriptModule> GetAllInterfaces()
        {
            var typeContext = BuildContext();
            var byModuleName = new Dictionary<string, TypeScriptModule>();

            new ProjectTraverser(this.Project, (ns) =>
            {
                new NamespaceTraverser(ns, (codeClass) =>
                {
                    if (codeClass.Attributes == null || codeClass.Attributes.Count == 0)
                        return;

                    CodeAttribute attribute;
                    if (!TryGetAttribute(codeClass.Attributes, InterfaceAttributeFullName, out attribute))
                        return;

                    var values = GetInterfaceValues(codeClass, attribute);

                    TypeScriptModule module;
                    if (!byModuleName.TryGetValue(values.Module, out module))
                    {
                        module = new TypeScriptModule { QualifiedName = values.Module };
                        byModuleName.Add(values.Module, module);
                    }

                    module.Interfaces.Add(BuildInterface(codeClass, values, typeContext));
                });
            });

            return byModuleName.Values
                .OrderBy(m => m.QualifiedName)
                .ToList();
        }
コード例 #10
0
ファイル: CodeTraverser.cs プロジェクト: dolly22/t4ts
        private void ProcessCodeEnum(TypeContext typeContext, IDictionary<CodeEnum, TypeScriptEnum> tsEnumMap,
            IDictionary<string, TypeScriptModule> byModuleName, CodeEnum codeEnum)
        {
            EnumType enumType;
            if (typeContext.TryGetEnumType(codeEnum.FullName, out enumType))
            {
                var values = enumType.AttributeValues;

                TypeScriptModule module;
                if (!byModuleName.TryGetValue(values.Module, out module))
                {
                    module = new TypeScriptModule { QualifiedName = values.Module };
                    byModuleName.Add(values.Module, module);
                }

                var tsEnum = BuildEnum(codeEnum, values, typeContext);
                tsEnumMap.Add(codeEnum, tsEnum);
                tsEnum.Module = module;
                module.Enums.Add(tsEnum);
            }
        }
コード例 #11
0
ファイル: CodeTraverser.cs プロジェクト: dolly22/t4ts
        private void ProcessCodeClass(TypeContext typeContext, IDictionary<CodeClass, TypeScriptInterface> tsMap, 
            IDictionary<string, TypeScriptModule> byModuleName, CodeClass codeClass)
        {
            InterfaceType interfaceType;
            if (typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
            {
                var values = interfaceType.AttributeValues;

                TypeScriptModule module;
                if (!byModuleName.TryGetValue(values.Module, out module))
                {
                    module = new TypeScriptModule { QualifiedName = values.Module };
                    byModuleName.Add(values.Module, module);
                }

                var tsInterface = BuildInterface(codeClass, values, typeContext);
                tsMap.Add(codeClass, tsInterface);
                tsInterface.Module = module;
                module.Interfaces.Add(tsInterface);
            }
        }