Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults();

            var cmdArgs = new CommandlineArguments(args);

            if (!cmdArgs.IsParsed)
            {
                return;
            }

            // Generator Start...

            var sw = Stopwatch.StartNew();

            Console.WriteLine("Project Compilation Start:" + cmdArgs.InputPath);

            var collector = new AssemblyAttributeCollector(cmdArgs.InputPath, cmdArgs.ConditionalSymbols);


            Console.WriteLine("Project Compilation Complete:" + sw.Elapsed.ToString());
            Console.WriteLine();

            sw.Restart();
            Console.WriteLine("Method Collect Start");

            collector.Visit();

            Console.WriteLine("Method Collect Complete:" + sw.Elapsed.ToString());

            Console.WriteLine("Output Generation Start");
            sw.Restart();


            var codeTemplate = new CodeTemplate
            {
                UnuseUnityAttribute = cmdArgs.UnuseUnityAttr,
                Namespace           = cmdArgs.NamespaceRoot,
                elementDefinitions  = collector.ElementDefinitions,
                enumDefinitions     = collector.EnumDefinitions,
                keyTupleDefinitions = collector.KeyTupleDefinitions
            };

            var sb = new StringBuilder();

            sb.AppendLine(codeTemplate.TransformText());
            Output(cmdArgs.OutputPath, sb.ToString());

            Console.WriteLine("String Generation Complete:" + sw.Elapsed.ToString());
            Console.WriteLine();
        }
Exemplo n.º 2
0
        public async Task GenerateFileAsync(
            string input,
            string output,
            bool unuseUnityAttr,
            string @namespace,
            string conditionalSymbol)
        {
            // Prepare args
            var namespaceDot       = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + ".";
            var conditionalSymbols = conditionalSymbol?.Split(',') ?? Array.Empty <string>();

            // Generator Start...

            var sw = Stopwatch.StartNew();

            logger("Project Compilation Start:" + input);

            var collector = new MethodCollector(input, conditionalSymbols, logger);

            logger("Project Compilation Complete:" + sw.Elapsed.ToString());

            sw.Restart();
            logger("Method Collect Start");

            var definitions    = collector.CollectServiceInterface();
            var hubDefinitions = collector.CollectHubInterface();

            GenericSerializationInfo[] genericInfos;
            EnumSerializationInfo[]    enumInfos;
            ExtractResolverInfo(definitions, out genericInfos, out enumInfos);
            ExtractResolverInfo(hubDefinitions.Select(x => x.hubDefinition).ToArray(), out var genericInfos2, out var enumInfos2);
            ExtractResolverInfo(hubDefinitions.Select(x => x.receiverDefintion).ToArray(), out var genericInfos3, out var enumInfos3);
            enumInfos    = enumInfos.Concat(enumInfos2).Concat(enumInfos3).Distinct().OrderBy(x => x.FullName).ToArray();
            genericInfos = genericInfos.Concat(genericInfos2).Concat(genericInfos3).Distinct().OrderBy(x => x.FullName).ToArray();

            logger("Method Collect Complete:" + sw.Elapsed.ToString());

            logger("Output Generation Start");
            sw.Restart();

            var resolverTemplate = new ResolverTemplate()
            {
                Namespace          = namespaceDot + "Resolvers",
                FormatterNamespace = namespaceDot + "Formatters",
                ResolverName       = "MagicOnionResolver",
                registerInfos      = genericInfos.OrderBy(x => x.FullName).Cast <IResolverRegisterInfo>().Concat(enumInfos.OrderBy(x => x.FullName)).ToArray()
            };

            var registerTemplate = new RegisterTemplate
            {
                Namespace           = @namespace,
                Interfaces          = definitions.Where(x => x.IsServiceDefinition).ToArray(),
                HubInterfaces       = hubDefinitions,
                UnuseUnityAttribute = unuseUnityAttr
            };

            if (Path.GetExtension(output) == ".cs")
            {
                var enumTemplates = enumInfos.GroupBy(x => x.Namespace)
                                    .OrderBy(x => x.Key)
                                    .Select(x => new EnumTemplate()
                {
                    Namespace = namespaceDot + "Formatters",
                    enumSerializationInfos = x.ToArray()
                })
                                    .ToArray();

                var texts = definitions
                            .GroupBy(x => x.Namespace)
                            .OrderBy(x => x.Key)
                            .Select(x => new CodeTemplate()
                {
                    Namespace  = x.Key,
                    Interfaces = x.ToArray()
                })
                            .ToArray();

                var hubTexts = hubDefinitions
                               .GroupBy(x => x.hubDefinition.Namespace)
                               .OrderBy(x => x.Key)
                               .Select(x => new HubTemplate()
                {
                    Namespace  = x.Key,
                    Interfaces = x.ToArray()
                })
                               .ToArray();

                var sb = new StringBuilder();
                sb.AppendLine("// <auto-generated />");
                sb.AppendLine(registerTemplate.TransformText());
                sb.AppendLine(resolverTemplate.TransformText());
                foreach (var item in enumTemplates)
                {
                    sb.AppendLine(item.TransformText());
                }

                foreach (var item in texts)
                {
                    sb.AppendLine(item.TransformText());
                }

                foreach (var item in hubTexts)
                {
                    sb.AppendLine(item.TransformText());
                }

                Output(output, sb.ToString());
            }
            else
            {
                Output(NormalizePath(output, registerTemplate.Namespace, "MagicOnionInitializer"), WithAutoGenerated(registerTemplate.TransformText()));
                Output(NormalizePath(output, resolverTemplate.Namespace, resolverTemplate.ResolverName), WithAutoGenerated(resolverTemplate.TransformText()));

                foreach (var enumTemplate in enumInfos)
                {
                    var x = new EnumTemplate()
                    {
                        Namespace = namespaceDot + "Formatters",
                        enumSerializationInfos = new[] { enumTemplate }
                    };

                    Output(NormalizePath(output, x.Namespace, enumTemplate.Name + "Formatter"), WithAutoGenerated(x.TransformText()));
                }

                foreach (var serviceClient in definitions)
                {
                    var x = new CodeTemplate()
                    {
                        Namespace  = serviceClient.Namespace,
                        Interfaces = new[] { serviceClient }
                    };

                    Output(NormalizePath(output, serviceClient.Namespace, serviceClient.ClientName), WithAutoGenerated(x.TransformText()));
                }

                foreach (var hub in hubDefinitions)
                {
                    var x = new HubTemplate()
                    {
                        Namespace  = hub.hubDefinition.Namespace,
                        Interfaces = new[] { hub }
                    };

                    Output(NormalizePath(output, hub.hubDefinition.Namespace, hub.hubDefinition.ClientName), WithAutoGenerated(x.TransformText()));
                }
            }

            if (definitions.Length == 0 && hubDefinitions.Length == 0)
            {
                logger("Generated result is empty, unexpected result?");
            }

            logger("Output Generation Complete:" + sw.Elapsed.ToString());
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var cmdArgs = new CommandlineArguments(args);

            if (!cmdArgs.IsParsed)
            {
                return;
            }

            // Generator Start...

            var sw = Stopwatch.StartNew();

            Console.WriteLine("Project Compilation Start:" + cmdArgs.InputPath);

            var collector = new MethodCollector(cmdArgs.InputPath, cmdArgs.ConditionalSymbols);

            Console.WriteLine("Project Compilation Complete:" + sw.Elapsed.ToString());
            Console.WriteLine();

            sw.Restart();
            Console.WriteLine("Method Collect Start");

            var definitions = collector.Visit();

            Console.WriteLine("Method Collect Complete:" + sw.Elapsed.ToString());

            Console.WriteLine("Output Generation Start");
            sw.Restart();

            var texts = definitions
                        .GroupBy(x => x.Namespace)
                        .Select(x => new CodeTemplate()
            {
                Namespace             = x.Key,
                ZeroFormatterResolver = cmdArgs.ResolverName,
                Interfaces            = x.ToArray()
            })
                        .ToArray();

            var registerTemplate = new RegisterTemplate
            {
                Namespace           = cmdArgs.NamespaceRoot,
                Interfaces          = definitions.Where(x => x.IsServiceDifinition).ToArray(),
                UnuseUnityAttribute = cmdArgs.UnuseUnityAttr
            };

            if (cmdArgs.IsSeparate)
            {
                var initializerPath = Path.Combine(cmdArgs.OutputPath, "MagicOnionInitializer.cs");
                Output(initializerPath, registerTemplate.TransformText());

                foreach (var item in texts)
                {
                    foreach (var interfaceDef in item.Interfaces)
                    {
                        var path      = Path.Combine(cmdArgs.OutputPath, interfaceDef.ToString().Replace(".", "\\") + ".cs");
                        var template2 = new CodeTemplate()
                        {
                            Namespace = interfaceDef.Namespace, ZeroFormatterResolver = cmdArgs.ResolverName, Interfaces = new[] { interfaceDef }
                        };
                        Output(path, template2.TransformText());
                    }
                }
            }
            else
            {
                var sb = new StringBuilder();
                sb.AppendLine(registerTemplate.TransformText());
                foreach (var item in texts)
                {
                    sb.AppendLine(item.TransformText());
                }
                Output(cmdArgs.OutputPath, sb.ToString());
            }

            Console.WriteLine("String Generation Complete:" + sw.Elapsed.ToString());
            Console.WriteLine();
        }