class GroongaSchema::CommandLine::GroongaSchemaDiff

Public Class Methods

new(args) click to toggle source
# File lib/groonga-schema/command-line/groonga-schema-diff.rb, line 34
def initialize(args)
  @args = args
  @format = :command
end
run(args=ARGV) click to toggle source
# File lib/groonga-schema/command-line/groonga-schema-diff.rb, line 29
def run(args=ARGV)
  new(args).run
end

Public Instance Methods

run() click to toggle source
# File lib/groonga-schema/command-line/groonga-schema-diff.rb, line 39
def run
  parse_arguments

  from_schema = parse_schema(@from)
  to_schema = parse_schema(@to)
  differ = GroongaSchema::Differ.new(from_schema, to_schema)
  diff = differ.diff
  $stdout.print(diff.to_groonga_command_list(:format => @format))

  if diff.same?
    0
  else
    1
  end
end

Private Instance Methods

open_resource(resource_path) { |response| ... } click to toggle source
# File lib/groonga-schema/command-line/groonga-schema-diff.rb, line 96
def open_resource(resource_path)
  uri = nil
  begin
    uri = URI.parse(resource_path)
  rescue URI::InvalidURIError
  end

  if uri and uri.respond_to?(:open)
    uri.open do |response|
      yield(response)
    end
  else
    File.open(resource_path) do |file|
      yield(file)
    end
  end
end
parse_arguments() click to toggle source
# File lib/groonga-schema/command-line/groonga-schema-diff.rb, line 56
def parse_arguments
  parser = OptionParser.new
  parser.banner += " FROM_SCHEMA TO_SCHEMA"

  available_formats = [:command, :uri]
  parser.on("--format=FORMAT", available_formats,
            "Specify output Groonga command format.",
            "Available formats: #{available_formats.join(", ")}",
            "(#{@format})") do |format|
    @format = format
  end

  rest_args = parser.parse(@args)

  if rest_args.size != 2
    $stderr.puts("Error: Both FROM_SCHEMA and TO_SCHEMA are required.")
    $stderr.puts(parser.help)
    exit(false)
  end
  @from, @to = rest_args
end
parse_schema(resource_path) click to toggle source
# File lib/groonga-schema/command-line/groonga-schema-diff.rb, line 78
def parse_schema(resource_path)
  open_resource(resource_path) do |resource|
    schema = GroongaSchema::Schema.new
    parser = Groonga::Command::Parser.new
    parser.on_command do |command|
      schema.apply_command(command)
    end
    parser.on_load_value do |command,|
      command.original_source.clear
    end
    resource.each_line do |line|
      parser << line
    end
    parser.finish
    schema
  end
end