Skip to content

Commit

Permalink
Merge pull request #75 from brightdigit/feature/install-command-tool
Browse files Browse the repository at this point in the history
Feature/install command tool
  • Loading branch information
leogdion authored Oct 25, 2018
2 parents c82799e + 1e7a40c commit f363194
Show file tree
Hide file tree
Showing 27 changed files with 1,074 additions and 39 deletions.
9 changes: 9 additions & 0 deletions Speculid-Installer/main.swift
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))
}
90 changes: 90 additions & 0 deletions Speculid-Mac-Installer/ServiceDelegate.swift
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
}


}
20 changes: 20 additions & 0 deletions Speculid-Mac-Installer/Speculid-Mac-Installer-Info.plist
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 Speculid-Mac-Installer/Speculid-Mac-Installer-Launchd.plist
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>
Loading

0 comments on commit f363194

Please sign in to comment.