private void GenerateHubInterfaces(Type hubType, ScriptBuilder scriptBuilder, bool generateAsModules, HashSet <string> requiredImports)
        {
            if (!hubType.BaseType.FullName.Contains(HUB_TYPE))
            {
                throw new ArgumentException("The supplied type does not appear to be a SignalR hub.", "hubType");
            }

            // Build the client interface
            if (generateAsModules)
            {
                scriptBuilder.AppendIndented("export ");
            }
            scriptBuilder.AppendLineIndented(string.Format("interface I{0}Client {{", hubType.Name));
            using (scriptBuilder.IncreaseIndentation())
            {
                if (!hubType.BaseType.IsGenericType)
                {
                    scriptBuilder.AppendLineIndented("/* Client interface not generated as hub doesn't derive from Hub<T> */");
                }
                else
                {
                    GenerateMethods(scriptBuilder, requiredImports, hubType.BaseType.GetGenericArguments().First());
                }
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();

            // Build the interface containing the SERVER methods
            if (generateAsModules)
            {
                scriptBuilder.AppendIndented("export ");
            }
            scriptBuilder.AppendLineIndented(string.Format("interface I{0} {{", hubType.Name));
            using (scriptBuilder.IncreaseIndentation())
            {
                GenerateMethods(scriptBuilder, requiredImports, hubType);
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();

            // Build the proxy class (represents the proxy generated by signalR).
            if (generateAsModules)
            {
                scriptBuilder.AppendIndented("export ");
            }
            scriptBuilder.AppendLineIndented(string.Format("interface I{0}Proxy {{", hubType.Name));
            using (scriptBuilder.IncreaseIndentation())
            {
                scriptBuilder.AppendLineIndented("server: I" + hubType.Name + ";");
                scriptBuilder.AppendLineIndented("client: I" + hubType.Name + "Client;");
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();
        }
예제 #2
0
        public void WhenAppendIndentedString_IndentedStringIsAppended()
        {
            _sb.IncreaseIndentation();
            _sb.AppendIndented("test");

            var script = _sb.ToString();

            Assert.Equal("\ttest", script);
        }
        private void AppendKoInterfaceDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            string typeName   = this.GetTypeName(classModel);
            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";

            sb.AppendIndented($"{visibility}interface {typeName}");
            if (classModel.BaseType != null)
            {
                sb.Append($" extends {this.GetFullyQualifiedTypeName(classModel.BaseType)}");
            }
            sb.AppendLine(" {");
            var members = new List <TsProperty>();

            if ((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties)
            {
                members.AddRange(classModel.Properties);
            }
            if ((generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
            {
                members.AddRange(classModel.Fields);
            }
            using (sb.IncreaseIndentation())
            {
                foreach (TsProperty property in members)
                {
                    if (property.IsIgnored)
                    {
                        continue;
                    }
                    string propTypeName = this.GetPropertyType(property);
                    if (IsCollection(property))
                    {
                        if (propTypeName.Length > 2 && propTypeName.Substring(propTypeName.Length - 2) == "[]")
                        {
                            propTypeName = propTypeName.Substring(0, propTypeName.Length - 2);
                        }
                        propTypeName = "ko.ObservableArray<" + propTypeName + ">";
                    }
                    else
                    {
                        propTypeName = "ko.Subscribable<" + propTypeName + ">";
                    }
                    sb.AppendLineIndented($"{this.GetPropertyName(property)}: {propTypeName};");
                }
            }
            sb.AppendLineIndented("}");
        }
        private void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput, bool ko)
        {
            string typeName = this.GetTypeName(classModel);

            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";

            sb.AppendIndented($"{visibility}class {typeName}");
            if (classModel.BaseType != null)
            {
                string baseTypeName = this.GetFullyQualifiedTypeName(classModel.BaseType);
                if (baseTypeName.StartsWith(classModel.Module.Name + ".", StringComparison.Ordinal))
                {
                    baseTypeName = baseTypeName.Substring(classModel.Module.Name.Length + 1);
                }

                sb.Append($" extends {baseTypeName}");
                sb.AppendLine(" {");
                using (sb.IncreaseIndentation())
                {
                    sb.AppendLineIndented("constructor() {");
                    using (sb.IncreaseIndentation())
                    {
                        sb.AppendLineIndented("super();");
                    }
                    sb.AppendLineIndented("}");
                }
            }
            else
            {
                sb.AppendLine(" {");
            }
            var members = new List <TsProperty>();

            if ((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties)
            {
                members.AddRange(classModel.Properties);
            }
            if ((generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
            {
                members.AddRange(classModel.Fields);
            }
            using (sb.IncreaseIndentation())
            {
                foreach (TsProperty property in members.Where(m => !m.IsIgnored))
                {
                    string propTypeName = this.GetPropertyType(property);
                    if (IsCollection(property))
                    {
                        if (propTypeName.EndsWith("[]", StringComparison.Ordinal))
                        {
                            propTypeName = propTypeName.Substring(0, propTypeName.Length - "[]".Length);
                        }
                        if (ko)
                        {
                            sb.AppendLineIndented($"{this.GetPropertyName(property)}: ko.ObservableArray<{propTypeName}> = ko.observableArray([]);");
                        }
                        else
                        {
                            sb.AppendLineIndented($"{this.GetPropertyName(property)}: {propTypeName}[];");
                        }
                    }
                    else
                    {
                        if (ko)
                        {
                            sb.AppendLineIndented($"{this.GetPropertyName(property)}: ko.Subscribable<{propTypeName}> = ko.observable(null);");
                        }
                        else
                        {
                            sb.AppendLineIndented($"{this.GetPropertyName(property)}: {propTypeName};");
                        }
                    }
                }
            }

            sb.AppendLineIndented("}");
            _generatedClasses.Add(classModel);
        }