diff --git a/virtual.xcodeproj/project.pbxproj b/virtual.xcodeproj/project.pbxproj index 702f050..cb62a07 100644 --- a/virtual.xcodeproj/project.pbxproj +++ b/virtual.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - 2A2221922571779B00701E83 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2221912571779B00701E83 /* Constants.swift */; }; 2A2221952571786A00701E83 /* VirtualCommandRun.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2221942571786A00701E83 /* VirtualCommandRun.swift */; }; 2A2221992571792B00701E83 /* VirtualVersionOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2221982571792B00701E83 /* VirtualVersionOptions.swift */; }; 2ABF25AC24CF7B07001BEDC2 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABF25AB24CF7B07001BEDC2 /* main.swift */; }; @@ -18,6 +17,16 @@ /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ + 2A2221B22572CD1200701E83 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; 2ABF25A624CF7B07001BEDC2 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -30,7 +39,6 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 2A2221912571779B00701E83 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; 2A2221942571786A00701E83 /* VirtualCommandRun.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VirtualCommandRun.swift; sourceTree = ""; }; 2A2221982571792B00701E83 /* VirtualVersionOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VirtualVersionOptions.swift; sourceTree = ""; }; 2ABF25A824CF7B07001BEDC2 /* virtual */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = virtual; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -89,7 +97,6 @@ 2AC0F3C124D099C700880496 /* VirtualSystem.swift */, 2A2221982571792B00701E83 /* VirtualVersionOptions.swift */, 2AC0F3C724D2526000880496 /* Utils.swift */, - 2A2221912571779B00701E83 /* Constants.swift */, ); path = virtual; sourceTree = ""; @@ -104,6 +111,7 @@ 2ABF25A424CF7B07001BEDC2 /* Sources */, 2ABF25A524CF7B07001BEDC2 /* Frameworks */, 2ABF25A624CF7B07001BEDC2 /* CopyFiles */, + 2A2221B22572CD1200701E83 /* Embed Frameworks */, ); buildRules = ( ); @@ -159,7 +167,6 @@ files = ( 2A2221952571786A00701E83 /* VirtualCommandRun.swift in Sources */, 2A2221992571792B00701E83 /* VirtualVersionOptions.swift in Sources */, - 2A2221922571779B00701E83 /* Constants.swift in Sources */, 2ABF25AC24CF7B07001BEDC2 /* main.swift in Sources */, 2AC0F3C224D099C700880496 /* VirtualSystem.swift in Sources */, 2AC0F3C024D099A500880496 /* VirtualCommand.swift in Sources */, @@ -302,6 +309,7 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = "$(SRCROOT)/virtual/Info.plist"; MACOSX_DEPLOYMENT_TARGET = 11.0; + MARKETING_VERSION = 0.0.2; PRODUCT_BUNDLE_IDENTIFIER = io.endfinger.virtual; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -322,6 +330,7 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = "$(SRCROOT)/virtual/Info.plist"; MACOSX_DEPLOYMENT_TARGET = 11.0; + MARKETING_VERSION = 0.0.2; PRODUCT_BUNDLE_IDENTIFIER = io.endfinger.virtual; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/virtual/Constants.swift b/virtual/Constants.swift deleted file mode 100644 index 8817c98..0000000 --- a/virtual/Constants.swift +++ /dev/null @@ -1,10 +0,0 @@ -// -// Constants.swift -// virtual -// -// Created by Kenneth Endfinger on 11/27/20. -// - -import Foundation - -let virtualToolVersion = "0.0.1" diff --git a/virtual/Info.plist b/virtual/Info.plist index 4fd340b..643c35a 100644 --- a/virtual/Info.plist +++ b/virtual/Info.plist @@ -2,11 +2,11 @@ - CFBundleShortVersionString - 1 - CFBundleName - virtual CFBundleIdentifier io.endfinger.virtual + CFBundleName + virtual + CFBundleShortVersionString + $(MARKETING_VERSION) diff --git a/virtual/VirtualCommandRun.swift b/virtual/VirtualCommandRun.swift index e470e74..bb3b65e 100644 --- a/virtual/VirtualCommandRun.swift +++ b/virtual/VirtualCommandRun.swift @@ -40,31 +40,32 @@ struct VirtualCommandRun: ParsableCommand { var version: Bool = false mutating func run() throws { - if version { - print("virtual version \(virtualToolVersion)") - VirtualCommand.exit() - } - enableRawMode(fileHandle: FileHandle.standardInput) let system = VirtualSystem(command: self) do { try system.start() - var lastState: VZVirtualMachine.State = .stopped - while system.machine != nil { + let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main) + timer.schedule(deadline: .now(), repeating: .milliseconds(100)) + + var lastMachineState: VZVirtualMachine.State = .stopped + timer.setEventHandler { let currentState = system.machine!.state - if currentState != lastState { + if currentState != lastMachineState { NSLog("Virtual Machine State: \(system.stateToString())") - lastState = currentState + lastMachineState = currentState } if currentState == .error { - VirtualCommand.exit() + VirtualCommand.exit(withError: ExitCode.failure) + } else if currentState == .stopped { + VirtualCommand.exit(withError: ExitCode.success) } - - sleep(1) } + timer.resume() + + dispatchMain() } catch { NSLog("\(error)") } diff --git a/virtual/VirtualSystem.swift b/virtual/VirtualSystem.swift index 80be56c..059cb3d 100644 --- a/virtual/VirtualSystem.swift +++ b/virtual/VirtualSystem.swift @@ -10,8 +10,6 @@ import Virtualization class VirtualSystem: NSObject, VZVirtualMachineDelegate { let command: VirtualCommandRun - let queue = DispatchQueue(label: "io.endfinger.virtual.vm") - var machine: VZVirtualMachine? init(command: VirtualCommandRun) { @@ -19,11 +17,11 @@ class VirtualSystem: NSObject, VZVirtualMachineDelegate { } func guestDidStop(_: VZVirtualMachine) { - NSLog("Guest Stop.") + NSLog("Virtual Machine Stopped") } func virtualMachine(_: VZVirtualMachine, didStopWithError error: Error) { - NSLog("Guest Stopped with error: \(String(describing: error))") + NSLog("Virtual Machine Stopped: \(String(describing: error))") } func start() throws { @@ -74,18 +72,16 @@ class VirtualSystem: NSObject, VZVirtualMachineDelegate { config.networkDevices = network try config.validate() - let vm = VZVirtualMachine(configuration: config, queue: queue) + let vm = VZVirtualMachine(configuration: config) vm.delegate = self machine = vm - queue.sync { - vm.start { result in - switch result { - case .success: - NSLog("Virtual Machine Started") - case let .failure(error): - NSLog("Virtual Machine Failure: \(error)") - } + vm.start { result in + switch result { + case .success: + NSLog("Virtual Machine Started") + case let .failure(error): + NSLog("Virtual Machine Failure: \(error)") } } } @@ -116,10 +112,8 @@ class VirtualSystem: NSObject, VZVirtualMachineDelegate { } func stop() throws { - if let machine = machine { - try queue.sync { - try machine.requestStop() - } + if let vm = machine { + try vm.requestStop() } } } diff --git a/virtual/VirtualVersionOptions.swift b/virtual/VirtualVersionOptions.swift index 03c0509..e89f202 100644 --- a/virtual/VirtualVersionOptions.swift +++ b/virtual/VirtualVersionOptions.swift @@ -6,6 +6,7 @@ // import ArgumentParser +import Cocoa struct VirtualVersionOptions: ParsableArguments { @Flag(name: .long, help: "Show the Tool Version") @@ -13,7 +14,13 @@ struct VirtualVersionOptions: ParsableArguments { func validate() throws { if version { - print(virtualToolVersion) + let bundleVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? + + if let versionAsString = bundleVersion { + print("\(versionAsString)") + } else { + print("unknown") + } throw ExitCode.success } }