-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from brightdigit/feature/install-command-tool
Feature/install command tool
- Loading branch information
Showing
27 changed files
with
1,074 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
|
||
let delegate = ServiceDelegate() | ||
let listener = NSXPCListener(machServiceName: Bundle.main.bundleIdentifier!) | ||
listener.delegate = delegate | ||
listener.resume() | ||
while !delegate.shouldQuit { | ||
RunLoop.current.run(until: Date(timeIntervalSinceNow: delegate.shouldQuitCheckInterval)) | ||
} |
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,90 @@ | ||
import Cocoa | ||
import CoreFoundation | ||
|
||
public enum InstallerErrorCode : Int { | ||
public typealias RawValue = Int | ||
case bundleNotFound = 1 | ||
case sharedSupportNotFound = 2 | ||
case speculidCommandNotFound = 3 | ||
case usrLocalBinDirNotFound = 4 | ||
} | ||
|
||
public struct InstallerError { | ||
private init () {} | ||
static func error(fromCode code: InstallerErrorCode) -> NSError { | ||
return NSError(domain: Bundle.main.bundleIdentifier!, code: code.rawValue, userInfo: nil) | ||
} | ||
} | ||
|
||
public class Installer: NSObject, InstallerProtocol { | ||
public func installCommandLineTool(fromBundleURL bundleURL: URL, _ completed: @escaping (NSError?) -> Void) { | ||
|
||
guard let bundle = Bundle(url: bundleURL) else { | ||
return completed(InstallerError.error(fromCode: .bundleNotFound)) | ||
} | ||
|
||
guard let speculidCommandURL = bundle.sharedSupportURL?.appendingPathComponent("speculid") else { | ||
return completed(InstallerError.error(fromCode: .sharedSupportNotFound)) | ||
} | ||
|
||
guard FileManager.default.fileExists(atPath: speculidCommandURL.path) else { | ||
return completed(InstallerError.error(fromCode: .speculidCommandNotFound)) | ||
} | ||
|
||
let binDirectoryURL = URL(fileURLWithPath: "/usr/local/bin", isDirectory: true) | ||
|
||
var isDirectory : ObjCBool = false | ||
|
||
let binDirExists = FileManager.default.fileExists(atPath: binDirectoryURL.path, isDirectory: &isDirectory) | ||
|
||
guard isDirectory.boolValue && binDirExists else { | ||
return completed(InstallerError.error(fromCode: .usrLocalBinDirNotFound)) | ||
} | ||
|
||
let destURL = binDirectoryURL.appendingPathComponent(speculidCommandURL.lastPathComponent) | ||
|
||
do { | ||
try FileManager.default.copyItem(at: speculidCommandURL, to: destURL) | ||
} catch let error as NSError { | ||
completed(error) | ||
} | ||
|
||
completed(nil) | ||
} | ||
|
||
public func hello(name: String, _ completed: @escaping (String) -> Void) { | ||
completed(["hello", name].joined(separator:" ")) | ||
} | ||
|
||
|
||
} | ||
|
||
@objc open class ServiceDelegate: NSObject, NSXPCListenerDelegate { | ||
|
||
private var connections = [NSXPCConnection]() | ||
public private(set) var shouldQuit = false | ||
public private(set) var shouldQuitCheckInterval = 1.0 | ||
public func listener(_: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { | ||
let exportedInterface = NSXPCInterface(with: InstallerProtocol.self) | ||
// let currentClasses: NSSet = exportedInterface.classes(for: #selector(ServiceProtocol.exportImageAtURL(_:toSpecifications:_:)), argumentIndex: 1, ofReply: false) as NSSet | ||
// let classes = currentClasses.addingObjects(from: [ImageSpecification.self, ImageFile.self, NSURL.self, NSColor.self]) | ||
// exportedInterface.setClasses(classes, for: #selector(ServiceProtocol.exportImageAtURL(_:toSpecifications:_:)), argumentIndex: 1, ofReply: false) | ||
newConnection.exportedInterface = exportedInterface | ||
let exportedObject = Installer() | ||
newConnection.exportedObject = exportedObject | ||
newConnection.invalidationHandler = { | ||
if let connectionIndex = self.connections.firstIndex(of: newConnection) { | ||
self.connections.remove(at: connectionIndex) | ||
} | ||
|
||
if self.connections.isEmpty { | ||
self.shouldQuit = true | ||
} | ||
} | ||
self.connections.append(newConnection) | ||
newConnection.resume() | ||
return true | ||
} | ||
|
||
|
||
} |
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,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleIdentifier</key> | ||
<string>com.brightdigit.Speculid-Mac-Installer</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>com.brightdigit.Speculid-Mac-Installer</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
<key>SMAuthorizedClients</key> | ||
<array> | ||
<string>anchor apple generic and identifier "com.brightdigit.Speculid-Mac-App" and (certificate leaf[field.1.2.840.113635.100.6.1.9] /* exists */ or certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = MLT7M394S7)</string> | ||
</array> | ||
</dict> | ||
</plist> |
13 changes: 13 additions & 0 deletions
13
Speculid-Mac-Installer/Speculid-Mac-Installer-Launchd.plist
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>Label</key> | ||
<string>com.brightdigit.Speculid-Mac-Installer</string> | ||
<key>MachServices</key> | ||
<dict> | ||
<key>com.brightdigit.Speculid-Mac-Installer</key> | ||
<true/> | ||
</dict> | ||
</dict> | ||
</plist> |
Oops, something went wrong.