Class: Wavesync::Commands::Sync

Inherits:
Command
  • Object
show all
Defined in:
lib/wavesync/commands/sync.rb

Constant Summary collapse

DEVICE_OPTION =
Option.new(short: '-d', long: '--device NAME', description: 'Name of device to sync (as defined in config)')
PAD_OPTION =
Option.new(short: '-p', long: '--pad', description: 'Pad tracks with silence so total length is a multiple of 64 bars (Octatrack only)')

Instance Method Summary collapse

Methods inherited from Command

#parse_options

Instance Method Details

#runObject

: () -> void



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wavesync/commands/sync.rb', line 17

def run
  options, config = parse_options(banner: 'Usage: wavesync sync [options]') do |opts, opts_hash|
    opts.on(*DEVICE_OPTION.to_a) { |value| opts_hash[:device] = value }
    opts.on(*PAD_OPTION.to_a) { opts_hash[:pad] = true }
  end

  device_configs = config.device_configs
  if options[:device]
    device_configs = device_configs.select { |device_config| device_config[:name] == options[:device] }
    if device_configs.empty?
      known = config.device_configs.map { |device_config| device_config[:name] }.join(', ')
      puts "Unknown device \"#{options[:device]}\". Devices in config: #{known}"
      exit 1
    end
  elsif device_configs.size > 1
    device_names = device_configs.map { |device_config| device_config[:name] }
    selected_name = Wavesync::UI.new.select('Select device', device_names)
    device_configs = device_configs.select { |device_config| device_config[:name] == selected_name }
  end

  device_pairs = device_configs.map do |device_config|
    device = Wavesync::Device.find_by(name: device_config[:model])
    unless device
      supported = Wavesync::Device.all.map(&:name).join(', ')
      puts "Unknown device model \"#{device_config[:model]}\" in config. Supported models: #{supported}"
      exit 1
    end
    [device_config, device]
  end #: Array[untyped]

  scanner = Wavesync::Scanner.new(config.library)

  device_pairs.each do |pair|
    device_config = pair[0] #: { name: String, model: String, path: String }
    device = pair[1] #: Wavesync::Device
    scanner.sync(device_config[:path], device, pad: options[:pad] || false)
  end
end