Skip to content

Commit

Permalink
Fix mdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Jan 20, 2024
1 parent 42f7e86 commit d4861b8
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 94 deletions.
5 changes: 1 addition & 4 deletions docs/docs/docs/fs2/custom-runners.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import cats.Traverse
import scala.concurrent.ExecutionContext
import io.github.vigoo.prox._

implicit val contextShift = IO.contextShift(ExecutionContext.global)
val (blocker, _) = Blocker[IO].allocated.unsafeRunSync()

val prox = ProxFS2[IO](blocker)
val prox = ProxFS2[IO]
import prox._
```

Expand Down
5 changes: 1 addition & 4 deletions docs/docs/docs/fs2/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import cats.effect._
import scala.concurrent.ExecutionContext
import io.github.vigoo.prox._

implicit val contextShift = IO.contextShift(ExecutionContext.global)
val (blocker, _) = Blocker[IO].allocated.unsafeRunSync()

val prox = ProxFS2[IO](blocker)
val prox = ProxFS2[IO]
import prox._
```

Expand Down
5 changes: 1 addition & 4 deletions docs/docs/docs/fs2/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ the `Prox` module:
import cats.effect._
import scala.concurrent.ExecutionContext
import io.github.vigoo.prox._

implicit val contextShift = IO.contextShift(ExecutionContext.global)
val (blocker, _) = Blocker[IO].allocated.unsafeRunSync()
```

```scala mdoc
val prox = ProxFS2[IO](blocker)
val prox = ProxFS2[IO]
import prox._
```

Expand Down
9 changes: 3 additions & 6 deletions docs/docs/docs/fs2/processgroups.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import cats.effect._
import scala.concurrent.ExecutionContext
import io.github.vigoo.prox._

implicit val contextShift = IO.contextShift(ExecutionContext.global)
val (blocker, _) = Blocker[IO].allocated.unsafeRunSync()

val prox = ProxFS2[IO](blocker)
val prox = ProxFS2[IO]
import prox._
```

Expand Down Expand Up @@ -44,12 +41,12 @@ following not very useful example capitalizes each word coming through:
```scala mdoc:silent
val customPipe: fs2.Pipe[IO, Byte, Byte] =
(s: fs2.Stream[IO, Byte]) => s
.through(fs2.text.utf8Decode) // decode UTF-8
.through(fs2.text.utf8.decode) // decode UTF-8
.through(fs2.text.lines) // split to lines
.map(_.split(' ').toVector) // split lines to words
.map(v => v.map(_.capitalize).mkString(" "))
.intersperse("\n") // remerge lines
.through(fs2.text.utf8Encode) // encode as UTF-8
.through(fs2.text.utf8.encode) // encode as UTF-8

val group3 = Process("echo", List("hello world")).via(customPipe).to(Process("wc", List("-w")))
```
19 changes: 8 additions & 11 deletions docs/docs/docs/fs2/redirection.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import cats.effect._
import scala.concurrent.ExecutionContext
import io.github.vigoo.prox._

implicit val contextShift = IO.contextShift(ExecutionContext.global)
val (blocker, _) = Blocker[IO].allocated.unsafeRunSync()

val prox = ProxFS2[IO](blocker)
val prox = ProxFS2[IO]
import prox._
```

Expand All @@ -34,13 +31,13 @@ Let's see an example of this (redirection methods are described below on this pa
import cats.implicits._

val proc1 = Process("echo", List("Hello world"))
val proc2 = proc1 ># fs2.text.utf8Decode
val proc2 = proc1 ># fs2.text.utf8.decode
```

It is no longer possible to redirect the output of `proc2`:

```scala mdoc:fail
val proc3 = proc2 >? fs2.text.utf8Decode[IO].andThen(fs2.text.lines)
val proc3 = proc2 >? fs2.text.utf8.decode[IO].andThen(fs2.text.lines)
```

Many redirection methods have an _operator_ version but all of them have alphanumberic
Expand Down Expand Up @@ -136,17 +133,17 @@ process they came from:


```scala mdoc:silent
import fs2.concurrent.Queue
import cats.effect.std.Queue

for {
errors <- Queue.unbounded[IO, String]
parseLines = fs2.text.utf8Decode[IO].andThen(fs2.text.lines)
parseLines = fs2.text.utf8.decode[IO].andThen(fs2.text.lines)

p1 = Process("proc1")
p2 = Process("proc2")
group = (p1 | p2).customizedPerProcess.errorsToSink {
case p if p == p1 => parseLines.andThen(_.map(s => "P1: " + s)).andThen(_.through(errors.enqueue))
case p if p == p2 => parseLines.andThen(_.map(s => "P2: " + s)).andThen(_.through(errors.enqueue))
case p if p == p1 => parseLines.andThen(_.map(s => "P1: " + s)).andThen(_.evalMap(errors.offer))
case p if p == p2 => parseLines.andThen(_.map(s => "P2: " + s)).andThen(_.evalMap(errors.offer))
}
} yield ()
```
Expand All @@ -171,7 +168,7 @@ These type aliases can be used to define functions performing redirection on arb

```scala mdoc
def logErrors[P <: Process.UnboundEProcess[_]](proc: P) = {
val target = fs2.text.utf8Decode[IO].andThen(fs2.text.lines).andThen(_.evalMap(line => IO(println(line))))
val target = fs2.text.utf8.decode[IO].andThen(fs2.text.lines).andThen(_.evalMap(line => IO(println(line))))
proc !> target
}

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/docs/fs2/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ implicit val runner: ProcessRunner[JVMProcessInfo] = new JVMProcessRunner
val process = Process("echo", List("hello"))

val result1 = process.run()
val result2 = process.start().use { fiber =>
val result2 = process.start().flatMap { fiber =>
fiber.join
}

Expand Down
13 changes: 6 additions & 7 deletions docs/docs/docs/zstream/custom-runners.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ title: Custom runners

```scala mdoc:invisible
import zio._
import zio.blocking.Blocking
import zio.stream._
import io.github.vigoo.prox._
import io.github.vigoo.prox.zstream._
Expand Down Expand Up @@ -39,21 +38,21 @@ class DockerizedProcessRunner[Info](processRunner: ProcessRunner[Info],
image: DockerImage)
extends ProcessRunner[DockerProcessInfo[Info]] {

override def startProcess[O, E](process: Process[O, E]): ZIO[Blocking, ProxError, RunningProcess[O, E, DockerProcessInfo[Info]]] = {
override def startProcess[O, E](process: Process[O, E]): ZIO[Any, ProxError, RunningProcess[O, E, DockerProcessInfo[Info]]] = {
for {
container <- generateContainerName
runningProcess <- processRunner
.startProcess(wrapInDocker(process, container))
} yield runningProcess.mapInfo(info => DockerProcessInfo(container, info))
}

override def startProcessGroup[O, E](processGroup: ProcessGroup[O, E]): ZIO[Blocking, ProxError, RunningProcessGroup[O, E, DockerProcessInfo[Info]]] = {
override def startProcessGroup[O, E](processGroup: ProcessGroup[O, E]): ZIO[Any, ProxError, RunningProcessGroup[O, E, DockerProcessInfo[Info]]] = {
ZIO.foreach(processGroup.originalProcesses.toVector)(key => generateContainerName.map(c => key -> c)).flatMap { keyAndNames =>
val nameMap = keyAndNames.toMap
val names = keyAndNames.map(_._2)
val modifiedProcessGroup = processGroup.map(new ProcessGroup.Mapper[O, E] {
def mapFirst[P <: Process[ZStream[Blocking, ProxError, Byte], E]](process: P): P = wrapInDocker(process, names.head).asInstanceOf[P]
def mapInnerWithIdx[P <: Process.UnboundIProcess[ZStream[Blocking, ProxError, Byte], E]](process: P, idx: Int): P =
def mapFirst[P <: Process[ZStream[Any, ProxError, Byte], E]](process: P): P = wrapInDocker(process, names.head).asInstanceOf[P]
def mapInnerWithIdx[P <: Process.UnboundIProcess[ZStream[Any, ProxError, Byte], E]](process: P, idx: Int): P =
wrapInDocker(process, names(idx)).asInstanceOf[P]
def mapLast[P <: Process.UnboundIProcess[O, E]](process: P): P = wrapInDocker(process, names.last).asInstanceOf[P]
})
Expand All @@ -62,8 +61,8 @@ class DockerizedProcessRunner[Info](processRunner: ProcessRunner[Info],
}
}

private def generateContainerName: ZIO[Blocking, ProxError, DockerContainer] =
ZIO.effect(DockerContainer(UUID.randomUUID().toString)).mapError(UnknownProxError)
private def generateContainerName: ZIO[Any, ProxError, DockerContainer] =
ZIO.attempt(DockerContainer(UUID.randomUUID().toString)).mapError(UnknownProxError)

private def wrapInDocker[O, E](process: Process[O, E], container: DockerContainer): Process[O, E] = {
val envVars = process.environmentVariables.flatMap { case (key, value) => List("-e", s"$key=$value") }.toList
Expand Down
9 changes: 4 additions & 5 deletions docs/docs/docs/zstream/processgroups.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ title: Process groups
# Connecting processes together via pipes
```scala mdoc:invisible
import zio._
import zio.blocking.Blocking
import zio.stream._
import zio.prelude._
import io.github.vigoo.prox._
Expand Down Expand Up @@ -36,14 +35,14 @@ val group1 = Process("grep", List("ERROR")) | Process("sort")
val group2 = group1 | Process("uniq", List("-c"))
```

A custom pipe (when using `via`) can be anything of the type `ZStream[Blocking, ProxError, Byte] => ZStream[Blocking, ProxError, Byte])`.
A custom pipe (when using `via`) can be anything of the type `ZStream[any, ProxError, Byte] => ZStream[any, ProxError, Byte])`.
The following not very useful example capitalizes each word coming through:

```scala mdoc:silent
val customPipe: ProxPipe[Byte, Byte] =
(s: ZStream[Blocking, ProxError, Byte]) => s
.transduce(ZTransducer.utf8Decode) // decode UTF-8
.transduce(ZTransducer.splitLines) // split to lines
(s: ZStream[Any, ProxError, Byte]) => s
.via(ZPipeline.utf8Decode.mapError(UnknownProxError.apply)) // decode UTF-8
.via(ZPipeline.splitLines) // split to lines
.map(_.split(' ').toVector) // split lines to words
.map(v => v.map(_.capitalize).mkString(" "))
.intersperse("\n") // remerge lines
Expand Down
Loading

0 comments on commit d4861b8

Please sign in to comment.