#!/usr/bin/env ruby
-require 'net/http'
-require 'net/https'
-require 'json'
-require 'uri'
+GROUPS = [
+ "generic",
+ "string",
+ "list",
+ "set",
+ "sorted_set",
+ "hash",
+ "pubsub",
+ "transactions",
+ "connection",
+ "server",
+ "scripting"
+].freeze
-dest = ARGV[0]
-tmpl = File.read './utils/help.h'
-
-url = URI.parse 'https://github.com/antirez/redis-doc/raw/master/commands.json'
-client = Net::HTTP.new url.host, url.port
-client.use_ssl = true
-res = client.get url.path
+GROUPS_BY_NAME = Hash[*
+ GROUPS.each_with_index.map do |n,i|
+ [n,i]
+ end.flatten
+].freeze
def argument arg
- name = arg['name'].is_a?(Array) ? arg['name'].join(' ') : arg['name']
- name = arg['enum'].join '|' if 'enum' == arg['type']
- name = arg['command'] + ' ' + name if arg['command']
- if arg['multiple']
- name = "(#{name})"
- name += arg['optional'] ? '*' : '+'
- elsif arg['optional']
- name = "(#{name})?"
+ name = arg["name"].is_a?(Array) ? arg["name"].join(" ") : arg["name"]
+ name = arg["enum"].join "|" if "enum" == arg["type"]
+ name = arg["command"] + " " + name if arg["command"]
+ if arg["multiple"]
+ name = "#{name} [#{name} ...]"
+ end
+ if arg["optional"]
+ name = "[#{name}]"
end
name
end
def arguments command
- return '-' unless command['arguments']
- command['arguments'].map do |arg|
+ return "-" unless command["arguments"]
+ command["arguments"].map do |arg|
argument arg
- end.join ' '
+ end.join " "
+end
+
+def commands
+ return @commands if @commands
+
+ require "rubygems"
+ require "net/http"
+ require "net/https"
+ require "json"
+ require "uri"
+
+ url = URI.parse "https://raw.github.com/antirez/redis-doc/master/commands.json"
+ client = Net::HTTP.new url.host, url.port
+ client.use_ssl = true
+ response = client.get url.path
+ if response.is_a?(Net::HTTPSuccess)
+ @commands = JSON.parse(response.body)
+ else
+ response.error!
+ end
+end
+
+def generate_groups
+ GROUPS.map do |n|
+ "\"#{n}\""
+ end.join(",\n ");
end
-case res
-when Net::HTTPSuccess
- first = true
- commands = JSON.parse(res.body)
- c = commands.map do |key, command|
- buf = if first
- first = false
- ' '
- else
- "\n ,"
+def generate_commands
+ commands.to_a.sort do |x,y|
+ x[0] <=> y[0]
+ end.map do |key, command|
+ group = GROUPS_BY_NAME[command["group"]]
+ if group.nil?
+ STDERR.puts "Please update groups array in #{__FILE__}"
+ raise "Unknown group #{command["group"]}"
end
- buf += " { \"#{key}\"\n" +
- " , \"#{arguments(command)}\"\n" +
- " , \"#{command['summary']}\"\n" +
- " , COMMAND_GROUP_#{command['group'].upcase}\n" +
- " , \"#{command['since']}\" }"
- end.join("\n")
- puts "\n// Auto-generated, do not edit.\n" + tmpl.sub('__COMMANDS__', c)
-else
- res.error!
-end
\ No newline at end of file
+
+ ret = <<-SPEC
+{ "#{key}",
+ "#{arguments(command)}",
+ "#{command["summary"]}",
+ #{group},
+ "#{command["since"]}" }
+ SPEC
+ ret.strip
+ end.join(",\n ")
+end
+
+# Write to stdout
+puts <<-HELP_H
+/* Automatically generated by #{__FILE__}, do not edit. */
+
+#ifndef __REDIS_HELP_H
+#define __REDIS_HELP_H
+
+static char *commandGroups[] = {
+ #{generate_groups}
+};
+
+struct commandHelp {
+ char *name;
+ char *params;
+ char *summary;
+ int group;
+ char *since;
+} commandHelp[] = {
+ #{generate_commands}
+};
+
+#endif
+HELP_H
+