Skip to content

Commit

Permalink
Minor cleanups, slightly faster loading, faster saving. Version bump.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsochantaris committed Oct 20, 2024
1 parent 8b7ac72 commit a9476c7
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 40 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/trailer-cli.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "trailer"
BuildableName = "trailer"
BlueprintName = "trailer"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "trailer"
BuildableName = "trailer"
BlueprintName = "trailer"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "update all -mono -v"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "trailer"
BuildableName = "trailer"
BlueprintName = "trailer"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
6 changes: 5 additions & 1 deletion Sources/trailer/Actions/Actions-Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,11 @@ extension Actions {
json.potentialString(named: "message")
}
let resolved = serverError ?? error.localizedDescription
try await retryOrFail("Failed with error: '\(resolved)'")
if config.globalLogLevel < .info {
try await retryOrFail("Failed with error: '\(resolved)' - query was: \(query.queryText)")
} else {
try await retryOrFail("Failed with error: '\(resolved)'")
}
return
}

Expand Down
18 changes: 17 additions & 1 deletion Sources/trailer/Core/Config.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import Foundation

struct Config {
enum LogLevel: Int, Comparable {
case debug, verbose, info

static func < (lhs: LogLevel, rhs: LogLevel) -> Bool {
lhs.rawValue < rhs.rawValue
}

static func > (lhs: LogLevel, rhs: LogLevel) -> Bool {
lhs.rawValue > rhs.rawValue
}
}

var globalLogLevel = LogLevel.info

var server = URL(string: "https://api.github.com/graphql")!

var maxNodeCost = 10000
var monochrome = false
var dryRun = false

private static let versionNumbers = [1, 5, 2]
static let emptyURL = URL(string: "http://github.com")!

private static let versionNumbers = [1, 6, 0]
let versionString = versionNumbers.map { String($0) }.joined(separator: ".")

static func isNewer(_ version: String) -> Bool {
Expand Down
8 changes: 4 additions & 4 deletions Sources/trailer/Core/DB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ enum DB {
await withTaskGroup(of: Void.self) { group in
let e = JSONDecoder()
for type in allTypes {
group.addTask { @MainActor in
type.loadAll(using: e)
group.addTask {
await type.loadAll(using: e)
}
}
group.addTask { @MainActor in
Expand Down Expand Up @@ -179,8 +179,8 @@ enum DB {
await withTaskGroup(of: Void.self) { group in
let e = JSONEncoder()
for type in allTypes {
group.addTask { @MainActor in
type.saveAll(using: e)
group.addTask {
await type.saveAll(using: e)
}
}
group.addTask { @MainActor in
Expand Down
13 changes: 2 additions & 11 deletions Sources/trailer/Core/Globals.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import Foundation

let emptyURL = URL(string: "http://github.com")!

enum LogLevel: Int {
case debug, verbose, info
}

@MainActor
var globalLogLevel = LogLevel.info

@MainActor
func log(level: LogLevel = .info, indent: Int = 0, _ message: @autoclosure () -> String = "", unformatted: Bool = false) {
if globalLogLevel.rawValue > level.rawValue { return }
func log(level: Config.LogLevel = .info, indent: Int = 0, _ message: @autoclosure () -> String = "", unformatted: Bool = false) {
if config.globalLogLevel > level { return }
if indent > 0 {
let spaces = String(repeating: " ", count: indent)
print(spaces, terminator: "")
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Core/mainasync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ struct MainApp {
}

if CommandLine.argument(exists: "-debug") {
globalLogLevel = .debug
config.globalLogLevel = .debug
log("Will be verbose [*(debug)*]")
} else if CommandLine.argument(exists: "-v") {
globalLogLevel = .verbose
config.globalLogLevel = .verbose
log("Will be verbose")
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Issue: Item, Announceable, Closeable, Sortable {
var updatedAt = Date.distantPast
var number = 0
var title = ""
var url = emptyURL
var url = Config.emptyURL
var state = ItemState.closed
var viewerDidAuthor = false
var syncNeedsReactions = false
Expand Down Expand Up @@ -47,7 +47,7 @@ struct Issue: Item, Announceable, Closeable, Sortable {
updatedAt = GHDateFormatter.parseGH8601(node.potentialString(named: "updatedAt")) ?? .distantPast
number = node.potentialInt(named: "number") ?? 0
title = node.potentialString(named: "title") ?? ""
url = URL(string: node.potentialString(named: "url") ?? "") ?? emptyURL
url = URL(string: node.potentialString(named: "url") ?? "") ?? Config.emptyURL
state = ItemState(rawValue: node.potentialString(named: "state") ?? "CLOSED") ?? ItemState.closed
viewerDidAuthor = node.potentialBool(named: "viewerDidAuthor") ?? false
return true
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/PullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
var number = 0
var title = ""
var headRefName = ""
var url = emptyURL
var url = Config.emptyURL
var viewerDidAuthor = false

var syncNeedsReactions = false
Expand Down Expand Up @@ -119,7 +119,7 @@ struct PullRequest: Item, Announceable, Closeable, Sortable {
mergedAt = GHDateFormatter.parseGH8601(node.potentialString(named: "mergedAt")) ?? .distantPast
number = node.potentialInt(named: "number") ?? 0
title = node.potentialString(named: "title") ?? ""
url = URL(string: node.potentialString(named: "url") ?? "") ?? emptyURL
url = URL(string: node.potentialString(named: "url") ?? "") ?? Config.emptyURL
viewerDidAuthor = node.potentialBool(named: "viewerDidAuthor") ?? false
return true
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Repo: Item, Announceable {
var createdAt = Date.distantPast
var updatedAt = Date.distantPast
var isFork = false
var url = emptyURL
var url = Config.emptyURL
var nameWithOwner = ""
var visibility = RepoVisibility.visible

Expand All @@ -42,7 +42,7 @@ struct Repo: Item, Announceable {
createdAt = GHDateFormatter.parseGH8601(node.potentialString(named: "createdAt")) ?? .distantPast
updatedAt = GHDateFormatter.parseGH8601(node.potentialString(named: "updatedAt")) ?? .distantPast
isFork = node.potentialBool(named: "isFork") ?? false
url = URL(string: node.potentialString(named: "url") ?? "") ?? emptyURL
url = URL(string: node.potentialString(named: "url") ?? "") ?? Config.emptyURL
nameWithOwner = node.potentialString(named: "nameWithOwner") ?? ""

return true
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/Status.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Status: Item {
var createdAt = Date.distantPast
var description = ""
var state = StatusState.expected
var targetUrl = emptyURL
var targetUrl = Config.emptyURL

private enum CodingKeys: CodingKey {
case id
Expand All @@ -53,7 +53,7 @@ struct Status: Item {
guard ((try? node.keys)?.count ?? 0) > 6 else { return false }

createdAt = GHDateFormatter.parseGH8601(node.potentialString(named: "createdAt")) ?? .distantPast
targetUrl = URL(string: node.potentialString(named: "targetUrl") ?? "") ?? emptyURL
targetUrl = URL(string: node.potentialString(named: "targetUrl") ?? "") ?? Config.emptyURL

if let nodeContext = node.potentialString(named: "context") {
context = nodeContext
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct User: Item {
var syncState = SyncState.none
var elementType: String

var avatarUrl = emptyURL
var avatarUrl = Config.emptyURL
var login = ""
var isMe = false

Expand All @@ -28,7 +28,7 @@ struct User: Item {

mutating func apply(_ node: TypedJson.Entry) -> Bool {
guard ((try? node.keys)?.count ?? 0) > 2 else { return false }
avatarUrl = URL(string: node.potentialString(named: "avatarUrl") ?? "") ?? emptyURL
avatarUrl = URL(string: node.potentialString(named: "avatarUrl") ?? "") ?? Config.emptyURL
login = node.potentialString(named: "login") ?? ""
return true
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/trailer/Data/Protocols/Databaseable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Foundation

@MainActor
protocol Databaseable {
static func saveAll(using encoder: JSONEncoder)
static func loadAll(using decoder: JSONDecoder)
static func saveAll(using encoder: JSONEncoder) async
static func loadAll(using decoder: JSONDecoder) async
static func processAnnouncements(notificationMode: NotificationMode)
static func purgeUntouchedItems()
static func purgeStaleRelationships()
Expand Down
23 changes: 14 additions & 9 deletions Sources/trailer/Data/Protocols/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ extension Item {
}
}

static func saveAll(using encoder: JSONEncoder) {
static func saveAll(using encoder: JSONEncoder) async {
var newItemCount = 0
allItems = allItems.mapValues {
if $0.syncState == .new {
Expand All @@ -130,17 +130,22 @@ extension Item {
log(level: .verbose, "Created \(newItemCount) \(typeName) item(s) after update")
}

do {
let data = try encoder.encode(allItems)
try data.write(to: dataURL)
} catch {
log("Error saving to \(dataURL)")
}
let items = allItems
let url = dataURL
await Task.detached {
do {
let data = try encoder.encode(items)
try data.write(to: url)
} catch {
await log("Error saving to \(url)")
}
}.value
}

static func loadAll(using decoder: JSONDecoder) {
static func loadAll(using decoder: JSONDecoder) async {
allItems.removeAll()
guard let d = try? Data(contentsOf: dataURL) else {
let url = dataURL
guard let d = await Task.detached(operation: { try? Data(contentsOf: url) }).value else {
return
}
do {
Expand Down

0 comments on commit a9476c7

Please sign in to comment.