|
| 1 | +require 'plist' |
| 2 | +require_relative '../../helper/app_size_metrics_helper' |
| 3 | + |
| 4 | +module Fastlane |
| 5 | + module Actions |
| 6 | + class IosSendAppSizeMetricsAction < Action |
| 7 | + def self.run(params) |
| 8 | + # Check input parameters |
| 9 | + base_url = URI(params[:api_base_url]) |
| 10 | + api_token = params[:api_token] |
| 11 | + if (api_token.nil? || api_token.empty?) && !base_url.is_a?(URI::File) |
| 12 | + UI.user_error!('An API token is required when using an `api_base_url` with a scheme other than `file://`') |
| 13 | + end |
| 14 | + |
| 15 | + # Build the payload base |
| 16 | + metrics_helper = Fastlane::WPMRT::AppSizeMetricsHelper.new( |
| 17 | + Platform: 'iOS', |
| 18 | + 'App Name': params[:app_name], |
| 19 | + 'App Version': params[:app_version], |
| 20 | + 'Build Type': params[:build_type], |
| 21 | + Source: params[:source] |
| 22 | + ) |
| 23 | + metrics_helper.add_metric(name: 'File Size', value: File.size(params[:ipa_path])) |
| 24 | + |
| 25 | + # Add app-thinning metrics to the payload if a `.plist` is provided |
| 26 | + app_thinning_plist_path = params[:app_thinning_plist_path] || File.join(File.dirname(params[:ipa_path]), 'app-thinning.plist') |
| 27 | + if File.exist?(app_thinning_plist_path) |
| 28 | + plist = Plist.parse_xml(app_thinning_plist_path) |
| 29 | + plist['variants'].each do |_key, variant| |
| 30 | + variant_descriptors = variant['variantDescriptors'] || [{ 'device' => 'Universal' }] |
| 31 | + variant_descriptors.each do |desc| |
| 32 | + variant_metadata = { device: desc['device'], 'OS Version': desc['os-version'] } |
| 33 | + metrics_helper.add_metric(name: 'Download Size', value: variant['sizeCompressedApp'], meta: variant_metadata) |
| 34 | + metrics_helper.add_metric(name: 'Install Size', value: variant['sizeUncompressedApp'], meta: variant_metadata) |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + # Send the payload |
| 40 | + metrics_helper.send_metrics( |
| 41 | + base_url: base_url, |
| 42 | + api_token: api_token, |
| 43 | + use_gzip: params[:use_gzip_content_encoding] |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + ##################################################### |
| 48 | + # @!group Documentation |
| 49 | + ##################################################### |
| 50 | + |
| 51 | + def self.description |
| 52 | + 'Send iOS app size metrics to our metrics server' |
| 53 | + end |
| 54 | + |
| 55 | + def self.details |
| 56 | + <<~DETAILS |
| 57 | + Send iOS app size metrics to our metrics server. |
| 58 | +
|
| 59 | + In order to get Xcode generate the `app-thinning.plist` file (during `gym` and the export of the `.xcarchive`), you need to: |
| 60 | + (1) Use either `ad-hoc`, `enterprise` or `development` export method (in particular, won't work with `app-store`), |
| 61 | + (2) Provide `thinning: '<thin-for-all-variants>'` as part of your `export_options` of `gym` (or in your `options.plist` file if you use raw `xcodebuild`) |
| 62 | + See https://help.apple.com/xcode/mac/11.0/index.html#/devde46df08a |
| 63 | +
|
| 64 | + For builds exported with the `app-store` method, `xcodebuild` won't generate an `app-thinning.plist` file; so you will only be able to get |
| 65 | + the Universal `.ipa` file size as a metric, but won't get the per-device, broken-down install and download sizes for each thinned variant. |
| 66 | +
|
| 67 | + See https://github.com/Automattic/apps-metrics for the API contract expected by the Metrics server you are expected to send those metrics to. |
| 68 | +
|
| 69 | + Tip: If you provide a `file://` URL for the `api_base_url`, the action will write the payload on disk at the specified path instead of sending |
| 70 | + the data to a endpoint over network. This can be useful e.g. to inspect the payload and debug it, or to store the metrics data as CI artefacts. |
| 71 | + DETAILS |
| 72 | + end |
| 73 | + |
| 74 | + def self.available_options |
| 75 | + [ |
| 76 | + FastlaneCore::ConfigItem.new( |
| 77 | + key: :api_base_url, |
| 78 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_API_BASE_URL', |
| 79 | + description: 'The endpoint API URL to publish metrics to. (Note: you can also point to a `file://` URL to write the payload to a file instead)', |
| 80 | + type: String, |
| 81 | + optional: false |
| 82 | + ), |
| 83 | + FastlaneCore::ConfigItem.new( |
| 84 | + key: :api_token, |
| 85 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_API_TOKEN', |
| 86 | + description: 'The bearer token to call the API. Required, unless `api_base_url` is a `file://` URL', |
| 87 | + type: String, |
| 88 | + optional: true |
| 89 | + ), |
| 90 | + FastlaneCore::ConfigItem.new( |
| 91 | + key: :use_gzip_content_encoding, |
| 92 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_USE_GZIP_CONTENT_ENCODING', |
| 93 | + description: 'Specify that we should use `Content-Encoding: gzip` and gzip the body when sending the request', |
| 94 | + type: FastlaneCore::Boolean, |
| 95 | + default_value: true |
| 96 | + ), |
| 97 | + FastlaneCore::ConfigItem.new( |
| 98 | + key: :app_name, |
| 99 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_APP_NAME', |
| 100 | + description: 'The name of the app for which we are publishing metrics, to help filter by app in the dashboard', |
| 101 | + type: String, |
| 102 | + optional: false |
| 103 | + ), |
| 104 | + FastlaneCore::ConfigItem.new( |
| 105 | + key: :app_version, |
| 106 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_APP_VERSION', |
| 107 | + description: 'The version of the app for which we are publishing metrics, to help filter by version in the dashboard', |
| 108 | + type: String, |
| 109 | + optional: false |
| 110 | + ), |
| 111 | + FastlaneCore::ConfigItem.new( |
| 112 | + key: :build_type, |
| 113 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_BUILD_TYPE', |
| 114 | + description: 'The build configuration for which we are publishing metrics, to help filter by build config in the dashboard. E.g. `Debug`, `Release`', |
| 115 | + type: String, |
| 116 | + optional: true |
| 117 | + ), |
| 118 | + FastlaneCore::ConfigItem.new( |
| 119 | + key: :source, |
| 120 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_SOURCE', |
| 121 | + description: 'The type of event at the origin of that build, to help filter data in the dashboard. E.g. `pr`, `beta`, `final-release`', |
| 122 | + type: String, |
| 123 | + optional: true |
| 124 | + ), |
| 125 | + FastlaneCore::ConfigItem.new( |
| 126 | + key: :ipa_path, |
| 127 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_IPA_PATH', |
| 128 | + description: 'The path to the .ipa to extract size information from', |
| 129 | + type: String, |
| 130 | + optional: false, |
| 131 | + default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH], |
| 132 | + verify_block: proc do |value| |
| 133 | + UI.user_error!('You must provide an path to an existing `.ipa` file') unless File.exist?(value) |
| 134 | + end |
| 135 | + ), |
| 136 | + FastlaneCore::ConfigItem.new( |
| 137 | + key: :app_thinning_plist_path, |
| 138 | + env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_APP_THINNING_PLIST_PATH', |
| 139 | + description: 'The path to the `app-thinning.plist` file to extract thinning size information from. ' \ |
| 140 | + + 'By default, will try to use the `app-thinning.plist` file next to the `ipa_path`, if that file exists', |
| 141 | + type: String, |
| 142 | + optional: true, |
| 143 | + default_value_dynamic: true |
| 144 | + ), |
| 145 | + ] |
| 146 | + end |
| 147 | + |
| 148 | + def self.return_type |
| 149 | + :integer |
| 150 | + end |
| 151 | + |
| 152 | + def self.return_value |
| 153 | + 'The HTTP return code from the call. Expect a 201 when new metrics were received successfully and entries created in the database' |
| 154 | + end |
| 155 | + |
| 156 | + def self.authors |
| 157 | + ['automattic'] |
| 158 | + end |
| 159 | + |
| 160 | + def self.is_supported?(platform) |
| 161 | + [:ios, :mac].include? platform |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | +end |
0 commit comments