Explorar el Código

fix initial preferences

Ivan Valkou hace 5 años
padre
commit
d1756ab019

+ 2 - 0
FreeAPS.xcodeproj/project.pbxproj

@@ -1932,6 +1932,7 @@
 					"$(inherited)",
 					"@executable_path/Frameworks",
 				);
+				MARKETING_VERSION = 0.1.0;
 				PRODUCT_BUNDLE_IDENTIFIER = ru.artpancreas.FreeAPS;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SWIFT_VERSION = 5.0;
@@ -1955,6 +1956,7 @@
 					"$(inherited)",
 					"@executable_path/Frameworks",
 				);
+				MARKETING_VERSION = 0.1.0;
 				PRODUCT_BUNDLE_IDENTIFIER = ru.artpancreas.FreeAPS;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SWIFT_VERSION = 5.0;

+ 14 - 14
FreeAPS/Resources/Info.plist

@@ -15,11 +15,24 @@
 	<key>CFBundlePackageType</key>
 	<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
 	<key>CFBundleShortVersionString</key>
-	<string>1.0</string>
+	<string>$(MARKETING_VERSION)</string>
 	<key>CFBundleVersion</key>
 	<string>1</string>
+	<key>LSApplicationQueriesSchemes</key>
+	<array>
+		<string>dexcomg6</string>
+		<string>dexcomcgm</string>
+		<string>dexcomshare</string>
+		<string>diabox</string>
+		<string>spikeapp</string>
+	</array>
 	<key>LSRequiresIPhoneOS</key>
 	<true/>
+	<key>NSAppTransportSecurity</key>
+	<dict>
+		<key>NSAllowsArbitraryLoads</key>
+		<true/>
+	</dict>
 	<key>NSBluetoothAlwaysUsageDescription</key>
 	<string>Bluetooth is used to communicate with insulin pump and continuous glucose monitor devices</string>
 	<key>NSBluetoothPeripheralUsageDescription</key>
@@ -55,18 +68,5 @@
 		<string>UIInterfaceOrientationLandscapeLeft</string>
 		<string>UIInterfaceOrientationLandscapeRight</string>
 	</array>
-	<key>NSAppTransportSecurity</key>
-	<dict>
-		<key>NSAllowsArbitraryLoads</key>
-		<true/>
-	</dict>
-	<key>LSApplicationQueriesSchemes</key>
-	<array>
-		<string>dexcomg6</string>
-		<string>dexcomcgm</string>
-		<string>dexcomshare</string>
-		<string>diabox</string>
-		<string>spikeapp</string>
-	</array>
 </dict>
 </plist>

+ 0 - 17
FreeAPS/Resources/json/defaults/preferences.json

@@ -1,17 +0,0 @@
-{
-    "max_iob": 0,
-    "max_daily_safety_multiplier": 3,
-    "current_basal_safety_multiplier": 4,
-    "autosens_max": 1.2,
-    "autosens_min": 0.7,
-    "rewind_resets_autosens": true,
-    "adv_target_adjustments": false,
-    "exercise_mode": false,
-    "wide_bg_target_range": false,
-    "sensitivity_raises_target": true,
-    "unsuspend_if_no_temp": false,
-    "enableSMB_with_COB": false,
-    "enableSMB_with_temptarget": false,
-    "enableUAM": true,
-    "curve": "rapid-acting"
-}

+ 1 - 1
FreeAPS/Sources/APS/APSManager.swift

@@ -108,7 +108,6 @@ final class BaseAPSManager: APSManager, Injectable {
             nightscout.fetchCarbs(),
             nightscout.fetchTempTargets()
         )
-        .flatMap { _ in self.dailyAutotune() }
         .flatMap { _ in self.determineBasal() }
         .sink { _ in } receiveValue: { [weak self] ok in
             guard let self = self else { return }
@@ -171,6 +170,7 @@ final class BaseAPSManager: APSManager, Injectable {
 
         let mainPublisher = makeProfiles()
             .flatMap { _ in self.autosens() }
+            .flatMap { _ in self.dailyAutotune() }
             .flatMap { _ in self.openAPS.determineBasal(currentTemp: temp, clock: now) }
             .map { suggestion -> Bool in
                 if let suggestion = suggestion {

+ 4 - 1
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -147,7 +147,10 @@ final class OpenAPS {
     func makeProfiles(useAutotune: Bool) -> Future<Autotune?, Never> {
         Future { promise in
             self.processQueue.async {
-                let preferences = self.loadFileFromStorage(name: Settings.preferences)
+                var preferences = self.loadFileFromStorage(name: Settings.preferences)
+                if preferences.isEmpty {
+                    preferences = Preferences().rawJSON
+                }
                 let pumpSettings = self.loadFileFromStorage(name: Settings.settings)
                 let bgTargets = self.loadFileFromStorage(name: Settings.bgTargets)
                 let basalProfile = self.loadFileFromStorage(name: Settings.basalProfile)

+ 10 - 1
FreeAPS/Sources/Services/SettingsManager/SettingsManager.swift

@@ -29,7 +29,16 @@ final class BaseSettingsManager: SettingsManager, Injectable {
         let storage = resolver.resolve(FileStorage.self)!
         settings = storage.retrieve(OpenAPS.FreeAPS.settings, as: FreeAPSSettings.self)
             ?? FreeAPSSettings(from: OpenAPS.defaults(for: OpenAPS.FreeAPS.settings))
-            ?? FreeAPSSettings(units: .mmolL, closedLoop: false, allowAnnouncements: false, useAutotune: true, isUploadEnabled: true, useLocalGlucoseSource: false, localGlucosePort: nil, debugOptions: false)
+            ?? FreeAPSSettings(
+                units: .mmolL,
+                closedLoop: false,
+                allowAnnouncements: false,
+                useAutotune: false,
+                isUploadEnabled: true,
+                useLocalGlucoseSource: false,
+                localGlucosePort: nil,
+                debugOptions: false
+            )
 
         injectServices(resolver)
     }

+ 224 - 0
scripts/build-number-update.swift

@@ -0,0 +1,224 @@
+#!/usr/bin/env xcrun --sdk macosx swift
+
+import Foundation
+
+// MARK: Types
+
+struct InfoPlist {
+    private typealias Plist = (dict: [String: Any], format: PropertyListSerialization.PropertyListFormat)
+
+    private var fileURL: URL
+    private var plist: Plist
+
+    enum Const: String {
+        case version = "CFBundleShortVersionString"
+        case build = "CFBundleVersion"
+        case settings = "PreferenceSpecifiers"
+        case settingsValue = "DefaultValue"
+    }
+
+    var version: String? {
+        get {
+            plist.dict[InfoPlist.Const.version.rawValue] as? String
+        }
+        set {
+            plist.dict[InfoPlist.Const.version.rawValue] = newValue
+        }
+    }
+
+    var build: String? {
+        get {
+            plist.dict[InfoPlist.Const.build.rawValue] as? String
+        }
+        set {
+            plist.dict[InfoPlist.Const.build.rawValue] = newValue
+        }
+    }
+
+    var settingsVersion: String? {
+        get {
+            (
+                plist
+                    .dict[InfoPlist.Const.settings.rawValue] as! [[String: Any]]
+            )[1][
+                InfoPlist.Const.settingsValue
+                    .rawValue
+            ] as? String
+        }
+        set {
+            var dictCopy = plist.dict
+            var specs = dictCopy[InfoPlist.Const.settings.rawValue] as! [[String: Any]]
+            specs[1][InfoPlist.Const.settingsValue.rawValue] = newValue ?? ""
+            dictCopy[InfoPlist.Const.settings.rawValue] = specs
+            plist.dict = dictCopy
+        }
+    }
+
+    private init(fileURL: URL, plist: Plist) {
+        self.fileURL = fileURL
+        self.plist = plist
+    }
+
+    init?(fromFileAtURL fileURL: URL) {
+        guard let plist = InfoPlist.readPlist(fromFileAtURL: fileURL) else { return nil }
+
+        self.init(fileURL: fileURL, plist: plist)
+    }
+
+    func save() {
+        InfoPlist.writePlist(plist, toFileAtURL: fileURL)
+    }
+
+    // MARK: Plist file read/write
+
+    private static func readPlist(fromFileAtURL fileURL: URL) -> Plist? {
+        var data: Data
+        do {
+            data = try Data(contentsOf: fileURL)
+        } catch {
+            return nil
+        }
+
+        var format: PropertyListSerialization.PropertyListFormat = .xml
+        let dict: [String: Any]
+        do {
+            dict = try PropertyListSerialization.propertyList(from: data, format: &format) as! [String: Any]
+        } catch {
+            print("error: Failed to deserialize plist read from file: \(fileURL.absoluteString)")
+            print("Error details: \(error)")
+            return nil
+        }
+
+        return (dict: dict, format: format)
+    }
+
+    private static func writePlist(_ plist: Plist, toFileAtURL fileURL: URL) {
+        let data: Data
+        do {
+            data = try PropertyListSerialization.data(fromPropertyList: plist.dict, format: plist.format, options: 0)
+        } catch {
+            print("error: Failed to serialize plist!")
+            return
+        }
+
+        do {
+            try data.write(to: fileURL)
+        } catch {
+            print("error: Failed to write file: \(fileURL.absoluteString)")
+            print("Error details: \(error)")
+            return
+        }
+    }
+}
+
+enum Branch: CustomStringConvertible {
+    private static var releasePrefix = "release"
+
+    case Release(version: String)
+    case Other(name: String)
+
+    init(_ string: String) {
+        let parts = string.components(separatedBy: "/")
+        if parts.count >= 2, parts[0] == Branch.releasePrefix {
+            self = .Release(version: parts[1])
+        } else {
+            self = .Other(name: string)
+        }
+    }
+
+    var description: String {
+        switch self {
+        case let .Release(version):
+            return "\(Branch.releasePrefix)/\(version)"
+        case let .Other(name):
+            return name
+        }
+    }
+}
+
+// MARK: Helpers
+
+func execute(command: String, args: [String]) -> String? {
+    let process = Process()
+    process.launchPath = command
+    process.arguments = args
+
+    let pipe = Pipe()
+    process.standardOutput = pipe
+    process.launch()
+
+    let data = pipe.fileHandleForReading.readDataToEndOfFile()
+
+    return String(data: data, encoding: .utf8)?
+        .trimmingCharacters(in: .newlines)
+}
+
+func checkdSYM(buildNumber: String?) {
+    // Не правильная версия в dSYM может привести к проблемам в работе с сервисами, которые эти dSYM используют (например: Crashlytics)
+    // http://tgoode.com/2014/06/05/sensible-way-increment-bundle-version-cfbundleversion-xcode/#comment-600
+    // http://tgoode.com/2014/06/05/sensible-way-increment-bundle-version-cfbundleversion-xcode/#comment-2704
+    // http://stackoverflow.com/q/13323728
+    // http://stackoverflow.com/a/22460268
+    if let dsymFolderPath = ProcessInfo.processInfo.environment["DWARF_DSYM_FOLDER_PATH"], !dsymFolderPath.isEmpty,
+       let productName = ProcessInfo.processInfo.environment["PRODUCT_NAME"], !productName.isEmpty
+    {
+        let dsymPlistURL = URL(fileURLWithPath: "\(dsymFolderPath)/\(productName).app.dSYM/Contents/Info.plist")
+
+        guard var dsymInfoPlist = InfoPlist(fromFileAtURL: dsymPlistURL) else {
+            print("Failed to read dsym at url \(dsymPlistURL). This is ok for debug builds.")
+            return
+        }
+
+        dsymInfoPlist.build = buildNumber
+        dsymInfoPlist.save()
+    }
+}
+
+// MARK: Implementation
+
+guard let currentBranch = execute(command: "/usr/bin/git", args: ["rev-parse", "--abbrev-ref", "HEAD"]).map(Branch.init) else {
+    print("error: Can't determine current branch!")
+    exit(1)
+}
+
+guard let commitsCount = execute(command: "/usr/bin/git", args: ["rev-list", "--count", "HEAD"]), !commitsCount.isEmpty else {
+    print("error: Can't determine commits count!")
+    exit(1)
+}
+
+guard let targetBuildDir = ProcessInfo.processInfo.environment["TARGET_BUILD_DIR"], !targetBuildDir.isEmpty else {
+    print("error: TARGET_BUILD_DIR environment variable is empty!")
+    exit(1)
+}
+
+guard let infoPlistPath = ProcessInfo.processInfo.environment["INFOPLIST_PATH"], !infoPlistPath.isEmpty else {
+    print("error: INFOPLIST_PATH environment variable is empty!")
+    exit(1)
+}
+
+guard let infoPlistFile = ProcessInfo.processInfo.environment["INFOPLIST_FILE"], !infoPlistFile.isEmpty else {
+    print("error: INFOPLIST_FILE environment variable is empty!")
+    exit(1)
+}
+
+guard let currentVersion = ProcessInfo.processInfo.environment["CURRENT_PROJECT_VERSION"] else {
+    print("error: Current version can't be determined!")
+    exit(1)
+}
+
+guard var buildInfoPlist = InfoPlist(fromFileAtURL: URL(fileURLWithPath: "\(targetBuildDir)/\(infoPlistPath)")) else {
+    print("error: Build Info Plist cannot be load!")
+    exit(1)
+}
+
+switch currentBranch {
+case let .Release(version):
+    buildInfoPlist.version = version
+    buildInfoPlist.build = commitsCount
+default:
+    buildInfoPlist.build = "\(commitsCount)"
+}
+
+buildInfoPlist.save()
+
+checkdSYM(buildNumber: buildInfoPlist.build)