[swift-users] Bls: Using Task on Linux

Mr Bee pak.lebah at yahoo.com
Mon Jan 9 07:30:25 CST 2017

Thank you. Yes, giving the full path to the executable name indeed solve the problem. I found it a few moments after I send this question. Sorry if the question sounds a bit silly. :)
Another problem from the same code above is it fails to supply data to stdin. It seems the input data doesn't get delivered to the pipe. So, it just stucks waiting forever. I had to kill the process manually. Which also raises another question, how to make Task doesn't wait forever? So, it has some kind of time out mechanism.
Thank you.
–Mr Bee

    Pada Senin, 9 Januari 2017 17:09, Alex Blewitt <alblue at apple.com> menulis:

 When you run it with absolute paths for the 'swift' and 'python' executables, does it work then?

On 9 Jan 2017, at 06:20, Mr Bee via swift-users <swift-users at swift.org> wrote:
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
swift-users mailing list
swift-users at swift.org

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

More information about the swift-users mailing list