Sha256: 8bbe27eb16c79e52707725c6f0ecaf11e64e05f8710824028d70324eb40a8b5b

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

# -*- compile-command: "../../.bin/bioshogi convert -f sfen assets/kifu_formats/sample.kif" -*-

module Bioshogi
  class Converter
    concern :CLI do
      included do
        desc "convert", "一括棋譜フォーマット変換"
        option :format,     type: :string,  aliases: "-f", default: "kif"
        option :output_dir, type: :string,  aliases: "-o"
        option :overwrite,  type: :boolean, aliases: "-w", default: false
        def convert(*files)
          Converter.new(files, options.to_options).call
        end
      end
    end

    def initialize(files, options = {})
      @files = files
      @options = {
        :format     => "kif",
        :output_dir => nil,
        :overwrite  => false,
      }.merge(options)
    end

    def call
      @files.each { |e| process_one(e) }
    end

    def process_one(in_file)
      in_file = Pathname(in_file).expand_path
      info = Parser.file_parse(in_file)
      str = info.public_send("to_#{@options[:format]}")
      case
      when @options[:overwrite]
        in_file.write(str)
        puts "[overwrite] #{in_file} => #{out_file}"
      when @options[:output_dir].present?
        out_file = output_dir + in_file.basename.sub_ext(extname)
        FileUtils.mkdir_p(out_file.dirname)
        out_file.write(str)
        puts "[output] #{in_file} => #{out_file}"
      else
        puts str
      end
    end

    def output_dir
      Pathname(@options[:output_dir]).expand_path
    end

    def extname
      ".#{@options[:format]}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bioshogi-0.0.10 lib/bioshogi/converter.rb
bioshogi-0.0.9 lib/bioshogi/converter.rb
bioshogi-0.0.8 lib/bioshogi/converter.rb
bioshogi-0.0.7 lib/bioshogi/converter.rb
bioshogi-0.0.5 lib/bioshogi/converter.rb
bioshogi-0.0.4 lib/bioshogi/converter.rb