[swift-users] Using Task on Linux

Mr Bee pak.lebah at yahoo.com
Mon Jan 9 00:20:13 CST 2017

I'm writing a simple editor on Linux for Swift language. I use Task (was NSTask) to run the Swift REPL. Unfortunately, Task failed to execute the Swift REPL for no obvious reasons. The Swift compiler and REPL are installed just fine and able to execute any Swift codes. However, my exact same code has no problem to run bash commands.
Here's the code:_____
import Foundation
extension Task {  func execute(command: String, currentDir: String = "~", arguments: [String] = [], input: String = "") -> String {    if !input.isEmpty {      let pipeIn = Pipe()      self.standardInput = pipeIn      // multiple inputs are separated by newline      if let input = input.data(using: String.Encoding.utf8) {        pipeIn.fileHandleForWriting.write(input)      }    }        let pipeOut = Pipe()    self.standardOutput = pipeOut        self.arguments = arguments    self.launchPath = command    self.currentDirectoryPath = currentDir        self.launch()    let output = pipeOut.fileHandleForReading.readDataToEndOfFile()    self.waitUntilExit()        return String(data: output, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!  }}
print(Task().execute(command: "swift", arguments: ["test.swift"]))  // <- FAILED// print(Task().execute(command: "python", arguments: ["test.py"])) // <- FAILED// print(Task().execute(command: "/bin/ls", arguments: ["-l"]))     // <- OK
The test code is just a simple hello world program, nothing fancy. Can anybody here enlighten me what did I wrong with the code? I'm using Swift v.3.0 on Ubuntu Linux 14.04.
Thank you.

–Mr Bee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170109/f8d63e3f/attachment.html>

More information about the swift-users mailing list