-
Notifications
You must be signed in to change notification settings - Fork 621
Added output configurations. Related to #3170 #3543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
d57526c
196feb9
23b3a7d
2c3042d
caef85d
2ca7ebe
4d279b2
6e17b14
8fd4473
a982110
8a6f34f
817865b
904173a
068fed1
e9376b2
d31b605
9483bab
cfa82ae
c13ae8d
d9be98e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,8 @@ import cats.effect.kernel.Resource | |
| import cats.syntax.all.* | ||
| import fs2.io.CollectionCompat.* | ||
|
|
||
| import java.lang | ||
| import java.lang.ProcessBuilder.Redirect | ||
|
|
||
|
|
||
| private[process] trait ProcessesCompanionPlatform { | ||
| def forAsync[F[_]](implicit F: Async[F]): Processes[F] = new UnsealedProcesses[F] { | ||
|
|
@@ -37,60 +38,81 @@ private[process] trait ProcessesCompanionPlatform { | |
| Resource | ||
| .make { | ||
| F.blocking { | ||
| val builder = new lang.ProcessBuilder((process.command :: process.args).asJava) | ||
| val builder = new java.lang.ProcessBuilder((process.command :: process.args).asJava) | ||
|
|
||
| process.workingDirectory.foreach { path => | ||
| builder.directory(path.toNioPath.toFile) | ||
| } | ||
|
|
||
| val env = builder.environment() | ||
| if (!process.inheritEnv) env.clear() | ||
| process.extraEnv.foreach { case (k, v) => | ||
| env.put(k, v) | ||
| } | ||
|
|
||
| process.workingDirectory.foreach { path => | ||
| builder.directory(path.toNioPath.toFile) | ||
| process.outputConfig.stdin match { | ||
| case StreamOutputMode.Inherit => builder.redirectInput(Redirect.INHERIT) | ||
| case StreamOutputMode.Ignore => builder.redirectInput(Redirect.DISCARD) | ||
| case StreamOutputMode.FileOutput(path) => | ||
| builder.redirectInput(Redirect.from(path.toNioPath.toFile)) | ||
| case StreamOutputMode.Pipe => | ||
| } | ||
|
|
||
| val env = builder.environment() | ||
| if (!process.inheritEnv) env.clear() | ||
| process.extraEnv.foreach { case (k, v) => | ||
| env.put(k, v) | ||
| process.outputConfig.stdout match { | ||
| case StreamOutputMode.Inherit => builder.redirectOutput(Redirect.INHERIT) | ||
| case StreamOutputMode.Ignore => builder.redirectOutput(Redirect.DISCARD) | ||
| case StreamOutputMode.FileOutput(path) => | ||
| builder.redirectOutput(Redirect.to(path.toNioPath.toFile)) | ||
| case StreamOutputMode.Pipe => | ||
| } | ||
|
|
||
| builder.start() | ||
| } | ||
| } { process => | ||
| F.delay(process.isAlive()) | ||
| .ifM( | ||
| evalOnVirtualThreadIfAvailable( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted by mistake? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Must've been deleted while solving merge conflicts adding them back |
||
| F.blocking { | ||
| process.destroy() | ||
| process.waitFor() | ||
| () | ||
| } | ||
| ), | ||
| F.unit | ||
| ) | ||
| process.outputConfig.stderr match { | ||
| case StreamOutputMode.Inherit => builder.redirectError(Redirect.INHERIT) | ||
| case StreamOutputMode.Ignore => builder.redirectError(Redirect.DISCARD) | ||
| case StreamOutputMode.FileOutput(path) => | ||
| builder.redirectError(Redirect.to(path.toNioPath.toFile)) | ||
| case StreamOutputMode.Pipe => | ||
| } | ||
|
|
||
| builder.start() | ||
| } | ||
| .map { process => | ||
| new UnsealedProcess[F] { | ||
| def isAlive = F.delay(process.isAlive()) | ||
| } { process => | ||
| F.delay(process.isAlive()) | ||
| .ifM( | ||
| F.blocking { | ||
| process.destroy() | ||
| process.waitFor() | ||
| () | ||
| }, | ||
| F.unit | ||
| ) | ||
| } | ||
| .map { process => | ||
| new UnsealedProcess[F] { | ||
| def isAlive = F.delay(process.isAlive()) | ||
|
|
||
| def exitValue = isAlive.ifM( | ||
| evalOnVirtualThreadIfAvailable(F.interruptible(process.waitFor())), | ||
| F.delay(process.exitValue()) | ||
| ) | ||
|
|
||
| def stdin = writeOutputStreamCancelable( | ||
| F.delay(process.getOutputStream()), | ||
| F.blocking(process.destroy()) | ||
| ) | ||
| def stdin = writeOutputStreamCancelable( | ||
| F.delay(process.getOutputStream()), | ||
| F.blocking(process.destroy()) | ||
| ) | ||
|
|
||
| def stdout = readInputStreamCancelable( | ||
| F.delay(process.getInputStream()), | ||
| F.blocking(process.destroy()), | ||
| 8192 | ||
| ) | ||
| def stdout = readInputStreamCancelable( | ||
| F.delay(process.getInputStream()), | ||
| F.blocking(process.destroy()), | ||
| 8192 | ||
| ) | ||
|
|
||
| def stderr = readInputStreamCancelable( | ||
| F.delay(process.getErrorStream()), | ||
| F.blocking(process.destroy()), | ||
| 8192 | ||
| ) | ||
|
|
||
| } | ||
| def stderr = readInputStreamCancelable( | ||
| F.delay(process.getErrorStream()), | ||
| F.blocking(process.destroy()), | ||
| 8192 | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,9 @@ sealed abstract class ProcessBuilder private { | |||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| def workingDirectory: Option[Path] | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** Configures how stdout and stderr should be handled. */ | ||||||||||||||||||||||||||||||
| def outputConfig: ProcessOutputConfig | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** @see [[command]] */ | ||||||||||||||||||||||||||||||
| def withCommand(command: String): ProcessBuilder | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -67,17 +70,34 @@ sealed abstract class ProcessBuilder private { | |||||||||||||||||||||||||||||
| /** @see [[workingDirectory]] */ | ||||||||||||||||||||||||||||||
| def withCurrentWorkingDirectory: ProcessBuilder | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** @see [[outputMode]] */ | ||||||||||||||||||||||||||||||
| def withOutputConfig(outputConfig: ProcessOutputConfig): ProcessBuilder | ||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think separating it out into three config methods for stdin/stdout/stderr (like the JDK process builder) will make the API easier to use. |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** Starts the process and returns a handle for interacting with it. | ||||||||||||||||||||||||||||||
| * Closing the resource will kill the process if it has not already terminated. | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| final def spawn[F[_]: Processes]: Resource[F, Process[F]] = | ||||||||||||||||||||||||||||||
| Processes[F].spawn(this) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| sealed trait StreamOutputMode | ||||||||||||||||||||||||||||||
| object StreamOutputMode { | ||||||||||||||||||||||||||||||
| case object Pipe extends StreamOutputMode | ||||||||||||||||||||||||||||||
| case object Inherit extends StreamOutputMode | ||||||||||||||||||||||||||||||
| case object Ignore extends StreamOutputMode | ||||||||||||||||||||||||||||||
| case class FileOutput(path: Path) extends StreamOutputMode | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| sealed trait StreamOutputMode | |
| object StreamOutputMode { | |
| case object Pipe extends StreamOutputMode | |
| case object Inherit extends StreamOutputMode | |
| case object Ignore extends StreamOutputMode | |
| case class FileOutput(path: Path) extends StreamOutputMode | |
| } | |
| sealed abstract class StreamRedirect | |
| object StreamRedirect { | |
| case object Pipe extends StreamRedirect | |
| case object Inherit extends StreamRedirect | |
| case object Discard extends StreamRedirect | |
| final case class File(path: Path) extends StreamRedirect | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
js.Dynamiccan you update theSpawnOptionsface to add the additional fields?