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

First draft for an automatic README.md version update #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions src/main/scala/ReleaseExtra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,48 @@ object ReleaseStateTransformations {
) getOrElse commentState
}


lazy val updateReadme: ReleaseStep = ReleaseStep(updateReadmeStep)
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
}

lazy val commitReadme: ReleaseStep = ReleaseStep(commitReadmeStep)
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
}

lazy val pushChanges: ReleaseStep = ReleaseStep(pushChangesAction, checkUpstream)
private[sbtrelease] lazy val checkUpstream = { st: State =>
if (!vcs(st).hasUpstream) {
Expand Down
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-release/update-readme/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
global/
7 changes: 7 additions & 0 deletions src/sbt-test/sbt-release/update-readme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A project

Add library in your `build.sbt`

```scala
libraryDependencies += "com.example" %% "lib" % "1.0.0"
```
32 changes: 32 additions & 0 deletions src/sbt-test/sbt-release/update-readme/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sbtrelease.ReleaseStateTransformations._


releaseProcess := Seq(
checkSnapshotDependencies,
inquireVersions,
runClean,
runTest,
setReleaseVersion,
commitReleaseVersion,
tagRelease,
updateReadme,
commitReadme,
setNextVersion,
commitNextVersion
)

TaskKey[Unit]("checkReadme") := {
val readmeFile = baseDirectory.value / "README.md"
val content = IO.read(readmeFile)

assert(content ==
"""|# A project
|
|Add library in your `build.sbt`
|
|```scala
|libraryDependencies += "com.example" %% "lib" % "2.3.4"
|```
|""".stripMargin, s"Readme wasn't updated correctly\n\n$content")

}
7 changes: 7 additions & 0 deletions src/sbt-test/sbt-release/update-readme/project/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("com.github.gseitz" % "sbt-release" % pluginVersion)
}
10 changes: 10 additions & 0 deletions src/sbt-test/sbt-release/update-readme/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$ exec git init .

> update

$ exec git add .
$ exec git commit -m init

> release release-version 2.3.4 next-version 2.3.5-SNAPSHOT

> checkReadme
1 change: 1 addition & 0 deletions src/sbt-test/sbt-release/update-readme/version.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version in ThisBuild := "1.0.0"