Class: Fastlane::Helper::Android::ToolsPathHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb

Overview

Helper to find the paths of common Android build and SDK tools on the current machine Based on ‘$ANDROID_HOME` and the common relative paths those tools are installed in.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdk_root: nil) ⇒ ToolsPathHelper

Returns a new instance of ToolsPathHelper.



12
13
14
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 12

def initialize(sdk_root: nil)
  @android_home = sdk_root || ENV['ANDROID_HOME'] || ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_SDK']
end

Instance Attribute Details

#android_homeObject (readonly)

Returns the value of attribute android_home.



10
11
12
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 10

def android_home
  @android_home
end

Instance Method Details

#adbObject



78
79
80
81
82
83
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 78

def adb
  @adb ||= find_tool_path!(
    binary: 'adb',
    search_paths: [File.join('platform-tools')]
  )
end

#avdmanagerObject



64
65
66
67
68
69
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 64

def avdmanager
  @avdmanager ||= find_tool_path!(
    binary: 'avdmanager',
    search_paths: cmdline_tools_search_paths
  )
end

#cmdline_tools_search_pathsObject



47
48
49
50
51
52
53
54
55
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 47

def cmdline_tools_search_paths
  # It appears that depending on the machines and versions of Android SDK, some versions
  # installed the command line tools in `tools` and not `latest` subdirectory, hence why
  # we search both (`latest` first, `tools` as fallback) to cover all our bases.
  [
    File.join('cmdline-tools', 'latest', 'bin'),
    File.join('cmdline-tools', 'tools', 'bin'),
  ]
end

#emulatorObject



71
72
73
74
75
76
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 71

def emulator
  @emulator ||= find_tool_path!(
    binary: 'emulator',
    search_paths: [File.join('emulator')]
  )
end

#find_tool_path(binary:, search_paths:) ⇒ String

Returns The absolute path of the tool if found, ‘nil` if not found.

Parameters:

  • binary (String)

    The name of the binary to search for

  • search_paths (Array<String>)

    The search paths, relative to ‘@android_home`, in which to search for the tools. If `android_home` is `nil` or the binary wasn’t found in any of the ‘search_paths`, will fallback to searching in `$PATH`.

Returns:

  • (String)

    The absolute path of the tool if found, ‘nil` if not found.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 20

def find_tool_path(binary:, search_paths:)
  bin_path = unless android_home.nil?
               search_paths
                 .map { |path| File.join(android_home, path, binary) }
                 .find { |path| File.executable?(path) }
             end

  # If not found in any of the `search_paths`, try to look for it in $PATH
  bin_path ||= Actions.sh('command', '-v', binary) { |err, res, _| res if err&.success? }&.chomp

  # Normalize return value to `nil` if it was not found, empty, or is not an executable
  bin_path = nil if !bin_path.nil? && (bin_path.empty? || !File.executable?(bin_path))

  bin_path
end

#find_tool_path!(binary:, search_paths:) ⇒ String

Returns The absolute path of the tool if found.

Parameters:

  • binary (String)

    The name of the binary to search for

  • search_paths (Array<String>)

    The search paths, relative to ‘@android_home`, in which to search for the tools. If `android_home` is `nil` or the binary wasn’t found in any of the ‘search_paths`, will fallback to searching in `$PATH`.

Returns:

  • (String)

    The absolute path of the tool if found.

Raises:

  • (FastlaneCore::Interface::FastlaneError)

    If the tool couldn’t be found.



41
42
43
44
45
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 41

def find_tool_path!(binary:, search_paths:)
  bin_path = find_tool_path(binary: binary, search_paths: search_paths)
  UI.user_error!("Unable to find path for #{binary} in #{search_paths.inspect}. Verify you installed the proper Android tools.") if bin_path.nil?
  bin_path
end

#sdkmanagerObject



57
58
59
60
61
62
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_tools_path_helper.rb', line 57

def sdkmanager
  @sdkmanager ||= find_tool_path!(
    binary: 'sdkmanager',
    search_paths: cmdline_tools_search_paths
  )
end