generate-command-help.rb 2.1 KB
Newer Older
1 2
#!/usr/bin/env ruby

3 4 5 6 7 8 9 10 11 12
GROUPS = [
  "generic",
  "string",
  "list",
  "set",
  "sorted_set",
  "hash",
  "pubsub",
  "transactions",
  "connection",
13
  "server",
14 15
  "scripting",
  "hyperloglog"
16 17 18 19 20 21 22 23
].freeze

GROUPS_BY_NAME = Hash[*
  GROUPS.each_with_index.map do |n,i|
    [n,i]
  end.flatten
].freeze

24
def argument arg
25 26 27 28 29 30 31 32
  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}]"
33 34 35 36 37
  end
  name
end

def arguments command
38 39
  return "-" unless command["arguments"]
  command["arguments"].map do |arg|
40
    argument arg
41 42 43 44 45 46
  end.join " "
end

def commands
  return @commands if @commands

A
antirez 已提交
47
  require "rubygems"
48 49 50 51 52
  require "net/http"
  require "net/https"
  require "json"
  require "uri"

53
  url = URI.parse "https://raw.github.com/antirez/redis-doc/master/commands.json"
54 55 56 57 58 59 60 61
  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
62 63
end

64 65 66 67 68 69
def generate_groups
  GROUPS.map do |n|
    "\"#{n}\""
  end.join(",\n    ");
end

70 71 72 73
def generate_commands
  commands.to_a.sort do |x,y|
    x[0] <=> y[0]
  end.map do |key, command|
74 75 76 77 78 79 80
    group = GROUPS_BY_NAME[command["group"]]
    if group.nil?
      STDERR.puts "Please update groups array in #{__FILE__}"
      raise "Unknown group #{command["group"]}"
    end

    ret = <<-SPEC
81 82 83
{ "#{key}",
    "#{arguments(command)}",
    "#{command["summary"]}",
84
    #{group},
85 86
    "#{command["since"]}" }
    SPEC
87 88
    ret.strip
  end.join(",\n    ")
89 90 91
end

# Write to stdout
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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
114