-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import sbt._ | ||
import sbt.Keys._ | ||
import mdoc.MdocPlugin | ||
import com.github.sbt.git.{GitPlugin, JGit} | ||
import com.github.sbt.git.SbtGit.GitKeys | ||
import org.eclipse.jgit.api._ | ||
import org.eclipse.jgit.diff.DiffEntry | ||
import org.eclipse.jgit.treewalk.CanonicalTreeParser | ||
import scala.jdk.CollectionConverters._ | ||
|
||
object ReadmePlugin extends AutoPlugin { | ||
override def requires = MdocPlugin && GitPlugin | ||
object autoImport { | ||
val readmeUpdate = inputKey[Unit]("Update the root README.md") | ||
val readmeBaseRef = settingKey[String]("The base ref to check modification against") | ||
val readmeCheckModification = taskKey[Unit]( | ||
"Check the diff against the target branch for modifications to generated files" | ||
) | ||
val readmeAdditionalFiles = settingKey[Map[File, String]]( | ||
"Additional (generated) files to copy into the root docs/ folder" | ||
) | ||
} | ||
|
||
import autoImport._ | ||
import MdocPlugin.autoImport._ | ||
|
||
override def projectSettings = Seq( | ||
libraryDependencies ++= Seq.empty, | ||
mdocIn := (LocalRootProject / baseDirectory).value / "docs" / "readme.md", | ||
mdocOut := (LocalRootProject / baseDirectory).value / "README.md", | ||
fork := true, | ||
readmeAdditionalFiles := Map.empty, | ||
readmeCheckModification := { | ||
val bref = readmeBaseRef.value | ||
val dir = (LocalRootProject / baseDirectory).value | ||
val additional = readmeAdditionalFiles.value | ||
val readmeIn = mdocIn.value | ||
val readmeOut = mdocOut.value | ||
val logger = streams.value.log | ||
checkModification(logger, bref, dir, readmeIn, readmeOut, additional, dir / "docs") | ||
}, | ||
readmeBaseRef := sys.env | ||
.get("README_BASE_REF") | ||
.map(_.trim) | ||
.filter(_.nonEmpty) | ||
.map(ref => s"${ref}^{tree}") | ||
.getOrElse("HEAD^{tree}"), | ||
readmeUpdate := { | ||
mdoc.evaluated | ||
val logger = streams.value.log | ||
val additional = readmeAdditionalFiles.value | ||
val addout = (LocalRootProject / baseDirectory).value / "docs" | ||
additional.toList.foreach { case (src, name) => | ||
val dest = addout / name | ||
logger.info(s"Copying $src -> $dest") | ||
IO.copyFile(src, dest) | ||
} | ||
() | ||
} | ||
) | ||
|
||
def checkModification( | ||
logger: Logger, | ||
baseRef: String, | ||
sourceRoot: File, | ||
readmeIn: File, | ||
readmeOut: File, | ||
additional: Map[File, String], | ||
additionalOut: File | ||
) = { | ||
val git = JGit(sourceRoot).porcelain | ||
val repo = git.getRepository | ||
val base = repo.resolve(baseRef) | ||
if (base eq null) sys.error(s"Cannot resolve base ref: $baseRef") | ||
val p = new CanonicalTreeParser() | ||
val reader = repo.newObjectReader() | ||
p.reset(reader, base) | ||
val diff = git | ||
.diff() | ||
.setOldTree(p) | ||
.setShowNameOnly(true) | ||
.call() | ||
.asScala | ||
.filter(e => e.getChangeType == DiffEntry.ChangeType.MODIFY) | ||
.map(_.getOldPath) | ||
.toSet | ||
|
||
val checker = Checker(sourceRoot, diff, logger) | ||
|
||
// check readme | ||
checker.check(readmeIn, readmeOut) | ||
// check others | ||
additional.toList.foreach { case (src, targetName) => | ||
val dest = additionalOut / targetName | ||
checker.check(src, dest) | ||
} | ||
() | ||
} | ||
|
||
def relativize(base: File, file: File): String = | ||
base | ||
.relativize(file) | ||
.map(_.toString) | ||
.getOrElse( | ||
sys.error( | ||
s"Cannot obtain path into repository for $file" | ||
) | ||
) | ||
|
||
final case class Checker(sourceRoot: File, diff: Set[String], logger: Logger) { | ||
def check(src: File, dest: File) = { | ||
val srcPath = relativize(sourceRoot, src) | ||
val destPath = relativize(sourceRoot, dest) | ||
logger.info(s"Check modification $srcPath -> $destPath …") | ||
if (diff.contains(srcPath) && !diff.contains(destPath)) { | ||
logger.error(s"You changed $srcPath but did not generate the output file") | ||
sys.error("Checking failed") | ||
} | ||
if (diff.contains(destPath) && !diff.contains(srcPath)) { | ||
logger.error( | ||
s"You changed $destPath but this is a generated file, please change $srcPath instead" | ||
) | ||
sys.error("Checking failed") | ||
} | ||
} | ||
} | ||
} |
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