Class: Fastlane::Actions::BundletoolAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/bundletool/actions/bundletool_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



200
201
202
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 200

def self.authors
  ['Martin Gonzalez']
end

.available_optionsObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 139

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :ks_path,
                                 description: 'Path to .jks file',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ks_password,
                                 description: '.jks password',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ks_key_alias,
                                 description: 'Alias for jks',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ks_key_alias_password,
                                 description: 'Alias password for .jks',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :bundletool_version,
                                 description: 'Version of bundletool to use, by default 0.11.0 will be used',
                                 is_string: true,
                                 default_value: '0.11.0'),
    FastlaneCore::ConfigItem.new(key: :download_url,
                                 description: 'Url to download bundletool from, should point to a jar file',
                                 is_string: true,
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :aab_path,
                                 description: 'Path where the aab file is',
                                 is_string: true,
                                 optional: false,
                                 verify_block: proc do |value|
                                   unless value && !value.empty?
                                     UI.user_error!('You must set aab_path.')
                                   end
                                 end),
    FastlaneCore::ConfigItem.new(key: :apk_output_path,
                                 description: 'Path where the apk file is going to be placed',
                                 is_string: true,
                                 optional: true,
                                 default_value: '.'),
    FastlaneCore::ConfigItem.new(key: :verbose,
                                 description: 'Show every messages of the action',
                                 is_string: false,
                                 type: Boolean,
                                 optional: true,
                                 default_value: false)

  ]
end

.clean_temp!Object



110
111
112
113
114
115
116
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 110

def self.clean_temp!
  cmd = "rm -rf #{@project_root}/bundletool_temp"
  Open3.popen3(cmd) do |_, _, stderr, wait_thr|
    exit_status = wait_thr.value
    raise stderr.read unless exit_status.success?
  end
end

.descriptionObject



196
197
198
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 196

def self.description
  'Extracts an universal apk from an .aab file'
end

.detailsObject



204
205
206
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 204

def self.details
  'Using the google oficial bundletool to extract an universal apk from .aab file to distribute it'
end

.download_bundletool(version, download_url) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 43

def self.download_bundletool(version, download_url)
  Dir.mkdir "#{@project_root}/bundletool_temp"

  unless download_url.nil?          
    puts_message("Downloading bundletool from #{download_url}")
    download_and_write_bundletool(download_url)
  else
    puts_message("Downloading bundletool (#{version}) from https://github.com/google/bundletool/releases/download/#{version}/bundletool-all-#{version}.jar...")
    download_and_write_bundletool("https://github.com/google/bundletool/releases/download/#{version}/bundletool-all-#{version}.jar")
  end
  puts_success('Downloaded bundletool')
rescue OpenURI::HTTPError => e
  clean_temp!
  puts_error!("Something went wrong when downloading bundletool version #{version}" + ". \nError message\n #{e.message}")
  false        
end

.extract_universal_apk_from(aab_path, apk_output_path, keystore_info) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 60

def self.extract_universal_apk_from(aab_path, apk_output_path, keystore_info)
  aab_absolute_path = Pathname.new(File.expand_path(aab_path)).to_s
  apk_output_absolute_path = Pathname.new(File.expand_path(apk_output_path)).to_s
  output_path = run_bundletool!(aab_absolute_path, keystore_info)
  prepare_apk!(output_path, apk_output_absolute_path)
rescue StandardError => e
  puts_error!("Bundletool could not extract universal apk from aab at #{aab_absolute_path}. \nError message\n #{e.message}")
ensure
  clean_temp!
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 208

def self.is_supported?(platform)
  [:android].include?(platform)
end

.prepare_apk!(output_path, target_path) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 90

def self.prepare_apk!(output_path, target_path)
  puts_message("Preparing apk to #{target_path}...")
  if File.file?(target_path)
    puts_important("Apk at path #{target_path} exists. Replacing it.")
  end
  target_dir_name = File.dirname(target_path)
  unless Dir.exist?(target_dir_name)
    puts_important("Creating path #{target_dir_name} since does not exist")
    FileUtils.mkdir_p target_dir_name
  end
  cmd = "mv \"#{output_path}\" \"#{@bundletool_temp_path}/output.zip\" &&
  unzip \"#{@bundletool_temp_path}/output.zip\" -d \"#{@bundletool_temp_path}\" &&
  mv \"#{@bundletool_temp_path}/universal.apk\" \"#{target_path}\""
  Open3.popen3(cmd) do |_, _, stderr, wait_thr|
    exit_status = wait_thr.value
    raise stderr.read unless exit_status.success?
  end
  puts_success("Preparing apk to #{target_path}")
end


189
190
191
192
193
194
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 189

def self.print_params(options)
  table_title = "Params for bundletool #{Fastlane::Bundletool::VERSION}"
  FastlaneCore::PrintTable.print_values(config: options,
                                        mask_keys: %i[ks_password ks_key_alias_password],
                                        title: table_title)
end

.puts_error!(message) ⇒ Object



134
135
136
137
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 134

def self.puts_error!(message)
  p message
  UI.user_error! message
end

.puts_important(message) ⇒ Object



128
129
130
131
132
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 128

def self.puts_important(message)
  if @verbose
    UI.important "#{message} #{Fastlane::Helper::Emojis.warning}"
  end
end

.puts_message(message) ⇒ Object



118
119
120
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 118

def self.puts_message(message)
  UI.message message if @verbose
end

.puts_success(message) ⇒ Object



122
123
124
125
126
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 122

def self.puts_success(message)
  if @verbose
    UI.success "#{message} #{Fastlane::Helper::Emojis.green_checkmark}"
  end
end

.run(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 9

def self.run(params)
  print_params(params)
  @project_root = Dir.pwd
  @bundletool_temp_path = "#{@project_root}/bundletool_temp"
  @verbose = params[:verbose]
  keystore_info = {}
  unless params[:ks_path].nil?
    keystore_info[:keystore_path] = params[:ks_path]
    keystore_info[:keystore_password] = params[:ks_password]
    keystore_info[:alias] = params[:ks_key_alias]
    keystore_info[:alias_password] = params[:ks_key_alias_password]
  end

  bundletool_version = params[:bundletool_version]
  download_url = params[:download_url]
  aab_path = params[:aab_path]
  output_path = params[:apk_output_path] || '.'

  return unless validate_aab!(aab_path)

  return unless download_bundletool(bundletool_version, download_url)

  extract_universal_apk_from(aab_path, output_path, keystore_info)
end

.run_bundletool!(aab_path, keystore_info) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 71

def self.run_bundletool!(aab_path, keystore_info)
  puts_message("Extracting apk from #{aab_path}...")
  output_path = "#{@bundletool_temp_path}/output.apks"
  keystore_params = ''

  unless keystore_info.empty?
    keystore_params = "--ks='#{keystore_info[:keystore_path]}' --ks-pass='pass:#{keystore_info[:keystore_password]}' --ks-key-alias='#{keystore_info[:alias]}' --key-pass='pass:#{keystore_info[:alias_password]}'"
  end

  cmd = "java -jar #{@bundletool_temp_path}/bundletool.jar build-apks --bundle=\"#{aab_path}\" --output=\"#{output_path}\" --mode=universal #{keystore_params}"

  Open3.popen3(cmd) do |_, _, stderr, wait_thr|
    exit_status = wait_thr.value
    raise stderr.read unless exit_status.success?
  end
  puts_success("Extracting apk from #{aab_path}")
  output_path
end

.validate_aab!(aab_path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 34

def self.validate_aab!(aab_path)
  puts_message('Checking if .aab file exists...')
  unless File.file?(aab_path)
    puts_error!(".aab file at #{aab_path} does not exist")
    return false
  end
  puts_success('Checking if .aab file exists')
end