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