Пример #1
0
        private void AddGenOutputFile()
        {
            var writer = new CSharpWriter();

            writer.WriteLine();
            writer.WriteLine("namespace JitsuGen.Output");
            using (writer.Curly())
            {
                writer.WriteLine("public static class GenOutput");
                using (writer.Curly())
                {
                    writer.WriteLine($"public static void FillDomain({writer.UseType(typeof(IGenDomain))} genDomain)");
                    using (writer.Curly())
                    {
                        foreach (var impl in implementations)
                        {
                            writer.WriteLine($"genDomain.RegisterGeneratedType(typeof({writer.UseType(impl.Key)}), typeof({impl.Value}));");
                        }
                    }
                }
            }
            writer.Builder.Insert(0, writer.GetUsingsString());
            AddFile(new GeneratedFileInfo
            {
                Path      = "GenOutput.cs",
                TypesUsed = writer.GetUsedTypes(),
                Content   = writer.Builder.ToString()
            });
        }
Пример #2
0
        public void GenerateFor(IFileGeneratingContext context, Type type)
        {
            var writer      = new CSharpWriter();
            var amClass     = type;
            var amClassName = writer.UseType(type);
            var desc        = new AmObjectTypeDescription(amClass, bindingTypeDescriptors);

            var className = "_am_" + desc.Name;

            writer.WriteLine();
            writer.WriteLine($"namespace JitsuGen.Output.{type.Namespace}");
            using (writer.Curly())
            {
                writer.WriteLine($"public class {className} : {amClassName}");
                using (writer.Curly())
                {
                    foreach (var bindingDesc in desc.Bindings)
                    {
                        writer.WriteLine($"private {writer.UseType(bindingDesc.MakeBindingType(amClass))} _{bindingDesc.Property.Name};");
                    }
                    writer.WriteLine();

                    foreach (var bindingDesc in desc.Bindings)
                    {
                        var getterString = bindingDesc.BuildGetterString($"_{bindingDesc.Property.Name}");
                        var setterString = bindingDesc.BuildSetterString($"_{bindingDesc.Property.Name}");
                        writer.WriteProperty("public override", writer.UseType(bindingDesc.Property.PropertyType), bindingDesc.Property.Name,
                                             getterString != null ? (Action <CSharpWriter>)(w => w.WriteLine(getterString)) : null,
                                             setterString != null ? (Action <CSharpWriter>)(w => w.WriteLine(setterString)) : null);
                        writer.WriteLine();
                    }

                    var baseCtor          = amClass.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance).Single();
                    var ctorParametersStr = string.Join(", ", baseCtor.GetParameters().Select(x => $"{writer.UseType(x.ParameterType)} {x.Name}"));
                    var ctorArgsStr       = string.Join(", ", baseCtor.GetParameters().Select(x => x.Name));
                    writer.WriteLine($"public {className}({ctorParametersStr})");
                    writer.WriteLine($": base({ctorArgsStr})");
                    using (writer.Curly())
                    { }
                    writer.WriteLine();

                    writer.WriteLine($"protected override {writer.UseType(typeof(List<IAmBinding>))} AmInitBindings()");
                    using (writer.Curly())
                    {
                        writer.WriteLine($"var list = new {writer.UseType(typeof(List<IAmBinding>))}();");
                        foreach (var bindingDesc in desc.Bindings)
                        {
                            var flagTypeStr = writer.UseType(typeof(AmBindingFlags));
                            var flagsStr    = string.Join(" | ", EnumerateFlags(bindingDesc.Flags).Select(x => $"{flagTypeStr}.{x}"));
                            writer.WriteLine($"_{bindingDesc.Property.Name} = new {writer.UseType(bindingDesc.MakeBindingType(amClass))}(this, \"{bindingDesc.Property.Name}\", {flagsStr});");
                            writer.WriteLine($"list.Add(_{bindingDesc.Property.Name});");
                        }
                        writer.WriteLine("return list;");
                    }
                }
            }
            writer.Builder.Insert(0, writer.GetUsingsString());
            var path    = Path.Combine(type.Namespace.Replace(".", "\\"), className + ".cs");
            var content = writer.Builder.ToString();

            context.AddFile(new GeneratedFileInfo
            {
                Path      = path,
                Content   = content,
                TypesUsed = writer.GetUsedTypes(),
                Template  = type,
                ImplementationFullName = $"{type.Namespace}.{className}"
            });
        }
        public void GenerateFor(IFileGeneratingContext context, Type type)
        {
            if (!type.IsInterface)
            {
                throw new ArgumentException("VerboseWrapperGenerator only works with interfaces.");
            }
            var className = "_verbose_" + type.Name;


            var writer = new CSharpWriter();

            writer.WriteLine();
            writer.WriteLine($"namespace {type.Namespace}");
            using (writer.Curly())
            {
                var interfaceName = writer.UseType(type);
                writer.WriteLine($"public class {className} : {interfaceName}");
                using (writer.Curly())
                {
                    writer.WriteLine($"private {interfaceName} real;");
                    writer.WriteLine();
                    writer.WriteLine($"public {className}({interfaceName} real)");
                    using (writer.Curly())
                        writer.WriteLine("this.real = real;");
                    writer.WriteLine();

                    foreach (var propertyInfo in type.GetProperties())
                    {
                        var writeGetter = propertyInfo.GetMethod != null
                            ? (Action <CSharpWriter>)(w =>
                        {
                            w.WriteLine($"{writer.UseType(typeof(Console))}.WriteLine($\"Getting {interfaceName}.{propertyInfo.Name}\");");
                            w.WriteLine($"return real.{propertyInfo.Name}; ");
                        })
                            : null;
                        var writeSetter = propertyInfo.SetMethod != null
                            ? (Action <CSharpWriter>)(w =>
                        {
                            w.WriteLine($"{writer.UseType(typeof(Console))}.WriteLine($\"Setting {interfaceName}.{propertyInfo.Name}\");");
                            w.WriteLine($"real.{propertyInfo.Name} = value;");
                        })
                            : null;
                        writer.WriteProperty("public", writer.UseType(propertyInfo.PropertyType), propertyInfo.Name, writeGetter, writeSetter);
                        writer.WriteLine();
                    }

                    foreach (var methodInfo in type.GetMethods())
                    {
                        if (methodInfo.IsSpecialName)
                        {
                            continue;
                        }

                        writer.WriteMethodDeclaration("public", methodInfo);
                        using (writer.Curly())
                        {
                            writer.WriteLine($"{writer.UseType(typeof(Console))}.WriteLine($\"Invoking {interfaceName}.{methodInfo.Name}()\");");
                            var paramString      = string.Join(", ", methodInfo.GetParameters().Select(x => x.Name));
                            var invocationString = $"real.{methodInfo.Name}({paramString});";
                            if (methodInfo.ReturnType == typeof(void))
                            {
                                writer.WriteLine(invocationString);
                            }
                            else
                            {
                                writer.WriteLine($"return {invocationString}");
                            }
                        }
                        writer.WriteLine();
                    }
                }
            }
            writer.Builder.Insert(0, writer.GetUsingsString());
            var path    = Path.Combine(type.Namespace.Replace(".", "\\"), className + ".cs");
            var content = writer.Builder.ToString();

            context.AddFile(new GeneratedFileInfo
            {
                Path      = path,
                Content   = content,
                TypesUsed = writer.GetUsedTypes(),
                Template  = type,
                ImplementationFullName = $"{type.Namespace}.{className}"
            });
        }