Class: BADB::Runner

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

Constant Summary collapse

CONFIG_FILE =
File.expand_path("~/.badb_config.yaml")

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



10
11
12
# File 'lib/badb.rb', line 10

def initialize
  read_settings
end

Instance Method Details

#adb_pathObject



28
29
30
31
32
33
34
35
# File 'lib/badb.rb', line 28

def adb_path
  return @adb_path if @adb_path
  @adb_path = `which adb`.strip
  if @adb_path.empty?
    raise "Can't find your adb command. Is your path set\?"
  end
  @adb_path
end

#choose_device(opt = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/badb.rb', line 48

def choose_device(opt={})
  opt = {
    :all => false,
    :save_choice => true
  }.merge(opt)
  devices = get_devices
  devices = filter_latest_device(devices) if !opt[:all]
  
  if devices.empty?
    raise "No devices attached"
  elsif devices.size == 1
    yield devices[0] if block_given?
  else
    choose do |menu|
      menu.prompt = "Choose your adb device: "
      
      devices.each do |device|
        menu.choice device_label(device) do
          save_choice(device) if opt[:save_choice]
          yield device if block_given?
        end
      end
    end
  end
end

#create_aliasObject



139
140
141
142
143
144
145
146
147
# File 'lib/badb.rb', line 139

def create_alias
  puts "Create an alias: "
  
  choose_device(:all => true,:save_choice => false) do |device|
    device_alias = ask("Enter an alias for #{device_label(device)}: ")
    Settings[:aliases][device] = device_alias
    save_settings
  end
end

#current_deviceObject



100
101
102
103
104
105
106
# File 'lib/badb.rb', line 100

def current_device
  devices = get_devices
  devices = filter_latest_device(devices)
  
  return devices[0] if devices.size == 1
  return nil
end

#device_label(device) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/badb.rb', line 129

def device_label(device)
  device_alias = Settings[:aliases][device]
  
  if device_alias
    "#{device} (#{device_alias})"
  else
    device
  end
end

#filter_latest_device(devices) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/badb.rb', line 82

def filter_latest_device(devices)
  hist = Settings[:device_history]
  
  latest_history_device = nil
  latest_history_index = -1
  
  devices.each do |d|
    index = hist.index(d)
    if index && index > latest_history_index
      latest_history_index = index
      latest_history_device = d
    end
  end
  
  return [latest_history_device] if latest_history_device
  devices
end

#get_devicesObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/badb.rb', line 37

def get_devices
  devices = []
  IO.popen("#{adb_path} devices").each_line do |line|
    line = line.strip
    if line =~ /^(.*)\tdevice$/
      devices << $1
    end
  end      
  devices
end

#list_devicesObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/badb.rb', line 149

def list_devices
  devices = get_devices
  current = current_device
  
  puts "List of devices: "
  devices.each do |d|
    
    if d == current_device
      puts device_label(d) + " #current"
    else
      puts device_label(d)
    end
  end
end

#read_settingsObject



14
15
16
17
18
19
20
21
22
# File 'lib/badb.rb', line 14

def read_settings
  Settings.use :config_file
  Settings({
             :device_history => [],
             :aliases => {}
           })
  FileUtils.touch(CONFIG_FILE)
  Settings.read(CONFIG_FILE)
end

#runObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/badb.rb', line 164

def run
  if !ARGV.empty?
    if ARGV[0] == "choose"
      choose_device(:all => true)
      return
    elsif ARGV[0] == "current"
      show_current_device
      return
    elsif ARGV[0] == "alias"
      create_alias
      return
    elsif ARGV[0] == "list"
      list_devices
      return
    elsif ARGV[0] == "help"
      show_help
      return
    elsif ARGV[0] == "adbhelp"
      Kernel.exec(adb_path,"help")
      return
    end
  end
  
  choose_device do |device|
    ENV["ANDROID_SERIAL"] = device
    # p [:device, device]
    # p [:cmd, adb_path, ARGV]
    Kernel.exec(adb_path,*ARGV)
  end
end

#save_choice(device) ⇒ Object



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

def save_choice(device)
  hist = Settings[:device_history]
  hist.delete(device)
  hist << device
  Settings[:device_history] = hist
  save_settings
end

#save_settingsObject



24
25
26
# File 'lib/badb.rb', line 24

def save_settings
  Settings.save!(CONFIG_FILE)
end

#show_current_deviceObject



108
109
110
111
112
113
114
115
116
# File 'lib/badb.rb', line 108

def show_current_device
  d = current_device
  
  if d
    puts "Current device is " + device_label(current_device)
  else
    puts "No current device."
  end
end

#show_helpObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/badb.rb', line 118

def show_help
  puts "badb - Better Android Debug Bridge\n\n"
  puts "Usage:"
  puts "badb choose - choose the current android device to map badb command to, via prompt"
  puts "badb alias - add a friendly name for a specific device, via prompt"
  puts "badb current - the current android device"
  puts "badb list - lists android devices, with aliases, indicating the current one"
  puts "badb help - this message"
  puts "badb adbhelp - the original adb help command"
end