Class: Nadb::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/nadb.rb

Instance Method Summary collapse

Constructor Details

#initializeTool

Returns a new instance of Tool.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nadb.rb', line 8

def initialize
  @config = {}
  @options = {}

  load_config

  @opt_parse = OptionParser.new do |opts|
    opts.banner = <<eos
A nicer interface to adb.
Usage: nadb [options] command

Options:
eos
    @options[:all] = false
    opts.on('--all', 'Run command on all connected devices') { @options[:all] = true }

    @options[:name] = nil
    opts.on('-n', '--name NAME', 'Name of device to run from') { |name| @options[:name] = name }

    @options[:index] = nil
    opts.on('-i',
            '--index INDEX',
            OptionParser::DecimalInteger,
            'The index of the target device in the adb devices output') { |index| @options[:index] = index }

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end
end

Instance Method Details

#adb_pathObject



50
51
52
# File 'lib/nadb.rb', line 50

def adb_path
  ENV['ANDROID_HOME'] + '/platform-tools/adb'
end

#construct_adb_command(command, device = nil) ⇒ Object

Construct an adb command



55
56
57
58
# File 'lib/nadb.rb', line 55

def construct_adb_command(command, device = nil)
  device_specifier = device.nil? ? "" : " -s #{device}"
  "#{adb_path}#{device_specifier} #{command}"
end

#get_adb_command_output(command) ⇒ Object



60
61
62
63
# File 'lib/nadb.rb', line 60

def get_adb_command_output(command)
  full_command = construct_adb_command command
  IO.popen(full_command + ' 2>&1', 'w+')
end

#get_connected_devicesObject

Get all currently connected android devices



75
76
77
78
79
80
# File 'lib/nadb.rb', line 75

def get_connected_devices
  get_adb_command_output('devices')
  .drop(1)
  .map { |line| line.split[0] }
  .reject { |d| d.nil? || d.empty? }
end

#load_configObject

Load config from the file if any exists



41
42
43
44
45
46
47
48
# File 'lib/nadb.rb', line 41

def load_config
  path = ENV['HOME'] + '/.nadb.config'
  if !File.exists?(path)
    return
  end

  @config = JSON.parse(File.read(path))
end

#run(argv) ⇒ Object

Bakes fresh cookies.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/nadb.rb', line 83

def run(argv)
  dup_argv = argv.dup
  begin
    @opt_parse.order! dup_argv
  rescue OptionParser::InvalidOption => e
  end

  command_to_run = dup_argv.join " "

  # Run on all devices
  if @options[:index]
    devices = get_connected_devices
    device = devices[@options[:index]]
    if device.nil?
      raise "Requested device #{@options[:index]} but only have #{devices.length} devices connected."
    end

    run_adb_command command_to_run, device

    return
  end

  # Run on a specific device
  if @options[:name]
    if !@config.key? "aliases"
      raise "No aliases defined in your ~/nadb.config file"
    end

    device = @config["aliases"][@options[:name]]
    if device.nil?
      raise "Requested device not found in aliases"
    end
    
    run_adb_command command_to_run, device

    return
  end

  # Run on all connected devices
  if @options[:all]
    devices = get_connected_devices
    devices.each do |device|
      run_adb_command command_to_run, device
    end
    return
  end

  # Just pass everything to adb as we got it
  passthrough_command = argv.join " "
  run_adb_command passthrough_command
end

#run_adb_command(command, device = nil) ⇒ Object

Run an adb commd on specified device, optionally printing the output



66
67
68
69
70
71
72
# File 'lib/nadb.rb', line 66

def run_adb_command(command, device = nil)
  full_command = construct_adb_command command, device
  
  puts full_command
  pio = IO.popen(full_command, 'w')
  Process.wait(pio.pid)
end