Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme during relaese #1084

Merged
merged 1 commit into from
Jan 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ releaseProcess := Seq[ReleaseStep](
setReleaseVersion,
commitReleaseVersion,
tagRelease,
updateReadme,
commitReadme,
releaseStepCommandAndRemaining("^ publishSigned"),
setNextVersion,
commitNextVersion,
Expand Down
105 changes: 105 additions & 0 deletions project/ReadmeReleasePlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import sbt._
import sbt.Keys._
import sbt.complete._
import sbt.complete.DefaultParsers._
import sbtrelease.ReleasePlugin.autoImport._
import sbtrelease.{ReleasePlugin, Vcs}

import scala.sys.process._

/**
* == ReadmeRelease Plugin ==
*
* Changes the version in the README.md during a release.
*
*/
object ReadmeReleasePlugin extends AutoPlugin {

override def requires: Plugins = ReleasePlugin

override def trigger = AllRequirements

object autoImport {

/**
* Update the readme file during a release
*/
val updateReadme = ReleaseStep(updateReadmeStep)

/**
* Commits the readme changes.
*/
val commitReadme = ReleaseStep(commitReadmeStep)

}


private def updateReadmeStep(state: State): State = {
val extracted = Project.extract(state)
val releaseVersion = extracted.get(version)
val base = extracted.get(baseDirectory)
val readmeFile = base / "README.md"

val versionRegex = """(\d{1,2}\.\d{1,2}\.\d{1,2})""".r
val updatedReadmeContent = versionRegex.replaceAllIn(
IO.read(readmeFile),
releaseVersion
)

IO.write(readmeFile, updatedReadmeContent)


state
}

private def commitReadmeStep(state: State): State = {
val log = toProcessLogger(state)
val base = vcs(state).baseDir
val sign = Project.extract(state).get(releaseVcsSign)
val readmeFile = base / "README.md"

val relativePath = IO
.relativize(base, readmeFile)
.getOrElse(
"Version file [%s] is outside of this VCS repository with base directory [%s]!" format (readmeFile, base)
)

vcs(state).add(relativePath) !! log
val vcsAddOutput = (vcs(state).status !!).trim
if (vcsAddOutput.isEmpty) {
state.log.info("README.md hasn't been changed.")
} else {
vcs(state).commit("Update release version in readme", sign) ! log
}

state
}

/**
* Extracts the used vcs.
*
* Copied from the sbt-release plugin.
* @param state sbt state
* @return vcs implementation
*/
private def vcs(state: State): Vcs =
Project
.extract(state)
.get(releaseVcs)
.getOrElse(sys.error("Aborting release. Working directory is not a repository of a recognized VCS."))

/**
* Creates a ProcessLogger from the current sbt state.
*
* Copied from the sbt-release plugin.
* @param state
* @return a process logger
*/
private def toProcessLogger(state: State): ProcessLogger = new ProcessLogger {
override def err(s: => String): Unit = state.log.info(s)
override def out(s: => String): Unit = state.log.info(s)
override def buffer[T](f: => T): T = state.log.buffer(f)
}


}