Beispiel #1
0
        public override IDictionary <string, string> GenerateCode(EventDefinition eventDefinition, CodeGeneratorContext context)
        {
            var result = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (String.IsNullOrWhiteSpace(Settings.DefinitionTemplates.EventTemplate.TemplatePath))
            {
                return(result);
            }

            var eventGenerator = TemplatesManager.GetGeneratorForTemplate(Settings.DefinitionTemplates.EventTemplate);

            var className = eventDefinition.Name.Dehumanize();

            string codeResult = eventGenerator(new
            {
                @event        = eventDefinition,
                className     = className,
                domain        = context.Domain,
                rootNamespace = Settings.RootNamespace,
                context       = context
            });

            var outputPath = Utility.ReplaceTokensInPath(Settings.DefinitionTemplates.EventTemplate.OutputPath, className, context, Settings);

            result.Add(outputPath, codeResult);

            return(result);
        }
        public override IDictionary <string, string> GenerateCode(DomainDefinition domainDefinition, CodeGeneratorContext context)
        {
            var result = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            var typeGenerator = ServiceProvider.GetRequiredService <ICodeGenerator <TypeDefinition> >();

            foreach (var type in domainDefinition.Types)
            {
                typeGenerator.GenerateCode(type, context)
                .ToList()
                .ForEach(x => result.Add(x.Key, x.Value));
            }

            var eventGenerator = ServiceProvider.GetRequiredService <ICodeGenerator <EventDefinition> >();

            foreach (var @event in domainDefinition.Events)
            {
                eventGenerator.GenerateCode(@event, context)
                .ToList()
                .ForEach(x => result.Add(x.Key, x.Value));
            }

            var commandGenerator = ServiceProvider.GetRequiredService <ICodeGenerator <CommandDefinition> >();

            foreach (var command in domainDefinition.Commands)
            {
                commandGenerator.GenerateCode(command, context)
                .ToList()
                .ForEach(x => result.Add(x.Key, x.Value));
            }

            if (String.IsNullOrWhiteSpace(Settings.DefinitionTemplates.DomainTemplate.TemplatePath))
            {
                return(result);
            }

            var domainGenerator = TemplatesManager.GetGeneratorForTemplate(Settings.DefinitionTemplates.DomainTemplate);

            var className = domainDefinition.Name.Dehumanize();

            string codeResult = domainGenerator(new
            {
                domain        = domainDefinition,
                className     = className,
                rootNamespace = Settings.RootNamespace,
                context       = context
            });

            var outputPath = Utility.ReplaceTokensInPath(Settings.DefinitionTemplates.DomainTemplate.OutputPath, className, context, Settings);

            result.Add(outputPath, codeResult);

            return(result);
        }
        public override IDictionary <string, string> GenerateCode(ProtocolDefinition protocolDefinition, CodeGeneratorContext context)
        {
            if (String.IsNullOrWhiteSpace(Settings.TemplatesPath))
            {
                Settings.TemplatesPath = Path.GetDirectoryName(Settings.TemplatesPath);
            }

            ICollection <DomainDefinition> domains = protocolDefinition.Domains;

            if (!Settings.IncludeDeprecatedDomains)
            {
                domains = domains.Where(d => d.Deprecated == false).ToList();
            }

            if (!Settings.IncludeExperimentalDomains)
            {
                domains = domains.Where(d => d.Experimental == false).ToList();
            }

            //Get commandinfos as an array.
            ICollection <CommandInfo> commands = new List <CommandInfo>();

            foreach (var domain in domains)
            {
                foreach (var command in domain.Commands)
                {
                    commands.Add(new CommandInfo
                    {
                        CommandName          = $"{domain.Name}.{command.Name}",
                        FullTypeName         = $"{domain.Name.Dehumanize()}.{command.Name.Dehumanize()}CommandSettings",
                        FullResponseTypeName = $"{domain.Name.Dehumanize()}.{command.Name.Dehumanize()}CommandResponse"
                    });
                }
            }

            //Get eventinfos as an array
            ICollection <EventInfo> events = new List <EventInfo>();

            foreach (var domain in domains)
            {
                foreach (var @event in domain.Events)
                {
                    events.Add(new EventInfo
                    {
                        EventName    = $"{domain.Name}.{@event.Name}",
                        FullTypeName = $"{domain.Name.Dehumanize()}.{@event.Name.Dehumanize()}EventArgs"
                    });
                }
            }

            //Get typeinfos as a dictionary.
            var types = GetTypesInDomain(domains);

            //Create an object that contains information that include templates can use.
            var includeData = new {
                chromeVersion  = protocolDefinition.BrowserVersion,
                runtimeVersion = Settings.RuntimeVersion,
                rootNamespace  = Settings.RootNamespace,
                domains        = domains,
                commands       = commands,
                events         = events,
                types          = types.Select(kvp => kvp.Value).ToList()
            };

            var result = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            //Generate include files from templates.
            foreach (var include in Settings.Include)
            {
                var includeCodeGenerator = TemplatesManager.GetGeneratorForTemplate(include);
                var includeCodeResult    = includeCodeGenerator(includeData);
                result.Add(include.OutputPath, includeCodeResult);
            }

            //Generate code for each domain, type, command, event from their respective templates.
            GenerateCode(domains, types)
            .ToList()
            .ForEach(x => result.Add(x.Key, x.Value));

            return(result);
        }
        public override IDictionary <string, string> GenerateCode(TypeDefinition typeDefinition, CodeGeneratorContext context)
        {
            var result = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (context.KnownTypes == null)
            {
                throw new InvalidOperationException("Expected knowntypes to be specified in context");
            }

            if (context.Domain == null)
            {
                throw new InvalidOperationException("Expected domain to be specified in context");
            }

            var typeInfo = context.KnownTypes[$"{context.Domain.Name}.{typeDefinition.Id}"];

            if (typeInfo.IsPrimitive)
            {
                return(result);
            }

            //Base the code generation template on the specified type definition type.
            CodeGenerationTemplateSettings templateSettings;

            switch (typeDefinition.Type)
            {
            case "object":
                templateSettings = Settings.DefinitionTemplates.TypeObjectTemplate;
                break;

            case "string":
                templateSettings = Settings.DefinitionTemplates.TypeEnumTemplate;
                break;

            default:
                throw new InvalidOperationException($"Unsupported Type Definition Type: {typeDefinition.Type}");
            }

            // Special override for the headers object to be an open object.
            // TODO: make this kind of override configurable.
            if (context.Domain.Name == "Network" && typeDefinition.Id == "Headers")
            {
                templateSettings = Settings.DefinitionTemplates.TypeHashTemplate;
            }

            if (String.IsNullOrWhiteSpace(templateSettings.TemplatePath))
            {
                return(result);
            }

            var typeGenerator = TemplatesManager.GetGeneratorForTemplate(templateSettings);

            var className  = typeDefinition.Id.Dehumanize();
            var codeResult = typeGenerator(new
            {
                type          = typeDefinition,
                className     = className,
                domain        = context.Domain,
                rootNamespace = Settings.RootNamespace,
                context       = context
            });

            var outputPath = Utility.ReplaceTokensInPath(templateSettings.OutputPath, className, context, Settings);

            result.Add(outputPath, codeResult);

            return(result);
        }