class CircuitPatchTools::Commands::Split

Constants

DEFAULT_OPTIONS
FIELDS
PATCH_REGEXP

Public Instance Methods

description() click to toggle source
# File lib/circuit_patch_tools/commands/split.rb, line 19
def description
  'extract patches from a single sysex file into one file per patch'
end
name() click to toggle source
# File lib/circuit_patch_tools/commands/split.rb, line 15
def name
  'split'
end
run(args) click to toggle source
# File lib/circuit_patch_tools/commands/split.rb, line 23
      def run(args)
        options = DEFAULT_OPTIONS.dup

        OptionParser.new do |opts|
          opts.banner = <<~END
            #{name}: #{description}

            Usage: circuit-patch #{name} [options] patch1.sysex [patch2.sysex ...]

            Options:
          END
          opts.on('-fPATTERN', '--filename=PATTERN',
                  'Pattern for filenames',
                  "Default: #{DEFAULT_OPTIONS[:filename].inspect}",
                  String) do |v|
            options[:filename] = v
          end
          opts.on('-h', '--help', 'Print this help') do
            puts opts
            return
          end
        end.parse!(args)

        extract_all options, args
      end

Private Instance Methods

extract_all(options, paths) click to toggle source
# File lib/circuit_patch_tools/commands/split.rb, line 50
def extract_all(options, paths)
  paths.each do |path|
    File.open(path, 'rb', encoding: Encoding::ASCII_8BIT)
      .read
      .scan(PATCH_REGEXP).each do |raw|
        extract_one options, raw
      end
  end
end
extract_one(options, raw) click to toggle source
# File lib/circuit_patch_tools/commands/split.rb, line 60
def extract_one(options, raw)
  patch = Patch.unpack(raw)

  metadata = FIELDS.map { |f| [f, patch.send(f)] }.to_h
  filename = format(options.fetch(:filename), metadata)

  $stderr.puts filename
  File.open(filename, 'wb', encoding: Encoding::ASCII_8BIT) do |f|
    f << raw
  end
end