2020-07-31 13:13:07 +00:00
|
|
|
package docspell.build
|
|
|
|
|
|
|
|
import sbt._
|
|
|
|
import scala.sys.process._
|
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
|
|
|
|
|
|
|
/** Helper for running external commands. */
|
|
|
|
object Cmd {
|
|
|
|
|
|
|
|
case class Result(rc: Int, out: String, err: String) {
|
|
|
|
|
|
|
|
def throwIfNot(success: Int): Result =
|
|
|
|
if (rc != success) sys.error(s"Unsuccessful return: $rc")
|
|
|
|
else this
|
|
|
|
}
|
|
|
|
|
|
|
|
def run(cmd: Seq[String], wd: File, logger: Logger): Unit = {
|
2022-01-27 19:23:15 +00:00
|
|
|
logger.info(s"Running ${cmd.mkString(" ")}")
|
2020-07-31 13:13:07 +00:00
|
|
|
val res = Cmd.exec(cmd, Some(wd))
|
|
|
|
logger.info(res.out)
|
|
|
|
logger.error(res.err)
|
|
|
|
res.throwIfNot(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
def exec(cmd: Seq[String], wd: Option[File]): Result = {
|
2022-03-20 12:22:00 +00:00
|
|
|
val command =
|
2022-03-21 14:32:26 +00:00
|
|
|
sys.props.get("os.name") match {
|
2022-03-21 18:25:47 +00:00
|
|
|
case Some(name) if name.toLowerCase.startsWith("windows") =>
|
|
|
|
Seq("cmd", "/C") ++ cmd
|
2022-03-20 12:22:00 +00:00
|
|
|
case _ => cmd
|
|
|
|
}
|
2020-07-31 13:13:07 +00:00
|
|
|
val capt = new Capture
|
2022-03-20 12:22:00 +00:00
|
|
|
val rc = Process(command, wd).!(capt.logger)
|
2020-07-31 13:13:07 +00:00
|
|
|
Result(rc, capt.out.get.mkString("\n"), capt.err.get.mkString("\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
final private class Capture {
|
|
|
|
val err = new AtomicReference[List[String]](Nil)
|
|
|
|
val out = new AtomicReference[List[String]](Nil)
|
|
|
|
|
|
|
|
val logger = ProcessLogger(
|
|
|
|
line => out.getAndAccumulate(List(line), _ ++ _),
|
|
|
|
line => err.getAndAccumulate(List(line), _ ++ _)
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|