Ejemplo n.º 1
0
        public void Generate()
        {
            string docsDir    = DocsDirFinder.FindDocsDir();
            string runnerDir  = DocsDirFinder.FindRunnerDir();
            string moduleName = "sample-configuration";

            string[] configs = { "mainnet.cfg", "goerli.cfg", "rinkeby.cfg", "ropsten.cfg" };

            StringBuilder docBuilder = new StringBuilder();

            docBuilder.AppendLine("---");
            docBuilder.AppendLine("description: Sample Fast Sync configurations for Nethermind");
            docBuilder.AppendLine("---");
            docBuilder.AppendLine();
            docBuilder.AppendLine("# Sample configuration");
            docBuilder.AppendLine();
            _markdownGenerator.OpenTabs(docBuilder);

            foreach (string config in configs)
            {
                _markdownGenerator.CreateTab(docBuilder, config);
                docBuilder.AppendLine("```yaml");
                string[] configData = File.ReadAllLines($"{runnerDir}/configs/{config}");

                foreach (string line in configData)
                {
                    docBuilder.AppendLine(line);
                }
                docBuilder.AppendLine("```");
                _markdownGenerator.CloseTab(docBuilder);
            }

            // Write sample docker-compose .env file
            var env = "mainnet_env";

            _markdownGenerator.CreateTab(docBuilder, env);
            docBuilder.AppendLine("```yaml");
            string[] envData = File.ReadAllLines($"./envs/{env}");

            foreach (string line in envData)
            {
                docBuilder.AppendLine(line);
            }
            docBuilder.AppendLine("```");
            _markdownGenerator.CloseTab(docBuilder);

            _markdownGenerator.CloseTabs(docBuilder);

            string path = string.Concat(docsDir, "/ethereum-client/configuration");

            _sharedContent.Save(moduleName, path, docBuilder);
        }
Ejemplo n.º 2
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();
                }
            }
Ejemplo n.º 3
0
        private void GenerateDocFileContent(Type rpcType, string docsDir)
        {
            StringBuilder docBuilder = new StringBuilder();

            string moduleName = rpcType.Name.Substring(1).Replace("Module", "");

            MethodInfo[] moduleMethods = rpcType.GetMethods().OrderBy(m => m.Name).ToArray();
            List <Type>  rpcTypesToDescribe;

            docBuilder.AppendLine(@$ "# {moduleName}");
            docBuilder.AppendLine();
            moduleName = moduleName.ToLower();

            foreach (MethodInfo method in moduleMethods)
            {
                JsonRpcMethodAttribute attribute = method.GetCustomAttribute <JsonRpcMethodAttribute>();
                bool isImplemented = attribute == null || attribute.IsImplemented;

                if (!isImplemented)
                {
                    continue;
                }

                string methodName = method.Name.Substring(method.Name.IndexOf('_'));
                docBuilder.AppendLine(@$ "## {moduleName}{methodName}");
                docBuilder.AppendLine();
                docBuilder.AppendLine(@$ "{attribute?.Description ?? " "} ");
                docBuilder.AppendLine();
                _markdownGenerator.OpenTabs(docBuilder);
                _markdownGenerator.CreateTab(docBuilder, "Request");
                docBuilder.AppendLine(@"### **Parameters**");
                docBuilder.AppendLine();

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

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

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

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

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

                _markdownGenerator.CloseTab(docBuilder);

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

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

            _sharedContent.Save(moduleName, docsDir + "/json-rpc-modules", docBuilder);
        }
Ejemplo n.º 4
0
        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);
            }
        }