# rubocop:disable Metrics/AbcSize require 'fastlane/erb_template_helper' include ERB::Util require 'ostruct' require 'cgi' module Fastlane module Actions module SharedValues S3_APK_OUTPUT_PATH ||= :S3_APK_OUTPUT_PATH S3_IPA_OUTPUT_PATH ||= :S3_IPA_OUTPUT_PATH S3_DSYM_OUTPUT_PATH ||= :S3_DSYM_OUTPUT_PATH S3_PLIST_OUTPUT_PATH ||= :S3_PLIST_OUTPUT_PATH S3_HTML_OUTPUT_PATH ||= :S3_HTML_OUTPUT_PATH S3_VERSION_OUTPUT_PATH ||= :S3_VERSION_OUTPUT_PATH S3_SOURCE_OUTPUT_PATH ||= :S3_SOURCE_OUTPUT_PATH end class AwsS3Action < Action def self.run(config) # Calling fetch on config so that default values will be used params = {} params[:apk] = config[:apk] params[:ipa] = config[:ipa] params[:dsym] = config[:dsym] params[:access_key] = config[:access_key] params[:secret_access_key] = config[:secret_access_key] params[:bucket] = config[:bucket] params[:endpoint] = config[:endpoint] params[:region] = config[:region] params[:app_directory] = config[:app_directory] params[:acl] = config[:acl] params[:source] = config[:source] params[:path] = config[:path] params[:upload_metadata] = config[:upload_metadata] params[:plist_template_path] = config[:plist_template_path] params[:plist_file_name] = config[:plist_file_name] params[:html_template_path] = config[:html_template_path] params[:html_file_name] = config[:html_file_name] params[:skip_html_upload] = config[:skip_html_upload] params[:version_template_path] = config[:version_template_path] params[:version_file_name] = config[:version_file_name] # Pulling parameters for other uses s3_region = params[:region] s3_access_key = params[:access_key] s3_secret_access_key = params[:secret_access_key] s3_bucket = params[:bucket] s3_endpoint = params[:endpoint] apk_file = params[:apk] ipa_file = params[:ipa] dsym_file = params[:dsym] s3_path = params[:path] acl = params[:acl].to_sym UI.user_error!("No S3 access key given, pass using `access_key: 'key'`") unless s3_access_key.to_s.length > 0 UI.user_error!("No S3 secret access key given, pass using `secret_access_key: 'secret key'`") unless s3_secret_access_key.to_s.length > 0 UI.user_error!("No S3 bucket given, pass using `bucket: 'bucket'`") unless s3_bucket.to_s.length > 0 UI.user_error!("No IPA or APK file path given, pass using `ipa: 'ipa path'` or `apk: 'apk path'`") if ipa_file.to_s.length == 0 && apk_file.to_s.length == 0 UI.user_error!("Please only give IPA path or APK path (not both)") if ipa_file.to_s.length > 0 && apk_file.to_s.length > 0 require 'aws-sdk' Aws.config.update({ region: s3_region, credentials: Aws::Credentials.new(s3_access_key, s3_secret_access_key) }) s3_client = if s3_endpoint Aws::S3::Client.new(endpoint: s3_endpoint) else Aws::S3::Client.new end upload_ipa(s3_client, params, s3_region, s3_access_key, s3_secret_access_key, s3_bucket, ipa_file, dsym_file, s3_path, acl) if ipa_file.to_s.length > 0 upload_apk(s3_client, params, s3_region, s3_access_key, s3_secret_access_key, s3_bucket, apk_file, s3_path, acl) if apk_file.to_s.length > 0 return true end def self.upload_ipa(s3_client, params, s3_region, s3_access_key, s3_secret_access_key, s3_bucket, ipa_file, dsym_file, s3_path, acl) s3_path = "v{CFBundleShortVersionString}_b{CFBundleVersion}/" unless s3_path app_directory = params[:app_directory] plist_template_path = params[:plist_template_path] plist_file_name = params[:plist_file_name] html_template_path = params[:html_template_path] html_file_name = params[:html_file_name] version_template_path = params[:version_template_path] version_file_name = params[:version_file_name] url_part = self.expand_path_with_substitutions_from_ipa_plist(ipa_file, s3_path) ipa_file_basename = File.basename(ipa_file) ipa_file_name = "#{url_part}#{ipa_file_basename}" ipa_file_data = File.open(ipa_file, 'rb') ipa_url = self.upload_file(s3_client, s3_bucket, app_directory, ipa_file_name, ipa_file_data, acl) # Setting action and environment variables Actions.lane_context[SharedValues::S3_IPA_OUTPUT_PATH] = ipa_url ENV[SharedValues::S3_IPA_OUTPUT_PATH.to_s] = ipa_url if dsym_file dsym_file_basename = File.basename(dsym_file) dsym_file_name = "#{url_part}#{dsym_file_basename}" dsym_file_data = File.open(dsym_file, 'rb') dsym_url = self.upload_file(s3_client, s3_bucket, app_directory, dsym_file_name, dsym_file_data, acl) # Setting action and environment variables Actions.lane_context[SharedValues::S3_DSYM_OUTPUT_PATH] = dsym_url ENV[SharedValues::S3_DSYM_OUTPUT_PATH.to_s] = dsym_url end if params[:upload_metadata] == false return true end ##################################### # # html and plist building # ##################################### # Gets info used for the plist info = FastlaneCore::IpaFileAnalyser.fetch_info_plist_file(ipa_file) build_num = info['CFBundleVersion'] bundle_id = info['CFBundleIdentifier'] bundle_version = info['CFBundleShortVersionString'] title = CGI.escapeHTML(info['CFBundleName']) full_version = "#{bundle_version}.#{build_num}" # Creating plist and html names plist_file_name ||= "#{url_part}#{URI.escape(title.delete(' '))}.plist" html_file_name ||= "index.html" version_file_name ||= "version.json" # grabs module eth = Fastlane::Helper::AwsS3Helper # Creates plist from template if plist_template_path && File.exist?(plist_template_path) plist_template = eth.load_from_path(plist_template_path) else plist_template = eth.load("s3_ios_plist_template") end plist_render = eth.render(plist_template, { url: ipa_url, ipa_url: ipa_url, build_num: build_num, bundle_id: bundle_id, bundle_version: bundle_version, title: title }) ##################################### # # plist uploading # ##################################### plist_url = self.upload_file(s3_client, s3_bucket, app_directory, plist_file_name, plist_render, acl) # Creates html from template if html_template_path && File.exist?(html_template_path) html_template = eth.load_from_path(html_template_path) else html_template = eth.load("s3_ios_html_template") end html_render = eth.render(html_template, { url: plist_url, plist_url: plist_url, ipa_url: ipa_url, build_num: build_num, bundle_id: bundle_id, bundle_version: bundle_version, title: title }) # Creates version from template if version_template_path && File.exist?(version_template_path) version_template = eth.load_from_path(version_template_path) else version_template = eth.load("s3_ios_version_template") end version_render = eth.render(version_template, { url: plist_url, plist_url: plist_url, ipa_url: ipa_url, build_num: build_num, bundle_version: bundle_version, full_version: full_version }) ##################################### # # html uploading # ##################################### skip_html = params[:skip_html_upload] html_url = self.upload_file(s3_client, s3_bucket, app_directory, html_file_name, html_render, acl) unless skip_html version_url = self.upload_file(s3_client, s3_bucket, app_directory, version_file_name, version_render, acl) # Setting action and environment variables Actions.lane_context[SharedValues::S3_PLIST_OUTPUT_PATH] = plist_url ENV[SharedValues::S3_PLIST_OUTPUT_PATH.to_s] = plist_url Actions.lane_context[SharedValues::S3_HTML_OUTPUT_PATH] = html_url unless skip_html ENV[SharedValues::S3_HTML_OUTPUT_PATH.to_s] = html_url unless skip_html Actions.lane_context[SharedValues::S3_VERSION_OUTPUT_PATH] = version_url ENV[SharedValues::S3_VERSION_OUTPUT_PATH.to_s] = version_url self.upload_source(s3_client, params, s3_bucket, params[:source], s3_path, acl) UI.success("Successfully uploaded ipa file to '#{Actions.lane_context[SharedValues::S3_IPA_OUTPUT_PATH]}'") UI.success("iOS app can be downloaded at '#{Actions.lane_context[SharedValues::S3_HTML_OUTPUT_PATH]}'") unless skip_html end def self.upload_apk(s3_client, params, s3_region, s3_access_key, s3_secret_access_key, s3_bucket, apk_file, s3_path, acl) version = get_apk_version(apk_file) version_code = version[0] version_name = version[1] title = version[2] s3_path = "#{version_code}_#{version_name}/" unless s3_path app_directory = params[:app_directory] html_template_path = params[:html_template_path] html_file_name = params[:html_file_name] version_template_path = params[:version_template_path] version_file_name = params[:version_file_name] url_part = s3_path apk_file_basename = File.basename(apk_file) apk_file_name = "#{url_part}#{apk_file_basename}" apk_file_data = File.open(apk_file, 'rb') apk_url = self.upload_file(s3_client, s3_bucket, app_directory, apk_file_name, apk_file_data, acl) # Setting action and environment variables Actions.lane_context[SharedValues::S3_APK_OUTPUT_PATH] = apk_url ENV[SharedValues::S3_APK_OUTPUT_PATH.to_s] = apk_url if params[:upload_metadata] == false return true end ##################################### # # html and plist building # ##################################### html_file_name ||= "index.html" version_file_name ||= "version.json" # grabs module eth = Fastlane::Helper::AwsS3Helper # Creates html from template if html_template_path && File.exist?(html_template_path) html_template = eth.load_from_path(html_template_path) else html_template = eth.load("s3_android_html_template") end html_render = eth.render(html_template, { apk_url: apk_url, version_code: version_code, version_name: version_name, title: title }) # Creates version from template if version_template_path && File.exist?(version_template_path) version_template = eth.load_from_path(version_template_path) else version_template = eth.load("s3_android_version_template") end version_render = eth.render(version_template, { apk_url: apk_url, version_code: version_code, version_name: version_name, full_version: "#{version_code}_#{version_name}" }) ##################################### # # html and plist uploading # ##################################### skip_html = params[:skip_html_upload] html_url = self.upload_file(s3_client, s3_bucket, app_directory, html_file_name, html_render, acl) unless skip_html version_url = self.upload_file(s3_client, s3_bucket, app_directory, version_file_name, version_render, acl) Actions.lane_context[SharedValues::S3_HTML_OUTPUT_PATH] = html_url unless skip_html ENV[SharedValues::S3_HTML_OUTPUT_PATH.to_s] = html_url unless skip_html Actions.lane_context[SharedValues::S3_VERSION_OUTPUT_PATH] = version_url ENV[SharedValues::S3_VERSION_OUTPUT_PATH.to_s] = version_url self.upload_source(s3_client, params, s3_bucket, params[:source], s3_path, acl) UI.success("Successfully uploaded apk file to '#{Actions.lane_context[SharedValues::S3_APK_OUTPUT_PATH]}'") UI.success("Android app can be downloaded at '#{Actions.lane_context[SharedValues::S3_HTML_OUTPUT_PATH]}'") unless skip_html end def self.upload_source(s3_client, params, s3_bucket, source_directory, s3_path, acl) if source_directory && File.directory?(source_directory) source_directory = File.absolute_path source_directory output_file_path = Tempfile.new('aws_s3_source').path output_file_path = other_action.zip( path: source_directory, output_path: output_file_path.gsub(/(?