Skip to content

Instantly share code, notes, and snippets.

@nafg
Last active April 1, 2020 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafg/4a7a4876fc057326895adc1dd58f4ef7 to your computer and use it in GitHub Desktop.
Save nafg/4a7a4876fc057326895adc1dd58f4ef7 to your computer and use it in GitHub Desktop.
Auto-generate your library's install instructions in README
val generateInstallInstructions = taskKey[Unit]("Generate install instructions in README.md")
generateInstallInstructions := {
val info = Def.task((projectID.value, description.value, (publish / skip).value)).all(ScopeFilter(inAnyProject)).value
val lines =
for ((moduleId, descr, noPublish) <- info.sortBy(_._1.name) if !noPublish) yield {
"// " + descr + "\n" +
s"""libraryDependencies += "${moduleId.organization}" %%% "${moduleId.name}" % "${moduleId.revision}""""
}
val block =
s"""|## Installation
|<!-- Begin autogenerated via sbt generateInstallInstructions -->
|```scala
|${lines.mkString("\n")}
|```
|<!-- End autogenerated via sbt generateInstallInstructions -->
|
|""".stripMargin
val readmePath = os.pwd / "README.md"
val currentReadme = os.read.lines(readmePath)
val (before, rest) = currentReadme.span(_.trim != "## Installation")
val (_, after) = rest.span(s => s.trim == "## Installation" || !s.startsWith("##"))
val newReadme = before.map(_ + "\n").mkString + block + after.map(_ + "\n").mkString
os.write.over(readmePath, newReadme)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment