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