Beispiel #1
0
        private static async Task <int> Main(string[] args)
        {
            try
            {
                var cmdArgs = new CommandlineArguments(args);
                if (!cmdArgs.IsParsed)
                {
                    return(0);
                }

                // Generator Start...
                var sw = Stopwatch.StartNew();
                Console.WriteLine("Project Compilation Start:" + cmdArgs.InputPath);

                var collector = await TypeCollector.CreateAsync(cmdArgs.InputPath, cmdArgs.ConditionalSymbols, true, cmdArgs.IsUseMap);

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

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

                (ObjectSerializationInfo[] objectInfo, EnumSerializationInfo[] enumInfo, GenericSerializationInfo[] genericInfo, UnionSerializationInfo[] unionInfo) = collector.Collect();

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

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

                FormatterTemplate[] objectFormatterTemplates = objectInfo
                                                               .GroupBy(x => x.Namespace)
                                                               .Select(x => new FormatterTemplate()
                {
                    Namespace = cmdArgs.GetNamespaceDot() + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key),
                    ObjectSerializationInfos = x.ToArray(),
                })
                                                               .ToArray();

                EnumTemplate[] enumFormatterTemplates = enumInfo
                                                        .GroupBy(x => x.Namespace)
                                                        .Select(x => new EnumTemplate()
                {
                    Namespace = cmdArgs.GetNamespaceDot() + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key),
                    EnumSerializationInfos = x.ToArray(),
                })
                                                        .ToArray();

                UnionTemplate[] unionFormatterTemplates = unionInfo
                                                          .GroupBy(x => x.Namespace)
                                                          .Select(x => new UnionTemplate()
                {
                    Namespace = cmdArgs.GetNamespaceDot() + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key),
                    UnionSerializationInfos = x.ToArray(),
                })
                                                          .ToArray();

                var resolverTemplate = new ResolverTemplate()
                {
                    Namespace          = cmdArgs.GetNamespaceDot() + "Resolvers",
                    FormatterNamespace = cmdArgs.GetNamespaceDot() + "Formatters",
                    ResolverName       = cmdArgs.ResolverName,
                    RegisterInfos      = genericInfo.Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo).ToArray(),
                };

                var sb = new StringBuilder();
                sb.AppendLine("// <auto-generated />");
                sb.AppendLine();
                sb.AppendLine(resolverTemplate.TransformText());

                foreach (EnumTemplate item in enumFormatterTemplates)
                {
                    var text = item.TransformText();
                    sb.AppendLine(text);
                }

                foreach (UnionTemplate item in unionFormatterTemplates)
                {
                    var text = item.TransformText();
                    sb.AppendLine(text);
                }

                foreach (FormatterTemplate item in objectFormatterTemplates)
                {
                    var text = item.TransformText();
                    sb.AppendLine(text);
                }

                Output(cmdArgs.OutputPath, sb.ToString());

                Console.WriteLine("String Generation Complete:" + sw.Elapsed.ToString());
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled Error:" + ex);
                return(1);
            }
        }
        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 TypeCollector(cmdArgs.InputPath, cmdArgs.ConditionalSymbols, true, cmdArgs.IsUseMap);

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

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

            var(objectInfo, enumInfo, genericInfo, unionInfo) = collector.Collect();

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

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

            var objectFormatterTemplates = objectInfo
                                           .GroupBy(x => x.Namespace)
                                           .Select(x => new FormatterTemplate()
            {
                Namespace = cmdArgs.GetNamespaceDot() + "Formatters." + x.Key,
                objectSerializationInfos = x.ToArray(),
            })
                                           .ToArray();

            var enumFormatterTemplates = enumInfo
                                         .GroupBy(x => x.Namespace)
                                         .Select(x => new EnumTemplate()
            {
                Namespace = cmdArgs.GetNamespaceDot() + "Formatters." + x.Key,
                enumSerializationInfos = x.ToArray()
            })
                                         .ToArray();

            var unionFormatterTemplates = unionInfo
                                          .GroupBy(x => x.Namespace)
                                          .Select(x => new UnionTemplate()
            {
                Namespace = cmdArgs.GetNamespaceDot() + "Formatters." + x.Key,
                unionSerializationInfos = x.ToArray()
            })
                                          .ToArray();

            var resolverTemplate = new ResolverTemplate()
            {
                Namespace          = cmdArgs.GetNamespaceDot() + "Resolvers",
                FormatterNamespace = cmdArgs.GetNamespaceDot() + "Formatters",
                ResolverName       = cmdArgs.ResolverName,
                registerInfos      = genericInfo.Cast <IResolverRegisterInfo>().Concat(enumInfo).Concat(unionInfo).Concat(objectInfo).ToArray()
            };

            var sb = new StringBuilder();

            sb.AppendLine(resolverTemplate.TransformText());
            sb.AppendLine();
            foreach (var item in enumFormatterTemplates)
            {
                var text = item.TransformText();
                sb.AppendLine(text);
            }
            sb.AppendLine();
            foreach (var item in unionFormatterTemplates)
            {
                var text = item.TransformText();
                sb.AppendLine(text);
            }
            sb.AppendLine();
            foreach (var item in objectFormatterTemplates)
            {
                var text = item.TransformText();
                sb.AppendLine(text);
            }

            Output(cmdArgs.OutputPath, sb.ToString());

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