generate-command-help.rb 2.1 KB

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