Skip to content

Commit

Permalink
Version 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Sep 15, 2024
1 parent 1a1bde3 commit a9ebc57
Show file tree
Hide file tree
Showing 115 changed files with 1,342 additions and 1,085 deletions.
18 changes: 9 additions & 9 deletions Sources/Pulse/Helpers/CoreData+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
import CoreData

extension NSManagedObjectContext {
func fetch<T: NSManagedObject>(_ entity: T.Type, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> [T] {
package func fetch<T: NSManagedObject>(_ entity: T.Type, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> [T] {
let request = NSFetchRequest<T>(entityName: String(describing: entity))
configure(request)
return try fetch(request)
}

func fetch<T: NSManagedObject, Value>(_ entity: T.Type, sortedBy keyPath: KeyPath<T, Value>, ascending: Bool = true, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> [T] {
package func fetch<T: NSManagedObject, Value>(_ entity: T.Type, sortedBy keyPath: KeyPath<T, Value>, ascending: Bool = true, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> [T] {
try fetch(entity) {
$0.sortDescriptors = [NSSortDescriptor(keyPath: keyPath, ascending: ascending)]
}
}

func first<T: NSManagedObject>(_ entity: T.Type, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> T? {
package func first<T: NSManagedObject>(_ entity: T.Type, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> T? {
try fetch(entity) {
$0.fetchLimit = 1
configure($0)
}.first
}

func count<T: NSManagedObject>(for entity: T.Type) throws -> Int {
package func count<T: NSManagedObject>(for entity: T.Type) throws -> Int {
try count(for: NSFetchRequest<T>(entityName: String(describing: entity)))
}

func performAndReturn<T>(_ closure: () throws -> T) throws -> T {
package func performAndReturn<T>(_ closure: () throws -> T) throws -> T {
var result: Result<T, Error>?
performAndWait {
result = Result { try closure() }
Expand All @@ -48,7 +48,7 @@ extension NSPersistentContainer {
return container
}

func loadStore() throws {
package func loadStore() throws {
var loadError: Swift.Error?
loadPersistentStores { description, error in
if let error = error {
Expand Down Expand Up @@ -130,8 +130,8 @@ extension NSRelationshipDescription {
}
}

enum KeyValueEncoding {
static func encodeKeyValuePairs(_ pairs: [String: String]?, sanitize: Bool = false) -> String {
package enum KeyValueEncoding {
package static func encodeKeyValuePairs(_ pairs: [String: String]?, sanitize: Bool = false) -> String {
var output = ""
let sorted = (pairs ?? [:]).sorted { $0.key < $1.key }
for (name, value) in sorted {
Expand All @@ -143,7 +143,7 @@ enum KeyValueEncoding {
return output
}

static func decodeKeyValuePairs(_ string: String) -> [String: String] {
package static func decodeKeyValuePairs(_ string: String) -> [String: String] {
let pairs = string.components(separatedBy: "\n")
var output: [String: String] = [:]
for pair in pairs {
Expand Down
32 changes: 18 additions & 14 deletions Sources/Pulse/Helpers/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ import Foundation
import CoreData
import CommonCrypto

var Files: FileManager { FileManager.default }
package var Files: FileManager { FileManager.default }

extension FileManager {
@discardableResult
func createDirectoryIfNeeded(at url: URL) -> Bool {
package func createDirectoryIfNeeded(at url: URL) -> Bool {
guard !fileExists(atPath: url.path) else { return false }
try? createDirectory(at: url, withIntermediateDirectories: true, attributes: [:])
return true
}
}

extension URL {
func appending(filename: String) -> URL {
package func appending(filename: String) -> URL {
appendingPathComponent(filename, isDirectory: false)
}

func appending(directory: String) -> URL {
package func appending(directory: String) -> URL {
appendingPathComponent(directory, isDirectory: true)
}

static var temp: URL {
package static var temp: URL {
let url = Files.temporaryDirectory
.appending(directory: "com.github.kean.logger")
Files.createDirectoryIfNeeded(at: url)
return url
}

static var logs: URL {
package static var logs: URL {
#if os(tvOS)
let searchPath = FileManager.SearchPathDirectory.cachesDirectory
#else
Expand All @@ -58,15 +58,15 @@ extension Data {
/// print("http://test.com".data(using: .utf8)!.sha1)
/// // prints "c6b6cafcb77f54d43cd1bd5361522a5e0c074b65"
/// ```
var sha1: Data {
package var sha1: Data {
Data(withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in
var hash = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CC_SHA1(bytes.baseAddress, CC_LONG(count), &hash)
return hash
})
}

var hexString: String {
package var hexString: String {
map { String(format: "%02x", $0) }.joined()
}
}
Expand Down Expand Up @@ -114,10 +114,14 @@ extension URL {
}
}

struct LoggerBlogDataStore {
let getDecompressedData: (LoggerBlobHandleEntity) -> Data?
package struct LoggerBlogDataStore {
package let getDecompressedData: (LoggerBlobHandleEntity) -> Data?

init(_ store: LoggerStore) {
package init(_ getDecompressedData: @escaping (LoggerBlobHandleEntity) -> Data?) {
self.getDecompressedData = getDecompressedData
}

package init(_ store: LoggerStore) {
self.getDecompressedData = { [weak store] in
store?.getDecompressedData(for: $0)
}
Expand All @@ -128,7 +132,7 @@ struct LoggerBlogDataStore {
}

/// The key for `NSManagedObjectContext` `userInfo`.
static let loggerStoreKey = "com.github.kean.pulse.associated-logger-store"
package static let loggerStoreKey = "com.github.kean.pulse.associated-logger-store"
}

struct TemporaryDirectory {
Expand All @@ -145,11 +149,11 @@ struct TemporaryDirectory {
}

extension Data {
func compressed() throws -> Data {
package func compressed() throws -> Data {
try (self as NSData).compressed(using: .lzfse) as Data
}

func decompressed() throws -> Data {
package func decompressed() throws -> Data {
try (self as NSData).decompressed(using: .lzfse) as Data
}
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/Pulse/Helpers/PulseDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import CoreData

final class PulseDocument {
let container: NSPersistentContainer
var context: NSManagedObjectContext { container.viewContext }
package final class PulseDocument {
package let container: NSPersistentContainer
package var context: NSManagedObjectContext { container.viewContext }

init(documentURL: URL) throws {
package init(documentURL: URL) throws {
guard Files.fileExists(atPath: documentURL.deletingLastPathComponent().path) else {
throw LoggerStore.Error.fileDoesntExist
}
Expand All @@ -20,7 +20,7 @@ final class PulseDocument {
try container.loadStore()
}

func close() throws {
package func close() throws {
let coordinator = container.persistentStoreCoordinator
for store in coordinator.persistentStores {
try coordinator.remove(store)
Expand All @@ -40,7 +40,7 @@ final class PulseDocument {
}()
}

final class PulseBlobEntity: NSManagedObject {
@NSManaged var key: String
@NSManaged var data: Data
package final class PulseBlobEntity: NSManagedObject {
@NSManaged package var key: String
@NSManaged package var data: Data
}
17 changes: 9 additions & 8 deletions Sources/Pulse/Helpers/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

import Foundation

final class Regex: @unchecked Sendable {
package final class Regex: @unchecked Sendable {
private let regex: NSRegularExpression

struct Options: OptionSet {
let rawValue: Int
package struct Options: OptionSet {
package let rawValue: Int
package init(rawValue: Int) { self.rawValue = rawValue }

static let caseInsensitive = Options(rawValue: 1 << 0)
static let multiline = Options(rawValue: 1 << 1)
static let dotMatchesLineSeparators = Options(rawValue: 1 << 2)
package static let caseInsensitive = Options(rawValue: 1 << 0)
package static let multiline = Options(rawValue: 1 << 1)
package static let dotMatchesLineSeparators = Options(rawValue: 1 << 2)
}

init(_ pattern: String, _ options: Options = []) throws {
package init(_ pattern: String, _ options: Options = []) throws {
var ops = NSRegularExpression.Options()
if options.contains(.caseInsensitive) { ops.insert(.caseInsensitive) }
if options.contains(.multiline) { ops.insert(.anchorsMatchLines) }
Expand All @@ -24,7 +25,7 @@ final class Regex: @unchecked Sendable {
self.regex = try NSRegularExpression(pattern: pattern, options: ops)
}

func isMatch(_ s: String) -> Bool {
package func isMatch(_ s: String) -> Bool {
let range = NSRange(s.startIndex..<s.endIndex, in: s)
return regex.firstMatch(in: s, options: [], range: range) != nil
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/LoggerStore/LoggerStore+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension LoggerStore {
/// is ignored completely.
public var willHandleEvent: @Sendable (Event) -> Event? = { $0 }

var isAutoStartingSession = true
package var isAutoStartingSession = true

/// Initializes the configuration.
///
Expand Down
4 changes: 2 additions & 2 deletions Sources/Pulse/LoggerStore/LoggerStore+Entities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ public final class LoggerBlobHandleEntity: NSManagedObject {
/// A decompressed blob size.
@NSManaged public var decompressedSize: Int32

@NSManaged var isUncompressed: Bool
@NSManaged package var isUncompressed: Bool

@NSManaged var rawContentType: String?
@NSManaged package var rawContentType: String?

/// A blob content type.
public var contentType: NetworkLogger.ContentType? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/LoggerStore/LoggerStore+Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension LoggerStore {
/// Returns Core Data model used by the store.
///
/// - warning: Model has to be loaded only once.
nonisolated(unsafe) static let model: NSManagedObjectModel = {
nonisolated(unsafe) package static let model: NSManagedObjectModel = {
typealias Entity = NSEntityDescription
typealias Attribute = NSAttributeDescription
typealias Relationship = NSRelationshipDescription
Expand Down
32 changes: 16 additions & 16 deletions Sources/Pulse/LoggerStore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public final class LoggerStore: @unchecked Sendable, Identifiable {
return container
}

func startSession(_ session: Session, info: Info.AppInfo) {
package func startSession(_ session: Session, info: Info.AppInfo) {
backgroundContext.performAndWait {
self.session = session
saveEntity(for: session, info: info)
Expand Down Expand Up @@ -342,7 +342,7 @@ extension LoggerStore {
}

/// Handles event emitted by the external store.
func handleExternalEvent(_ event: Event) {
package func handleExternalEvent(_ event: Event) {
perform { _ in self._handle(event) }
}

Expand Down Expand Up @@ -710,7 +710,7 @@ extension LoggerStore {

// MARK: - Performing Changes

func perform(_ changes: @escaping (NSManagedObjectContext) -> Void) {
package func perform(_ changes: @escaping (NSManagedObjectContext) -> Void) {
if options.contains(.synchronous) {
backgroundContext.performAndWait {
changes(backgroundContext)
Expand Down Expand Up @@ -1252,17 +1252,17 @@ extension LoggerStore {
// MARK: - LoggerStore (Manifest)

extension LoggerStore {
private struct Manifest: Codable {
var storeId: UUID
var version: Version
var lastSweepDate: Date?
package struct Manifest: Codable {
package var storeId: UUID
package var version: Version
package var lastSweepDate: Date?

init(storeId: UUID, version: Version) {
package init(storeId: UUID, version: Version) {
self.storeId = storeId
self.version = version
}

init?(url: URL) {
package init?(url: URL) {
guard let data = try? Data(contentsOf: url),
let manifest = try? JSONDecoder().decode(Manifest.self, from: data) else {
return nil
Expand All @@ -1273,14 +1273,14 @@ extension LoggerStore {
}

extension Version {
static let minimumSupportedVersion = Version(3, 1, 0)
static let currentStoreVersion = Version(3, 6, 0)
static let currentProtocolVersion = Version(4, 0, 0)
package static let minimumSupportedVersion = LoggerStore.Version(3, 1, 0)
package static let currentStoreVersion = LoggerStore.Version(3, 6, 0)
package static let currentProtocolVersion = LoggerStore.Version(4, 0, 0)
}

// MARK: - Constants

let manifestFilename = "manifest.json"
let databaseFilename = "logs.sqlite"
let infoFilename = "info.json"
let blobsDirectoryName = "blobs"
package let manifestFilename = "manifest.json"
package let databaseFilename = "logs.sqlite"
package let infoFilename = "info.json"
package let blobsDirectoryName = "blobs"
Loading

0 comments on commit a9ebc57

Please sign in to comment.