|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import ArgumentParser |
| 18 | +import Foundation |
| 19 | +import Utils |
| 20 | + |
| 21 | +private enum Constants {} |
| 22 | + |
| 23 | +extension Constants { |
| 24 | + // Binary Size Metrics flag for the Metrics Service. |
| 25 | + static let metric = "BinarySize" |
| 26 | + static let cocoapodSizeReportFile = "binary_report.json" |
| 27 | +} |
| 28 | + |
| 29 | +/// Pod Config |
| 30 | +struct PodConfigs: Codable { |
| 31 | + let pods: [Pod] |
| 32 | +} |
| 33 | + |
| 34 | +struct Pod: Codable { |
| 35 | + let sdk: String |
| 36 | + let path: String |
| 37 | +} |
| 38 | + |
| 39 | +/// Cocoapods-size tool report, |
| 40 | +struct SDKBinaryReport: Codable { |
| 41 | + let combinedPodsExtraSize: Int |
| 42 | + |
| 43 | + enum CodingKeys: String, CodingKey { |
| 44 | + case combinedPodsExtraSize = "combined_pods_extra_size" |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Metrics Service API request data |
| 49 | +public struct BinaryMetricsReport: Codable { |
| 50 | + let metric: String |
| 51 | + let results: [Result] |
| 52 | + let log: String |
| 53 | +} |
| 54 | + |
| 55 | +public struct Result: Codable { |
| 56 | + let sdk, type: String |
| 57 | + let value: Int |
| 58 | +} |
| 59 | + |
| 60 | +extension BinaryMetricsReport { |
| 61 | + func toData() -> Data { |
| 62 | + let jsonData = try! JSONEncoder().encode(self) |
| 63 | + return jsonData |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func CreatePodConfigJSON(of sdks: [String], from sdk_dir: URL) throws { |
| 68 | + var pods: [Pod] = [] |
| 69 | + for sdk in sdks { |
| 70 | + let pod: Pod = Pod(sdk: sdk, path: sdk_dir.path) |
| 71 | + pods.append(pod) |
| 72 | + } |
| 73 | + let podConfigs: PodConfigs = PodConfigs(pods: pods) |
| 74 | + try JSONParser.writeJSON(of: podConfigs, to: "./cocoapods_source_config.json") |
| 75 | +} |
| 76 | + |
| 77 | +// Create a JSON format data prepared to send to the Metrics Service. |
| 78 | +func CreateMetricsRequestData(of sdks: [String], type: String, |
| 79 | + log: String) throws -> BinaryMetricsReport { |
| 80 | + var reports: [Result] = [] |
| 81 | + // Create a report for each individual SDK and collect all of them into reports. |
| 82 | + for sdk in sdks { |
| 83 | + // Create a report, generated by cocoapods-size, for each SDK. `.stdout` is used |
| 84 | + // since `pipe` could cause the tool to hang. That is probably caused by cocopod-size |
| 85 | + // is using pipe and a pipe is shared by multiple parent/child process and cause |
| 86 | + // deadlock. `.stdout` is a quick way to resolve at the moment. The difference is |
| 87 | + // that `.stdout` will print out logs in the console while pipe can assign logs a |
| 88 | + // variable. |
| 89 | + Shell.run( |
| 90 | + "cd cocoapods-size && python3 measure_cocoapod_size.py --cocoapods \(sdk) --cocoapods_source_config ../cocoapods_source_config.json --json \(Constants.cocoapodSizeReportFile)", |
| 91 | + stdout: .stdout |
| 92 | + ) |
| 93 | + let SDKBinarySize = try JSONParser.readJSON( |
| 94 | + of: SDKBinaryReport.self, |
| 95 | + from: "cocoapods-size/\(Constants.cocoapodSizeReportFile)" |
| 96 | + ) |
| 97 | + // Append reports for data for API request to the Metrics Service. |
| 98 | + reports.append(Result(sdk: sdk, type: type, value: SDKBinarySize.combinedPodsExtraSize)) |
| 99 | + } |
| 100 | + let metricsRequestReport = BinaryMetricsReport( |
| 101 | + metric: Constants.metric, |
| 102 | + results: reports, |
| 103 | + log: log |
| 104 | + ) |
| 105 | + return metricsRequestReport |
| 106 | +} |
| 107 | + |
| 108 | +public func CreateMetricsRequestData(SDK: [String], SDKRepoDir: URL, |
| 109 | + logPath: String) throws -> BinaryMetricsReport? { |
| 110 | + try CreatePodConfigJSON(of: SDK, from: SDKRepoDir) |
| 111 | + let data = try CreateMetricsRequestData( |
| 112 | + of: SDK, |
| 113 | + type: "firebase-ios-sdk-testing", |
| 114 | + log: logPath |
| 115 | + ) |
| 116 | + return data |
| 117 | +} |
0 commit comments