Ejemplo n.º 1
0
        public void GenerateCode(TextWriter writer, ProtocolDescriptor protocol)
        {
            StartTextWriter(writer);

            WIL("using System;");
            WIL("using System.Collections.Generic;");
            WIL("using Newtonsoft.Json;");

            WL();

            using (WILBlock("namespace DumbPrograms.ChromeDevTools.Protocol"))
            {
                foreach (var domain in protocol.Domains)
                {
                    WL();
                    using (WILBlock($"namespace {domain.Name}"))
                    {
                        if (domain.Types != null)
                        {
                            WL();
                            WIL("#region Types");

                            foreach (var type in domain.Types)
                            {
                                WILSummary(type.Description);
                                WILObsolete(type.Deprecated);

                                switch (type.Type)
                                {
                                case JsonTypes.Any:
                                case JsonTypes.Boolean:
                                case JsonTypes.Integer:
                                case JsonTypes.Number:
                                case JsonTypes.Array:
                                case JsonTypes.String:
                                    var nativeType = GetCSharpType(type.Type, optional: false, type.ArrayType);
                                    WIL($"[JsonConverter(typeof(JSAliasConverter<{type.Name}, {nativeType}>))]");
                                    using (WILBlock($"public class {type.Name} : JS{(type.EnumValues != null ? "Enum" : $"Alias<{nativeType}>")}"))
                                    {
                                        if (type.EnumValues != null)
                                        {
                                            Debug.Assert(nativeType == "string");

                                            foreach (var value in type.EnumValues)
                                            {
                                                WILSummary($"{type.Name} of '{value}'");
                                                WIL($"public static {type.Name} {GetCSharpIdentifier(value)} => New<{type.Name}>(\"{value}\");");
                                            }
                                        }
                                    }
                                    break;
        public void GenerateCode(TextWriter writer, ProtocolDescriptor protocol)
        {
            StartTextWriter(writer);

            WIL("using System;");
            WIL("using System.Threading;");
            WIL("using System.Threading.Tasks;");

            WL();

            using (WILBlock("namespace DumbPrograms.ChromeDevTools"))
            {
                const string InspectorClient = nameof(InspectorClient);

                using (WILBlock($"partial class {InspectorClient}"))
                {
                    foreach (var domain in protocol.Domains)
                    {
                        var fieldName = $"__{domain.Name}__";

                        WILSummary(domain.Description);

                        WILObsolete(domain.Deprecated);
                        WIL($"public {domain.Name}{InspectorClient} {domain.Name} => {fieldName} ?? ({fieldName} = new {domain.Name}{InspectorClient}(this));");

                        WILObsolete(domain.Deprecated);
                        WIL($"private {domain.Name}{InspectorClient} {fieldName};");
                    }

                    foreach (var domain in protocol.Domains)
                    {
                        WILSummary($"Inspector client for domain {domain.Name}.");

                        WILObsolete(domain.Deprecated);

                        using (WILBlock($"public class {domain.Name}{InspectorClient}"))
                        {
                            WIL($"private readonly {InspectorClient} {InspectorClient};");

                            WL();

                            using (WILBlock($"internal {domain.Name}{InspectorClient}({InspectorClient} inspectionClient)"))
                            {
                                WIL($"{InspectorClient} = inspectionClient;");
                            }

                            if (domain.Commands != null)
                            {
                                foreach (var command in domain.Commands)
                                {
                                    WILSummary(command.Description);

                                    if (command.Parameters != null)
                                    {
                                        foreach (var parameter in command.Parameters)
                                        {
                                            WILXmlDocElement(new XElement("param", new XAttribute("name", parameter.Name), parameter.Description));
                                        }
                                    }
                                    WILXmlDocElement(new XElement("param", new XAttribute("name", "cancellation")));

                                    WILObsolete(command.Deprecated);

                                    var commandName         = GetCSharpIdentifier(command.Name);
                                    var commandType         = $"Protocol.{domain.Name}.{commandName}Command";
                                    var commandResponseType = $"Protocol.{domain.Name}.{commandName}Response";
                                    var returnType          = command.Returns == null ? "Task" : $"Task<{commandResponseType}>";

                                    WIL($"public {returnType} {commandName}");
                                    using (WILBlock(blockType: BlockType.Brace))
                                    {
                                        WILParameters(domain.Name, command.Parameters);
                                    }
                                    using (WILBlock())
                                    {
                                        using (WILBlock($"return {InspectorClient}.InvokeCommandCore", BlockType.Brace))
                                        {
                                            using (WILBlock($"new {commandType}"))
                                            {
                                                if (command.Parameters != null)
                                                {
                                                    foreach (var parameter in command.Parameters)
                                                    {
                                                        WIL($"{GetCSharpIdentifier(parameter.Name)} = @{parameter.Name},");
                                                    }
                                                }
                                            }
                                            WIL(", cancellation");
                                        }
                                        WIL($";");
                                    }
                                }
                            }

                            if (domain.Events != null)
                            {
                                foreach (var @event in domain.Events)
                                {
                                    var csEventName = GetCSharpIdentifier(@event.Name);

                                    WILSummary(@event.Description);
                                    WILObsolete(@event.Deprecated);

                                    using (WILBlock($"public event Func<Protocol.{domain.Name}.{csEventName}Event, Task> {csEventName}"))
                                    {
                                        WIL($"add => {InspectorClient}.AddEventHandlerCore(\"{domain.Name}.{@event.Name}\", value);");
                                        WIL($"remove => {InspectorClient}.RemoveEventHandlerCore(\"{domain.Name}.{@event.Name}\", value);");
                                    }
                                }

                                foreach (var @event in domain.Events)
                                {
                                    var csEventName = GetCSharpIdentifier(@event.Name);

                                    WILSummary(@event.Description);
                                    WILObsolete(@event.Deprecated);

                                    using (WILBlock($"public Task<Protocol.{domain.Name}.{csEventName}Event> {csEventName}Event(Func<Protocol.{domain.Name}.{csEventName}Event, Task<bool>> until = null)"))
                                    {
                                        WIL($"return {InspectorClient}.SubscribeUntilCore(\"{domain.Name}.{@event.Name}\", until);");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }