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