From f4b722bd2da78a2d7ef8cb0a63ab9218cc939d71 Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Thu, 19 Sep 2024 13:44:50 -0700 Subject: [PATCH] Implement userAgent for Data Connect (#8) --- .github/workflows/spm.yml | 2 ++ Package.swift | 19 +++++++++++---- Sources/Internal/Component.swift | 23 ++++++++++++++++++ Sources/Internal/Version.swift | 20 ++++++++++++++++ Tests/Unit/UserAgentTests.swift | 40 ++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 Sources/Internal/Component.swift create mode 100644 Sources/Internal/Version.swift create mode 100644 Tests/Unit/UserAgentTests.swift diff --git a/.github/workflows/spm.yml b/.github/workflows/spm.yml index cd856ff..a6b28f8 100644 --- a/.github/workflows/spm.yml +++ b/.github/workflows/spm.yml @@ -33,6 +33,8 @@ jobs: target: [iOS, tvOS, macOS, catalyst, visionOS] xcode: [Xcode_15.2, Xcode_15.4, Xcode_16_Release_Candidate] runs-on: ${{ matrix.os }} + env: + FIREBASE_MAIN: 1 steps: - uses: actions/checkout@v4 - name: Xcode diff --git a/Package.swift b/Package.swift index e683f4c..6be7ef6 100644 --- a/Package.swift +++ b/Package.swift @@ -19,8 +19,6 @@ import class Foundation.ProcessInfo import PackageDescription -// let firebaseVersion = "10.25.0" - let package = Package( name: "FirebaseDataConnect", platforms: [.iOS(.v12), .macCatalyst(.v13), .macOS(.v10_15), .tvOS(.v13), .watchOS(.v7)], @@ -31,8 +29,7 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/firebase/firebase-ios-sdk", - from: "11.0.0"), + firebaseDependency(), .package( url: "https://github.com/grpc/grpc-swift.git", from: "1.19.1" // TODO: Constrain to a range at time of release @@ -43,6 +40,8 @@ let package = Package( name: "FirebaseDataConnect", dependencies: [ .product(name: "GRPC", package: "grpc-swift"), + .product(name: "FirebaseCore", package: "firebase-ios-sdk"), + // TODO: Investigate switching Auth and AppCheck to interop. .product(name: "FirebaseAuth", package: "firebase-ios-sdk"), .product(name: "FirebaseAppCheck", package: "firebase-ios-sdk"), @@ -56,3 +55,15 @@ let package = Package( ), ] ) + +func firebaseDependency() -> Package.Dependency { + let firebaseURL = "https://github.com/firebase/firebase-ios-sdk" + + // Point SPM CI to the tip of main of https://github.com/firebase/firebase-ios-sdk so that the + // release process can defer publishing the GoogleAppMeasurement tag until after testing. + if ProcessInfo.processInfo.environment["FIREBASE_MAIN"] != nil { + return .package(url: firebaseURL, branch: "main") + } + + return .package(url: firebaseURL, exact: "11.3.0") +} diff --git a/Sources/Internal/Component.swift b/Sources/Internal/Component.swift new file mode 100644 index 0000000..869196c --- /dev/null +++ b/Sources/Internal/Component.swift @@ -0,0 +1,23 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import Foundation + +/// Class for registration with the Firebase component system, including userAgent functionality. +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) +@objc(FIRDataConnectComponent) class DataConnectComponent: NSObject { + @objc class func sdkVersion() -> String { + return Version.version + } +} diff --git a/Sources/Internal/Version.swift b/Sources/Internal/Version.swift new file mode 100644 index 0000000..cbcb7c6 --- /dev/null +++ b/Sources/Internal/Version.swift @@ -0,0 +1,20 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import Foundation + +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) +struct Version { + static let version = "11.3.0-beta" +} diff --git a/Tests/Unit/UserAgentTests.swift b/Tests/Unit/UserAgentTests.swift new file mode 100644 index 0000000..c186055 --- /dev/null +++ b/Tests/Unit/UserAgentTests.swift @@ -0,0 +1,40 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import Foundation +import XCTest + +import FirebaseCore +@testable import FirebaseDataConnect + +final class UserAgentTests: XCTestCase { + static var options: FirebaseOptions = { + let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000", + gcmSenderID: "00000000000000000-00000000000-000000000") + options.projectID = "user-agent-test" + options.apiKey = "testUserAgentDummyApiKey" + return options + }() + + override class func setUp() { + FirebaseApp.configure(name: "user-agent", options: options) + } + + /// Confirm that Data Connect gets added to the user agent. + func testUserAgent() { + let userAgent = FirebaseApp.firebaseUserAgent() + let version = Version.version + XCTAssertTrue(userAgent.contains("fire-dc/\(version)")) + } +}