Exemplo n.º 1
0
 /// <summary>
 /// Configures class to export constructor
 /// If constructor body is not specified, then default body will be generated.
 /// Default body is empty if there are no inheritance.
 /// If there is inheritance then RT will try to generate optimal super() call
 /// that can be controlled by <see cref="TsBaseParamAttribute"/>
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="conf"></param>
 /// <param name="exportConstructors">When true, constructor will be exported</param>
 /// <param name="constructorBody">Optional constructor body implementation</param>
 /// <returns></returns>
 public static T WithConstructor <T>(this T conf, RtRaw constructorBody = null, bool exportConstructors = true)
     where T : ClassExportBuilder
 {
     conf.Attr.AutoExportConstructors = exportConstructors;
     conf.Blueprint.ConstructorBody   = constructorBody;
     return(conf);
 }
Exemplo n.º 2
0
        private void AddViewModelToNode(Type element, RtClass result)
        {
            var collectionName = new RtRaw($"static Name = '{result.Name.TypeName}';");

            result.Members.Insert(0, collectionName);

            var initializationMethod = GenerateInitializationMethod(element, result);

            result.Members.Add(initializationMethod);
        }
Exemplo n.º 3
0
 private void CodeBlock(RtRaw content)
 {
     Br();
     AppendTabs();
     WriteLine("{");
     Tab();
     Visit(content);
     UnTab();
     AppendTabs();
     WriteLine("}");
 }
Exemplo n.º 4
0
 public override void Visit(RtRaw node)
 {
     if (node == null)
     {
         return;
     }
     if (string.IsNullOrEmpty(node.RawContent))
     {
         return;
     }
     WriteLines(node.RawContent);
 }
Exemplo n.º 5
0
        public override RtFuncion GenerateNode(MethodInfo element, RtFuncion result, TypeResolver resolver)
        {
            result = base.GenerateNode(element, result, resolver);
            if (result == null)
            {
                return(null);
            }

            // here we are overriding return type to corresponding promise
            var  retType = result.ReturnType;
            bool isVoid  = (retType is RtSimpleTypeName) && (((RtSimpleTypeName)retType).TypeName == "void");

            // we use TypeResolver to get "any" type to avoid redundant type name construction
            // (or because I'm too lazy to manually construct "any" type)
            if (isVoid)
            {
                retType = resolver.ResolveTypeName(typeof(object));
            }

            // Here we override TS method return type to make it angular.IPromise
            // We are using RtSimpleType with generig parameter of existing method type
            result.ReturnType = new RtSimpleTypeName(new[] { retType }, "angular", "IPromise");

            // Here we retrieve method parameters
            // We are using .GetName() extension method to retrieve parameter name
            // It is supplied within Reinforced.Typings and retrieves parameter name
            // including possible name override with Fluent configuration or
            // [TsParameter] attribute
            var p = element.GetParameters().Select(c => string.Format("'{0}': {0}", c.GetName()));

            // Joining parameters for method body code
            var dataParameters = string.Join(", ", p);

            // Here we get path to controller
            // It is quite simple solution requiring /{controller}/{action} route
            string controller = element.DeclaringType.Name.Replace("Controller", String.Empty);
            string path       = String.Format("/{0}/{1}", controller, element.Name);

            const string code = @"var params = {{ {1} }};
return this.http.post('{0}', params)
    .then((response) => {{ response.data['requestParams'] = params; return response.data; }});";

            RtRaw body = new RtRaw(String.Format(code, path, dataParameters));

            result.Body = body;

            // That's all. here we return node that will be written to target file.
            // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts
            return(result);
        }
        public override RtClass GenerateNode(Type element, RtClass result, TypeResolver resolver)
        {
            result = base.GenerateNode(element, result, resolver);
            if (result == null) return null;

            // We add some docs to keep you oriented
            result.Documentation = new RtJsdocNode(){Description = "Result of AngularControllerGenerator activity"};

            // Here we just create ng.IHttpService type name because it is used several times
            var httpServiceType = new RtSimpleTypeName("IHttpService") { Namespace = "angular" };

            // Here we are declaring constructor for our angular service using $http as parameter
            // It is quite simple so no more details
            RtConstructor constructor = new RtConstructor();
            constructor.Arguments.Add(new RtArgument(){Type = httpServiceType,Identifier = new RtIdentifier("$http")});
            constructor.Body = new RtRaw("this.http = $http;");

            // Here we declaring class field for storing $http instance
            RtField httpServiceField = new RtField()
            {
                Type = httpServiceType,
                Identifier = new RtIdentifier("http"),
                AccessModifier = AccessModifier.Private,
                Documentation = new RtJsdocNode() { Description = "Keeps $http instance received on construction"}
            };

            // Here we are adding our constructor and field to resulting class
            result.Members.Add(httpServiceField);
            result.Members.Add(constructor);

            // Also we will add controller registration to our app instance
            // To automatically get it registered in Angular's IoC
            const string initializerFormat =
                "if (window['app']) window['app'].factory('Api.{0}', ['$http', ($http: angular.IHttpService) => new {1}($http)]);";

            RtRaw registration = new RtRaw(String.Format(initializerFormat,element.Name,result.Name));
            
            // Since RtModule.compilationUnits is not typed and could contain any type then we 
            // simply add RtRaw node here with registration glue code
            // app variable is declared in /Scripts/ReinforcedTypings/App.ts since it is used in 
            // corresponding client script
            Context.Location.CurrentModule.CompilationUnits.Add(registration);

            // That's all. here we return node that will be written to target file.
            // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts
            return result;
        }
        public override RtFuncion GenerateNode(MethodInfo element, RtFuncion result, TypeResolver resolver)
        {
            result = base.GenerateNode(element, result, resolver);
            if (result == null) return null;

            // here we are overriding return type to corresponding promise
            var retType = result.ReturnType;
            bool isVoid = (retType is RtSimpleTypeName) && (((RtSimpleTypeName)retType).TypeName == "void");

            // we use TypeResolver to get "any" type to avoid redundant type name construction
            // (or because I'm too lazy to manually construct "any" type)
            if (isVoid) retType = resolver.ResolveTypeName(typeof(object));

            // Here we override TS method return type to make it angular.IPromise
            // We are using RtSimpleType with generig parameter of existing method type
            result.ReturnType = new RtSimpleTypeName(new[] { retType }, "angular", "IPromise");

            // Here we retrieve method parameters
            // We are using .GetName() extension method to retrieve parameter name
            // It is supplied within Reinforced.Typings and retrieves parameter name 
            // including possible name override with Fluent configuration or 
            // [TsParameter] attribute
            var p = element.GetParameters().Select(c => string.Format("'{0}': {0}", c.GetName()));

            // Joining parameters for method body code
            var dataParameters = string.Join(", ", p);

            // Here we get path to controller
            // It is quite simple solution requiring /{controller}/{action} route
            string controller = element.DeclaringType.Name.Replace("Controller", String.Empty);
            string path = String.Format("/{0}/{1}", controller, element.Name);

            const string code = @"var params = {{ {1} }};
return this.http.post('{0}', params)
    .then((response) => {{ response.data['requestParams'] = params; return response.data; }});";

            RtRaw body = new RtRaw(String.Format(code, path, dataParameters));
            result.Body = body;

            // That's all. here we return node that will be written to target file.
            // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts
            return result;
        }
Exemplo n.º 8
0
        private RtRaw GenerateEnumDescription(Type element, RtEnum resultEnum)
        {
            var enumDescription = new StringBuilder();

            enumDescription.AppendLine();

            var enumName = resultEnum.EnumName;

            var tsType = $"{{[key in {enumName}]: {{long: string, short: string}}}}";

            enumDescription.AppendLine($"export const {enumName}Descriptions: {tsType} = {{");

            foreach (var resultEnumValue in resultEnum.Values)
            {
                var descriptionObj = GetElementDescription(element, resultEnumValue.EnumValueName);
                enumDescription.AppendLine($"\t[{enumName}.{resultEnumValue.EnumValueName}]: {descriptionObj},");
            }

            enumDescription.AppendLine("};");

            var item = new RtRaw(enumDescription.ToString());

            return(item);
        }
Exemplo n.º 9
0
 public abstract void Visit(RtRaw node);
 public override void Visit(RtRaw node)
 {
 }
Exemplo n.º 11
0
 public abstract T Visit(RtRaw node);
Exemplo n.º 12
0
        public override RtClass GenerateNode(Type element, RtClass result, TypeResolver resolver)
        {
            result = base.GenerateNode(element, result, resolver);
            if (result == null)
            {
                return(null);
            }

            // We add some docs to keep you oriented
            result.Documentation = new RtJsdocNode()
            {
                Description = "Result of AngularControllerGenerator activity"
            };

            // Here we just create ng.IHttpService type name because it is used several times
            var httpServiceType = new RtSimpleTypeName("IHttpService")
            {
                Namespace = "angular"
            };

            // Here we are declaring constructor for our angular service using $http as parameter
            // It is quite simple so no more details
            RtConstructor constructor = new RtConstructor();

            constructor.Arguments.Add(new RtArgument()
            {
                Type = httpServiceType, Identifier = new RtIdentifier("$http")
            });
            constructor.Body = new RtRaw("this.http = $http;");

            // Here we declaring class field for storing $http instance
            RtField httpServiceField = new RtField()
            {
                Type           = httpServiceType,
                Identifier     = new RtIdentifier("http"),
                AccessModifier = AccessModifier.Private,
                Documentation  = new RtJsdocNode()
                {
                    Description = "Keeps $http instance received on construction"
                }
            };

            // Here we are adding our constructor and field to resulting class
            result.Members.Add(httpServiceField);
            result.Members.Add(constructor);

            // Also we will add controller registration to our app instance
            // To automatically get it registered in Angular's IoC
            const string initializerFormat =
                "if (window['app']) window['app'].factory('Api.{0}', ['$http', ($http: angular.IHttpService) => new {1}($http)]);";

            RtRaw registration = new RtRaw(String.Format(initializerFormat, element.Name, result.Name));

            // Since RtModule.compilationUnits is not typed and could contain any type then we
            // simply add RtRaw node here with registration glue code
            // app variable is declared in /Scripts/ReinforcedTypings/App.ts since it is used in
            // corresponding client script
            Context.Location.CurrentModule.CompilationUnits.Add(registration);

            // That's all. here we return node that will be written to target file.
            // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts
            return(result);
        }