generate-command-help.rb 2.1 KB

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