Skip to content

Commit

Permalink
Improve platform README updating (#127)
Browse files Browse the repository at this point in the history
* Don't update README on release (it's done on every commit to `main` anyway)
* Migrate to using a plugin instead of calling a function inside the provider
  • Loading branch information
turikhay authored Mar 24, 2024
1 parent 2193abe commit fd90c53
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
10 changes: 10 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
plugins {
`kotlin-dsl`
`java-gradle-plugin`
}

repositories {
gradlePluginPortal()
}

gradlePlugin {
plugins {
create("platformReadme") {
id = "platform-readme"
implementationClass = "PlatformReadmePlugin"
}
}
}

dependencies {
// See https://github.com/gradle/gradle/issues/15383
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
Expand Down
64 changes: 39 additions & 25 deletions buildSrc/src/main/kotlin/PlatformReadme.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.create
import java.io.StringWriter

private val PLATFORM_START = "<!-- platform.start -->"
private val PLATFORM_END = "<!-- platform.end -->"
interface PlatformReadmeExtension {
val contents: Property<String>
}

fun generatePlatformReadme(project: Project): String {
val writer = StringWriter()
project.rootProject.file("README.md").reader().use { reader ->
reader.useLines { lines ->
var blockAtLineIndex = -1
for (indexed in lines.withIndex()) {
val lineIndex = indexed.index
val line = indexed.value
if (line == PLATFORM_START) {
blockAtLineIndex = lineIndex
continue
}
if (line == PLATFORM_END) {
blockAtLineIndex = -1
continue
class PlatformReadmePlugin : Plugin<Project> {
private val PLATFORM_START = "<!-- platform.start -->"
private val PLATFORM_END = "<!-- platform.end -->"

private fun generatePlatformReadme(project: Project): String {
val writer = StringWriter()
project.rootProject.file("README.md").reader().use { reader ->
reader.useLines { lines ->
var blockAtLineIndex = -1
for (indexed in lines.withIndex()) {
val lineIndex = indexed.index
val line = indexed.value
if (line == PLATFORM_START) {
blockAtLineIndex = lineIndex
continue
}
if (line == PLATFORM_END) {
blockAtLineIndex = -1
continue
}
if (blockAtLineIndex > 0) {
continue
}
writer.write(line)
writer.write("\n")
}
if (blockAtLineIndex > 0) {
continue
throw IllegalArgumentException("platform.start block was never closed " +
"(see line ${blockAtLineIndex + 1})")
}
writer.write(line)
writer.write("\n")
}
if (blockAtLineIndex > 0) {
throw IllegalArgumentException("platform.start block was never closed " +
"(see line ${blockAtLineIndex + 1})")
}
}
return writer.toString()
}

override fun apply(p: Project) {
val ext = p.extensions.create<PlatformReadmeExtension>("platformReadme")
ext.contents.set(p.provider { generatePlatformReadme(p) })
}
return writer.toString()
}
16 changes: 5 additions & 11 deletions packages/single/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("java-shadow")
id("com.modrinth.minotaur")
id("io.papermc.hangar-publish-plugin")
id("platform-readme")
}

dependencies {
Expand All @@ -20,7 +21,6 @@ val commonChangelog = """
[GitHub](https://github.com/turikhay/MapModCompanion/releases/tag/v${project.version})
""".trimIndent()
val allVersions = provider { rootProject.file("VERSIONS.txt").readLines() }
val platformReadme = provider { generatePlatformReadme(project) }

modrinth {
token = System.getenv("MODRINTH_TOKEN")
Expand All @@ -39,7 +39,7 @@ modrinth {
"release"
}
}
syncBodyFrom = platformReadme
syncBodyFrom = platformReadme.contents
file = dedupShadowJar.singleFile
gameVersions = allVersions
loaders.addAll(listOf(
Expand Down Expand Up @@ -105,7 +105,7 @@ hangarPublish {
}
}
pages {
resourcePage(platformReadme)
resourcePage(platformReadme.contents)
}
}
}
Expand All @@ -115,15 +115,9 @@ tasks {
archiveFileName = "MapModCompanion-shadow.jar"
}
getByName("modrinth") {
dependsOn(
dedupShadowJar,
modrinthSyncBody
)
dependsOn(dedupShadowJar)
}
getByName("publishPluginPublicationToHangar") {
dependsOn(
dedupShadowJar,
getByName("syncAllPluginPublicationPagesToHangar")
)
dependsOn(dedupShadowJar)
}
}

0 comments on commit fd90c53

Please sign in to comment.