-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IOS-8495: Start caching responses in release scripts
- Loading branch information
1 parent
7905418
commit 53b0b8e
Showing
9 changed files
with
149 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
scripts/ReleaseScripts/SharedReleaseScript/Sources/cache.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Foundation | ||
|
||
enum InputCacheError: Error { | ||
case failedToDecodeJson | ||
} | ||
|
||
public enum InputCacheKeys: String { | ||
case version | ||
case sdkHash | ||
case sdkVersion | ||
case chatHash | ||
case chatVersion | ||
case releaseNotes | ||
} | ||
|
||
public func writeToCache(key: InputCacheKeys, value: String) { | ||
do { | ||
var cache = try decodeCache() | ||
cache[key.rawValue] = value | ||
try writeToURL(cache: cache) | ||
} catch { | ||
print("Non-fatal: failed to write \(key.rawValue):\(value) pair in cache.json with error \(String(describing: error))") | ||
} | ||
} | ||
|
||
public func readFromCache(key: InputCacheKeys) -> String? { | ||
do { | ||
let cache = try decodeCache() | ||
return cache[key.rawValue] | ||
} catch { | ||
print("Non-fatal: failed to read \(key.rawValue) from cache.json with error \(String(describing: error))") | ||
return nil | ||
} | ||
} | ||
|
||
private func decodeCache() throws -> [String: String] { | ||
try changeCurrentWorkDirectoryToRootDirectory() | ||
|
||
let cacheURL = try ensureCacheFile() | ||
let cacheData = try Data(contentsOf: cacheURL) | ||
let cache = try JSONSerialization.jsonObject(with: cacheData) as? [String: String] | ||
|
||
guard let cache else { | ||
throw InputCacheError.failedToDecodeJson | ||
} | ||
|
||
return cache | ||
} | ||
|
||
private let cacheURL = URL(fileURLWithPath: "scripts/ReleaseScripts/Resources/cache.json") | ||
|
||
private func ensureCacheFile() throws -> URL { | ||
let fileManager = FileManager.default | ||
|
||
if !fileManager.fileExists(atPath: cacheURL.path) { | ||
fileManager.createFile(atPath: cacheURL.path, contents: nil, attributes: nil) | ||
let emptyCache: [String: String] = [:] | ||
try writeToURL(cache: emptyCache) | ||
} | ||
|
||
return cacheURL | ||
} | ||
|
||
private func writeToURL(cache: [String: String]) throws { | ||
let jsonData = try JSONSerialization.data(withJSONObject: cache, options: []) | ||
try jsonData.write(to: cacheURL, options: []) | ||
} |
30 changes: 26 additions & 4 deletions
30
scripts/ReleaseScripts/SharedReleaseScript/Sources/filesystem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,36 @@ | ||
import Foundation | ||
|
||
public enum FileSystemError: Error { | ||
case cannotChangeToRootDirectory | ||
case cannotChangeToDirectory(directory: AvailableDirectories, path: String) | ||
} | ||
|
||
private let fileManager = FileManager.default | ||
|
||
public enum AvailableDirectories { | ||
case root | ||
case currentScriptDirectory | ||
|
||
var isInRootDirectory: Bool { | ||
if case .root = self { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
private var currentDirectory: AvailableDirectories = .currentScriptDirectory | ||
|
||
public func changeCurrentWorkDirectoryToRootDirectory() throws { | ||
guard !currentDirectory.isInRootDirectory else { return } | ||
let rootDirectoryPath = "../../../" | ||
let fileManager = FileManager.default | ||
try changeDirectory(path: rootDirectoryPath, directory: .root) | ||
} | ||
|
||
guard fileManager.changeCurrentDirectoryPath(rootDirectoryPath) else { | ||
throw FileSystemError.cannotChangeToRootDirectory | ||
private func changeDirectory(path: String, directory: AvailableDirectories) throws { | ||
guard fileManager.changeCurrentDirectoryPath(path) else { | ||
throw FileSystemError.cannotChangeToDirectory(directory: directory, path: path) | ||
} | ||
|
||
currentDirectory = directory | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters