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