|
| 1 | +// |
| 2 | +// Copyright © 2024 naveenrajm7. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +@MainActor |
| 20 | +@objc(UTMScriptingImportCommand) |
| 21 | +class UTMScriptingImportCommand: NSCreateCommand, UTMScriptable { |
| 22 | + |
| 23 | + private var data: UTMData? { |
| 24 | + (NSApp.scriptingDelegate as? AppDelegate)?.data |
| 25 | + } |
| 26 | + |
| 27 | + @objc override func performDefaultImplementation() -> Any? { |
| 28 | + if createClassDescription.implementationClassName == "UTMScriptingVirtualMachineImpl" { |
| 29 | + withScriptCommand(self) { [self] in |
| 30 | + // Retrieve the import file URL from the evaluated arguments |
| 31 | + guard let fileUrl = evaluatedArguments?["file"] as? URL else { |
| 32 | + throw ScriptingError.fileNotSpecified |
| 33 | + } |
| 34 | + |
| 35 | + // Validate the file (UTM is a directory) path |
| 36 | + guard FileManager.default.fileExists(atPath: fileUrl.path) else { |
| 37 | + throw ScriptingError.fileNotFound |
| 38 | + } |
| 39 | + return try await importVirtualMachine(from: fileUrl).objectSpecifier |
| 40 | + } |
| 41 | + return nil |
| 42 | + } else { |
| 43 | + return super.performDefaultImplementation() |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private func importVirtualMachine(from url: URL) async throws -> UTMScriptingVirtualMachineImpl { |
| 48 | + guard let data = data else { |
| 49 | + throw ScriptingError.notReady |
| 50 | + } |
| 51 | + |
| 52 | + // import the VM |
| 53 | + let vm = try await data.importNewUTM(from: url) |
| 54 | + |
| 55 | + // return VM scripting object |
| 56 | + return UTMScriptingVirtualMachineImpl(for: vm, data: data) |
| 57 | + } |
| 58 | + |
| 59 | + enum ScriptingError: Error, LocalizedError { |
| 60 | + case notReady |
| 61 | + case fileNotFound |
| 62 | + case fileNotSpecified |
| 63 | + |
| 64 | + var errorDescription: String? { |
| 65 | + switch self { |
| 66 | + case .notReady: return NSLocalizedString("UTM is not ready to accept commands.", comment: "UTMScriptingAppDelegate") |
| 67 | + case .fileNotFound: return NSLocalizedString("A valid UTM file must be specified.", comment: "UTMScriptingAppDelegate") |
| 68 | + case .fileNotSpecified: return NSLocalizedString("No file specified in the command.", comment: "UTMScriptingAppDelegate") |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments