]> git.saurik.com Git - redis.git/blame - utils/generate-command-help.rb
previouse INCR implementation restored, was actually faster for some reson not fully...
[redis.git] / utils / generate-command-help.rb
CommitLineData
5397f2b5
TH
1#!/usr/bin/env ruby
2
a2a69d58
PN
3GROUPS = [
4 "generic",
5 "string",
6 "list",
7 "set",
8 "sorted_set",
9 "hash",
10 "pubsub",
11 "transactions",
12 "connection",
13 "server"
14].freeze
15
16GROUPS_BY_NAME = Hash[*
17 GROUPS.each_with_index.map do |n,i|
18 [n,i]
19 end.flatten
20].freeze
21
5397f2b5 22def argument arg
50d0e82d
PN
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}]"
5397f2b5
TH
31 end
32 name
33end
34
35def arguments command
50d0e82d
PN
36 return "-" unless command["arguments"]
37 command["arguments"].map do |arg|
5397f2b5 38 argument arg
50d0e82d
PN
39 end.join " "
40end
41
42def 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
5397f2b5
TH
59end
60
a2a69d58
PN
61def generate_groups
62 GROUPS.map do |n|
63 "\"#{n}\""
64 end.join(",\n ");
65end
66
50d0e82d
PN
67def generate_commands
68 commands.to_a.sort do |x,y|
69 x[0] <=> y[0]
70 end.map do |key, command|
a2a69d58
PN
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
50d0e82d
PN
78{ "#{key}",
79 "#{arguments(command)}",
80 "#{command["summary"]}",
a2a69d58 81 #{group},
50d0e82d
PN
82 "#{command["since"]}" }
83 SPEC
a2a69d58
PN
84 ret.strip
85 end.join(",\n ")
50d0e82d
PN
86end
87
88# Write to stdout
a2a69d58
PN
89puts <<-HELP_H
90/* Automatically generated by #{__FILE__}, do not edit. */
91
92#ifndef __REDIS_HELP_H
93#define __REDIS_HELP_H
94
95static char *commandGroups[] = {
96 #{generate_groups}
97};
98
99struct 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
110HELP_H
50d0e82d 111