How to run an embedded binary in SwiftUI Mac app?

468 Views Asked by At

I'm trying to develop a really simple XML validator based on XIDEL.

Here's my SwiftUI code so far, that executes the embedded XIDEL binary, but I can't figure out how to pass XML that should be validated. My goal is to select a XML file from my computer and show XIDEL results in a content view inside my app.

struct ContentView: View {

@State var message = "Hello, World!"
@State var isRunning = false

var body: some View {
    VStack {
        Text("XML Validator")
            .font(.largeTitle)
            .padding()
        HStack {
            TextField("Message", text: $message)
                .padding(.leading)
            Button(action: {

                let task = Process()
                let bundle = Bundle.main
                let execURL = bundle.url(forResource: "xidel", withExtension: nil)
                guard execURL != nil else {
                    print("XIDEL executable could not be found!")
                    return
                }
                task.executableURL = execURL!
                task.arguments = ["-e=//recipe/flavor1/text() my.xml"]
                do {
                    try task.run()
                    print("XIDEL executed successfully!")
                    self.isRunning = true
                } catch {
                    print("Error running XIDEL: \(error)")
                    self.isRunning = false
                }
                                    
            }) {
                Text("Validate")
            }.disabled(isRunning)
                .padding(.trailing)
        }
    }.frame(maxWidth: .infinity, maxHeight: .infinity)
}

}

enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

Try the following

    guard let path = Bundle.main.path(forResource: "my", ofType: "xml") else {
       print("my.xml could not be found!")
       return 
    }
    task.arguments = ["-e=//recipe/flavor1/text()", path]