Using Task on Linux

Hi,
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.
Regards,

–Mr Bee

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

Alex

···

On 9 Jan 2017, at 06:20, Mr Bee via swift-users <swift-users@swift.org> wrote:

Hi,

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.

Regards,

–Mr Bee

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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.
Regards,
–Mr Bee

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

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

···

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

–Mr Bee
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users