generate-command-help.rb 2.0 KB

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