public override void Visit(RtClass rtClass)
 {
     foreach (var member in rtClass.Members)
     {
         Visit(member);
     }
 }
Пример #2
0
        private void BuildClientClass(string typeName, SignalRGenerationOptions options, Type element, RtInterface result, TypeResolver resolver)
        {
            RtClass clientImpl = new RtClass
            {
                Name       = new RtSimpleTypeName(typeName),
                Export     = true,
                Decorators = { },
                Members    =
                {
                    new RtConstructor
                    {
                        Arguments =   { new RtArgument
                                        {
                                            Type       = new RtSimpleTypeName(options.HubConnectionProviderType),
                                            Identifier = new RtIdentifier("hubConnectionProvider")
                                        } },
                        Body      = new RtRaw($"this.hubConnection = hubConnectionProvider.getHubConnection(\"{options.HubPath}\");"),
                        LineAfter = " "
                    }
                }
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result, options.HubPath));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
            Context.AddNewLine();
        }
Пример #3
0
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            var clientImpl = new RtClass()
            {
                Name         = new RtSimpleTypeName($"{element.Name}Client"),
                Export       = true,
                Decorators   = { new RtDecorator("Injectable()\r\n") },
                Implementees = { result.Name },
                Members      =
                {
                    new RtField
                    {
                        AccessModifier = AccessModifier.Private,
                        Identifier     = new RtIdentifier("hubConnection"),
                        Type           = new RtSimpleTypeName("Promise<HubConnection>")
                    },
                    new RtConstructor
                    {
                        Arguments =   { new RtArgument
                                        {
                                            Type       = new RtSimpleTypeName("HubConnectionProvider"),
                                            Identifier = new RtIdentifier("hubConnectionProvider")
                                        } },
                        Body = new RtRaw("this.hubConnection = hubConnectionProvider.getHubConnection(\"/hub\");"),
                    }
                },
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
        }
        public override void Visit(RtClass node)
        {
            if (node == null) return;
            Visit(node.Documentation);
            var prev = Context;
            Context = WriterContext.Class;
            AppendTabs();
            if (prev == WriterContext.Module) Write("export ");
            else Write("declare ");

            Write("class ");
            Visit(node.Name);
            if (node.Extendee != null)
            {
                Write(" extends ");
                Visit(node.Extendee);
            }
            if (node.Implementees.Count > 0)
            {
                Write(" implements ");
                SequentialVisit(node.Implementees, ", ");
            }
            Br(); AppendTabs();
            Write("{"); Br();
            Tab();
            var members = node.Members.OrderBy(c => c is RtConstructor ? 0 : 1);
            foreach (var rtMember in members)
            {
                Visit(rtMember);
            }
            UnTab();
            AppendTabs(); WriteLine("}");
            Context = prev;
        }
Пример #5
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);
        }
Пример #6
0
        public override void Visit(RtClass node)
        {
            if (node == null)
            {
                return;
            }
            Visit(node.Documentation);
            var prev = Context;

            Context = WriterContext.Class;
            AppendTabs();
            if (prev == WriterContext.Module)
            {
                Write("export ");
            }
            else
            {
                Write("declare ");
            }
            if (node.Abstract)
            {
                Write("abstract ");
            }
            Write("class ");
            Visit(node.Name);
            if (node.Extendee != null)
            {
                Write(" extends ");
                Visit(node.Extendee);
            }
            if (node.Implementees.Count > 0)
            {
                Write(" implements ");
                SequentialVisit(node.Implementees, ", ");
            }
            Br();
            AppendTabs();
            Write("{");
            Br();
            Tab();
            var members = node.Members.OrderBy(c => c is RtConstructor ? 0 : 1);

            foreach (var rtMember in members)
            {
                Visit(rtMember);
            }
            UnTab();
            AppendTabs();
            WriteLine("}");
            Context = prev;
        }
Пример #7
0
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            var typeName   = element.IsInterface && element.Name.StartsWith('I') ? element.Name.Substring(1) : element.Name;
            var clientImpl = new RtClass
            {
                Name       = new RtSimpleTypeName(typeName),
                Export     = true,
                Decorators = { new RtDecorator("Injectable()\r\n") },
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
        }
Пример #8
0
        public override RtClass GenerateNode(Type element, RtClass result, TypeResolver resolver)
        {
            result = base.GenerateNode(element, result, resolver);

            try
            {
                AddViewModelToNode(element, result);
            }
            catch (Exception e)
            {
                TsConfigurationBuilderExtensions.LogAnything(e, "e");
                throw;
            }

            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;
        }
Пример #10
0
        public override void Visit(RtClass node)
        {
            if (node == null)
            {
                return;
            }
            Visit(node.Documentation);
            var prev = Context;

            Context = WriterContext.Class;
            AppendTabs();
            Decorators(node);
            if (node.Export)
            {
                Write("export ");
            }
            if (node.Abstract)
            {
                Write("abstract ");
            }
            Write("class ");
            Visit(node.Name);
            if (node.Extendee != null)
            {
                Write(" extends ");
                Visit(node.Extendee);
            }
            if (node.Implementees.Count > 0)
            {
                Write(" implements ");
                SequentialVisit(node.Implementees, ", ");
            }
            Br(); AppendTabs();
            Write("{"); Br();
            Tab();
            var members = DoSortMembers(node.Members);

            foreach (var rtMember in members)
            {
                Visit(rtMember);
            }
            UnTab();
            AppendTabs(); WriteLine("}");
            Context = prev;
        }
Пример #11
0
        private RtRaw GenerateInitializationMethod(Type element, RtClass result)
        {
            var initializationMethod = GenerateInitializationMethodText(element, result);

            return(new RtRaw(initializationMethod));

            string GenerateInitializationMethodText(Type element, RtClass result)
            {
                var initializeMethod = new StringBuilder();

                initializeMethod.AppendLine();
                initializeMethod.AppendLine($"static init = (): {result.Name.TypeName} => ({{");

                var fields     = result.Members.Where(m => m is RtField).Select(m => m as RtField);
                var properties = element.GetProperties();

                foreach (var field in fields)
                {
                    if (field.Type is RtSimpleTypeName fieldType)
                    {
                        var propertyType = properties
                                           .First(p => p.Name.ToLower() == field.Identifier.IdentifierName.ToLower())
                                           .PropertyType;

                        var defaultType = fieldType.TypeName switch
                        {
                            var type when type == "number" => "-1",
                            var type when type == "string" => "''",
                            var type when type == "boolean" => "false",
                            var type when type == "Date" => "new Date()",
                            _ => GetDefaultForComplexType(propertyType),
                        };

                        if (defaultType != null)
                        {
                            initializeMethod.AppendLine($"\t {field.Identifier.IdentifierName}: {defaultType},");
                        }
                    }
                }
                initializeMethod.AppendLine("});");

                return(initializeMethod.ToString());
            }
        }
        public override void Visit(RtClass node)
        {
            if (node == null)
            {
                return;
            }
            Visit(node.Documentation);
            var prev = Context;

            Context = WriterContext.Class;
            AppendTabs();
            Decorators(node);
            if (node.Export)
            {
                Write("export ");
            }
            Write("class ");
            Visit(node.Name);
            if (node.Extendee != null)
            {
                Write(" extends ");
                Visit(node.Extendee);
            }
            if (node.Implementees.Count > 0)
            {
                Write(" implements ");
                SequentialVisit(node.Implementees, ", ");
            }
            Br(); AppendTabs();
            Write("{"); Br();
            Tab();
            var members = node.Members.OrderBy(c => c is RtConstructor ? int.MinValue : (c is RtMember ? ((RtMember)c).Order : (double?)null));

            foreach (var rtMember in members)
            {
                Visit(rtMember);
            }
            UnTab();
            AppendTabs(); WriteLine("}");
            Context = prev;
        }
Пример #13
0
        public override RtClass GenerateNode(Type element, RtClass result, TypeResolver resolver)
        {
            // obtain current namespace
            var ns = this.Context.Location.CurrentNamespace;
            var r  = base.GenerateNode(element, result, resolver);

            foreach (var rMember in r.Members)
            {
                var m = rMember as RtFuncion;
                if (m != null)
                {
                    m.AccessModifier = null;

                    ns.CompilationUnits.Add(m);
                }
            }

            // return null instead of result to
            // suppress writing AST of original class
            // to resulting file
            return(null);
        }
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            var options    = SignalRGenerationOptions.All[element];
            var hub        = options.HubPath;
            var typeName   = element.IsInterface && element.Name.StartsWith('I') ? element.Name.Substring(1) : element.Name;
            var clientImpl = new RtClass()
            {
                Name         = new RtSimpleTypeName($"{typeName}Invoker"), //Naming becomes hard with a strongly typed "client" API on the server already
                Export       = true,
                Decorators   = {  },
                Implementees = { result.Name },
                Members      =
                {
                    new RtField
                    {
                        AccessModifier = AccessModifier.Private,
                        Identifier     = new RtIdentifier("hubConnection"),
                        Type           = new RtSimpleTypeName("Promise<HubConnection>")
                    },
                    new RtConstructor
                    {
                        Arguments =   { new RtArgument
                                        {
                                            Type       = new RtSimpleTypeName(options.HubConnectionProviderType),
                                            Identifier = new RtIdentifier("hubConnectionProvider")
                                        } },
                        Body      = new RtRaw($"this.hubConnection = hubConnectionProvider.getHubConnection(\"{hub}\");"),
                        LineAfter = " "
                    }
                },
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
            Context.AddNewLine();
        }
Пример #15
0
 public abstract void Visit(RtClass node);
Пример #16
0
 public abstract T Visit(RtClass node);
Пример #17
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);
        }