2
0

generate-command-help.rb 2.2 KB

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