浏览代码

scripting: moved create command to its own handler

osy 2 年之前
父节点
当前提交
2bd691eae0
共有 4 个文件被更改,包括 28 次插入19 次删除
  1. 0 4
      Platform/macOS/UTMPatches.swift
  2. 11 3
      Scripting/UTM.sdef
  3. 13 8
      Scripting/UTMScriptingCreateCommand.swift
  4. 4 4
      UTM.xcodeproj/project.pbxproj

+ 0 - 4
Platform/macOS/UTMPatches.swift

@@ -123,10 +123,6 @@ extension NSApplication {
         }
     }
     
-    @objc func handleCreateCommand(_ command: NSCreateCommand) {
-        (scriptingDelegate as? AppDelegate)?.handleCreateCommand(command)
-    }
-    
     fileprivate static func patchApplicationScripting() {
         patch(#selector(Self.value(forKey:)),
               with: #selector(Self.xxx_value(forKey:)),

+ 11 - 3
Scripting/UTM.sdef

@@ -8,6 +8,17 @@
     <suite name="UTM Suite" code="UTMs" description="UTM virtual machines scripting suite.">
         <access-group identifier="com.utmapp.UTM.vm-access" />
         
+        <command name="make" code="corecrel" description="Create a new virtual machine.">
+            <cocoa class="UTMScriptingCreateCommand"/>
+            <parameter name="new" code="kocl" type="type" description="Specify 'virtual machine' here.">
+                <cocoa key="ObjectClass"/>
+            </parameter>
+            <parameter name="with properties" code="prdt" type="record" description="You must specify the backend as well as a configuration with a name. If this is a QEMU virtual machine, you must specify the architecture in the configuration as well.">
+                <cocoa key="KeyDictionary"/>
+            </parameter>
+            <result type="specifier" description="The new virtual machine (as a specifier)."/>
+        </command>
+        
         <class-extension extends="application" description="An application's top level scripting object.">
           <element type="virtual machine" access="r">
             <cocoa key="scriptingVirtualMachines"/>
@@ -15,9 +26,6 @@
           <property name="auto terminate" code="kRlW" type="boolean" description="Auto terminate the application when all windows are closed?">
               <cocoa key="isAutoTerminate"/>
           </property>
-          <responds-to command="make">
-            <cocoa method="handleCreateCommand:"/>
-          </responds-to>
         </class-extension>
         
         <enumeration name="backend" code="VmEb" description="Backend type.">

+ 13 - 8
Scripting/UTMScriptingAppDelegate.swift → Scripting/UTMScriptingCreateCommand.swift

@@ -17,7 +17,8 @@
 import Foundation
 
 @MainActor
-@objc extension AppDelegate: UTMScriptable {
+@objc(UTMScriptingCreateCommand)
+class UTMScriptingCreateCommand: NSCreateCommand, UTMScriptable {
     private var bytesInMib: Int {
         1048576
     }
@@ -26,14 +27,17 @@ import Foundation
         1073741824
     }
     
-    @objc func handleCreateCommand(_ command: NSCreateCommand) {
-        if command.createClassDescription.implementationClassName == "UTMScriptingVirtualMachineImpl" {
-            let properties = command.resolvedKeyDictionary
-            withScriptCommand(command) { [self] in
-                guard let backend = properties["backend"] as? AEKeyword, let backend = UTMScriptingBackend(rawValue: backend) else {
+    private var data: UTMData? {
+        (NSApp.scriptingDelegate as? AppDelegate)?.data
+    }
+    
+    @objc override func performDefaultImplementation() -> Any? {
+        if createClassDescription.implementationClassName == "UTMScriptingVirtualMachineImpl" {
+            withScriptCommand(self) { [self] in
+                guard let backend = resolvedKeyDictionary["backend"] as? AEKeyword, let backend = UTMScriptingBackend(rawValue: backend) else {
                     throw ScriptingError.backendNotFound
                 }
-                guard let configuration = properties["configuration"] as? [AnyHashable : Any] else {
+                guard let configuration = resolvedKeyDictionary["configuration"] as? [AnyHashable : Any] else {
                     throw ScriptingError.configurationNotFound
                 }
                 if backend == .qemu {
@@ -44,8 +48,9 @@ import Foundation
                     throw ScriptingError.backendNotFound
                 }
             }
+            return nil
         } else {
-            command.performDefaultImplementation()
+            return super.performDefaultImplementation()
         }
     }
     

+ 4 - 4
UTM.xcodeproj/project.pbxproj

@@ -583,9 +583,9 @@
 		CE25124929BFDBA6000790AB /* UTMScriptingGuestFileImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25124829BFDBA6000790AB /* UTMScriptingGuestFileImpl.swift */; };
 		CE25124B29BFE273000790AB /* UTMScriptable.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25124A29BFE273000790AB /* UTMScriptable.swift */; };
 		CE25124D29C55816000790AB /* UTMScriptingConfigImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25124C29C55816000790AB /* UTMScriptingConfigImpl.swift */; };
-		CE25124F29C7E379000790AB /* UTMScriptingAppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25124E29C7E379000790AB /* UTMScriptingAppDelegate.swift */; };
 		CE25125129C806AF000790AB /* UTMScriptingDeleteCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25125029C806AF000790AB /* UTMScriptingDeleteCommand.swift */; };
 		CE25125329C80A18000790AB /* UTMScriptingCloneCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25125229C80A18000790AB /* UTMScriptingCloneCommand.swift */; };
+		CE25125529C80CD4000790AB /* UTMScriptingCreateCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE25125429C80CD4000790AB /* UTMScriptingCreateCommand.swift */; };
 		CE2D926A24AD46670059923A /* VMDisplayMetalViewController+Pointer.h in Sources */ = {isa = PBXBuildFile; fileRef = 83FBDD53242FA71900D2C5D7 /* VMDisplayMetalViewController+Pointer.h */; };
 		CE2D926B24AD46670059923A /* qapi-types-rocker.c in Sources */ = {isa = PBXBuildFile; fileRef = CE23C14D23FCEC09001177D6 /* qapi-types-rocker.c */; };
 		CE2D926E24AD46670059923A /* qapi-commands-crypto.c in Sources */ = {isa = PBXBuildFile; fileRef = CE23C0AE23FCEC01001177D6 /* qapi-commands-crypto.c */; };
@@ -2106,9 +2106,9 @@
 		CE25124829BFDBA6000790AB /* UTMScriptingGuestFileImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingGuestFileImpl.swift; sourceTree = "<group>"; };
 		CE25124A29BFE273000790AB /* UTMScriptable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptable.swift; sourceTree = "<group>"; };
 		CE25124C29C55816000790AB /* UTMScriptingConfigImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingConfigImpl.swift; sourceTree = "<group>"; };
-		CE25124E29C7E379000790AB /* UTMScriptingAppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingAppDelegate.swift; sourceTree = "<group>"; };
 		CE25125029C806AF000790AB /* UTMScriptingDeleteCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingDeleteCommand.swift; sourceTree = "<group>"; };
 		CE25125229C80A18000790AB /* UTMScriptingCloneCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingCloneCommand.swift; sourceTree = "<group>"; };
+		CE25125429C80CD4000790AB /* UTMScriptingCreateCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingCreateCommand.swift; sourceTree = "<group>"; };
 		CE258ACC22715F8300E5A333 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
 		CE2B89332262A21E00C6D9D8 /* UTMVirtualMachine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UTMVirtualMachine.h; sourceTree = "<group>"; };
 		CE2B89352262B2F600C6D9D8 /* UTMVirtualMachineDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UTMVirtualMachineDelegate.h; sourceTree = "<group>"; };
@@ -3463,12 +3463,12 @@
 				CEFE98DE29485237007CB7A8 /* UTM.sdef */,
 				CE25124A29BFE273000790AB /* UTMScriptable.swift */,
 				CEC794BB2949663C00121A9F /* UTMScripting.swift */,
-				CE25124E29C7E379000790AB /* UTMScriptingAppDelegate.swift */,
 				CEFE98E029485776007CB7A8 /* UTMScriptingVirtualMachineImpl.swift */,
 				CEC794B9294924E300121A9F /* UTMScriptingSerialPortImpl.swift */,
 				CE25124829BFDBA6000790AB /* UTMScriptingGuestFileImpl.swift */,
 				CE25124629BFDB87000790AB /* UTMScriptingGuestProcessImpl.swift */,
 				CE25124C29C55816000790AB /* UTMScriptingConfigImpl.swift */,
+				CE25125429C80CD4000790AB /* UTMScriptingCreateCommand.swift */,
 				CE25125029C806AF000790AB /* UTMScriptingDeleteCommand.swift */,
 				CE25125229C80A18000790AB /* UTMScriptingCloneCommand.swift */,
 			);
@@ -4122,7 +4122,6 @@
 				CE0B6D3724AD57FD00FE012D /* qapi-events-block-core.c in Sources */,
 				848A98B0286A0F74006F0550 /* UTMAppleConfiguration.swift in Sources */,
 				CE0B6D5E24AD584D00FE012D /* qapi-visit-authz.c in Sources */,
-				CE25124F29C7E379000790AB /* UTMScriptingAppDelegate.swift in Sources */,
 				CE0B6D4624AD584C00FE012D /* qapi-events-rdma.c in Sources */,
 				2C6D9E03256EE454003298E6 /* VMDisplayQemuTerminalWindowController.swift in Sources */,
 				CE6D21DD2553A6ED001D29C5 /* VMConfirmActionModifier.swift in Sources */,
@@ -4333,6 +4332,7 @@
 				CE0B6CF724AD568400FE012D /* UTMLegacyQemuConfiguration+Display.m in Sources */,
 				CE0B6D6F24AD584D00FE012D /* qapi-events-transaction.c in Sources */,
 				CE0B6D2524AD57FC00FE012D /* qapi-commands-sockets.c in Sources */,
+				CE25125529C80CD4000790AB /* UTMScriptingCreateCommand.swift in Sources */,
 				CE0B6D1624AD57FC00FE012D /* qapi-commands-block.c in Sources */,
 				843232B928C4816100CFBC97 /* UTMDownloadSupportToolsTask.swift in Sources */,
 				CE0B6D4724AD584C00FE012D /* qapi-visit-net.c in Sources */,