예제 #1
0
        private string GenerateHubs(Assembly assembly, TypeConverter converter)
        {
            var hubs = assembly.GetTypes()
                       .Where(t => t.GetDnxCompatible().BaseType != null && t.GetDnxCompatible().BaseType.FullName != null && t.GetDnxCompatible().BaseType.FullName.Contains(HUB_TYPE))
                       .OrderBy(t => t.FullName)
                       .ToList();

            if (!hubs.Any())
            {
                return("");
            }

            var scriptBuilder = new ScriptBuilder("    ");

            // Output signalR style promise interface:
            scriptBuilder.AppendLine("interface ISignalRPromise<T> {");
            using (scriptBuilder.IncreaseIndentation())
            {
                scriptBuilder.AppendLineIndented("done(cb: (result: T) => any): ISignalRPromise<T>;");
                scriptBuilder.AppendLineIndented("error(cb: (error: any) => any): ISignalRPromise<T>;");
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();
            hubs.ForEach(h => GenerateHubInterfaces(new SignalRHubDesc(h), scriptBuilder, converter));
            // Generate client connection interfaces
            scriptBuilder.AppendLineIndented("interface SignalR {");
            using (scriptBuilder.IncreaseIndentation())
            {
                hubs.ForEach(h => scriptBuilder.AppendLineIndented(h.Name.ToCamelCase() + ": I" + h.Name + "Proxy;"));
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();
            return(scriptBuilder.ToString());
        }
예제 #2
0
        public void WriteHubs(SignalRHubDesc[] hubs, TypeConverter converter, CustomIndentedTextWriter writer)
        {
            var hubList       = hubs.ToList();
            var scriptBuilder = new ScriptBuilder("    ");

            // Output signalR style promise interface:
            scriptBuilder.AppendLine("interface ISignalRPromise<T> {");
            using (scriptBuilder.IncreaseIndentation())
            {
                scriptBuilder.AppendLineIndented("done(cb: (result: T) => any): ISignalRPromise<T>;");
                scriptBuilder.AppendLineIndented("error(cb: (error: any) => any): ISignalRPromise<T>;");
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();
            hubList.ForEach(h => GenerateHubInterfaces(h, scriptBuilder, converter));
            // Generate client connection interfaces
            scriptBuilder.AppendLineIndented("interface SignalR {");
            using (scriptBuilder.IncreaseIndentation())
            {
                hubList.ForEach(h => scriptBuilder.AppendLineIndented(h.HubClassName.ToCamelCase() + ": I" + h.HubClassName + "Proxy;"));
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();

            writer.WriteLine(scriptBuilder.ToString());
        }
        public override string GenerateHubs(Assembly assembly, bool generateAsModules)
        {
            var hubs = assembly.GetTypes()
                       .Where(t => t.BaseType != null && t.BaseType.FullName != null && t.BaseType.FullName.Contains(HUB_TYPE))
                       .OrderBy(t => t.FullName)
                       .ToList();

            if (!hubs.Any())
            {
                Console.WriteLine("No SignalR hubs found");
                return("");
            }
            Console.WriteLine(hubs.Count + " SignalR hubs found");
            var requiredImports = new HashSet <string>();

            foreach (var hub in hubs)
            {
                requiredImports.Add(hub.Namespace.Split('.')[0]);
            }

            var scriptBuilder = new ScriptBuilder("    ");

            scriptBuilder.AppendLine();

            hubs.ForEach(h => GenerateHubInterfaces(h, scriptBuilder, generateAsModules, requiredImports));

            // Generate client connection interfaces
            if (generateAsModules)
            {
                scriptBuilder.Append("export ");
            }
            scriptBuilder.AppendLineIndented("interface SignalR {");
            using (scriptBuilder.IncreaseIndentation())
            {
                hubs.ForEach(h => scriptBuilder.AppendLineIndented(h.Name.ToCamelCase() + ": I" + h.Name + "Proxy;"));
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();

            var output = scriptBuilder.ToString();

            // Output imports if required
            // TODO: this isn't going to work if SignalR hubs are in more than one assembly.
            if (generateAsModules)
            {
                var imports = "import Classes = require(\"./classes\");\r\n";
                foreach (var ns in requiredImports)
                {
                    imports += string.Format("import {0} = Classes.{0};\r\n", ns);
                }
                imports += "\r\n";
                output   = imports + output;
            }

            return(output);
        }
        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();
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        private void BuildUpdateCommand()
        {
            ScriptBuilder.Append("UPDATE ");
            ScriptBuilder.AppendFormat(Table.ToString());
            ScriptBuilder.Append(" SET ");

            foreach (var column in Table.GetAdditionalColumns())
            {
                ScriptBuilder.Append(column.Name);
                ScriptBuilder.Append(Strings.Equal);
                ScriptBuilder.Append($"@{column.Name}");

                ScriptBuilder.AppendIf(Columns.IsNotLast(column), Strings.Comma);
                ScriptBuilder.Append(Strings.WhiteSpace);
            }

            var primaryColumn = Table.GetPrimaryColumnName();

            ScriptBuilder.Append("WHERE ");
            ScriptBuilder.Append(primaryColumn);
            ScriptBuilder.Append(Strings.Equal);
            ScriptBuilder.Append($"@{primaryColumn}");

            ScriptBuilder.AppendLine();
            ScriptBuilder.AppendLine("ELSE");
        }
예제 #6
0
        private void AppendInterfaceDef(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            AddNamespaceHeaderEnum(classModel.Name, classModel.Type.Assembly.FullName, sb);

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

            _docAppender.AppendClassDoc(sb, classModel, typeName);

            sb.AppendFormatIndented("{0}interface {1}", visibility, typeName);
            if (classModel.BaseType != null)
            {
                sb.AppendFormat(" extends {0}", this.GetFullyQualifiedTypeName(classModel.BaseType));
            }

            var interfaces = classModel.Interfaces.Where(m => CsharpTypeInfoProvider.ExposedInterfaces.Contains(m.Type)).ToList();

            if (interfaces.Count > 0)
            {
                var implementations = interfaces.Select(GetFullyQualifiedTypeName).ToArray();
                var prefixFormat    = " implements {0}";

                sb.AppendFormat(prefixFormat, string.Join(" ,", implementations));
            }

            sb.AppendLine(" {");

            GenerateProperties(classModel, sb, generatorOutput);

            sb.AppendLineIndented("}");
            _generatedClasses.Add(classModel);
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        protected override void InternalBuildCommandText()
        {
            SetAlterTable();
            ScriptBuilder.Append(" ADD");
            SetColumnInfo();

            ScriptBuilder.AppendLine();
            ScriptBuilder.AppendLine($"comment on column {Table.GetPgSqlName()}.{Column.GetPgSqlName()} is '{Column.Description}';");
        }
예제 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="column"></param>
        private void BuildColumn(ColumnInfo column)
        {
            ScriptBuilder.AppendLine();
            ScriptBuilder.Append($"{column.GetPgSqlName()} ");
            ScriptBuilder.Append(column.Type.GetColumnType());

            BuildNullableInfo(column);
            BuildPrimaryKeyAndIdentity(column);
            ScriptBuilder.AppendIf(Columns.IsNotLast(column), Strings.Comma);
        }
예제 #9
0
 private static void AppendDelimiter(ScriptBuilder scriptBuilder, ref int i)
 {
     if (i++ == 0) {
         scriptBuilder.Append("  ");
     }
     else {
         scriptBuilder.AppendLine();
         scriptBuilder.Append(" ,");
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="classModel"></param>
        /// <param name="sb"></param>
        /// <param name="generatorOutput"></param>
        protected override void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            string typeName   = this.GetTypeName(classModel);
            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";

            sb.AppendFormatIndented("{0}interface {1}", visibility, typeName);
            if (classModel.BaseType != null)
            {
                sb.AppendFormat(" extends {0}", 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 (var property in members)
                {
                    if (property.IsIgnored)
                    {
                        continue;
                    }
                    var propTypeName = this.GetPropertyType(property);
                    if (property.PropertyType.IsCollection())
                    {
                        //Note: new member functon checking if property is collection or not
                        //Also remove the array brackets from the name
                        if (propTypeName.Length > 2 && propTypeName.Substring(propTypeName.Length - 2) == "[]")
                        {
                            propTypeName = propTypeName.Substring(0, propTypeName.Length - 2);
                        }
                        propTypeName = "KnockoutViewModelArray<" + propTypeName + ">";
                    }
                    else if (property.PropertyType.Type.IsClass && property.PropertyType.Type != typeof(string))
                    {
                        // KnockoutViewModel does not emit observable objects for classes, it leaves the class in-situ.
                        // It's just value types, collections and strings that are observable.
                    }
                    else
                    {
                        propTypeName = "KnockoutObservable<" + propTypeName + ">";
                    }
                    sb.AppendLineIndented(string.Format("{0}: {1};", this.GetPropertyName(property), propTypeName));
                }
            }
            sb.AppendLineIndented("}");
            _generatedClasses.Add(classModel);
        }
예제 #11
0
        private void AppendMemberDoc(ScriptBuilder sb, MemberInfo member)
        {
            var provider = GetXmlDocProvider(member.DeclaringType.Assembly);
            if (provider == null) {
                return;
            }

            var doc = provider.GetDocumentation(member);
            if (string.IsNullOrWhiteSpace(doc)) {
                return;
            }

            sb.AppendLine();
            sb.AppendFormatIndented("/**");
            sb.AppendLine();
            sb.AppendFormatIndented(" * {0}", doc);
            sb.AppendLine();
            sb.AppendFormatIndented(" */");
            sb.AppendLine();
        }
        /// <summary>
        ///
        /// </summary>
        protected virtual void BuildColumnType()
        {
            var type = Column.Type.GetColumnType();

            if (type == DatabaseColumn.Type.GetColumnType())
            {
                return;
            }

            ScriptBuilder.AppendLine($"ALTER COLUMN {Column.GetMySqlName()} TYPE {type};");
        }
        /// <summary>
        ///
        /// </summary>
        private void BuildUpdateCommand()
        {
            ScriptBuilder.Append("WITH upsert AS (UPDATE ");
            ScriptBuilder.Append($"{Table.GetPgSqlName()} SET ");
            Table.Columns.ForEach(BuildUpdateSet);

            var primaryColumn = Table.GetPrimaryColumnName();

            ScriptBuilder.Append($"WHERE \"{primaryColumn}\" = @{primaryColumn}");
            ScriptBuilder.AppendLine(" RETURNING *)");
        }
예제 #14
0
 private static void AppendDelimiter(ScriptBuilder scriptBuilder, ref int i)
 {
     if (i++ == 0)
     {
         scriptBuilder.Append("  ");
     }
     else
     {
         scriptBuilder.AppendLine();
         scriptBuilder.Append(" ,");
     }
 }
예제 #15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="column"></param>
        private void BuildColumn(ColumnInfo column)
        {
            ScriptBuilder.AppendLine();
            ScriptBuilder.Append($"{column.GetSqlName()} ");
            ScriptBuilder.Append(column.Type.GetColumnType());

            BuildIdentity(column);
            BuildNullableInfo(column);

            ScriptBuilder.AppendIf(column.IsPrimary(), " PRIMARY KEY");
            ScriptBuilder.Append(Strings.Comma);
        }
예제 #16
0
        /// <summary>
        ///
        /// </summary>
        protected void BuildMultipleColumnUnique()
        {
            ScriptBuilder.AppendLine($"ALTER TABLE {Table.GetPgSqlName()}");
            ScriptBuilder.Append($"ADD CONSTRAINT {UniqueName} UNIQUE (");

            foreach (var column in UniqueColumns)
            {
                ScriptBuilder.Append(column.GetPgSqlName());
                ScriptBuilder.AppendIf(UniqueColumns.IsNotLast(column), " ,");
            }

            ScriptBuilder.Append(");");
        }
예제 #17
0
        /// <summary>
        ///
        /// </summary>
        private void AlterPrimaryKeyIdentitySequence()
        {
            if (Table.PrimaryKey?.PrimaryColumn?.Identity == null)
            {
                return;
            }

            ScriptBuilder.AppendLine();
            ScriptBuilder.AppendLine();
            ScriptBuilder.AppendLine($"ALTER SEQUENCE \"{GetSequenceName()}\"");
            ScriptBuilder.Append("OWNED BY ");
            ScriptBuilder.Append($"{Table.GetPgSqlName()}.{Table.PrimaryKey.PrimaryColumn.GetPgSqlName()}");
            ScriptBuilder.AppendLine(Strings.Semicolon);
        }
예제 #18
0
        /// <summary>
        ///
        /// </summary>
        private void BuildIfStatement()
        {
            ScriptBuilder.Append("IF EXISTS(SELECT * FROM ");
            ScriptBuilder.AppendFormat(Table.GetSqlName());
            ScriptBuilder.Append(" WHERE ");

            var primaryColumn = Table.GetPrimaryColumnName();

            ScriptBuilder.Append(primaryColumn);
            ScriptBuilder.Append(Strings.Equal);
            ScriptBuilder.Append($"@{primaryColumn}");
            ScriptBuilder.Append(Strings.RBracket);
            ScriptBuilder.AppendLine();
        }
예제 #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="column"></param>
        private void BuildColumn(ColumnInfo column)
        {
            ScriptBuilder.AppendLine();
            ScriptBuilder.Append($"{column.GetMySqlName()} ");
            ScriptBuilder.Append(column.Type.GetColumnType());

            if (column.Identity != null)
            {
                ScriptBuilder.Append(" AUTO_INCREMENT");
            }

            BuildNullableInfo(column);
            ScriptBuilder.Append($" COMMENT '{column.Description}'");
            ScriptBuilder.AppendIf(Columns.IsNotLast(column), Strings.Comma);
        }
        /// <summary>
        ///
        /// </summary>
        private void BuildInsertCommand()
        {
            ScriptBuilder.Append("INSERT INTO ");
            ScriptBuilder.Append($"{Table.GetPgSqlName()}");
            ScriptBuilder.Append(Strings.LBracket);

            Table.Columns.ForEach(BuildColumn);
            ScriptBuilder.AppendLine(Strings.RBracket);

            ScriptBuilder.Append("SELECT ");
            Table.Columns.ForEach(BuildValue);

            ScriptBuilder.AppendLine();
            ScriptBuilder.Append("WHERE NOT EXISTS (SELECT 1 FROM upsert);");
        }
예제 #21
0
        private void AppendMemberDoc(ScriptBuilder sb, MemberInfo member)
        {
            var provider = GetXmlDocProvider(member.DeclaringType.Assembly);

            if (provider == null)
            {
                return;
            }

            var doc = provider.GetDocumentation(member);

            if (string.IsNullOrWhiteSpace(doc))
            {
                return;
            }

            sb.AppendLine();
            sb.AppendFormatIndented("/**");
            sb.AppendLine();
            sb.AppendFormatIndented(" * {0}", doc);
            sb.AppendLine();
            sb.AppendFormatIndented(" */");
            sb.AppendLine();
        }
예제 #22
0
        private void AppendModelDoc(ScriptBuilder sb, Type type)
        {
            var provider = GetXmlDocProvider(type.Assembly);

            if (provider == null)
            {
                return;
            }

            var doc = provider.GetDocumentation(type);

            if (string.IsNullOrEmpty(doc))
            {
                return;
            }

            sb.AppendLine();
            sb.AppendFormatIndented("/**");
            sb.AppendLine();
            sb.AppendFormatIndented(" * {0}", doc);
            sb.AppendLine();
            sb.AppendFormatIndented(" */");
            sb.AppendLine();
        }
        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("}");
        }
예제 #24
0
        protected override void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            string typeName   = this.GetTypeName(classModel);
            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";

            sb.AppendFormatIndented(
                "{0}class {1} extends {2}",
                visibility,
                typeName,
                //all bottom-level classes must extend Backbone.Model.
                classModel.BaseType != null ? this.GetFullyQualifiedTypeName(classModel.BaseType) : "Backbone.Model");

            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 (var property in members)
                {
                    if (property.IsIgnored)
                    {
                        continue;
                    }

                    sb.AppendLineIndented(string.Format(
                                              "get {0}(): {1} {{ return this.get(\"{0}\"); }}",
                                              this.GetPropertyName(property), this.GetPropertyType(property)));

                    sb.AppendLineIndented(string.Format(
                                              "set {0}(v: {1}) {{ this.set(\"{0}\", v); }}",
                                              this.GetPropertyName(property), this.GetPropertyType(property)));
                }
            }

            sb.AppendLineIndented("}");

            _generatedClasses.Add(classModel);
        }
        /// <summary>
        ///
        /// </summary>
        private void BuildInsertCommand()
        {
            ScriptBuilder.Append("INSERT INTO ");
            ScriptBuilder.Append($"{Table.GetMySqlName()}");

            ScriptBuilder.Append(Strings.LBracket);
            Table.Columns.ForEach(BuildColumn);
            ScriptBuilder.AppendLine(Strings.RBracket);

            ScriptBuilder.Append("VALUES");
            ScriptBuilder.Append(Strings.LBracket);
            Table.Columns.ForEach(BuildValue);
            ScriptBuilder.AppendLine(Strings.RBracket);

            ScriptBuilder.AppendLine("ON DUPLICATE KEY UPDATE");
            Table.Columns.ForEach(BuildColumnAndValue);
        }
        protected override void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            string typeName = this.GetTypeName(classModel);
            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";
            sb.AppendFormatIndented(
                "{0}class {1} extends {2}",
                visibility,
                typeName,
                //all bottom-level classes must extend Backbone.Model.
                classModel.BaseType != null ? this.GetFullyQualifiedTypeName(classModel.BaseType) : "Backbone.Model");

            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 (var property in members)
                {
                    if (property.IsIgnored)
                    {
                        continue;
                    }

                    sb.AppendLineIndented(string.Format(
                        "get {0}(): {1} {{ return this.get(\"{0}\"); }}",
                        this.GetPropertyName(property), this.GetPropertyType(property)));

                    sb.AppendLineIndented(string.Format(
                        "set {0}(v: {1}) {{ this.set(\"{0}\", v); }}",
                        this.GetPropertyName(property), this.GetPropertyType(property)));
                }
            }

            sb.AppendLineIndented("}");

            _generatedClasses.Add(classModel);
        }
        /// <summary>
        ///
        /// </summary>
        protected virtual void BuildColumnAllowNull()
        {
            if (Column.IsAllowNull == DatabaseColumn.IsAllowNull)
            {
                return;
            }

            ScriptBuilder.AppendLine($"ALTER COLUMN {Column.GetMySqlName()}");
            if (Column.IsAllowNull)
            {
                ScriptBuilder.Append(" DROP NOT NULL");
            }

            if (!Column.IsAllowNull)
            {
                ScriptBuilder.Append(" SET NOT NULL");
            }

            ScriptBuilder.Append(Strings.Semicolon);
        }
예제 #28
0
        /// <summary>
        ///
        /// </summary>
        private void BuildInsertCommand()
        {
            ScriptBuilder.Append("INSERT INTO ");
            ScriptBuilder.AppendFormat(Table.ToString());
            ScriptBuilder.Append(Strings.LBracket);

            foreach (var column in Columns)
            {
                ScriptBuilder.Append(column.Name);
                ScriptBuilder.AppendIf(Columns.IsNotLast(column), ", ");
            }

            ScriptBuilder.AppendLine(Strings.RBracket);
            ScriptBuilder.Append("VALUES(");

            foreach (var column in Columns)
            {
                ScriptBuilder.Append($"@{column.Name}");
                ScriptBuilder.AppendIf(Columns.IsNotLast(column), ", ");
            }

            ScriptBuilder.Append(Strings.RBracket);
            ScriptBuilder.Append(Strings.Semicolon);
        }
예제 #29
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="columnInfo"></param>
 private void SetColumnComment(ColumnInfo columnInfo)
 {
     ScriptBuilder.AppendLine($"comment on column {Table.GetPgSqlName()}.{columnInfo.GetPgSqlName()} is '{columnInfo.Description}';");
 }
        protected override void AppendModule(TsModule module, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            // Use reflection to grab the internal property which was removed!
            var typeConverters = (TypeConvertorCollection)_typeConvertersMember.GetValue(this);

            var classes = module.Classes.Where(c => !typeConverters.IsConvertorRegistered(c.Type) && !c.IsIgnored)
                          // .OrderBy(c => GetTypeName(c))			// Sorting breaks inheritance
                          .ToList();
            var baseClasses = classes
                              .Where(c => c.BaseType != null)
                              .Select(c => c.BaseType.Type.FullName)
                              .Distinct()
                              .OrderBy(c => c)
                              .ToList();
            var enums = module.Enums.Where(e => !typeConverters.IsConvertorRegistered(e.Type) && !e.IsIgnored).OrderBy(e => GetTypeName(e)).ToList();

            if ((generatorOutput == TsGeneratorOutput.Enums && enums.Count == 0) ||
                (generatorOutput == TsGeneratorOutput.Properties && classes.Count == 0) ||
                (enums.Count == 0 && classes.Count == 0))
            {
                return;
            }

            if (generatorOutput == TsGeneratorOutput.Properties && !classes.Any(c => c.Fields.Any() || c.Properties.Any()))
            {
                return;
            }

            if (generatorOutput == TsGeneratorOutput.Constants && !classes.Any(c => c.Constants.Any()))
            {
                return;
            }

            string moduleName           = GetModuleName(module);
            bool   generateModuleHeader = moduleName != string.Empty;

            if (generateModuleHeader)
            {
                if (generatorOutput != TsGeneratorOutput.Enums &&
                    (generatorOutput & TsGeneratorOutput.Constants) != TsGeneratorOutput.Constants)
                {
                    sb.Append(Mode == TsGenerationModes.Definitions ? "declare " : " export ");
                }

                sb.AppendLine($"{(Mode == TsGenerationModes.Definitions ? "namespace" : "module")} {moduleName} {{");
            }

            using (sb.IncreaseIndentation())
            {
                if ((generatorOutput & TsGeneratorOutput.Enums) == TsGeneratorOutput.Enums)
                {
                    foreach (TsEnum enumModel in enums)
                    {
                        this.AppendEnumDefinition(enumModel, sb, generatorOutput);
                    }
                }

                if (((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties) ||
                    (generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
                {
                    foreach (TsClass baseClassModel in classes.Where(c => baseClasses.Contains(c.Type.FullName)))
                    {
                        this.AppendClassDefinition(baseClassModel, sb, generatorOutput);
                    }
                }

                if (((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties) ||
                    (generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
                {
                    foreach (TsClass classModel in classes.Where(c => !baseClasses.Contains(c.Type.FullName)))
                    {
                        this.AppendClassDefinition(classModel, sb, generatorOutput);
                    }
                }

                if ((generatorOutput & TsGeneratorOutput.Constants) == TsGeneratorOutput.Constants)
                {
                    foreach (TsClass classModel in classes)
                    {
                        if (classModel.IsIgnored)
                        {
                            continue;
                        }

                        this.AppendConstantModule(classModel, sb);
                    }
                }
            }
            if (generateModuleHeader)
            {
                sb.AppendLine("}");
            }
        }
예제 #31
0
        private void GenerateHubInterfaces(SignalRHubDesc hub, ScriptBuilder scriptBuilder, TypeConverter converter)
        {
            if (!hub.HubType.GetDnxCompatible().BaseType.FullName.Contains(HUB_TYPE))
            {
                throw new ArgumentException("The supplied type does not appear to be a SignalR hub.", "hubType");
            }
            // Build the client interface
            scriptBuilder.AppendLineIndented(string.Format("export interface I{0}Client {{", hub.HubClassName));
            using (scriptBuilder.IncreaseIndentation())
            {
                if (!hub.HubType.GetDnxCompatible().BaseType.GetDnxCompatible().IsGenericType)
                {
                    scriptBuilder.AppendLineIndented("/* Client interface not generated as hub doesn't derive from Hub<T> */");
                }
                else
                {
                    GenerateMethods(scriptBuilder, hub, converter, true, (mi, tc) => GenerateMethodDeclaration(mi, tc, hub, true));
                }
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();

            // Build the function for creating a hub proxy
            if (string.IsNullOrEmpty(hub.HubSignalRName))
            {
                scriptBuilder.AppendLineIndented($"/* function {hub.HubClassName}Client_CreateHubProxy not generated as hub type {hub.HubType.FullName} does not have a HubNameAttribute or the hub name is an empty string. */");
            }
            else
            {
                scriptBuilder.AppendLineIndented($"export function {hub.HubClassName}Client_CreateHubProxy(connection: SignalR.Hub.Connection): SignalR.Hub.Proxy {{");
                using (scriptBuilder.IncreaseIndentation())
                    scriptBuilder.AppendLineIndented($"return connection.createHubProxy('{hub.HubSignalRName}');");
                scriptBuilder.AppendLineIndented("}");
            }
            scriptBuilder.AppendLine();

            // Build the function for wiring up a proxy to a client interface
            if (!hub.HubType.GetDnxCompatible().BaseType.GetDnxCompatible().IsGenericType)
            {
                scriptBuilder.AppendLineIndented($"/* function {hub.HubClassName}Client_BindProxy not generated as hub doesn't derive from Hub<T> */");
            }
            else
            {
                scriptBuilder.AppendLineIndented($"export function {hub.HubClassName}Client_BindProxy(proxy: SignalR.Hub.Proxy, client: I{hub.HubClassName}Client): void {{");
                using (scriptBuilder.IncreaseIndentation())
                {
                    GenerateMethods(scriptBuilder, hub, converter, true, (mi, tc) => GenerateMethodProxyBinding(mi, tc, hub));
                }
                scriptBuilder.AppendLineIndented("}");
            }
            scriptBuilder.AppendLine();

            // Build the interface containing the SERVER methods
            scriptBuilder.AppendLineIndented($"interface I{hub.HubClassName} {{");
            using (scriptBuilder.IncreaseIndentation())
            {
                GenerateMethods(scriptBuilder, hub, converter, false, (mi, tc) => GenerateMethodDeclaration(mi, tc, hub, false));
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();
            // Build the proxy class (represents the proxy generated by signalR).
            scriptBuilder.AppendLineIndented(string.Format("interface I{0}Proxy {{", hub.HubClassName));
            using (scriptBuilder.IncreaseIndentation())
            {
                scriptBuilder.AppendLineIndented("server: I" + hub.HubClassName + ";");
                scriptBuilder.AppendLineIndented("client: I" + hub.HubClassName + "Client;");
            }
            scriptBuilder.AppendLineIndented("}");
            scriptBuilder.AppendLine();
        }
        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);
        }
예제 #33
0
        private void AppendClassDef(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            AddNamespaceHeader(classModel.Name, sb);

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

            AddRequestRenameInformation(sb, classModel);
            AddDocCommentForCustomJsonConverter(sb, classModel);
            _docAppender.AppendClassDoc(sb, classModel, typeName);

            sb.AppendFormatIndented("{0}class {1}", visibility, typeName);
            if (CsharpTypeInfoProvider.StringAbstractions.Contains(classModel.Type))
            {
                sb.AppendLineIndented(" extends String {}");
                return;
            }

            void EnforceBaseClass <TInterface, TBase>()
            {
                if (classModel.BaseType != null)
                {
                    return;
                }
                if (classModel.Type == typeof(TBase))
                {
                    return;
                }
                if (typeof(TInterface).IsAssignableFrom(classModel.Type))
                {
                    classModel.BaseType = new TsClass(typeof(TBase));
                }
            }

            EnforceBaseClass <IAnalyzer, AnalyzerBase>();
            EnforceBaseClass <ITokenizer, TokenizerBase>();
            EnforceBaseClass <ITokenFilter, TokenFilterBase>();
            EnforceBaseClass <ICharFilter, CharFilterBase>();
            EnforceBaseClass <IProperty, PropertyBase>();
            EnforceBaseClass <IResponse, ResponseBase>();

            if (classModel.BaseType != null)
            {
                sb.AppendFormat(" extends {0}", this.GetFullyQualifiedTypeName(classModel.BaseType));
            }

            var interfaces = classModel.Interfaces.Where(m => CsharpTypeInfoProvider.ExposedInterfaces.Contains(m.Type)).ToList();

            if (interfaces.Count > 0)
            {
                var implementations = interfaces.Select(GetFullyQualifiedTypeName).ToArray();
                var prefixFormat    = " implements {0}";

                sb.AppendFormat(prefixFormat, string.Join(" ,", implementations));
            }

            sb.AppendLine(" {");

            GenerateProperties(classModel, sb, generatorOutput);

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

            //generate a closed cat response type (NEST uses catresponse<TRecord> for all)
            if (typeof(ICatRecord).IsAssignableFrom(classModel.Type))
            {
                var catResponseName = classModel.Type.Name.Replace("Record", "Response");
                AddNamespaceHeader(classModel.Name, sb);
                sb.AppendLineIndented($"class {catResponseName} extends ResponseBase {{");
                using (sb.IncreaseIndentation())
                    sb.AppendLineIndented($"records: {typeName}[];");
                sb.AppendLineIndented("}");
            }
        }