/// <summary>
        /// Build the model.
        /// </summary>
        /// <returns>The script model with the classes.</returns>
        public TsModel Build()
        {
            var model = new TsModel(this.Classes.Values, this.Enums.Values);

            model.RunVisitor(new TypeResolver(model));
            return(model);
        }
        /// <summary>
        /// Generates TypeScript definitions for classes and/or enums in the model.
        /// </summary>
        /// <param name="model">The code model with classes to generate definitions for.</param>
        /// <param name="generatorOutput">The type of definitions to generate</param>
        /// <returns>TypeScript definitions for classes and/or enums in the model..</returns>
        public string Generate(TsModel model, TsGeneratorOutput generatorOutput)
        {
            var sb = new IndentedStringBuilder();

            if ((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties ||
                (generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
            {
                if ((generatorOutput & TsGeneratorOutput.Constants) == TsGeneratorOutput.Constants)
                {
                    // We can't generate constants together with properties or fields, because we can't set values in a .d.ts file.
                    throw new InvalidOperationException("Cannot generate constants together with properties or fields");
                }

                foreach (var reference in Options.References.Concat(model.References))
                {
                    AppendReference(reference, sb);
                }
                sb.AppendLine();
            }

            // We can't just sort by the module name, because a formatter can jump in and change it so
            // format by the desired target name
            foreach (var module in model.Modules.OrderBy(GetModuleName))
            {
                AppendModule(module, sb, generatorOutput);
            }

            return(sb.ToString());
        }
        /// <summary>
        /// Initializes a new instance of the TypeResolver.
        /// </summary>
        /// <param name="model">The model to process.</param>
        public TypeResolver(TsModel model)
        {
            _model = model;

            foreach (var classModel in model.Classes)
            {
                _knownTypes[classModel.Type] = classModel;
            }

            foreach (var enumModel in model.Enums)
            {
                _knownTypes[enumModel.Type] = enumModel;
            }
        }
Exemple #4
0
 /// <summary>
 /// When overridden in a derived class, it can examine or modify the whole model.
 /// </summary>
 /// <param name="model">The code model being visited.</param>
 public virtual void VisitModel(TsModel model)
 {
 }
 /// <summary>
 /// Generates TypeScript definitions for properties and enums in the model.
 /// </summary>
 /// <param name="model">The code model with classes to generate definitions for.</param>
 /// <returns>TypeScript definitions for classes in the model.</returns>
 public string Generate(TsModel model)
 {
     return(Generate(model, TsGeneratorOutput.Properties | TsGeneratorOutput.Enums));
 }