From 0c3d9dcc2b13ef51088dcd91f0236a8067822c28 Mon Sep 17 00:00:00 2001 From: carrypan Date: Tue, 9 Mar 2021 21:02:04 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20copyToRelease=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=8D=E5=B8=A6=E7=89=88=E6=9C=AC=E5=8F=B7=20#54?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/devops/DevOpsBootPlugin.kt | 52 ++++--------------- .../kotlin/com/tencent/devops/Extensions.kt | 35 +++++++++++++ .../devops/actions/CopyToReleaseAction.kt | 30 +++++++++++ .../devops/actions/KtLintCheckAction.kt | 31 +++++++++++ .../devops/actions/KtLintFormatAction.kt | 31 +++++++++++ devops-boot-sample/build.gradle.kts | 6 +-- 6 files changed, 141 insertions(+), 44 deletions(-) create mode 100644 devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/Extensions.kt create mode 100644 devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/CopyToReleaseAction.kt create mode 100644 devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintCheckAction.kt create mode 100644 devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintFormatAction.kt diff --git a/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/DevOpsBootPlugin.kt b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/DevOpsBootPlugin.kt index 56df360..5348e97 100644 --- a/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/DevOpsBootPlugin.kt +++ b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/DevOpsBootPlugin.kt @@ -1,5 +1,8 @@ package com.tencent.devops +import com.tencent.devops.actions.CopyToReleaseAction +import com.tencent.devops.actions.KtLintCheckAction +import com.tencent.devops.actions.KtLintFormatAction import io.spring.gradle.dependencymanagement.DependencyManagementPlugin import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension import org.gradle.api.JavaVersion @@ -132,29 +135,8 @@ class DevOpsBootPlugin : Plugin { project.run { val ktLint = configurations.create("ktlint") dependencies.add("ktlint", "com.pinterest:ktlint:0.37.2") - - val outputDir = "$buildDir/reports/ktlint/" - val inputFiles = fileTree(mapOf("dir" to "src", "include" to "**/*.kt")) - - tasks.create("ktlintCheck", JavaExec::class.java) { - it.group = "verification" - it.inputs.files(inputFiles) - it.outputs.dir(outputDir) - it.description = "Check Kotlin code style." - it.classpath = ktLint - it.main = "com.pinterest.ktlint.Main" - it.args = listOf("src/**/*.kt") - } - - tasks.create("ktlintFormat", JavaExec::class.java) { - it.group = "formatting" - it.inputs.files(inputFiles) - it.outputs.dir(outputDir) - it.description = "Fix Kotlin code style deviations." - it.classpath = ktLint - it.main = "com.pinterest.ktlint.Main" - it.args = listOf("-F", "src/**/*.kt") - } + tasks.create(KtLintCheckAction.TASK_NAME, JavaExec::class.java, KtLintCheckAction(ktLint)) + tasks.create(KtLintFormatAction.TASK_NAME, JavaExec::class.java, KtLintFormatAction(ktLint)) } } @@ -162,15 +144,9 @@ class DevOpsBootPlugin : Plugin { * 配置copyToRelease task */ private fun configureCopyToRelease(project: Project) { - project.run { - val copyToRelease = tasks.register("copyToRelease", Copy::class.java) { copy -> - copy.from("build/libs") { - it.include("**/*.jar") - } - copy.into("${project.rootDir}/release") - copy.outputs.upToDateWhen { false } - } - tasks.getByName("build").dependsOn(copyToRelease) + with(project.tasks) { + val copyToRelease = register(CopyToReleaseAction.TASK_NAME, Copy::class.java, CopyToReleaseAction()) + getByName("build").dependsOn(copyToRelease) } } @@ -178,20 +154,14 @@ class DevOpsBootPlugin : Plugin { * 查找java version, 默认1.8 */ private fun findJavaVersion(project: Project): String { - return if (project.hasProperty(DEVOPS_JAVA_VERSION)) { - project.property(DEVOPS_JAVA_VERSION).toString() - } else { - JavaVersion.VERSION_1_8.toString() - } + return project.findPropertyOrDefault(DEVOPS_JAVA_VERSION, JavaVersion.VERSION_1_8.toString()) } /** * 查找是否配置kotlin支持,默认开启 */ private fun isKotlinSupport(project: Project): Boolean { - return if (project.hasProperty(DEVOPS_KOTLIN)) { - project.property(DEVOPS_KOTLIN).toString().toBoolean() - } else true + return project.findPropertyOrNull(DEVOPS_KOTLIN)?.toBoolean() ?: true } /** @@ -215,7 +185,7 @@ class DevOpsBootPlugin : Plugin { private const val DEVOPS_JAVA_VERSION = "devops.javaVersion" /** - * DevOps Java 版本号 + * 是否开启kotlin支持 */ private const val DEVOPS_KOTLIN = "devops.kotlin" diff --git a/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/Extensions.kt b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/Extensions.kt new file mode 100644 index 0000000..38d2667 --- /dev/null +++ b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/Extensions.kt @@ -0,0 +1,35 @@ +package com.tencent.devops + +import org.gradle.api.Project + +/** + * 查找属性, 如果[name]不存在则返回null。 + * gradle提供的findProperty方法支持从gradle.property和命令行参数-Pkey=value获取, 后者优先级更高。此扩展方法增加了获取属性的范围 + * 优先级如下: + * 1. 命令行 -Dkey=value + * 2. 系统环境变量key=value + * 3. 命令行 -Pkey=value + * 4. 配置文件gradle.properties key=value + */ +fun Project.findPropertyOrNull(name: String): String? { + return System.getProperty(name) ?: System.getenv(name) ?: run { + if (hasProperty(name)) { + return property(name).toString() + } else null + } +} + +/** + * 查找属性, 如果[name]不存在则返回空字符串 + */ +fun Project.findPropertyOrEmpty(name: String): String { + return findPropertyOrNull(name) ?: "" +} + +/** + * 查找属性, 如果[name]不存在则返回默认值[default] + */ +fun Project.findPropertyOrDefault(name: String, default: String): String { + return findPropertyOrNull(name) ?: default +} + diff --git a/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/CopyToReleaseAction.kt b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/CopyToReleaseAction.kt new file mode 100644 index 0000000..db61a7e --- /dev/null +++ b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/CopyToReleaseAction.kt @@ -0,0 +1,30 @@ +package com.tencent.devops.actions + +import com.tencent.devops.findPropertyOrEmpty +import org.gradle.api.Action +import org.gradle.api.tasks.Copy +import org.springframework.boot.gradle.tasks.bundling.BootJar + +/** + * copy to release action + */ +class CopyToReleaseAction: Action { + + override fun execute(copy: Copy) { + with(copy) { + val withVersion = project.findPropertyOrEmpty(COPY_WITH_VERSION).toBoolean() + val bootJar = project.tasks.withType(BootJar::class.java).first() + from(bootJar.archiveFile) + into("${project.rootDir}/release") + if (!withVersion) { + rename { it.replace("-${project.version}", "") } + } + outputs.upToDateWhen { false } + } + } + + companion object { + const val TASK_NAME = "copyToRelease" + private const val COPY_WITH_VERSION = "devops.copyWithVersion" + } +} diff --git a/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintCheckAction.kt b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintCheckAction.kt new file mode 100644 index 0000000..f2611b6 --- /dev/null +++ b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintCheckAction.kt @@ -0,0 +1,31 @@ +package com.tencent.devops.actions + +import org.gradle.api.Action +import org.gradle.api.file.FileCollection +import org.gradle.api.tasks.JavaExec + +/** + * ktlint check action + */ +class KtLintCheckAction( + private val ktLint: FileCollection +): Action { + + override fun execute(javaExec: JavaExec) { + with(javaExec) { + val outputDir = "${project.buildDir}/reports/ktlint/" + val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt")) + group = "verification" + inputs.files(inputFiles) + outputs.dir(outputDir) + description = "Check Kotlin code style." + classpath = ktLint + main = "com.pinterest.ktlint.Main" + args = listOf("src/**/*.kt") + } + } + + companion object { + const val TASK_NAME = "ktlintCheck" + } +} diff --git a/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintFormatAction.kt b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintFormatAction.kt new file mode 100644 index 0000000..63df94b --- /dev/null +++ b/devops-boot-project/devops-boot-tools/devops-boot-gradle-plugin/src/main/kotlin/com/tencent/devops/actions/KtLintFormatAction.kt @@ -0,0 +1,31 @@ +package com.tencent.devops.actions + +import org.gradle.api.Action +import org.gradle.api.file.FileCollection +import org.gradle.api.tasks.JavaExec + +/** + * ktlint format action + */ +class KtLintFormatAction( + private val ktLint: FileCollection +): Action { + + override fun execute(javaExec: JavaExec) { + with(javaExec) { + val outputDir = "${project.buildDir}/reports/ktlint/" + val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt")) + group = "formatting" + inputs.files(inputFiles) + outputs.dir(outputDir) + description = "Fix Kotlin code style deviations." + classpath = ktLint + main = "com.pinterest.ktlint.Main" + args = listOf("-F", "src/**/*.kt") + } + } + + companion object { + const val TASK_NAME = "ktlintFormat" + } +} diff --git a/devops-boot-sample/build.gradle.kts b/devops-boot-sample/build.gradle.kts index 5798691..8a35e96 100644 --- a/devops-boot-sample/build.gradle.kts +++ b/devops-boot-sample/build.gradle.kts @@ -2,12 +2,12 @@ plugins { id("com.tencent.devops.boot") version "0.0.4-SNAPSHOT" } -group="com.tencent.devops.sample" -version="1.0.0-SNAPSHOT" allprojects { - apply(plugin = "com.tencent.devops.boot") + group="com.tencent.devops.sample" + version="1.0.0-SNAPSHOT" + apply(plugin = "com.tencent.devops.boot") repositories { mavenLocal() mavenCentral()