public CommandInterfaceBuilder WithClass(TypeDeclarationSyntax classDefinition)
 {
     namespaceDeclaration = namespaceDeclaration.AddMembers((ClassDeclarationSyntax)classDefinition);
     return(this);
 }
예제 #2
0
        private static NamespaceDeclarationSyntax Go(NamespaceDeclarationSyntax ns, Configuration config, Specification spec)
        {
            var className = ClassNameNormaliser(spec.Info.Title);

            var constructorCode = $@"if (!string.IsNullOrWhiteSpace(url)){{ this.url = url;}} else {{ this.url = ""{spec.Schemes[0]}://{spec.Host}""; }}";
            if (config.IncludeHTTPClientForCSharp)
            {
                constructorCode += Environment.NewLine + " if (httpClient == null){ this.httpClient = new SwaggerHTTPClient();} else { this.httpClient = httpClient; }";
            }

            var baseClass = SyntaxFactory.ClassDeclaration(className)
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                .AddMembers(Field("url", "string").AddModifiers(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)),
                            Field("httpClient", "ISwaggerHTTPClient").AddModifiers(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)),
                Constructor(className, constructorCode, new SimplifiedParameter("url", "string", "null", true), new SimplifiedParameter("httpClient", "ISwaggerHTTPClient", config.IncludeHTTPClientForCSharp ? "null" : null, true)))
                .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken));

            if (spec.Definations != null)
            {
                foreach (var defination in spec.Definations)
                {
                    baseClass = AddDefination(baseClass, defination, config);
                }
            }

            foreach (var path in spec.Paths)
            {
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Delete, path.Delete, path.Path), config);
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Get, path.Get, path.Path), config);
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Post, path.Post, path.Path), config);
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Put, path.Put, path.Path), config);
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Head, path.Head, path.Path), config);
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Options, path.Options, path.Path), config);
                baseClass = AddOperation(baseClass, OperationConfig.FromValues(HTTPAction.Patch, path.Patch, path.Path), config);
            }

            if (config.IncludeHTTPClientForCSharp)
            {
                baseClass = baseClass.WithTrailingTrivia(SyntaxFactory.Comment(httpCode));
            }

            return ns.AddMembers(baseClass);
        }