Esempio n. 1
0
        private static void GenTypeInternal(Type type)
        {
            string path = GenPath + "/" + type.Name + "Formatter.cs";

            Debug.Log("Gen " + type.FullName + "\n" + path);

            //if (File.Exists(path))
            //    return;
            FormatterTemplate formatterTemplate = new FormatterTemplate();

            formatterTemplate.Namespace = NameSpace;
            var fields = (from fieldInfo in type.GetFields()
                          where !fieldInfo.IsStatic &&
                          !fieldInfo.IsDefined(typeof(IgnoreDataMemberAttribute)) &&
                          !fieldInfo.IsDefined(typeof(NonSerializedAttribute))
                          select fieldInfo)
                         .ToArray();
            var members = (from fieldInfo in fields
                           select new MemberSerializationInfo()
            {
                IsField = true,
                Name = fieldInfo.Name,
                MemberName = fieldInfo.Name,
                Type = GetPrimitiveTypeName(fieldInfo.FieldType),
                ShortTypeName = fieldInfo.FieldType.Name,
                IsReadable = true,
                IsWritable = true
            })
                          .ToArray();
            ObjectSerializationInfo objectSerializationInfo = new ObjectSerializationInfo()
            {
                Members               = members,
                Name                  = type.Name,
                FullName              = type.FullName,
                Namespace             = type.Namespace,
                IsClass               = type.IsClass,
                HasConstructor        = true,
                ConstructorParameters = new MemberSerializationInfo[0]
            };

            formatterTemplate.objectSerializationInfos = new[] { objectSerializationInfo };

            var sb = new StringBuilder();

            sb.AppendLine(formatterTemplate.TransformText());
            Directory.CreateDirectory(Path.GetDirectoryName(path));
            File.WriteAllText(path, sb.ToString());
        }
Esempio n. 2
0
        public async Task GenerateFileAsync(
            string input,
            string output,
            string conditionalSymbol,
            string resolverName,
            string @namespace,
            bool useMapMode,
            string multipleIfDirectiveOutputSymbols)
        {
            var namespaceDot          = string.IsNullOrWhiteSpace(@namespace) ? string.Empty : @namespace + ".";
            var conditionalSymbols    = conditionalSymbol?.Split(',') ?? Array.Empty <string>();
            var multipleOutputSymbols = multipleIfDirectiveOutputSymbols?.Split(',') ?? Array.Empty <string>();

            var sw = Stopwatch.StartNew();

            foreach (var multioutSymbol in multipleOutputSymbols.Length == 0 ? new[] { string.Empty } : multipleOutputSymbols)
            {
                logger("Project Compilation Start:" + input);

                var compilation = (Path.GetExtension(input) == ".csproj")
                    ? await MessagePackCompilation.CreateFromProjectAsync(input.Split(','), conditionalSymbols.Concat(new[] { multioutSymbol }).ToArray(), cancellationToken)
                                  .ConfigureAwait(false) : await MessagePackCompilation.CreateFromDirectoryAsync(input, conditionalSymbols.Concat(new[] { multioutSymbol }).ToArray(), cancellationToken).ConfigureAwait(false);

                var collector = new TypeCollector(compilation, true, useMapMode, x => Console.WriteLine(x));

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

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

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

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

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

                if (Path.GetExtension(output) == ".cs")
                {
                    // SingleFile Output
                    var objectFormatterTemplates = objectInfo
                                                   .GroupBy(x => x.Namespace)
                                                   .Select(x => new FormatterTemplate()
                    {
                        Namespace = namespaceDot + "Formatters" + ((x.Key == null) ? string.Empty : "." + x.Key),
                        ObjectSerializationInfos = x.ToArray(),
                    })
                                                   .ToArray();

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

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

                    var resolverTemplate = new ResolverTemplate()
                    {
                        Namespace          = namespaceDot + "Resolvers",
                        FormatterNamespace = namespaceDot + "Formatters",
                        ResolverName       = 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);
                    }

                    if (multioutSymbol == string.Empty)
                    {
                        await OutputAsync(output, sb.ToString(), cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        var fname = Path.GetFileNameWithoutExtension(output) + "." + MultiSymbolToSafeFilePath(multioutSymbol) + ".cs";
                        var text  = $"#if {multioutSymbol}" + Environment.NewLine + sb.ToString() + Environment.NewLine + "#endif";
                        await OutputAsync(Path.Combine(Path.GetDirectoryName(output), fname), text, cancellationToken).ConfigureAwait(false);
                    }
                }
                else
                {
                    // Multiple File output
                    foreach (var x in objectInfo)
                    {
                        var template = new FormatterTemplate()
                        {
                            Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace),
                            ObjectSerializationInfos = new[] { x },
                        };

                        var text = template.TransformText();
                        await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false);
                    }

                    foreach (var x in enumInfo)
                    {
                        var template = new EnumTemplate()
                        {
                            Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace),
                            EnumSerializationInfos = new[] { x },
                        };

                        var text = template.TransformText();
                        await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false);
                    }

                    foreach (var x in unionInfo)
                    {
                        var template = new UnionTemplate()
                        {
                            Namespace = namespaceDot + "Formatters" + ((x.Namespace == null) ? string.Empty : "." + x.Namespace),
                            UnionSerializationInfos = new[] { x },
                        };

                        var text = template.TransformText();
                        await OutputToDirAsync(output, template.Namespace, x.Name + "Formatter", multioutSymbol, text, cancellationToken).ConfigureAwait(false);
                    }

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

                    await OutputToDirAsync(output, resolverTemplate.Namespace, resolverTemplate.ResolverName, multioutSymbol, resolverTemplate.TransformText(), cancellationToken).ConfigureAwait(false);
                }
            }

            logger("Output Generation Complete:" + sw.Elapsed.ToString());
        }