All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.gradle_plugins.toolkit.build_logic.process-readme.gradle Maven / Gradle / Ivy

There is a newer version: 0.64.11
Show newest version
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')
            }
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy