1

将 gradle 构建代码块提取到外部脚本的推荐方法是什么?请注意,这些脚本应该支持对 gradle 项目、extra 等的引用。所以 - 在 buildSrc 中编译 kt 文件不是我想要的。

我尝试创建类似的文件logger.gradle.kts,这些文件似乎“知道”项目 ref / compile,但是fun我在其中编写的任何内容在主构建文件中都不可引用,尽管我这样应用:

apply (from = "logger.gradle.kts")

作为构建的一部分,我得到的错误是:

Unresolved reference: logInfo-在logInfo哪里。funlogger.gradle.kts

这是我正在使用的记录器文件:

import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutputFactory
import org.gradle.internal.logging.services.DefaultStyledTextOutputFactory
import org.gradle.internal.logging.text.StyledTextOutput.Style



fun <R> callBasedOnContext(
    ifBuildScript: KotlinBuildScript.() -> R,
    ifSettingsScript: KotlinSettingsScript.() -> R
): R {
    /*
     * A bit of a hack to get around a compiler error when trying to do
     * `is KotlinBuildScript` and `is KotlinSettingsScript`.
     */
    val kotlinProjectClass = KotlinBuildScript::class
    val kotlinSettingsClass = KotlinSettingsScript::class

    return when {
        kotlinProjectClass.isInstance(this) -> (this as KotlinBuildScript).ifBuildScript()
        kotlinSettingsClass.isInstance(this) -> (this as KotlinSettingsScript).ifSettingsScript()
        else -> throw AssertionError("$this is not being applied to a supported type.")
    }
}

val extra: ExtraPropertiesExtension by lazy {
    callBasedOnContext(
        ifBuildScript = { extra },
        ifSettingsScript = { (settings as ExtensionAware).extra }
    )
}

fun hasPropertyHelper(propertyName: String): Boolean {
    return callBasedOnContext(
        ifBuildScript = { hasProperty(propertyName) },
        ifSettingsScript = { (settings as ExtensionAware).extra.properties.containsKey(propertyName) }
    )
}

fun propertyHelper(propertyName: String): Any? {
    return callBasedOnContext(
        ifBuildScript = { property(propertyName) },
        ifSettingsScript = { (settings as ExtensionAware).extra.properties[propertyName] }
    )
}

extra["logDebug"] = this::logDebug
extra["logInfo"] = this::logInfo
extra["logWarn"] = this::logWarn
extra["logError"] = this::logError
extra["logTitle"] = this::logTitle
extra["logStyles"] = this::logStyles

val loggerOut = DefaultStyledTextOutputFactory(null, null).create("styled_output")
val loggerOutError = loggerOut.withStyle(Style.Failure)
val loggerOutWarn = loggerOut.withStyle(Style.Description)
val loggerOutInfo = loggerOut.withStyle(Style.Success)
val loggerOutDebug = loggerOut.withStyle(Style.Normal)
val loggerOutTitle = loggerOut.withStyle(Style.Header)

fun log(message: String, out: StyledTextOutput) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        project.logger.quiet(message)
    } else {
        out.println(message)
    }
}

fun logTitle(message: String) {
    log("\n---------------------------------------------", loggerOutTitle)
    log("  ${message.toUpperCase()}", loggerOutTitle)
    log("---------------------------------------------", loggerOutTitle)
}

fun logDebug(message: String) {
    log("[DEBUG] " + message, loggerOutDebug)
}

fun logInfo(message: String) {
    log("[INFO] " + message, loggerOutInfo)
}

fun logWarn(message: String) {
    log("[WARN] " + message, loggerOutWarn)
}

fun logError(message: String) {
    log("[ERROR] " + message, loggerOutError)
}

fun logStyles() {
    val out = DefaultStyledTextOutputFactory(null, null).create("styled_test")

    log("Style: Normal", out.withStyle(Style.Normal))
    log("Style: Header", out.withStyle(Style.Header))
    log("Style: UserInput", out.withStyle(Style.UserInput))
    log("Style: Identifier", out.withStyle(Style.Identifier))
    log("Style: Description", out.withStyle(Style.Description))
    log("Style: ProgressStatus", out.withStyle(Style.ProgressStatus))
    log("Style: Success", out.withStyle(Style.Success))
    log("Style: SuccessHeader", out.withStyle(Style.SuccessHeader))
    log("Style: Failure", out.withStyle(Style.Failure))
    log("Style: FailureHeader", out.withStyle(Style.FailureHeader))
    log("Style: Info", out.withStyle(Style.Info))
    log("Style: Error", out.withStyle(Style.Error))
}

以及 build.gradle.kts 中的用法:

 plugins {
    base
    idea
    `java-library`
    scala
    apply(from = "gradle_scripts/logger.gradle.kts")
}

并像这样调用:

logInfo("Application Version: ${version}")

该方法失败:

Unresolved reference: logInfo

注意:如果我将函数添加到extra(这是我在 hos 某处看到的在脚本文件之间公开方法的建议):

extra["logDebug"] = this::logDebug
extra["logInfo"] = this::logInfo
extra["logWarn"] = this::logWarn
extra["logError"] = this::logError
extra["logTitle"] = this::logTitle
extra["logStyles"] = this::logStyles

然后它失败了:

Logger_gradle@a1f3cf9 is not being applied to a supported type.
4

1 回答 1

0

如果我没记错的话,Gradle Kotlin DSL 不支持这个操作。

如果您尝试将“冗余”public修饰符添加到logInfo(...)

public fun logInfo(s: String) {
    println(s)
}

你会得到一个编译错误:

修饰符“public”不适用于“local function”

推荐的方法是利用扩展功能,例如:

val Project.isSnapshotBuild
    get() = (version as String).contains("snapshot", true)

在此处查看完整的示例。

于 2021-12-31T07:40:01.067 回答