Kaynağa Gözat

SHA for submodules

Jonas Björkert 1 yıl önce
ebeveyn
işleme
b79d0904ba

+ 1 - 3
.gitignore

@@ -79,6 +79,4 @@ fastlane/screenshots
 fastlane/test_output
 fastlane/FastlaneRunner
 
-ConfigOverride.xcconfig
-
-branch.txt
+ConfigOverride.xcconfig

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 19
Trio.xcodeproj/project.pbxproj


+ 3 - 23
Trio/Sources/APS/APSManager.swift

@@ -872,31 +872,11 @@ final class BaseAPSManager: APSManager, Injectable {
             }
             let af = pref.adjustmentFactor
             let insulin_type = pref.curve
-//            let buildDate = Bundle.main.buildDate // TODO: fix this
+            let buildDate = BuildDetails.default.buildDate()
             let version = Bundle.main.releaseVersionNumber
             let build = Bundle.main.buildVersionNumber
 
-            // Read branch information from branch.txt instead of infoDictionary
-            var branch = "Unknown"
-            if let branchFileURL = Bundle.main.url(forResource: "branch", withExtension: "txt"),
-               let branchFileContent = try? String(contentsOf: branchFileURL)
-            {
-                let lines = branchFileContent.components(separatedBy: .newlines)
-                for line in lines {
-                    let components = line.components(separatedBy: "=")
-                    if components.count == 2 {
-                        let key = components[0].trimmingCharacters(in: .whitespaces)
-                        let value = components[1].trimmingCharacters(in: .whitespaces)
-
-                        if key == "BRANCH" {
-                            branch = value
-                            break
-                        }
-                    }
-                }
-            } else {
-                branch = "Unknown"
-            }
+            var branch = BuildDetails.default.branchAndSha
 
             let copyrightNotice_ = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
             let pump_ = pumpManager?.localizedTitle ?? ""
@@ -930,7 +910,7 @@ final class BaseAPSManager: APSManager, Injectable {
                 Build_Number: build ?? "1",
                 Branch: branch,
                 CopyRightNotice: String(copyrightNotice_.prefix(32)),
-                Build_Date: Date(), // TODO: fix this
+                Build_Date: buildDate ?? Date(),
                 Algorithm: algo_,
                 AdjustmentFactor: af,
                 Pump: pump_,

+ 5 - 1
Trio/Sources/Application/TrioApp.swift

@@ -63,9 +63,13 @@ import Swinject
     }
 
     init() {
+        let submodulesInfo = BuildDetails.default.submodules.map { key, value in
+            "\(key): \(value.branch) \(value.commitSHA)"
+        }.joined(separator: ", ")
+
         debug(
             .default,
-            "Trio Started: v\(Bundle.main.releaseVersionNumber ?? "")(\(Bundle.main.buildVersionNumber ?? "")) [buildDate: \(String(describing: BuildDetails.default.buildDate()))] [buildExpires: \(String(describing: BuildDetails.default.calculateExpirationDate()))]"
+            "Trio Started: v\(Bundle.main.releaseVersionNumber ?? "")(\(Bundle.main.buildVersionNumber ?? "")) [buildDate: \(String(describing: BuildDetails.default.buildDate()))] [buildExpires: \(String(describing: BuildDetails.default.calculateExpirationDate()))] [submodules: \(submodulesInfo)]"
         )
 
         // Setup up the Core Data Stack

+ 16 - 7
Trio/Sources/Helpers/BuildDetails.swift

@@ -1,9 +1,3 @@
-//
-//  BuildDetails.swift
-//  Trio
-//
-//  Created by Jonas Björkert on 2024-05-09.
-//
 import Foundation
 
 class BuildDetails {
@@ -14,7 +8,7 @@ class BuildDetails {
     init() {
         guard let url = Bundle.main.url(forResource: "BuildDetails", withExtension: "plist"),
               let data = try? Data(contentsOf: url),
-              let parsed = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]
+              let parsed = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any]
         else {
             dict = [:]
             return
@@ -32,6 +26,21 @@ class BuildDetails {
         return "\(branch) \(sha)"
     }
 
+    /// Returns a dictionary of submodule details.
+    /// The keys are the submodule names, and the values are tuples (branch, commitSHA).
+    var submodules: [String: (branch: String, commitSHA: String)] {
+        guard let subs = dict["com-trio-submodules"] as? [String: [String: Any]] else {
+            return [:]
+        }
+        var result = [String: (branch: String, commitSHA: String)]()
+        for (name, info) in subs {
+            let branch = info["branch"] as? String ?? String(localized: "Unknown")
+            let commitSHA = info["commit_sha"] as? String ?? String(localized: "Unknown")
+            result[name] = (branch: branch, commitSHA: commitSHA)
+        }
+        return result
+    }
+
     // Determine if the build is from TestFlight
     func isTestFlightBuild() -> Bool {
         #if targetEnvironment(simulator)

+ 50 - 23
scripts/capture-build-details.sh

@@ -3,36 +3,63 @@
 #  Trio
 #
 #  Created by Jonas Björkert on 2024-05-08.
-# Enable debugging if needed
-#set -x
+
+# Path to BuildDetails.plist in the built product
 info_plist_path="${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/BuildDetails.plist"
+
 # Ensure the path to BuildDetails.plist is valid.
-if [ "${info_plist_path}" == "/" -o ! -e "${info_plist_path}" ]; then
+if [ "${info_plist_path}" = "/" -o ! -e "${info_plist_path}" ]; then
     echo "BuildDetails.plist file does not exist at path: ${info_plist_path}" >&2
     exit 1
-else
-    echo "Gathering build details..."
-    # Capture the current date and write it to BuildDetails.plist
-    plutil -replace com-trio-build-date -string "$(date)" "${info_plist_path}"
+fi
+
+echo "Gathering build details..."
+
+# Capture the current date
+plutil -replace com-trio-build-date -string "$(date -u '+%a %b %e %H:%M:%S UTC %Y')" "${info_plist_path}"
 
-    # Retrieve the current branch, if available
-    git_branch=$(git symbolic-ref --short -q HEAD || echo "")
+# --- Root repo details ---
+# Retrieve current branch (or tag) and commit SHA.
+git_branch=$(git symbolic-ref --short -q HEAD || echo "")
+git_tag=$(git describe --tags --exact-match 2>/dev/null || echo "")
+git_commit_sha=$(git log -1 --format="%h" --abbrev=7)
+git_branch_or_tag="${git_branch:-${git_tag}}"
+if [ -z "${git_branch_or_tag}" ]; then
+    git_branch_or_tag="detached"
+fi
 
-    # Attempt to retrieve the current tag
-    git_tag=$(git describe --tags --exact-match 2>/dev/null || echo "")
+plutil -replace com-trio-branch -string "${git_branch_or_tag}" "${info_plist_path}"
+plutil -replace com-trio-commit-sha -string "${git_commit_sha}" "${info_plist_path}"
 
-    # Retrieve the current SHA of the latest commit
-    git_commit_sha=$(git log -1 --format="%h" --abbrev=7)
+# --- Submodule details ---
+# Remove an existing submodules key if it exists, then create an empty dictionary.
+# (Using PlistBuddy, which is available on macOS)
+submodules_key="com-trio-submodules"
+if /usr/libexec/PlistBuddy -c "Print :${submodules_key}" "${info_plist_path}" 2>/dev/null; then
+    /usr/libexec/PlistBuddy -c "Delete :${submodules_key}" "${info_plist_path}"
+fi
+/usr/libexec/PlistBuddy -c "Add :${submodules_key} dict" "${info_plist_path}"
 
-    # Determine the branch or tag information, or fallback to SHA if in detached state
-    git_branch_or_tag="${git_branch:-${git_tag}}"
-    if [ -z "${git_branch_or_tag}" ]; then
-        git_branch_or_tag="detached"
-    fi
+# Gather submodule details.
+# We use git submodule foreach to output lines in the form:
+#   submodule_name|branch_or_tag|commit_sha
+submodules_info=$(git submodule foreach --quiet '
+  sub_git_branch=$(git symbolic-ref --short -q HEAD || echo "")
+  sub_git_tag=$(git describe --tags --exact-match 2>/dev/null || echo "")
+  sub_git_commit_sha=$(git log -1 --format="%h" --abbrev=7)
+  sub_git_branch_or_tag="${sub_git_branch:-${sub_git_tag}}"
+  if [ -z "${sub_git_branch_or_tag}" ]; then
+    sub_git_branch_or_tag="detached"
+  fi
+  echo "$name|$sub_git_branch_or_tag|$sub_git_commit_sha"
+')
 
-    # Update BuildDetails.plist with the branch or tag information
-    plutil -replace com-trio-branch -string "${git_branch_or_tag}" "${info_plist_path}"
+# For each line, add a dictionary entry for that submodule.
+echo "${submodules_info}" | while IFS="|" read -r submodule_name sub_branch sub_sha; do
+    # Create a dictionary for this submodule
+    /usr/libexec/PlistBuddy -c "Add :${submodules_key}:${submodule_name} dict" "${info_plist_path}"
+    /usr/libexec/PlistBuddy -c "Add :${submodules_key}:${submodule_name}:branch string ${sub_branch}" "${info_plist_path}"
+    /usr/libexec/PlistBuddy -c "Add :${submodules_key}:${submodule_name}:commit_sha string ${sub_sha}" "${info_plist_path}"
+done
 
-    # Update BuildDetails.plist with the SHA information
-    plutil -replace com-trio-commit-sha -string "${git_commit_sha}" "${info_plist_path}"
-fi
+echo "BuildDetails.plist has been updated at: ${info_plist_path}"