How to run bash script after generating scaladoc using doc task?

雨燕双飞 提交于 2019-12-01 03:48:40

问题


I have a short Bash script that does a find-and-replace on my Scaladoc comments in order to generate links to external documentation of a third-party library. I would like this script to run every time I generate Scaladocs using the doc task.

How can I achieve this?


回答1:


It's actually pretty easy. First, I inspected doc to see what it was (inspect doc on the sbt prompt), noticed it was a task, and proceeded with declaring a dependency on itself on build.sbt:

doc in Compile <<= doc in Compile map { (file) =>
  Seq("bash", "-c", "ls >tmp.log").! // CWD is sbt's current dir
  file
}

That thing I used to execute bash is the same library as scala.sys.process, so you can look up Scaladoc for that. This was tested on SBT 0.12.2, and I think there might be a small difference on SBT 0.11.x or 0.10.x.




回答2:


In sbt 0.13 and the latest releases the use case can also be achieved with := and .value macros (that are both aimed at being simpler than <<=):

doc in Compile := {
  val f = (doc in Compile).value
  // execute a shell script if you want with sbt's Process API
  // http://www.scala-sbt.org/0.13/docs/Process.html
  val ec = (baseDirectory.value / "myBashScript.sh").getAbsolutePath !
  val log = streams.value.log
  log.debug(s"Exit code: $ec")
  f
}

You may also like triggeredBy method for tasks as follows:

lazy val runMyBashScriptTask = taskKey[Unit]("Run myBashScript")

runMyBashScriptTask := {
  val ec = (baseDirectory.value / "myBashScript.sh").getAbsolutePath !
  val log = streams.value.log
  log.debug(s"Exit code: $ec")
}

runMyBashScriptTask <<= runMyBashScriptTask triggeredBy (doc in Compile)

It assumes that myBashScript.sh is in the main directory of the project as pointed by baseDirectory setting.



来源:https://stackoverflow.com/questions/15212472/how-to-run-bash-script-after-generating-scaladoc-using-doc-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!