|
| 1 | +// swift-tools-version: 5.9 |
| 2 | +// The swift-tools-version declares the minimum version of Swift required to build this package. |
| 3 | + |
| 4 | +// Copyright 2024, the Chromium project authors. Please see the AUTHORS file |
| 5 | +// for details. All rights reserved. Use of this source code is governed by a |
| 6 | +// BSD-style license that can be found in the LICENSE file. |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import PackageDescription |
| 10 | + |
| 11 | +enum ConfigurationError: Error { |
| 12 | + case fileNotFound(String) |
| 13 | + case parsingError(String) |
| 14 | + case invalidFormat(String) |
| 15 | +} |
| 16 | + |
| 17 | +let functionsDirectory = String(URL(string: #file)!.deletingLastPathComponent().absoluteString |
| 18 | + .dropLast()) |
| 19 | + |
| 20 | +func loadFirebaseSDKVersion() throws -> String { |
| 21 | + let firebaseCoreScriptPath = NSString.path(withComponents: [ |
| 22 | + functionsDirectory, |
| 23 | + "..", |
| 24 | + "generated_firebase_sdk_version.txt", |
| 25 | + ]) |
| 26 | + do { |
| 27 | + let version = try String(contentsOfFile: firebaseCoreScriptPath, encoding: .utf8) |
| 28 | + .trimmingCharacters(in: .whitespacesAndNewlines) |
| 29 | + return version |
| 30 | + } catch { |
| 31 | + throw ConfigurationError |
| 32 | + .fileNotFound("Error loading or parsing generated_firebase_sdk_version.txt: \(error)") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func loadPubspecVersions() throws -> (packageVersion: String, firebaseCoreVersion: String) { |
| 37 | + let pubspecPath = NSString.path(withComponents: [functionsDirectory, "..", "..", "pubspec.yaml"]) |
| 38 | + do { |
| 39 | + let yamlString = try String(contentsOfFile: pubspecPath, encoding: .utf8) |
| 40 | + let lines = yamlString.split(separator: "\n") |
| 41 | + |
| 42 | + guard let packageVersionLine = lines.first(where: { $0.starts(with: "version:") }) else { |
| 43 | + throw ConfigurationError.invalidFormat("No package version line found in pubspec.yaml") |
| 44 | + } |
| 45 | + var packageVersion = packageVersionLine.split(separator: ":")[1] |
| 46 | + .trimmingCharacters(in: .whitespaces) |
| 47 | + .replacingOccurrences(of: "+", with: "-") |
| 48 | + packageVersion = packageVersion.replacingOccurrences(of: "^", with: "") |
| 49 | + |
| 50 | + guard let firebaseCoreVersionLine = lines.first(where: { $0.contains("firebase_core:") }) else { |
| 51 | + throw ConfigurationError |
| 52 | + .invalidFormat("No firebase_core dependency version line found in pubspec.yaml") |
| 53 | + } |
| 54 | + var firebaseCoreVersion = firebaseCoreVersionLine.split(separator: ":")[1] |
| 55 | + .trimmingCharacters(in: .whitespaces) |
| 56 | + firebaseCoreVersion = firebaseCoreVersion.replacingOccurrences(of: "^", with: "") |
| 57 | + |
| 58 | + return (packageVersion, firebaseCoreVersion) |
| 59 | + } catch { |
| 60 | + throw ConfigurationError.fileNotFound("Error loading or parsing pubspec.yaml: \(error)") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +let library_version: String |
| 65 | +let firebase_sdk_version_string: String |
| 66 | +let firebase_core_version_string: String |
| 67 | +let shared_spm_tag = "-firebase-core-swift" |
| 68 | + |
| 69 | +do { |
| 70 | + library_version = try loadPubspecVersions().packageVersion |
| 71 | + firebase_sdk_version_string = try loadFirebaseSDKVersion() |
| 72 | + firebase_core_version_string = try loadPubspecVersions().firebaseCoreVersion |
| 73 | +} catch { |
| 74 | + fatalError("Failed to load configuration: \(error)") |
| 75 | +} |
| 76 | + |
| 77 | +guard let firebase_sdk_version = Version(firebase_sdk_version_string) else { |
| 78 | + fatalError("Invalid Firebase SDK version: \(firebase_sdk_version_string)") |
| 79 | +} |
| 80 | + |
| 81 | +guard let shared_spm_version = Version("\(firebase_core_version_string)\(shared_spm_tag)") else { |
| 82 | + fatalError("Invalid firebase_core version: \(firebase_core_version_string)\(shared_spm_tag)") |
| 83 | +} |
| 84 | + |
| 85 | +let package = Package( |
| 86 | + name: "cloud_functions", |
| 87 | + platforms: [ |
| 88 | + .iOS("13.0"), |
| 89 | + ], |
| 90 | + products: [ |
| 91 | + .library(name: "cloud-functions", targets: ["cloud_functions"]), |
| 92 | + ], |
| 93 | + dependencies: [ |
| 94 | + .package(url: "https://github.com/firebase/firebase-ios-sdk", from: firebase_sdk_version), |
| 95 | + .package(url: "https://github.com/firebase/flutterfire", exact: shared_spm_version), |
| 96 | + ], |
| 97 | + targets: [ |
| 98 | + .target( |
| 99 | + name: "cloud_functions", |
| 100 | + dependencies: [ |
| 101 | + .product(name: "FirebaseFunctions", package: "firebase-ios-sdk"), |
| 102 | + // Wrapper dependency |
| 103 | + .product(name: "firebase-core-shared", package: "flutterfire"), |
| 104 | + ], |
| 105 | + resources: [ |
| 106 | + .process("Resources"), |
| 107 | + ], |
| 108 | + cSettings: [ |
| 109 | + .headerSearchPath("include"), |
| 110 | + .define("LIBRARY_VERSION", to: "\"\(library_version)\""), |
| 111 | + .define("LIBRARY_NAME", to: "\"flutter-fire-fn\""), |
| 112 | + ] |
| 113 | + ), |
| 114 | + ] |
| 115 | +) |
0 commit comments