Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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 ScriptBuilder(this.IndentationString);

            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 _references.Concat(model.References))
                {
                    this.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(m => GetModuleName(m)))
            {
                this.AppendModule(module, sb, generatorOutput);
            }

            return(sb.ToString());
        }
Пример #3
0
        /// <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));
            foreach (KeyValuePair <string, int> entry in ModuleSortOrders)
            {
                var module = model.Modules.FirstOrDefault(c => c.Name == entry.Key);
                if (module != null)
                {
                    module.SortOrder = entry.Value;
                }
            }
            return(model);
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the TypeResolver.
        /// </summary>
        /// <param name="model">The model to process.</param>
        public TypeResolver(TsModel model)
        {
            _model      = model;
            _modules    = new Dictionary <string, TsModule>();
            _knownTypes = new Dictionary <Type, TsType>();

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

            foreach (var enumModel in model.Enums)
            {
                _knownTypes[enumModel.Type] = enumModel;
            }
        }
Пример #5
0
 /// <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(this.Generate(model, TsGeneratorOutput.Properties | TsGeneratorOutput.Enums));
 }
Пример #6
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)
 {
 }