Ejemplo n.º 1
0
        /// <summary>
        /// Generates class definition and appends it to the output.
        /// </summary>
        /// <param name="classModel">The class to generate definition for.</param>
        /// <param name="sb">The output.</param>
        /// <param name="generatorOutput"></param>
        protected virtual void AppendConstantModule(TsClass classModel, ScriptBuilder sb)
        {
            if (!classModel.Constants.Any())
            {
                return;
            }

            string typeName = this.GetTypeName(classModel);

            sb.AppendLineIndented(string.Format("export namespace {0} {{", typeName));

            using (sb.IncreaseIndentation()) {
                foreach (var property in classModel.Constants)
                {
                    if (property.IsIgnored)
                    {
                        continue;
                    }

                    _docAppender.AppendConstantModuleDoc(sb, property, this.GetPropertyName(property), this.GetPropertyType(property));
                    sb.AppendFormatIndented("export const {0}: {1} = {2};", this.GetPropertyName(property), this.GetPropertyType(property), this.GetPropertyConstantValue(property));
                    sb.AppendLine();
                }
            }
            sb.AppendLineIndented("}");

            _generatedClasses.Add(classModel);
        }
Ejemplo n.º 2
0
        protected virtual void AppendEnumDefinition(TsEnum enumModel, ScriptBuilder sb, TsGeneratorOutput output)
        {
            string typeName   = this.GetTypeName(enumModel);
            string visibility = (output & TsGeneratorOutput.Enums) == TsGeneratorOutput.Enums || (output & TsGeneratorOutput.Constants) == TsGeneratorOutput.Constants ? "export " : "";

            _docAppender.AppendEnumDoc(sb, enumModel, typeName);

            string constSpecifier = this.GenerateConstEnums ? "const " : string.Empty;

            sb.AppendLineIndented(string.Format("{0}{2}enum {1} {{", visibility, typeName, constSpecifier));

            using (sb.IncreaseIndentation()) {
                int i = 1;
                foreach (var v in enumModel.Values)
                {
                    _docAppender.AppendEnumValueDoc(sb, v);
                    sb.AppendLineIndented(string.Format(i < enumModel.Values.Count ? "{0} = {1}," : "{0} = {1}", v.Name, v.Value));
                    i++;
                }
            }

            sb.AppendLineIndented("}");

            _generatedEnums.Add(enumModel);
        }
Ejemplo n.º 3
0
        protected virtual void AppendEnumDefinition(TsEnum enumModel, ScriptBuilder sb, TsGeneratorOutput output)
        {
            string typeName   = this.GetTypeName(enumModel);
            string visibility = (output & TsGeneratorOutput.Enums) == TsGeneratorOutput.Enums || (output & TsGeneratorOutput.Constants) == TsGeneratorOutput.Constants ? "export " : "";

            _docAppender.AppendEnumDoc(sb, enumModel, typeName);

            string constSpecifier = this.GenerateConstEnums ? "const " : string.Empty;

            sb.AppendLineIndented(string.Format("{0}{2}enum {1} {{", visibility, typeName, constSpecifier));

            using (sb.IncreaseIndentation()) {
                int    i                = 1;
                string valuesList       = "";
                string descriptionsList = "";

                foreach (var v in enumModel.Values)
                {
                    _docAppender.AppendEnumValueDoc(sb, v);
                    bool appendComma = enumModel.IsOutputValuesList || enumModel.IsOutputDescriptionsList || (i < enumModel.Values.Count);
                    if (enumModel.IsValueAsStringOfName)
                    {
                        sb.AppendLineIndented(string.Format(appendComma ? "{0} = '{0}'," : "{0} = '{0}'", v.Name));
                        if (enumModel.IsOutputValuesList)
                        {
                            valuesList += string.Format((i < enumModel.Values.Count) ? "{0}|" : "{0}", v.Name);
                        }
                    }
                    else
                    {
                        sb.AppendLineIndented(string.Format(appendComma ? "{0} = {1}," : "{0} = {1}", v.Name, v.Value));
                        if (enumModel.IsOutputValuesList)
                        {
                            valuesList += string.Format(i < enumModel.Values.Count ? "{0}|" : "{0}", v.Value);
                        }
                    }
                    if (enumModel.IsOutputDescriptionsList)
                    {
                        descriptionsList += string.Format(i < enumModel.Values.Count ? "{0}|" : "{0}", v.Description);
                    }
                    i++;
                }
                if (enumModel.IsOutputValuesList)
                {
                    sb.AppendLineIndented(string.Format(enumModel.IsOutputDescriptionsList ? "_ValuesList = '{0}'," : "_ValuesList = '{0}'", valuesList));
                }
                if (enumModel.IsOutputDescriptionsList)
                {
                    sb.AppendLineIndented(string.Format("_DescriptionsList = '{0}'", descriptionsList));
                }
            }

            sb.AppendLineIndented("}");

            _generatedEnums.Add(enumModel);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates class definition and appends it to the output.
        /// </summary>
        /// <param name="classModel">The class to generate definition for.</param>
        /// <param name="sb">The output.</param>
        /// <param name="generatorOutput"></param>
        protected virtual void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            string typeName   = this.GetTypeName(classModel);
            string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";

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

            if (classModel.Interfaces.Count > 0)
            {
                var implementations = classModel.Interfaces.Select(GetFullyQualifiedTypeName).ToArray();

                var prefixFormat = classModel.Type.IsInterface ? " extends {0}"
                    : classModel.BaseType != null ? " , {0}"
                    : " extends {0}";

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

            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;
                    }

                    _docAppender.AppendPropertyDoc(sb, property, this.GetPropertyName(property), this.GetPropertyType(property));
                    sb.AppendLineIndented(string.Format("{0}: {1};", this.GetPropertyName(property), this.GetPropertyType(property)));
                }
            }

            sb.AppendLineIndented("}");

            _generatedClasses.Add(classModel);
        }
Ejemplo n.º 5
0
        protected virtual void AppendModule(TsModule module, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            var classes = module.Classes.Where(c => !_typeConvertors.IsConvertorRegistered(c.Type) && !c.IsIgnored).OrderBy(c => GetTypeName(c)).ToList();
            var enums   = module.Enums.Where(e => !_typeConvertors.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;
            }

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

            if (generateModuleHeader)
            {
                if (generatorOutput != TsGeneratorOutput.Enums &&
                    (generatorOutput & TsGeneratorOutput.Constants) != TsGeneratorOutput.Constants)
                {
                    sb.Append("declare ");
                }

                sb.AppendLine(string.Format("namespace {0} {{", moduleName));
            }

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

                if (((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties) ||
                    (generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
                {
                    foreach (var classModel in classes)
                    {
                        this.AppendClassDefinition(classModel, sb, generatorOutput);
                    }
                }

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

                        this.AppendConstantModule(classModel, sb);
                    }
                }
            }
            if (generateModuleHeader)
            {
                sb.AppendLine("}");
            }
        }