Exemple #1
0
        private void GenerateDocFileContent(Type cliType, string docsDir)
        {
            StringBuilder docBuilder = new StringBuilder();

            string moduleName = cliType.Name.Replace("CliModule", "");

            MethodInfo[] moduleMethods = cliType.GetMethods(BindingFlags.Public | BindingFlags.Instance).OrderBy(m => m.Name).ToArray();
            List <Type>  cliTypesToDescribe;

            docBuilder.AppendLine(@$ "# {moduleName}");
            docBuilder.AppendLine();
            moduleName = moduleName.ToLower();
            // ToFix: GetType, ToString, Equals, GetHashCode force excluding below
            foreach (MethodInfo method in moduleMethods.Where(method => method.Name != "GetType" && method.Name != "ToString" && method.Name != "Equals" && method.Name != "GetHashCode"))
            {
                CliFunctionAttribute attributeFun  = method.GetCustomAttribute <CliFunctionAttribute>();
                CliPropertyAttribute attributeProp = method.GetCustomAttribute <CliPropertyAttribute>();

                ParameterInfo[] parameters = method.GetParameters();
                cliTypesToDescribe = new List <Type>();

                StringBuilder methodArg    = new StringBuilder();
                StringBuilder paramBuilder = new StringBuilder();

                if (parameters.Length == 0)
                {
                    paramBuilder.AppendLine("_None_");
                }
                else
                {
                    paramBuilder.AppendLine("| Parameter name | Type |");
                    paramBuilder.AppendLine("| :--- | :--- |");
                    methodArg.Append("(");
                    string cliParameterType;
                    foreach (ParameterInfo parameter in method.GetParameters())
                    {
                        cliParameterType = _sharedContent.GetTypeToWrite(parameter.ParameterType, cliTypesToDescribe);
                        paramBuilder.AppendLine($"| {parameter.Name} | `{cliParameterType}` |");
                        methodArg.Append($"{parameter.Name}, ");
                    }

                    methodArg.Remove(methodArg.Length - 2, 2);
                    methodArg.Append(")");
                }

                docBuilder.AppendLine(@$ "## {moduleName}.{attributeFun?.FunctionName
                                                              ?? attributeProp?.PropertyName 
                                                              ?? method.Name.Substring(0,1).ToLower() + method.Name.Substring(1)
                    }{methodArg}");
                docBuilder.AppendLine();
                docBuilder.AppendLine(@$ "{attributeFun?.Description ?? attributeProp?.Description ?? " "} ");
                docBuilder.AppendLine();
                _markdownGenerator.OpenTabs(docBuilder);
                _markdownGenerator.CreateTab(docBuilder, "Request");
                docBuilder.AppendLine(@"### **Parameters**");
                docBuilder.AppendLine();

                docBuilder.Append(paramBuilder);

                _markdownGenerator.CloseTab(docBuilder);
                docBuilder.AppendLine();
                _markdownGenerator.CreateTab(docBuilder, "Response");
                docBuilder.AppendLine(@$ "### Return type");
                docBuilder.AppendLine();

                Type   returnType    = method.ReturnType;
                string returnRpcType = _sharedContent.GetTypeToWrite(returnType, cliTypesToDescribe);

                docBuilder.AppendLine(@$ "`{returnRpcType}`");

                _markdownGenerator.CloseTab(docBuilder);

                if (cliTypesToDescribe.Count != 0)
                {
                    docBuilder.AppendLine();
                    _markdownGenerator.CreateTab(docBuilder, "Object definitions");
                    _sharedContent.AddObjectsDescription(docBuilder, cliTypesToDescribe);
                    _markdownGenerator.CloseTab(docBuilder);
                }

                _markdownGenerator.CloseTabs(docBuilder);
                docBuilder.AppendLine();

                if (attributeFun?.ExampleRequest != null || attributeFun?.ExampleResponse != null)
                {
                    docBuilder.AppendLine(@"### **Example**");
                    _markdownGenerator.OpenTabs(docBuilder);
                    _markdownGenerator.CreateTab(docBuilder, "Request");
                    _markdownGenerator.CreateCodeBlock(docBuilder, $"{attributeFun?.ExampleRequest ?? ""}");
                    _markdownGenerator.CloseTab(docBuilder);
                    _markdownGenerator.CreateTab(docBuilder, "Response");
                    _markdownGenerator.CreateCodeBlock(docBuilder, $"{attributeFun?.ExampleResponse ?? ""}");
                    _markdownGenerator.CloseTab(docBuilder);
                    _markdownGenerator.CloseTabs(docBuilder);
                    docBuilder.AppendLine();
                }
            }
        private void GenerateDocFileContent(string moduleName, Dictionary <string, MethodData> moduleMethods, string docsDir)
        {
            StringBuilder rpcBuilder = new StringBuilder();
            StringBuilder cliBuilder = new StringBuilder();

            rpcBuilder.AppendLine($"# {moduleName}");
            rpcBuilder.AppendLine();

            cliBuilder.AppendLine($"# {moduleName}");
            cliBuilder.AppendLine();

            string[] methodsNames = moduleMethods.Keys.OrderBy(n => n).ToArray();
            foreach (string methodName in methodsNames)
            {
                moduleMethods.TryGetValue(methodName, out MethodData methodData);

                if (methodData == null)
                {
                    continue;
                }

                string rpcMethodName = $"{moduleName}_{methodName}";
                string cliMethodName = $"{moduleName}.{methodName}";


                ParameterInfo[] parameters       = methodData.Parameters;
                List <string>   defaultArguments = new List <string>();
                List <string>   exampleArguments = new List <string>();
                List <Type>     typesToDescribe  = new List <Type>();

                StringBuilder paramBuilder   = new StringBuilder();
                StringBuilder returnBuilder  = new StringBuilder();
                StringBuilder objectsBuilder = new StringBuilder();


                if (parameters.Length > 0)
                {
                    paramBuilder.AppendLine("| Parameter | Type | Description |");
                    paramBuilder.AppendLine("| :--- | :--- | :--- |");
                    foreach (ParameterInfo parameter in parameters)
                    {
                        JsonRpcParameterAttribute parameterAttribute =
                            parameter.GetCustomAttribute <JsonRpcParameterAttribute>();
                        string parameterType = _sharedContent.GetTypeToWrite(parameter.ParameterType, typesToDescribe);
                        paramBuilder.AppendLine(
                            $"| {parameter.Name} | `{parameterType}` | {parameterAttribute?.Description ?? ""} |");
                        defaultArguments.Add(parameter.Name);
                        if (parameterAttribute?.ExampleValue?.Length > 0)
                        {
                            exampleArguments.Add(parameterAttribute.ExampleValue);
                        }
                    }
                }
                else
                {
                    paramBuilder.AppendLine("| This method doesn't have parameters. |");
                    paramBuilder.AppendLine("| :--- |");
                }

                string rpcInvocation = _markdownGenerator.GetRpcInvocationExample(rpcMethodName, defaultArguments);
                string cliInvocation = _markdownGenerator.GetCliInvocationExample(cliMethodName, defaultArguments, methodData.IsFunction);

                if (exampleArguments.Count != defaultArguments.Count)
                {
                    exampleArguments = defaultArguments;
                }


                if (methodData.InvocationType != InvocationType.Cli)
                {
                    bool isImplemented = methodData.IsImplemented ?? true;
                    if (!isImplemented)
                    {
                        continue;
                    }

                    Type   returnType        = GetTypeFromWrapper(methodData.ReturnType);
                    string returnTypeToWrite = _sharedContent.GetTypeToWrite(returnType, typesToDescribe);
                    returnBuilder.AppendLine("| Returned type | Description |");
                    returnBuilder.AppendLine("| :--- | :--- |");
                    returnBuilder.AppendLine(@$ "| `{returnTypeToWrite}` | {methodData.ResponseDescription} |");

                    rpcBuilder.AppendLine($"## {rpcMethodName}");
                    TryAddDescription(rpcBuilder, methodData.Description);
                    TryAddHint(rpcBuilder, methodData.EdgeCaseHint);
                    cliBuilder.AppendLine();
                    rpcBuilder.AppendLine("| Invocation |");
                    rpcBuilder.AppendLine("| :--- |");
                    rpcBuilder.AppendLine($"| `{rpcInvocation}` |");
                    rpcBuilder.AppendLine();
                    rpcBuilder.Append(paramBuilder);
                    rpcBuilder.AppendLine();
                    rpcBuilder.Append(returnBuilder);
                    rpcBuilder.AppendLine();

                    _markdownGenerator.OpenTabs(rpcBuilder);
                    _markdownGenerator.CreateTab(rpcBuilder, $"Example request of {rpcMethodName}");
                    _markdownGenerator.CreateCurlExample(rpcBuilder, rpcMethodName, exampleArguments);
                    _markdownGenerator.CloseTab(rpcBuilder);

                    if (methodData.ExampleResponse != null)
                    {
                        string exampleResponse = CreateRpcExampleResponse(methodData.ExampleResponse);
                        _markdownGenerator.CreateTab(rpcBuilder, $"Example response of {rpcMethodName}");
                        _markdownGenerator.CreateCodeBlock(rpcBuilder, exampleResponse);
                        _markdownGenerator.CloseTab(rpcBuilder);
                    }

                    if (typesToDescribe.Count != 0)
                    {
                        objectsBuilder.AppendLine();
                        _markdownGenerator.CreateTab(objectsBuilder, $"Objects in {rpcMethodName}");
                        _sharedContent.AddObjectsDescription(objectsBuilder, typesToDescribe);
                        _markdownGenerator.CloseTab(objectsBuilder);
                    }

                    rpcBuilder.Append(objectsBuilder);
                    _markdownGenerator.CloseTabs(rpcBuilder);
                    rpcBuilder.AppendLine();

                    if (methodData.InvocationType == InvocationType.Both)
                    {
                        rpcBuilder.AppendLine($"[See also CLI {cliMethodName}](https://docs.nethermind.io/nethermind/nethermind-utilities/cli/{moduleName}#{moduleName.ToLower()}-{methodName.ToLower()})");

                        CreateCliContent(cliBuilder, paramBuilder, returnBuilder, objectsBuilder, cliMethodName, exampleArguments, cliInvocation, methodData);
                        cliBuilder.AppendLine($"[See also JSON RPC {rpcMethodName}](https://docs.nethermind.io/nethermind/ethereum-client/json-rpc/{moduleName}#{rpcMethodName.ToLower()})");
                    }
                }
                else
                {
                    Type   returnType        = methodData.ReturnType;
                    string returnTypeToWrite = _sharedContent.GetTypeToWrite(returnType, typesToDescribe);
                    returnBuilder.AppendLine("| Returned type | Description |");
                    returnBuilder.AppendLine("| :--- | :--- |");
                    returnBuilder.AppendLine(@$ "| `{returnTypeToWrite}` | {methodData.ResponseDescription} |");

                    CreateCliContent(cliBuilder, paramBuilder, returnBuilder, objectsBuilder, cliMethodName, exampleArguments, cliInvocation, methodData);
                }
            }

            int emptyModuleLength = "# only_some_module_name".Length;

            if (rpcBuilder.Length > emptyModuleLength)
            {
                _sharedContent.Save(moduleName, string.Concat(docsDir, "/ethereum-client/json-rpc"), rpcBuilder);
            }
            if (cliBuilder.Length > emptyModuleLength)
            {
                _sharedContent.Save(moduleName, string.Concat(docsDir, "/nethermind-utilities/cli"), cliBuilder);
            }
        }