Skip to content

Commit b79d090

Browse files
committed
SHA for submodules
1 parent c19be39 commit b79d090

File tree

6 files changed

+75
-76
lines changed

6 files changed

+75
-76
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,4 @@ fastlane/screenshots
7979
fastlane/test_output
8080
fastlane/FastlaneRunner
8181

82-
ConfigOverride.xcconfig
83-
84-
branch.txt
82+
ConfigOverride.xcconfig

Trio.xcodeproj/project.pbxproj

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,7 +3153,6 @@
31533153
buildConfigurationList = 388E596725AD948E0019842D /* Build configuration list for PBXNativeTarget "Trio" */;
31543154
buildPhases = (
31553155
3811DEF525CA169200A708ED /* Swiftformat */,
3156-
DD88C8E02C4D716400F2D558 /* Run Script: get branch name and commit ID */,
31573156
388E595425AD948C0019842D /* Sources */,
31583157
388E595525AD948C0019842D /* Frameworks */,
31593158
388E595625AD948C0019842D /* Resources */,
@@ -3477,24 +3476,6 @@
34773476
shellPath = /bin/sh;
34783477
shellScript = "\"${SRCROOT}/scripts/capture-build-details.sh\"\n";
34793478
};
3480-
DD88C8E02C4D716400F2D558 /* Run Script: get branch name and commit ID */ = {
3481-
isa = PBXShellScriptBuildPhase;
3482-
buildActionMask = 2147483647;
3483-
files = (
3484-
);
3485-
inputFileListPaths = (
3486-
);
3487-
inputPaths = (
3488-
);
3489-
name = "Run Script: get branch name and commit ID";
3490-
outputFileListPaths = (
3491-
);
3492-
outputPaths = (
3493-
);
3494-
runOnlyForDeploymentPostprocessing = 0;
3495-
shellPath = /bin/sh;
3496-
shellScript = "# Prints a message\necho \"writing BRANCH to branch.txt\"\n\n# Retrieves version, branch, and tag information from Git\ngit_version=$(git log -1 --format=\"%h\" --abbrev=7)\ngit_branch=$(git symbolic-ref --short -q HEAD)\ngit_tag=$(git describe --tags --exact-match 2>/dev/null)\n\n# Determines branch or tag information\ngit_branch_or_tag=\"${git_branch:-${git_tag}}\"\ngit_branch_or_tag_version=\"${git_branch_or_tag} ${git_version}\"\n\necho \"BRANCH = ${git_branch_or_tag_version}\" > \"./branch.txt\"\n\n# Prints a message about the working directory and branch.txt\necho \"branch.txt is created/modified in ${PWD}\"\n";
3497-
};
34983479
/* End PBXShellScriptBuildPhase section */
34993480

35003481
/* Begin PBXSourcesBuildPhase section */

Trio/Sources/APS/APSManager.swift

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -872,31 +872,11 @@ final class BaseAPSManager: APSManager, Injectable {
872872
}
873873
let af = pref.adjustmentFactor
874874
let insulin_type = pref.curve
875-
// let buildDate = Bundle.main.buildDate // TODO: fix this
875+
let buildDate = BuildDetails.default.buildDate()
876876
let version = Bundle.main.releaseVersionNumber
877877
let build = Bundle.main.buildVersionNumber
878878

879-
// Read branch information from branch.txt instead of infoDictionary
880-
var branch = "Unknown"
881-
if let branchFileURL = Bundle.main.url(forResource: "branch", withExtension: "txt"),
882-
let branchFileContent = try? String(contentsOf: branchFileURL)
883-
{
884-
let lines = branchFileContent.components(separatedBy: .newlines)
885-
for line in lines {
886-
let components = line.components(separatedBy: "=")
887-
if components.count == 2 {
888-
let key = components[0].trimmingCharacters(in: .whitespaces)
889-
let value = components[1].trimmingCharacters(in: .whitespaces)
890-
891-
if key == "BRANCH" {
892-
branch = value
893-
break
894-
}
895-
}
896-
}
897-
} else {
898-
branch = "Unknown"
899-
}
879+
var branch = BuildDetails.default.branchAndSha
900880

901881
let copyrightNotice_ = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
902882
let pump_ = pumpManager?.localizedTitle ?? ""
@@ -930,7 +910,7 @@ final class BaseAPSManager: APSManager, Injectable {
930910
Build_Number: build ?? "1",
931911
Branch: branch,
932912
CopyRightNotice: String(copyrightNotice_.prefix(32)),
933-
Build_Date: Date(), // TODO: fix this
913+
Build_Date: buildDate ?? Date(),
934914
Algorithm: algo_,
935915
AdjustmentFactor: af,
936916
Pump: pump_,

Trio/Sources/Application/TrioApp.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ import Swinject
6363
}
6464

6565
init() {
66+
let submodulesInfo = BuildDetails.default.submodules.map { key, value in
67+
"\(key): \(value.branch) \(value.commitSHA)"
68+
}.joined(separator: ", ")
69+
6670
debug(
6771
.default,
68-
"Trio Started: v\(Bundle.main.releaseVersionNumber ?? "")(\(Bundle.main.buildVersionNumber ?? "")) [buildDate: \(String(describing: BuildDetails.default.buildDate()))] [buildExpires: \(String(describing: BuildDetails.default.calculateExpirationDate()))]"
72+
"Trio Started: v\(Bundle.main.releaseVersionNumber ?? "")(\(Bundle.main.buildVersionNumber ?? "")) [buildDate: \(String(describing: BuildDetails.default.buildDate()))] [buildExpires: \(String(describing: BuildDetails.default.calculateExpirationDate()))] [submodules: \(submodulesInfo)]"
6973
)
7074

7175
// Setup up the Core Data Stack

Trio/Sources/Helpers/BuildDetails.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
//
2-
// BuildDetails.swift
3-
// Trio
4-
//
5-
// Created by Jonas Björkert on 2024-05-09.
6-
//
71
import Foundation
82

93
class BuildDetails {
@@ -14,7 +8,7 @@ class BuildDetails {
148
init() {
159
guard let url = Bundle.main.url(forResource: "BuildDetails", withExtension: "plist"),
1610
let data = try? Data(contentsOf: url),
17-
let parsed = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
11+
let parsed = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any]
1812
else {
1913
dict = [:]
2014
return
@@ -32,6 +26,21 @@ class BuildDetails {
3226
return "\(branch) \(sha)"
3327
}
3428

29+
/// Returns a dictionary of submodule details.
30+
/// The keys are the submodule names, and the values are tuples (branch, commitSHA).
31+
var submodules: [String: (branch: String, commitSHA: String)] {
32+
guard let subs = dict["com-trio-submodules"] as? [String: [String: Any]] else {
33+
return [:]
34+
}
35+
var result = [String: (branch: String, commitSHA: String)]()
36+
for (name, info) in subs {
37+
let branch = info["branch"] as? String ?? String(localized: "Unknown")
38+
let commitSHA = info["commit_sha"] as? String ?? String(localized: "Unknown")
39+
result[name] = (branch: branch, commitSHA: commitSHA)
40+
}
41+
return result
42+
}
43+
3544
// Determine if the build is from TestFlight
3645
func isTestFlightBuild() -> Bool {
3746
#if targetEnvironment(simulator)

scripts/capture-build-details.sh

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,63 @@
33
# Trio
44
#
55
# Created by Jonas Björkert on 2024-05-08.
6-
# Enable debugging if needed
7-
#set -x
6+
7+
# Path to BuildDetails.plist in the built product
88
info_plist_path="${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/BuildDetails.plist"
9+
910
# Ensure the path to BuildDetails.plist is valid.
10-
if [ "${info_plist_path}" == "/" -o ! -e "${info_plist_path}" ]; then
11+
if [ "${info_plist_path}" = "/" -o ! -e "${info_plist_path}" ]; then
1112
echo "BuildDetails.plist file does not exist at path: ${info_plist_path}" >&2
1213
exit 1
13-
else
14-
echo "Gathering build details..."
15-
# Capture the current date and write it to BuildDetails.plist
16-
plutil -replace com-trio-build-date -string "$(date)" "${info_plist_path}"
14+
fi
15+
16+
echo "Gathering build details..."
17+
18+
# Capture the current date
19+
plutil -replace com-trio-build-date -string "$(date -u '+%a %b %e %H:%M:%S UTC %Y')" "${info_plist_path}"
1720

18-
# Retrieve the current branch, if available
19-
git_branch=$(git symbolic-ref --short -q HEAD || echo "")
21+
# --- Root repo details ---
22+
# Retrieve current branch (or tag) and commit SHA.
23+
git_branch=$(git symbolic-ref --short -q HEAD || echo "")
24+
git_tag=$(git describe --tags --exact-match 2>/dev/null || echo "")
25+
git_commit_sha=$(git log -1 --format="%h" --abbrev=7)
26+
git_branch_or_tag="${git_branch:-${git_tag}}"
27+
if [ -z "${git_branch_or_tag}" ]; then
28+
git_branch_or_tag="detached"
29+
fi
2030

21-
# Attempt to retrieve the current tag
22-
git_tag=$(git describe --tags --exact-match 2>/dev/null || echo "")
31+
plutil -replace com-trio-branch -string "${git_branch_or_tag}" "${info_plist_path}"
32+
plutil -replace com-trio-commit-sha -string "${git_commit_sha}" "${info_plist_path}"
2333

24-
# Retrieve the current SHA of the latest commit
25-
git_commit_sha=$(git log -1 --format="%h" --abbrev=7)
34+
# --- Submodule details ---
35+
# Remove an existing submodules key if it exists, then create an empty dictionary.
36+
# (Using PlistBuddy, which is available on macOS)
37+
submodules_key="com-trio-submodules"
38+
if /usr/libexec/PlistBuddy -c "Print :${submodules_key}" "${info_plist_path}" 2>/dev/null; then
39+
/usr/libexec/PlistBuddy -c "Delete :${submodules_key}" "${info_plist_path}"
40+
fi
41+
/usr/libexec/PlistBuddy -c "Add :${submodules_key} dict" "${info_plist_path}"
2642

27-
# Determine the branch or tag information, or fallback to SHA if in detached state
28-
git_branch_or_tag="${git_branch:-${git_tag}}"
29-
if [ -z "${git_branch_or_tag}" ]; then
30-
git_branch_or_tag="detached"
31-
fi
43+
# Gather submodule details.
44+
# We use git submodule foreach to output lines in the form:
45+
# submodule_name|branch_or_tag|commit_sha
46+
submodules_info=$(git submodule foreach --quiet '
47+
sub_git_branch=$(git symbolic-ref --short -q HEAD || echo "")
48+
sub_git_tag=$(git describe --tags --exact-match 2>/dev/null || echo "")
49+
sub_git_commit_sha=$(git log -1 --format="%h" --abbrev=7)
50+
sub_git_branch_or_tag="${sub_git_branch:-${sub_git_tag}}"
51+
if [ -z "${sub_git_branch_or_tag}" ]; then
52+
sub_git_branch_or_tag="detached"
53+
fi
54+
echo "$name|$sub_git_branch_or_tag|$sub_git_commit_sha"
55+
')
3256

33-
# Update BuildDetails.plist with the branch or tag information
34-
plutil -replace com-trio-branch -string "${git_branch_or_tag}" "${info_plist_path}"
57+
# For each line, add a dictionary entry for that submodule.
58+
echo "${submodules_info}" | while IFS="|" read -r submodule_name sub_branch sub_sha; do
59+
# Create a dictionary for this submodule
60+
/usr/libexec/PlistBuddy -c "Add :${submodules_key}:${submodule_name} dict" "${info_plist_path}"
61+
/usr/libexec/PlistBuddy -c "Add :${submodules_key}:${submodule_name}:branch string ${sub_branch}" "${info_plist_path}"
62+
/usr/libexec/PlistBuddy -c "Add :${submodules_key}:${submodule_name}:commit_sha string ${sub_sha}" "${info_plist_path}"
63+
done
3564

36-
# Update BuildDetails.plist with the SHA information
37-
plutil -replace com-trio-commit-sha -string "${git_commit_sha}" "${info_plist_path}"
38-
fi
65+
echo "BuildDetails.plist has been updated at: ${info_plist_path}"

0 commit comments

Comments
 (0)