From 2706487604996107fbc795f34e60dc6f2a5eab46 Mon Sep 17 00:00:00 2001 From: marionbarker Date: Fri, 20 Sep 2024 07:28:31 -0700 Subject: [PATCH] Cache profile expiration date in app and remove hardcoded provisioning profile path --- .../Diagnostics/BuildDetails.swift | 40 ++++++++++++++++++- .../Scripts/capture-build-details.sh | 14 ------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/LoopCaregiver/LoopCaregiver/Diagnostics/BuildDetails.swift b/LoopCaregiver/LoopCaregiver/Diagnostics/BuildDetails.swift index f14ea462..0d930790 100644 --- a/LoopCaregiver/LoopCaregiver/Diagnostics/BuildDetails.swift +++ b/LoopCaregiver/LoopCaregiver/Diagnostics/BuildDetails.swift @@ -11,6 +11,7 @@ class BuildDetails { static var `default` = BuildDetails() let dict: [String: Any] + private var cachedProfileExpirationDate: Date? init() { guard let url = Bundle.main.url(forResource: "BuildDetails", withExtension: ".plist"), @@ -20,6 +21,7 @@ class BuildDetails { return } dict = parsed + cachedProfileExpirationDate = loadProfileExpirationDate() } var buildDateString: String? { @@ -43,11 +45,11 @@ class BuildDetails { } var profileExpiration: Date? { - return dict["com-app-profile-expiration"] as? Date + return cachedProfileExpirationDate } var profileExpirationString: String { - if let profileExpiration { + if let profileExpiration = cachedProfileExpirationDate { return "\(profileExpiration)" } else { return "N/A" @@ -62,4 +64,38 @@ class BuildDetails { var workspaceGitBranch: String? { return dict["com-app-workspace-git-branch"] as? String } + + private func loadProfileExpirationDate() -> Date? { + guard + let profilePath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision"), + let profileData = try? Data(contentsOf: URL(fileURLWithPath: profilePath)), + let profileNSString = NSString(data: profileData, encoding: String.Encoding.ascii.rawValue) + else { + print( + "WARNING: Could not find or read `embedded.mobileprovision`. If running on Simulator, there are no provisioning profiles." + ) + return nil + } + + let regexPattern = "ExpirationDate[\\W]*?(.*?)" + guard let regex = try? NSRegularExpression(pattern: regexPattern, options: []), + let match = regex.firstMatch( + in: profileNSString as String, + options: [], + range: NSRange(location: 0, length: profileNSString.length) + ), + let range = Range(match.range(at: 1), in: profileNSString as String) + else { + print("Warning: Could not create regex or find match.") + return nil + } + + let dateString = String(profileNSString.substring(with: NSRange(range, in: profileNSString as String))) + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" + dateFormatter.locale = Locale(identifier: "en_US_POSIX") + dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) + + return dateFormatter.date(from: dateString) + } } diff --git a/LoopCaregiver/Scripts/capture-build-details.sh b/LoopCaregiver/Scripts/capture-build-details.sh index 44f58f2f..d3b315c9 100755 --- a/LoopCaregiver/Scripts/capture-build-details.sh +++ b/LoopCaregiver/Scripts/capture-build-details.sh @@ -25,7 +25,6 @@ info() { } info_plist_path="${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/BuildDetails.plist" -provisioning_profile_path="${HOME}/Library/MobileDevice/Provisioning Profiles/${EXPANDED_PROVISIONING_PROFILE}.mobileprovision" xcode_build_version=${XCODE_PRODUCT_BUILD_VERSION:-$(xcodebuild -version | grep version | cut -d ' ' -f 3)} while [[ $# -gt 0 ]] do @@ -34,10 +33,6 @@ do info_plist_path="${2}" shift 2 ;; - -p|--provisioning-profile-path) - provisioning_profile_path="${2}" - shift 2 - ;; esac done @@ -67,15 +62,6 @@ plutil -replace com-app-srcroot -string "${PWD}" "${info_plist_path}" plutil -replace com-app-build-date -string "$(date)" "${info_plist_path}" plutil -replace com-app-xcode-version -string "${xcode_build_version}" "${info_plist_path}" -if [ -e "${provisioning_profile_path}" ]; then - profile_expire_date=$(security cms -D -i "${provisioning_profile_path}" | plutil -p - | grep ExpirationDate | cut -b 23-) - # Convert to plutil format - profile_expire_date=$(date -j -f "%Y-%m-%d %H:%M:%S" "${profile_expire_date}" +"%Y-%m-%dT%H:%M:%SZ") - plutil -replace com-app-profile-expiration -date "${profile_expire_date}" "${info_plist_path}" -else - warn "Invalid provisioning profile path ${provisioning_profile_path}" -fi - # determine if this is a workspace build # if so, fill out the git revision and branch if [ -e ../.git ]