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

allprojects {
    TaskProvider processReadme = tasks.register('processReadme') { Task task ->
        task.group = 'documentation'
        task.outputs.upToDateWhen { false }

        task.ext.readmeFileProcessors = []

        FileTree files = project.fileTree(project.projectDir) {
            include('README.md')
        }
        task.doLast {
            List readmeFileProcessors = task.ext.readmeFileProcessors
            if (readmeFileProcessors.isEmpty()) {
                return
            }

            files.visit { FileVisitDetails details ->
                if (details.directory) {
                    return
                }

                File readmeFile = details.file
                logger.lifecycle('Processing {}', readmeFile)
                readmeFileProcessors.forEach { fileProcessor -> fileProcessor(readmeFile) }
            }
        }

        ProviderFactory providers = project.providers

        task.ext.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 = unwrapProviders(providers.gradleProperty(property).get())
                    String propertyValue = "$propertyValueObj"
                    if (tag.contains('code-')) {
                        propertyValue = "`$propertyValue`"
                    }
                    return "$propertyValue"
                }

                readmeFile.setText(content, 'UTF-8')
            }
        )

        task.ext.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 = unwrapProviders(providers.gradleProperty(property).get())
                    if (!(propertyValueObj instanceof Iterable)) {
                        throw new GradleException("`$property` property is not iterable: ${propertyValueObj?.class}")
                    }
                    if (tag.contains('code-')) {
                        propertyValueObj = propertyValueObj.collect { "`$it`" }
                    }
                    String propertyValue = propertyValueObj.collect { "\n* $it" }.join('') + '\n'
                    return "$propertyValue"
                }

                readmeFile.setText(content, 'UTF-8')
            }
        )

        task.ext.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')
            }
        )

        task.ext.readmeFileProcessors.add(
            { File readmeFile ->
                String content = readmeFile.getText('UTF-8')

                content = content.replaceAll(
                    /[\s\S]*?/
                ) {
                    String tag = it[1]
                    String pluginId = it[2]
                    logger.lifecycle('  Handling {}: {}', tag, pluginId)
                    String lastVersion = getPluginLastVersionWithCurrentAsFallback(pluginId)
                    String usage = ''
                    if (tag.endsWith('-kotlin')) {
                        usage = "```kotlin\nplugins {\n    id(\"$pluginId\") version \"$lastVersion\"\n}\n```"
                    } else {
                        usage = "```groovy\nplugins {\n    id '$pluginId' version '$lastVersion'\n}\n```"
                    }
                    return "\n$usage\n"
                }

                readmeFile.setText(content, 'UTF-8')
            }
        )
    }

    processReadme.get()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy