Last active
April 1, 2020 15:19
-
-
Save nafg/4a7a4876fc057326895adc1dd58f4ef7 to your computer and use it in GitHub Desktop.
Auto-generate your library's install instructions in README
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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