Exemplo n.º 1
0
        protected virtual void AppendModule(TsModule module, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
        {
            var classes = module.Classes.Where(c => !_typeConvertors.IsConvertorRegistered(c.Type) && !c.IsIgnored).ToList();
            var enums   = module.Enums.Where(e => !_typeConvertors.IsConvertorRegistered(e.Type) && !e.IsIgnored).ToList();

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

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

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

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

                indentationLevelScope = 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.DecreaseIndentation(indentationLevelScope);
                sb.AppendLine("}");
            }
        }
 protected override void AppendEnumDefinition(TsEnum enumModel, ScriptBuilder sb, TsGeneratorOutput output)
 {
     base.AppendEnumDefinition(enumModel, sb, TsGeneratorOutput.Enums);
 }
Exemplo n.º 3
0
 public void AppendPropertyDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
 }
Exemplo n.º 4
0
 /// <summary>
 /// Generates reference to other d.ts file and appends it to the output.
 /// </summary>
 /// <param name="reference">The reference file to generate reference for.</param>
 /// <param name="sb">The output</param>
 protected virtual void AppendReference(string reference, ScriptBuilder sb)
 {
     sb.AppendFormat("/// <reference path=\"{0}\" />", reference);
     sb.AppendLine();
 }
Exemplo n.º 5
0
 public void AppendEnumDoc(ScriptBuilder sb, TsEnum enumModel, string enumName)
 {
 }
Exemplo n.º 6
0
 public void AppendEnumValueDoc(ScriptBuilder sb, TsEnumValue value)
 {
 }
Exemplo n.º 7
0
 public void AppendConstantModuleDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType)
 {
 }
Exemplo n.º 8
0
 public void AppendClassDoc(ScriptBuilder sb, TsClass classModel, string className)
 {
 }
Exemplo n.º 9
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}interface {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.Where(p => !p.IsIgnored).OrderBy(p => this.GetPropertyName(p)))
                {
                    _docAppender.AppendPropertyDoc(sb, property, this.GetPropertyName(property), this.GetPropertyType(property));

                    var tmpProperty = classModel.Type.GetProperty(property.Name);
                    if (tmpProperty?.GetCustomAttribute <RequiredAttribute>() != null)
                    {
                        sb.AppendLineIndented(string.Format("{0}: {1};", this.GetPropertyName(property), this.GetPropertyType(property)));
                    }
                    else if (property.PropertyType.Type.IsClass || property.PropertyType.Type.IsNullable())
                    {
                        sb.AppendLineIndented(string.Format("{0}?: {1};", this.GetPropertyName(property), this.GetPropertyType(property)));
                    }
                    else
                    {
                        sb.AppendLineIndented(string.Format("{0}: {1};", this.GetPropertyName(property), this.GetPropertyType(property)));
                    }
                }
            }

            sb.AppendLineIndented("}");

            _generatedClasses.Add(classModel);
        }
 /// <summary>
 /// Initializes a new instance of the indentationLevel class for the specific ScriptBuilder
 /// </summary>
 /// <param name="sb">The Scriptbuilder associated with this Indentation level.</param>
 internal IndentationLevelScope(ScriptBuilder sb)
 {
     _sb = sb;
 }
 /// <summary>
 /// Initializes a new instance of the indentationLevel class for the specific ScriptBuilder
 /// </summary>
 /// <param name="sb">The Scriptbuilder associated with this Indentation level.</param>
 internal IndentationLevelScope(ScriptBuilder sb)
 {
     _sb = sb;
 }