Skip to content

Commit 196b15c

Browse files
committed
Android: Extract some code in helper method to make it more readable
1 parent 4000c75 commit 196b15c

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_send_app_size_metrics.rb

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,9 @@ def self.run(params)
4040
if params[:include_split_sizes]
4141
apkanalyzer_bin = params[:apkanalyzer_binary] || find_apkanalyzer_binary!
4242
unless params[:aab_path].nil?
43-
check_bundletool_installed!
44-
UI.message("[App Size Metrics] Generating the various APK splits from #{params[:aab_path]}...")
45-
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir|
46-
Action.sh('bundletool', 'build-apks', '--bundle', params[:aab_path], '--output-format', 'DIRECTORY', '--output', tmp_dir)
47-
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) }
48-
UI.message("[App Size Metrics] Generated #{apks.length} APKs.")
49-
50-
apks.each do |apk|
51-
split_name = File.basename(apk, '.apk')
52-
add_apk_size_metrics(helper: metrics_helper, apkanalyzer_bin: apkanalyzer_bin, apk: apk, split_name: split_name)
53-
end
54-
55-
UI.message('[App Size Metrics] Done computing splits sizes.')
43+
generate_split_apks(aab_path: params[:aab_path]) do |apk|
44+
split_name = File.basename(apk, '.apk')
45+
add_apk_size_metrics(helper: metrics_helper, apkanalyzer_bin: apkanalyzer_bin, apk: apk, split_name: split_name)
5646
end
5747
end
5848
unless params[:universal_apk_path].nil?
@@ -68,34 +58,49 @@ def self.run(params)
6858
)
6959
end
7060

71-
def self.check_bundletool_installed!
72-
Action.sh('command', '-v', 'bundletool', print_command: false, print_command_output: false)
73-
rescue StandardError
74-
UI.user_error!('bundletool is required to build the split APKs. Install it with `brew install bundletool`')
75-
raise
76-
end
61+
# @!group Small helper methods
62+
class << self
63+
def check_bundletool_installed!
64+
Action.sh('command', '-v', 'bundletool', print_command: false, print_command_output: false)
65+
rescue StandardError
66+
UI.user_error!('bundletool is required to build the split APKs. Install it with `brew install bundletool`')
67+
raise
68+
end
7769

78-
def self.find_apkanalyzer_binary
79-
sdk_root = ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_HOME']
80-
if sdk_root
81-
pattern = File.join(sdk_root, 'cmdline-tools', '{latest,tools}', 'bin', 'apkanalyzer')
82-
apkanalyzer_bin = Dir.glob(pattern).find { |path| File.executable?(path) }
70+
def find_apkanalyzer_binary
71+
sdk_root = ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_HOME']
72+
if sdk_root
73+
pattern = File.join(sdk_root, 'cmdline-tools', '{latest,tools}', 'bin', 'apkanalyzer')
74+
apkanalyzer_bin = Dir.glob(pattern).find { |path| File.executable?(path) }
75+
end
76+
apkanalyzer_bin || Action.sh('command', '-v', 'apkanalyzer', print_command_output: false) { |_| nil }
8377
end
84-
apkanalyzer_bin || Action.sh('command', '-v', 'apkanalyzer', print_command_output: false) { |_| nil }
85-
end
8678

87-
def self.find_apkanalyzer_binary!
88-
apkanalyzer_bin = find_apkanalyzer_binary
89-
UI.user_error!('Unable to find `apkanalyzer` executable in `$PATH` nor `$ANDROID_SDK_ROOT`. Make sure you installed the Android SDK Command-line Tools') if apkanalyzer_bin.nil?
90-
apkanalyzer_bin
91-
end
79+
def find_apkanalyzer_binary!
80+
apkanalyzer_bin = find_apkanalyzer_binary
81+
UI.user_error!('Unable to find `apkanalyzer` executable in `$PATH` nor `$ANDROID_SDK_ROOT`. Make sure you installed the Android SDK Command-line Tools') if apkanalyzer_bin.nil?
82+
apkanalyzer_bin
83+
end
84+
85+
def add_apk_size_metrics(helper:, apkanalyzer_bin:, apk:, split_name:)
86+
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...")
87+
file_size = Action.sh(apkanalyzer_bin, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i
88+
download_size = Action.sh(apkanalyzer_bin, 'apk', 'download-size', apk, print_command: false, print_command_output: false).chomp.to_i
89+
helper.add_metric(name: APK_OPTIMIZED_FILE_SIZE_KEY, value: file_size, metadata: { split: split_name })
90+
helper.add_metric(name: APK_OPTIMIZED_DOWNLOAD_SIZE_KEY, value: download_size, metadata: { split: split_name })
91+
end
9292

93-
def self.add_apk_size_metrics(helper:, apkanalyzer_bin:, apk:, split_name:)
94-
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...")
95-
file_size = Action.sh(apkanalyzer_bin, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i
96-
download_size = Action.sh(apkanalyzer_bin, 'apk', 'download-size', apk, print_command: false, print_command_output: false).chomp.to_i
97-
helper.add_metric(name: APK_OPTIMIZED_FILE_SIZE_KEY, value: file_size, metadata: { split: split_name })
98-
helper.add_metric(name: APK_OPTIMIZED_DOWNLOAD_SIZE_KEY, value: download_size, metadata: { split: split_name })
93+
def generate_split_apks(aab_path:, &block)
94+
check_bundletool_installed!
95+
UI.message("[App Size Metrics] Generating the various APK splits from #{aab_path}...")
96+
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir|
97+
Action.sh('bundletool', 'build-apks', '--bundle', aab_path, '--output-format', 'DIRECTORY', '--output', tmp_dir)
98+
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) }
99+
UI.message("[App Size Metrics] Generated #{apks.length} APKs.")
100+
apks.each(&block)
101+
UI.message('[App Size Metrics] Done computing splits sizes.')
102+
end
103+
end
99104
end
100105

101106
#####################################################

0 commit comments

Comments
 (0)