-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve platform README updating (#127)
* 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
Showing
3 changed files
with
54 additions
and
36 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
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() | ||
} |
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