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



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

def self.authors
  ["Martin Gonzalez"]
end

.available_optionsObject



131
132
133
134
135
136
137
138
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
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 131

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: :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: false,
                                 verify_block: proc do |value|
                                                 unless value && !value.empty?
                                                   UI.user_error!('You must set apk_output_path.')
                                                 end
                                               end),
    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



107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 107

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



188
189
190
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 188

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

.detailsObject



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

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

.download_bundletool(version) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 35

def self.download_bundletool(version)
  begin
    puts_message("Downloading bundletool (#{version}) from https://github.com/google/bundletool/releases/download/#{version}/bundletool-all-#{version}.jar...")
    Dir.mkdir "#{@project_root}/bundletool_temp"
    open("https://github.com/google/bundletool/releases/download/#{version}/bundletool-all-#{version}.jar") do |bundletool|
      File.open("#{@bundletool_temp_path}/bundletool.jar", "wb") do |file|
        file.write(bundletool.read)
      end
    end
    puts_success('Downloading bundletool')
  rescue StandardError => e
    puts_error!("Something went wrong when downloading bundletool version #{version}")
  end
end

.extract_universal_apk_from(aab_path, apk_output_path, keystore_info) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 50

def self.extract_universal_apk_from(aab_path, apk_output_path, keystore_info)
  begin
    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}")
  ensure
    clean_temp!
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.prepare_apk!(output_path, target_path) ⇒ Object



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

def self.prepare_apk!(output_path, target_path)
  puts_message("Preparing apk to #{target_path}...")
  puts_important("Apk at path #{target_path} exists. Replacing it.") if File.file?(target_path)
  target_dir_name = File.dirname(target_path)
  unless Dir.exist?(target_dir_name)
    puts_important("Creating path #{target_path} since does not exist")
    Dir.mkdir 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


181
182
183
184
185
186
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 181

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

.puts_error!(message) ⇒ Object



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

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

.puts_important(message) ⇒ Object



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

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

.puts_message(message) ⇒ Object



115
116
117
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 115

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

.puts_success(message) ⇒ Object



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

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

.run(params) ⇒ Object



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

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]
  aab_path = params[:aab_path]
  output_path = params[:apk_output_path]

  validate_aab!(aab_path) 
  download_bundletool(bundletool_version)
  extract_universal_apk_from(aab_path, output_path, keystore_info)
end

.run_bundletool!(aab_path, keystore_info) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 63

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



29
30
31
32
33
# File 'lib/fastlane/plugin/bundletool/actions/bundletool_action.rb', line 29

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