Skip to content

Commit

Permalink
Start building out Application type
Browse files Browse the repository at this point in the history
  • Loading branch information
hisaac committed Dec 15, 2024
1 parent 930ec6d commit d32db42
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/SysInfoKit/Sources/Application.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import AppKit
import Foundation
import Path
import Version

public struct Application {
public let name: String
public let version: Version
public let bundleIdentifier: String
public let path: AbsolutePath

public init(
name: String,
version: Version,
bundleIdentifier: String,
path: AbsolutePath
) {
self.name = name
self.version = version
self.bundleIdentifier = bundleIdentifier
self.path = path
}
}

public extension Application {
init(at path: AbsolutePath) throws {
self.path = path

let infoPlistPath = path.appending(components: "Contents", "Info.plist")
let infoPlistContents = try Data(contentsOf: infoPlistPath)
let infoPlist = try PropertyListDecoder().decode(ApplicationInfoPlist.self, from: infoPlistContents)

self.name = infoPlist.name
self.version = infoPlist.version
self.bundleIdentifier = infoPlist.bundleIdentifier
}

init(with bundleIdentifier: String) throws {
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier) else {
throw NSError(domain: "ApplicationErrorDomain", code: 1, userInfo: [NSLocalizedDescriptionKey: "Application not found"])
}
let path = try AbsolutePath(validating: url.path())
try self.init(at: path)
}
}

public struct ApplicationInfoPlist: Decodable {
public let name: String
public let version: Version
public let bundleIdentifier: String
}

0 comments on commit d32db42

Please sign in to comment.