Skip to content

Commit

Permalink
refactor: App version shared and moved to Core, abstracted from UIKit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Mar 13, 2024
1 parent 4433608 commit 0d2e2b1
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 18 deletions.
43 changes: 26 additions & 17 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Alamofire/Alamofire",
"state" : {
"revision" : "3dc6a42c7727c49bf26508e29b0a0b35f9c7e1ad",
"version" : "5.8.1"
"revision" : "723fa5a6c65812aec4a0d7cc432ee198883b6e00",
"version" : "5.9.0"
}
},
{
"identity" : "cocoalumberjack",
"kind" : "remoteSourceControl",
"location" : "https://github.com/CocoaLumberjack/CocoaLumberjack",
"state" : {
"revision" : "363ed23d19a931809ea834a7d722da830353806a",
"version" : "3.8.2"
"revision" : "4b8714a7fb84d42393314ce897127b3939885ec3",
"version" : "3.8.5"
}
},
{
Expand All @@ -32,53 +32,62 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Infomaniak/ios-login",
"state" : {
"revision" : "904c1ac39b4db56212302b464a0b2e023d9b5791",
"version" : "6.0.0"
"revision" : "94dee9d95d92c5fbe152476b78b04985fbddaa7c",
"version" : "6.0.1"
}
},
{
"identity" : "osinfo",
"kind" : "remoteSourceControl",
"location" : "https://github.com/MarcoEidinger/OSInfo.git",
"state" : {
"revision" : "54fd6673923ef5966268918152235b0e15ed057c",
"version" : "1.0.1"
}
},
{
"identity" : "realm-core",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/realm-core.git",
"state" : {
"revision" : "7227d6a447821c28895daa099b6c7cd4c99d461b",
"version" : "13.25.1"
"revision" : "a5e87a39cffdcc591f3203c11cfca68100d0b9a6",
"version" : "13.26.0"
}
},
{
"identity" : "realm-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/realm-swift",
"state" : {
"revision" : "836cc4b8619886f979f8961c3f592a82b0741591",
"version" : "10.45.3"
"revision" : "7a2f7d41ffa409978a6b5e475c2ee57f08222705",
"version" : "10.48.0"
}
},
{
"identity" : "sentry-cocoa",
"kind" : "remoteSourceControl",
"location" : "https://github.com/getsentry/sentry-cocoa",
"state" : {
"revision" : "3b9a8e69ca296bd8cd0e317ad7a448e5daf4a342",
"version" : "8.18.0"
"revision" : "38f4f70d07117b9f958a76b1bff278c2f29ffe0e",
"version" : "8.21.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"location" : "https://github.com/apple/swift-log",
"state" : {
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version" : "1.5.3"
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "zipfoundation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/weichsel/ZIPFoundation.git",
"state" : {
"revision" : "a3f5c2bae0f04b0bce9ef3c4ba6bd1031a0564c4",
"version" : "0.9.17"
"revision" : "b979e8b52c7ae7f3f39fa0182e738e9e7257eb78",
"version" : "0.9.18"
}
}
],
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ let package = Package(
.package(url: "https://github.com/getsentry/sentry-cocoa", .upToNextMajor(from: "8.18.0")),
.package(url: "https://github.com/realm/realm-swift", .upToNextMajor(from: "10.45.0")),
.package(url: "https://github.com/CocoaLumberjack/CocoaLumberjack", .upToNextMajor(from: "3.8.0")),
.package(url: "https://github.com/weichsel/ZIPFoundation.git", .upToNextMajor(from: "0.9.0"))
.package(url: "https://github.com/weichsel/ZIPFoundation.git", .upToNextMajor(from: "0.9.0")),
.package(url: "https://github.com/MarcoEidinger/OSInfo.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
Expand All @@ -34,6 +35,7 @@ let package = Package(
.product(name: "Sentry", package: "sentry-cocoa"),
.product(name: "RealmSwift", package: "realm-swift"),
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
.product(name: "OSInfo", package: "OSInfo"),
]
),
.testTarget(
Expand Down
36 changes: 36 additions & 0 deletions Sources/InfomaniakCore/Platform/CorePlatform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Infomaniak Core - iOS
Copyright (C) 2024 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation
import OSInfo

public enum CorePlatform {

/// Unified version descriptor for IK platform apps
public static var appVersionLabel: String = {
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
let release = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? ?? "x.x"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String? ?? "x"
let betaRelease = Bundle.main.isRunningInTestFlight ? "beta" : ""

// Returns "macOS" on unmodified iOS and catalyst apps running on macOS
let systemName = OS.current.name

return "\(appName) \(systemName) version \(release)-\(betaRelease)\(build)"
}()
}
72 changes: 72 additions & 0 deletions Sources/InfomaniakCore/Platform/PlatformDetectable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Infomaniak Core - iOS
Copyright (C) 2024 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation

/// Something to help with current running context
public protocol PlatformDetectable {
/// We are running in Mac Catalyst mode
var isMacCatalyst: Bool { get }

/// We are running an iOS App on Mac
var isiOSAppOnMac: Bool { get }

/// We are running on Mac
var isMac: Bool { get }

/// We are running in extension mode
var isInExtension: Bool { get }

/// We are running a debug build
var isDebug: Bool { get }
}

@available(macOS 10.16, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
public struct PlatformDetector: PlatformDetectable {
public init() {}

public var isMacCatalyst: Bool = {
#if targetEnvironment(macCatalyst)
true
#else
false
#endif
}()

public var isiOSAppOnMac: Bool = ProcessInfo().isiOSAppOnMac

public var isMac: Bool {
isMacCatalyst || isiOSAppOnMac
}

public var isInExtension: Bool = {
guard Bundle.main.bundlePath.hasSuffix(".appex") else {
return false
}

return true
}()

public var isDebug: Bool = {
#if DEBUG
true
#else
false
#endif
}()
}
34 changes: 34 additions & 0 deletions Tests/InfomaniakCoreTests/Platform/UTCorePlatform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Infomaniak Core - iOS
Copyright (C) 2024 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCore
import XCTest

final class UTCorePlatform: XCTestCase {

func testVersionLabel() {
// GIVEN
let expectedPrefix = "xctest macOS version"

// WHEN
let versionLabel = CorePlatform.appVersionLabel

// THEN
XCTAssertTrue(versionLabel.hasPrefix(expectedPrefix))
}
}

0 comments on commit 0d2e2b1

Please sign in to comment.