将渐变构建代码块提取到外部脚本的推荐方法是什么?注意,这些脚本应该支持对gradle项目、extra等的引用。所以,在buildSrc中编译kt文件不是我想要的。
我尝试过创建像CCD_ 1这样的文件;知道";项目引用/编译,但我在其中编写的任何fun
在主构建文件中都是不可引用的,尽管我是这样应用的:
`apply (from = "logger.gradle.kts")`
作为构建的一部分,我得到的错误是:
Unresolved reference: logInfo
-其中logInfo
是logger.gradle.kts
中的fun
。
这是我正在使用的记录器文件:
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.
Gradle Kotlin DSL不支持此操作,如果我没有错的话。
如果您尝试添加一个";冗余的";public
对logInfo(...)
:的修改
public fun logInfo(s: String) {
println(s)
}
你会得到一个编译错误:
修饰符"public"不适用于"本地函数">
推荐的方法是利用扩展函数,例如:
val Project.isSnapshotBuild
get() = (version as String).contains("snapshot", true)
请在此处查看完整的样本。