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.



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

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.



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

def android_home
  @android_home
end

Instance Method Details

#adbObject



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

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

#avdmanagerObject



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

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

#cmdline_tools_search_pathsObject



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

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



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

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.



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

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.



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

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



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

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