-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// | ||
// Globals.swift | ||
// OTCore | ||
// | ||
// Created by Steffan Andrews on 2021-01-12. | ||
// Copyright © 2021 Steffan Andrews. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
#if os(iOS) || os(tvOS) || os(watchOS) | ||
import UIKit | ||
#endif | ||
|
||
// MARK: - Global bundle properties | ||
|
||
/// OTCore: | ||
/// Global convenience properties | ||
public enum Globals { | ||
|
||
/// General main bundle-related | ||
public struct bundle { | ||
|
||
/// OTCore: | ||
/// Returns the name of the app | ||
public static let name: String = Bundle.mainInfoDictionary(key: kCFBundleNameKey) ?? "" | ||
|
||
/// OTCore: | ||
/// Returns the app's bundle ID | ||
public static let bundleID: String = Bundle.main.bundleIdentifier ?? "" | ||
|
||
/// OTCore: | ||
/// Returns the value of the app bundle's `CFBundleShortVersionString` key | ||
public static let versionShort: String = Bundle.mainInfoDictionary(key: "CFBundleShortVersionString" as CFString) ?? "" | ||
|
||
/// OTCore: | ||
/// Returns the major version number from the value of the app bundle's `CFBundleShortVersionString` key | ||
public static let versionMajor: Int = Int(versionShort.components(separatedBy: ".").first ?? "0") ?? 0 | ||
|
||
/// OTCore: | ||
/// Returns the value from the app bundle's `kCFBundleVersionKey` key | ||
public static let versionBuildNumber: String = Bundle.mainInfoDictionary(key: kCFBundleVersionKey) ?? "" | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
// MARK: - Global system properties | ||
|
||
extension Globals { | ||
|
||
/// OTCore: | ||
/// General system-related | ||
public enum system { | ||
|
||
#if os(macOS) | ||
/// OTCore: | ||
/// Local Mac computer name | ||
public static var name: String { | ||
Host.current().localizedName ?? "" | ||
} | ||
#elseif os(iOS) || os(tvOS) | ||
/// OTCore: | ||
/// Local device name | ||
public static var name: String { | ||
UIDevice.current.name | ||
} | ||
#endif | ||
|
||
#if os(macOS) | ||
|
||
/// OTCore: | ||
/// Returns local Mac's mainboard serial number. | ||
/// (Computes lazily, once upon first access and retains value persistently until app quits.) | ||
public static let serialNumber: String? = Self.getSysInfoString(key: kIOPlatformSerialNumberKey) | ||
|
||
/// OTCore: | ||
/// Returns local Mac's hardware UUID string. | ||
/// (Computes lazily, once upon first access and retains value persistently until app quits.) | ||
public static let hardwareUUID: String? = Self.getSysInfoString(key: kIOPlatformUUIDKey) | ||
|
||
/// Internal use. | ||
internal static func getSysInfoString(key: String) -> String? { | ||
|
||
let platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) | ||
|
||
defer { | ||
IOObjectRelease(platformExpert) | ||
} | ||
|
||
guard platformExpert > 0 else { | ||
return nil | ||
} | ||
|
||
guard let serialNumber = | ||
IORegistryEntryCreateCFProperty(platformExpert, | ||
key as CFString, | ||
kCFAllocatorDefault, | ||
0) | ||
.takeUnretainedValue() as? String | ||
else { | ||
return nil | ||
} | ||
|
||
return serialNumber.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) | ||
|
||
} | ||
|
||
#endif | ||
|
||
} | ||
} | ||
|
||
extension Bundle { | ||
|
||
/// OTCore: | ||
/// Convenience function to get bundle data | ||
public class func mainInfoDictionary(key: String) -> String? { | ||
self.main.infoDictionary?[key] as? String | ||
} | ||
|
||
/// OTCore: | ||
/// Convenience function to get bundle data | ||
public class func mainInfoDictionary(key: CFString) -> String? { | ||
self.main.infoDictionary?[key as String] as? String | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Globals Tests.swift | ||
// OTCore | ||
// | ||
// Created by Steffan Andrews on 2021-01-12. | ||
// Copyright © 2021 Steffan Andrews. All rights reserved. | ||
// | ||
|
||
#if !os(watchOS) | ||
|
||
import XCTest | ||
@testable import OTCore | ||
|
||
class Globals_Tests: XCTestCase { | ||
|
||
override func setUp() { super.setUp() } | ||
override func tearDown() { super.tearDown() } | ||
|
||
func testBundle() { | ||
|
||
XCTAssertEqual(Globals.bundle.name, "xctest") | ||
XCTAssertEqual(Globals.bundle.bundleID, "com.apple.dt.xctest.tool") | ||
_ = Globals.bundle.versionShort // XCTest doesn't return a value | ||
XCTAssertEqual(Globals.bundle.versionMajor, 0) // XCTest doesn't return a value | ||
XCTAssertTrue(Globals.bundle.versionBuildNumber != "") | ||
|
||
} | ||
|
||
func testSystem() { | ||
|
||
// values cannot be tested explicitly since they vary by system | ||
|
||
XCTAssertTrue(Globals.system.name != "") | ||
|
||
#if os(macOS) | ||
XCTAssertNotNil(Globals.system.serialNumber) | ||
XCTAssertNotNil(Globals.system.hardwareUUID) | ||
#endif | ||
|
||
} | ||
|
||
} | ||
|
||
#endif |