Skip to content

Commit 56ac6bb

Browse files
authored
Cache profile expiration date in app and remove hardcoded provisioning profile path (#2226)
1 parent 1aaee2d commit 56ac6bb

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

Common/Models/BuildDetails.swift

+38-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class BuildDetails {
1313
static var `default` = BuildDetails()
1414

1515
let dict: [String: Any]
16+
private var cachedProfileExpirationDate: Date?
1617

1718
init() {
1819
guard let url = Bundle.main.url(forResource: "BuildDetails", withExtension: ".plist"),
@@ -23,6 +24,7 @@ class BuildDetails {
2324
return
2425
}
2526
dict = parsed
27+
cachedProfileExpirationDate = loadProfileExpirationDate()
2628
}
2729

2830
var buildDateString: String? {
@@ -46,11 +48,11 @@ class BuildDetails {
4648
}
4749

4850
var profileExpiration: Date? {
49-
return dict["com-loopkit-Loop-profile-expiration"] as? Date
51+
return cachedProfileExpirationDate
5052
}
5153

5254
var profileExpirationString: String {
53-
if let profileExpiration = profileExpiration {
55+
if let profileExpiration = cachedProfileExpirationDate {
5456
return "\(profileExpiration)"
5557
} else {
5658
return "N/A"
@@ -65,5 +67,39 @@ class BuildDetails {
6567
var workspaceGitBranch: String? {
6668
return dict["com-loopkit-LoopWorkspace-git-branch"] as? String
6769
}
70+
71+
private func loadProfileExpirationDate() -> Date? {
72+
guard
73+
let profilePath = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision"),
74+
let profileData = try? Data(contentsOf: URL(fileURLWithPath: profilePath)),
75+
let profileNSString = NSString(data: profileData, encoding: String.Encoding.ascii.rawValue)
76+
else {
77+
print(
78+
"WARNING: Could not find or read `embedded.mobileprovision`. If running on Simulator, there are no provisioning profiles."
79+
)
80+
return nil
81+
}
82+
83+
let regexPattern = "<key>ExpirationDate</key>[\\W]*?<date>(.*?)</date>"
84+
guard let regex = try? NSRegularExpression(pattern: regexPattern, options: []),
85+
let match = regex.firstMatch(
86+
in: profileNSString as String,
87+
options: [],
88+
range: NSRange(location: 0, length: profileNSString.length)
89+
),
90+
let range = Range(match.range(at: 1), in: profileNSString as String)
91+
else {
92+
print("Warning: Could not create regex or find match.")
93+
return nil
94+
}
95+
96+
let dateString = String(profileNSString.substring(with: NSRange(range, in: profileNSString as String)))
97+
let dateFormatter = DateFormatter()
98+
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
99+
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
100+
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
101+
102+
return dateFormatter.date(from: dateString)
103+
}
68104
}
69105

Scripts/capture-build-details.sh

-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ info() {
2525
}
2626

2727
info_plist_path="${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/BuildDetails.plist"
28-
provisioning_profile_path="${HOME}/Library/MobileDevice/Provisioning Profiles/${EXPANDED_PROVISIONING_PROFILE}.mobileprovision"
2928
xcode_build_version=${XCODE_PRODUCT_BUILD_VERSION:-$(xcodebuild -version | grep version | cut -d ' ' -f 3)}
3029
while [[ $# -gt 0 ]]
3130
do
@@ -34,10 +33,6 @@ do
3433
info_plist_path="${2}"
3534
shift 2
3635
;;
37-
-p|--provisioning-profile-path)
38-
provisioning_profile_path="${2}"
39-
shift 2
40-
;;
4136
esac
4237
done
4338

@@ -67,15 +62,6 @@ plutil -replace com-loopkit-Loop-srcroot -string "${PWD}" "${info_plist_path}"
6762
plutil -replace com-loopkit-Loop-build-date -string "$(date)" "${info_plist_path}"
6863
plutil -replace com-loopkit-Loop-xcode-version -string "${xcode_build_version}" "${info_plist_path}"
6964

70-
if [ -e "${provisioning_profile_path}" ]; then
71-
profile_expire_date=$(security cms -D -i "${provisioning_profile_path}" | plutil -p - | grep ExpirationDate | cut -b 23-)
72-
# Convert to plutil format
73-
profile_expire_date=$(date -j -f "%Y-%m-%d %H:%M:%S" "${profile_expire_date}" +"%Y-%m-%dT%H:%M:%SZ")
74-
plutil -replace com-loopkit-Loop-profile-expiration -date "${profile_expire_date}" "${info_plist_path}"
75-
else
76-
warn "Invalid provisioning profile path ${provisioning_profile_path}"
77-
fi
78-
7965
# determine if this is a workspace build
8066
# if so, fill out the git revision and branch
8167
if [ -e ../.git ]

0 commit comments

Comments
 (0)