0

I use swift run gradlew build, but it was stuck on Start Daemom.

#!/usr/bin/swift sh

import Foundation


func shell(_ command: String) -> Void {
    let task = Process()
    task.standardOutput = FileHandle.standardOutput
    task.standardError = FileHandle.standardError
    task.standardInput = FileHandle.standardInput

    task.arguments = ["-c", command]
    task.launchPath = "/bin/sh"
    task.currentDirectoryPath = "/Users/MyProjectPath"
    task.launch()
    
    task.waitUntilExit()
    
    
    assert(task.terminationStatus == 0, "\(command)")
}




shell("./gradlew --debug --info :app:assembleRelease")

Output:

08:52:39.590 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTING
08:52:39.591 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Waiting until process started: Gradle build daemon.
08:52:39.601 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTED
08:52:39.602 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'Gradle build daemon'
08:52:39.602 [DEBUG] [org.gradle.launcher.daemon.client.DefaultDaemonStarter] Gradle daemon process is starting. Waiting for the daemon to detach...
08:52:39.602 [DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled...
08:52:39.604 [DEBUG] [org.gradle.launcher.daemon.bootstrap.DaemonOutputConsumer] Starting consuming the daemon process output.

> Starting Daemon 

It start success daemon and wait daemon output, for some reason not get daemon's output.

fengxing
  • 374
  • 1
  • 4
  • 10
  • Is that a Swift Script? Because if that's the case, there is like an issue with async where the script finished first. You need to add a "infinite loop" at the end. – Larme Apr 08 '21 at 12:32
  • `task.waitUntilExit()` will wait sh exit – fengxing Apr 08 '21 at 12:52
  • The issue is that the script will end before. I know, I’ve played with Swift Script. – Larme Apr 08 '21 at 12:53
  • Convert it to Swift Script use https://github.com/mxcl/swift-sh. For some reason, in swift script, gradle was wait start a demon. – fengxing Apr 08 '21 at 13:03
  • If you test curl http://stackoverflow.com vs ls command, does it work? – Larme Apr 08 '21 at 13:08
  • yes, `shell("curl https://www.stackoverflow.com")` works ok – fengxing Apr 08 '21 at 13:19
  • I would use https://stackoverflow.com/questions/28590701/multiple-workers-in-swift-command-line-tool That's what I use for an async task, be a "Shell command", like `carthage`, etc, or a `URLSession` task. – Larme Apr 08 '21 at 13:49

1 Answers1

0

According to apples documentation task.launchPath and task.launch() is "Deprecated" and not recommended to use any more. Instead use task.executableURL and try task.run().

DoTryCatch
  • 1,052
  • 6
  • 17
  • "'executableURL' is only available in macOS 10.13 or newer". I doubt if it solves the problem anyway, and even if it did, there should be a solution prior to macOS 10.13. – superarts.org Nov 20 '21 at 18:48