Exemplo n.º 1
0
        public static IDisposable NameSpaceOf(this IndentedTextWriter writer, INamedTypeSymbol type)
        {
            return(type.GetContainingTypes().Reverse().Aggregate(writer.Block($"namespace {type.ContainingNamespace}"), CreateContainingBlock));

            IDisposable CreateContainingBlock(IDisposable previousLevel, INamedTypeSymbol containingType)
            {
                var containingTypeDisposable = writer.Block($"partial class {containingType.Name}");

                return(Disposable.Create(() =>
                {
                    containingTypeDisposable.Dispose();
                    previousLevel.Dispose();
                }));
            }
        }
Exemplo n.º 2
0
        private void Generate(LifecycleMethods methods)
        {
            var names = methods.Owner.GetSymbolNames();

            var(dispose, appendToConstructor, appendToFinalizer) = methods.Disposes.Any()
                                ? GetDispose(names, methods)
                                : (null, null, null);

            var constructor = methods.Constructors.Any() || appendToConstructor.HasValue()
                                ? GetConstructor(names, methods, appendToConstructor)
                                : null;

            var finalize = methods.Finalizers.Any() || appendToFinalizer.HasValue()
                                ? GetFinalizer(names, methods, appendToFinalizer)
                                : null;

            var code = new StringWriter();

            using (var writer = new IndentedTextWriter(code, "\t"))
            {
                writer.WriteLine("// <auto-generated>");
                writer.WriteLine("// ***************************************************************************************************************************");
                writer.WriteLine("// This file has been generated by Uno.CodeGen (ClassLifecycleGenerator), available at https://github.com/nventive/Uno.CodeGen");
                writer.WriteLine("// ***************************************************************************************************************************");
                writer.WriteLine("// </auto-generated>");
                writer.WriteLine("#pragma warning disable");
                writer.WriteLine();
                writer.WriteLine("using global::System;");

                using (writer.NameSpaceOf(methods.Owner))
                {
                    using (writer.Block($"partial class {names.NameWithGenerics} {(methods.Disposes.Any() ? ": global::System.IDisposable" : "")}"))
                    {
                        writer.WriteLine(constructor);
                        writer.WriteLine(dispose);
                        writer.WriteLine(finalize);
                    }
                }
            }

            _context.AddCompilationUnit(names.FilePath, code.ToString());
        }