Skip to content

Commit 543cfae

Browse files
committed
Android: Extract some code in helper method to make it more readable
1 parent 127ce99 commit 543cfae

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

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

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,9 @@ def self.run(params)
3939
# Add optimized file and download sizes for each split `.apk` metrics to the payload if a `:include_split_sizes` is enabled
4040
if params[:include_split_sizes]
4141
unless params[:aab_path].nil?
42-
check_bundletool_installed!
43-
UI.message("[App Size Metrics] Generating the various APK splits from #{params[:aab_path]}...")
44-
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir|
45-
Action.sh('bundletool', 'build-apks', '--bundle', params[:aab_path], '--output-format', 'DIRECTORY', '--output', tmp_dir)
46-
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) }
47-
UI.message("[App Size Metrics] Generated #{apks.length} APKs.")
48-
49-
apks.each do |apk|
50-
split_name = File.basename(apk, '.apk')
51-
add_apk_size_metrics(helper: metrics_helper, apk: apk, split_name: split_name)
52-
end
53-
54-
UI.message('[App Size Metrics] Done computing splits sizes.')
42+
generate_split_apks(aab_path: params[:aab_path]) do |apk|
43+
split_name = File.basename(apk, '.apk')
44+
add_apk_size_metrics(helper: metrics_helper, apk: apk, split_name: split_name)
5545
end
5646
end
5747
unless params[:universal_apk_path].nil?
@@ -67,29 +57,46 @@ def self.run(params)
6757
)
6858
end
6959

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

77-
def self.apkanalyzer_binary!
78-
Action.sh('command', '-v', 'apkanalyzer')
79-
rescue StandardError
80-
sdk_root = ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_HOME']
81-
UI.user_error!('Unable to find ANDROID_SDK_ROOT / ANDROID_HOME') if sdk_root.nil?
82-
apkanalyzer_bin = File.join(sdk_root, 'cmdline-tools', 'latest', 'bin', 'apkanalyzer')
83-
UI.user_error!('Unable to find `apkanalyzer` executable. Make sure you installed the Android SDK Command-line Tools') unless File.executable?(apkanalyzer_bin)
84-
apkanalyzer_bin
85-
end
69+
def apkanalyzer_binary!
70+
@apkanalyzer_binary ||= begin
71+
Action.sh('command', '-v', 'apkanalyzer')
72+
rescue StandardError
73+
sdk_root = ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_HOME']
74+
UI.user_error!('Unable to find ANDROID_SDK_ROOT / ANDROID_HOME') if sdk_root.nil?
75+
apkanalyzer_bin = File.join(sdk_root, 'cmdline-tools', 'latest', 'bin', 'apkanalyzer')
76+
UI.user_error!('Unable to find `apkanalyzer` executable. Make sure you installed the Android SDK Command-line Tools') unless File.executable?(apkanalyzer_bin)
77+
apkanalyzer_bin
78+
end
79+
end
8680

87-
def self.add_apk_size_metrics(helper:, apk:, split_name:)
88-
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...")
89-
file_size = Action.sh(apkanalyzer_binary!, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i
90-
download_size = Action.sh(apkanalyzer_binary!, 'apk', 'download-size', apk, print_command: false, print_command_output: false).chomp.to_i
91-
helper.add_metric(name: APK_OPTIMIZED_FILE_SIZE_KEY, value: file_size, metadata: { split: split_name })
92-
helper.add_metric(name: APK_OPTIMIZED_DOWNLOAD_SIZE_KEY, value: download_size, metadata: { split: split_name })
81+
def generate_split_apks(aab_path:, &block)
82+
check_bundletool_installed!
83+
UI.message("[App Size Metrics] Generating the various APK splits from #{aab_path}...")
84+
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir|
85+
Action.sh('bundletool', 'build-apks', '--bundle', aab_path, '--output-format', 'DIRECTORY', '--output', tmp_dir)
86+
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) }
87+
UI.message("[App Size Metrics] Generated #{apks.length} APKs.")
88+
apks.each(&block)
89+
UI.message('[App Size Metrics] Done computing splits sizes.')
90+
end
91+
end
92+
93+
def add_apk_size_metrics(helper:, apk:, split_name:)
94+
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...")
95+
file_size = Action.sh(apkanalyzer_binary!, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i
96+
download_size = Action.sh(apkanalyzer_binary!, '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 })
99+
end
93100
end
94101

95102
#####################################################

0 commit comments

Comments
 (0)