Exemplo n.º 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);
        }
Exemplo n.º 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());
        }
Exemplo n.º 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), PropertyVisibilityFormatter);
            return(model);
        }
Exemplo n.º 4
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();
            }

            foreach (var module in model.Modules)
            {
                this.AppendModule(module, sb, generatorOutput);
            }

            return(sb.ToString());
        }
Exemplo n.º 5
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;
            }
        }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 7
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));
 }
Exemplo n.º 8
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>
 /// 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)
 {
 }
        public void WhenModelContainsReference_ReferenceIsAddedToOutput()
        {
            var model = new TsModel();
            model.References.Add("knockout.d.ts");

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.Contains("/// <reference path=\"knockout.d.ts\" />", script);
        }