name.remal.gradle_plugins.toolkit.build_logic.process-readme.gradle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of build-logic Show documentation
Show all versions of build-logic Show documentation
Remal Gradle plugins: toolkit: build-logic
allprojects {
tasks.create('processReadme') { Task task ->
task.group = 'documentation'
task.outputs.upToDateWhen { false }
List readmeFileProcessors = []
task.ext.readmeFileProcessors = readmeFileProcessors
task.doLast {
if (readmeFileProcessors.isEmpty()) return
FileTree files = project.fileTree(project.projectDir) {
include('README.md')
}
files.visit { FileVisitDetails details ->
if (details.directory) return
File readmeFile = details.file
logger.lifecycle('Processing {}', readmeFile)
readmeFileProcessors.forEach { fileProcessor -> fileProcessor(readmeFile) }
}
}
readmeFileProcessors.add(
{ File readmeFile ->
String content = readmeFile.getText('UTF-8')
content = content.replaceAll(
/[\s\S]*?/
) {
String tag = it[1]
String property = it[2]
logger.lifecycle(' Handling {}: {}', tag, property)
Object propertyValueObj = project.property(property)
if (propertyValueObj instanceof Provider) {
propertyValueObj = propertyValueObj.getOrNull()
}
String propertyValue = propertyValueObj?.toString()
return "$propertyValue"
}
readmeFile.setText(content, 'UTF-8')
}
)
readmeFileProcessors.add(
{ File readmeFile ->
String content = readmeFile.getText('UTF-8')
content = content.replaceAll(
/[\s\S]*?/
) {
String tag = it[1]
String lang = it[2] ?: ''
String filePath = it[3]
logger.lifecycle(' Handling {}{}: {}', tag, lang.isEmpty() ? '' : " ($lang)", filePath)
File file = readmeFile.parentFile.toPath().resolve(filePath).toFile()
String includeContent = file.getText('UTF-8')
.replace('\r\n', '\n')
.replace('\n\r', '\n')
.replace('\r', '\n')
while (includeContent.startsWith('\n')) {
includeContent = includeContent.substring(1)
}
while (includeContent.endsWith('\n')) {
includeContent = includeContent.substring(0, includeContent.length() - 1)
}
return "\n```$lang\n$includeContent\n```\n"
}
readmeFile.setText(content, 'UTF-8')
}
)
}
}