Skip to content

Commit

Permalink
add emulator helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjar7 committed Jan 1, 2025
1 parent 5fa5bb6 commit 2433103
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def self.run(params)
end

def self.setup_emulator(params)
sdk_dir = params[:sdk_dir]
emulator = Helper::EmulatorHelper.new
adb = Helper::AdbHelper.new
avdmanager = Helper::AvdHelper.new

Expand All @@ -78,7 +78,7 @@ def self.setup_emulator(params)
sleep(5)

UI.message("Starting Android emulator...")
system("#{sdk_dir}/emulator/emulator -avd #{params[:emulator_name]} -port #{params[:emulator_port]} > /dev/null 2>&1 &")
emulator.start_emulator(name: params[:emulator_name], port: params[:emulator_port])
adb.trigger(command: "wait-for-device")

sleep(5) while adb.trigger(command: "shell getprop sys.boot_completed").strip != "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,42 @@ def create_avd(name:, package:, device: "pixel_7_pro")
trigger(command: command)
end
end

class EmulatorHelper
attr_accessor :emulator_path

def initialize(emulator_path: nil)
android_home = ENV.fetch('ANDROID_HOME', nil) || ENV.fetch('ANDROID_SDK_ROOT', nil)
if (emulator_path.nil? || emulator_path == "avdmanager") && android_home
emulator_path = File.join(android_home, "emulator", "emulator")
end
UI.message("This is the emulator path: #{emulator_path}")

self.emulator_path = Helper.get_executable_path(File.expand_path(emulator_path))
end

def trigger(command: nil)
raise "emulator_path is not set" unless emulator_path

# Build and execute the command
command = [emulator_path.shellescape, command].compact.join(" ").strip
Action.sh(command)
end

# Start an emulator instance
def start_emulator(name:, port:)
raise "Emulator name is required" if name.nil? || name.empty?
raise "Port is required" if port.nil? || port.to_s.empty?

command = [
"-avd #{name.shellescape}",
"-port #{port.shellescape}",
"> /dev/null 2>&1 &"
].join(" ")

UI.message("Starting emulator #{name} on port #{port}...")
trigger(command: command)
end
end
end
end

0 comments on commit 2433103

Please sign in to comment.