2
0

generate-command-help.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env ruby
  2. GROUPS = [
  3. "generic",
  4. "string",
  5. "list",
  6. "set",
  7. "sorted_set",
  8. "hash",
  9. "pubsub",
  10. "transactions",
  11. "connection",
  12. "server",
  13. "scripting",
  14. "hyperloglog",
  15. "cluster",
  16. "geo",
  17. "stream"
  18. ].freeze
  19. GROUPS_BY_NAME = Hash[*
  20. GROUPS.each_with_index.map do |n,i|
  21. [n,i]
  22. end.flatten
  23. ].freeze
  24. def argument arg
  25. name = arg["name"].is_a?(Array) ? arg["name"].join(" ") : arg["name"]
  26. name = arg["enum"].join "|" if "enum" == arg["type"]
  27. name = arg["command"] + " " + name if arg["command"]
  28. if arg["multiple"]
  29. name = "#{name} [#{name} ...]"
  30. end
  31. if arg["optional"]
  32. name = "[#{name}]"
  33. end
  34. name
  35. end
  36. def arguments command
  37. return "-" unless command["arguments"]
  38. command["arguments"].map do |arg|
  39. argument arg
  40. end.join " "
  41. end
  42. def commands
  43. return @commands if @commands
  44. require "rubygems"
  45. require "net/http"
  46. require "net/https"
  47. require "json"
  48. require "uri"
  49. url = URI.parse "https://raw.githubusercontent.com/antirez/redis-doc/master/commands.json"
  50. client = Net::HTTP.new url.host, url.port
  51. client.use_ssl = true
  52. response = client.get url.path
  53. if response.is_a?(Net::HTTPSuccess)
  54. @commands = JSON.parse(response.body)
  55. else
  56. response.error!
  57. end
  58. end
  59. def generate_groups
  60. GROUPS.map do |n|
  61. "\"#{n}\""
  62. end.join(",\n ");
  63. end
  64. def generate_commands
  65. commands.to_a.sort do |x,y|
  66. x[0] <=> y[0]
  67. end.map do |key, command|
  68. group = GROUPS_BY_NAME[command["group"]]
  69. if group.nil?
  70. STDERR.puts "Please update groups array in #{__FILE__}"
  71. raise "Unknown group #{command["group"]}"
  72. end
  73. ret = <<-SPEC
  74. { "#{key}",
  75. "#{arguments(command)}",
  76. "#{command["summary"]}",
  77. #{group},
  78. "#{command["since"]}" }
  79. SPEC
  80. ret.strip
  81. end.join(",\n ")
  82. end
  83. # Write to stdout
  84. puts <<-HELP_H
  85. /* Automatically generated by #{__FILE__}, do not edit. */
  86. #ifndef __REDIS_HELP_H
  87. #define __REDIS_HELP_H
  88. static char *commandGroups[] = {
  89. #{generate_groups}
  90. };
  91. struct commandHelp {
  92. char *name;
  93. char *params;
  94. char *summary;
  95. int group;
  96. char *since;
  97. } commandHelp[] = {
  98. #{generate_commands}
  99. };
  100. #endif
  101. HELP_H