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