[Groonga-commit] groonga/groonga-schema at 341f9b3 [master] Add groonga-schema-diff

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Aug 8 18:09:25 JST 2016


Kouhei Sutou	2016-08-08 18:09:25 +0900 (Mon, 08 Aug 2016)

  New Revision: 341f9b3c48f0226ee2c9364cb465392c2a9561f9
  https://github.com/groonga/groonga-schema/commit/341f9b3c48f0226ee2c9364cb465392c2a9561f9

  Message:
    Add groonga-schema-diff
    
    It's not completed yet.

  Added files:
    lib/groonga-schema/command-line/groonga-schema-diff.rb
  Copied files:
    bin/groonga-schema-diff
      (from lib/groonga-schema.rb)
  Modified files:
    groonga-schema.gemspec
    lib/groonga-schema.rb
    lib/groonga-schema/diff.rb

  Copied: bin/groonga-schema-diff (+5 -3) 87%
  Mode: 100644 -> 100755
===================================================================
--- lib/groonga-schema.rb    2016-08-08 18:08:58 +0900 (0c80219)
+++ bin/groonga-schema-diff    2016-08-08 18:09:25 +0900 (0dc54bb)
@@ -1,3 +1,5 @@
+#!/usr/bin/env ruby
+#
 # Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
@@ -14,6 +16,6 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-require "groonga-schema/differ"
-require "groonga-schema/schema"
-require "groonga-schema/version"
+require "groonga-schema"
+
+exit(GroongaSchema::CommandLine::GroongaSchemaDiff.run(ARGV))

  Modified: groonga-schema.gemspec (+3 -3)
===================================================================
--- groonga-schema.gemspec    2016-08-08 18:08:58 +0900 (2f9503f)
+++ groonga-schema.gemspec    2016-08-08 18:09:25 +0900 (5adac29)
@@ -44,9 +44,9 @@ Gem::Specification.new do |spec|
   spec.files += Dir.glob("lib/**/*.rb")
   spec.files += Dir.glob("doc/text/*")
   spec.test_files += Dir.glob("test/**/*")
-  # Dir.chdir("bin") do
-  #   spec.executables = Dir.glob("*")
-  # end
+  Dir.chdir("bin") do
+    spec.executables = Dir.glob("*")
+  end
 
   spec.homepage = "https://github.com/groonga/groonga-schema"
   spec.licenses = ["LGPLv2.1+"]

  Modified: lib/groonga-schema.rb (+1 -0)
===================================================================
--- lib/groonga-schema.rb    2016-08-08 18:08:58 +0900 (0c80219)
+++ lib/groonga-schema.rb    2016-08-08 18:09:25 +0900 (bba3bba)
@@ -14,6 +14,7 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
+require "groonga-schema/command-line/groonga-schema-diff"
 require "groonga-schema/differ"
 require "groonga-schema/schema"
 require "groonga-schema/version"

  Added: lib/groonga-schema/command-line/groonga-schema-diff.rb (+82 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga-schema/command-line/groonga-schema-diff.rb    2016-08-08 18:09:25 +0900 (d2fd924)
@@ -0,0 +1,82 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "optparse"
+
+require "groonga/command/parser"
+
+require "groonga-schema/differ"
+
+module GroongaSchema
+  module CommandLine
+    class GroongaSchemaDiff
+      class << self
+        def run(args=ARGV)
+          new(args).run
+        end
+      end
+
+      def initialize(args)
+        @args = args
+      end
+
+      def run
+        parse_arguments
+
+        from_schema = parse_schema(@from_path)
+        to_schema = parse_schema(@to_path)
+        differ = GroongaSchema::Differ.new(from_schema, to_schema)
+        diff = differ.diff
+        $stdout.print(diff.to_groonga_command_list)
+
+        if diff.same?
+          0
+        else
+          1
+        end
+      end
+
+      private
+      def parse_arguments
+        parser = OptionParser.new
+        parser.banner += " FROM_SCHEMA TO_SCHEMA"
+        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_path, @to_path = rest_args
+      end
+
+      def parse_schema(path)
+        File.open(path) do |file|
+          schema = GroongaSchema::Schema.new
+          parser = Groonga::Command::Parser.new
+          parser.on_command do |command|
+            schema.apply_command(command)
+          end
+          file.each_line do |line|
+            parser << line
+          end
+          parser.finish
+          schema
+        end
+      end
+    end
+  end
+end

  Modified: lib/groonga-schema/diff.rb (+38 -0)
===================================================================
--- lib/groonga-schema/diff.rb    2016-08-08 18:08:58 +0900 (32857f1)
+++ lib/groonga-schema/diff.rb    2016-08-08 18:09:25 +0900 (98966af)
@@ -62,5 +62,43 @@ module GroongaSchema
         @added_columns.empty? and
         @changed_columns.empty?
     end
+
+    def to_groonga_command_list
+      converter = GroongaCommandListConverter.new(self)
+      converter.convert
+    end
+
+    class GroongaCommandListConverter
+      def initialize(diff)
+        @diff = diff
+        @buffer = ""
+      end
+
+      def convert
+        @buffer.clear
+        convert_added_plugins
+        convert_removed_plugins
+        @buffer
+      end
+
+      private
+      def convert_added_plugins
+        return if****@diff*****_plugins.empty?
+
+        @buffer << "\n" unles****@buffe*****?
+        @diff.added_plugins.sort_by(&:name).each do |plugin|
+          @buffer << "plugin_register #{plugin.name}\n"
+        end
+      end
+
+      def convert_removed_plugins
+        return if****@diff*****_plugins.empty?
+
+        @buffer << "\n" unles****@buffe*****?
+        @diff.removed_plugins.sort_by(&:name).each do |plugin|
+          @buffer << "plugin_unregister #{plugin.name}\n"
+        end
+      end
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
다운로드 



More information about the Groonga-commit mailing list
Back to archive index