From 4e2983de6d0f560de52ec056078c2587673f09a7 Mon Sep 17 00:00:00 2001 From: Oleksii Kucheruk Date: Sun, 20 Aug 2023 18:41:53 +0300 Subject: [PATCH] Improve readability --- README.md | 26 +++++++------- .../local/gradle/BuildConfiguration.kt | 21 ------------ .../co/raccoons/local/gradle/Defaults.kt | 9 ----- .../co/raccoons/local/gradle/GradleBuild.kt | 34 +++++++++++++++++++ .../co/raccoons/local/gradle/Presets.kt | 10 ++++++ .../checkstyle/CheckstyleConfiguration.kt | 12 +++---- .../checkstyle/CheckstyleReportFormat.kt | 4 +-- .../gradle/jacoco/JacocoConfiguration.kt | 11 +++--- .../raccoons/local/gradle/java/Dependency.kt | 11 +++--- .../local/gradle/java/DependencyNotation.kt | 4 +-- .../local/gradle/java/DependencyScope.kt | 4 +-- .../gradle/java/JavaLibraryConfiguration.kt | 14 ++++---- .../local/gradle/java/TestImplementation.kt | 12 +++---- .../local/gradle/javacompile/Version.kt | 15 ++++---- .../gradle/javadoc/JavadocConfiguration.kt | 4 +-- .../local/gradle/javadoc/JavadocTag.kt | 4 +-- .../local/gradle/repository/Repository.kt | 2 ++ .../co/raccoons/local/gradle/test/TestNG.kt | 4 +-- lib/build.gradle.kts | 26 +++++++------- 19 files changed, 112 insertions(+), 115 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/co/raccoons/local/gradle/BuildConfiguration.kt delete mode 100644 buildSrc/src/main/kotlin/co/raccoons/local/gradle/Defaults.kt create mode 100644 buildSrc/src/main/kotlin/co/raccoons/local/gradle/GradleBuild.kt create mode 100644 buildSrc/src/main/kotlin/co/raccoons/local/gradle/Presets.kt diff --git a/README.md b/README.md index 3ddd5af..7c80f62 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ build.gradle.kts --- ~~~Kotlin -import co.raccoons.local.gradle.BuildConfiguration +import co.raccoons.local.gradle.GradleBuild import co.raccoons.local.gradle.checkstyle.CheckstyleConfiguration import co.raccoons.local.gradle.checkstyle.CheckstyleReportFormat import co.raccoons.local.gradle.jacoco.JacocoConfiguration @@ -27,42 +27,42 @@ import co.raccoons.local.gradle.javadoc.JavadocTag import co.raccoons.local.gradle.repository.Repository import co.raccoons.local.gradle.test.TestNG -BuildConfiguration.of(project) +GradleBuild.of(project) .use(Repository.MAVEN_CENTRAL) .use(Repository.MAVEN_LOCAL) - .use(Configuration.javaLibrary()) + .use(Configuration.javaLibrary) .use(Version.JAVA.of(20)) - .use(Configuration.testNG()) - .use(Configuration.jacoco()) - .use(Configuration.javadoc()) - .use(Configuration.checkstyle()) + .use(Configuration.testNG) + .use(Configuration.jacoco) + .use(Configuration.javadoc) + .use(Configuration.checkstyle) -class Configuration { +internal class Configuration { companion object { - fun javaLibrary() = + val javaLibrary = JavaLibraryConfiguration.default() - fun testNG() = + val testNG = TestNG.Builder() .addDependency(TestImplementation("org.testng", "testng", "7.8.0")) .addDependency(TestImplementation("org.slf4j", "slf4j-simple", "2.0.7")) .build() - fun jacoco() = + val jacoco = JacocoConfiguration.Builder() .enable(JacocoReportFormat.HTML) .enable(JacocoReportFormat.XML) .build() - fun javadoc() = + val javadoc = JavadocConfiguration.Builder() .addTag(JavadocTag("apiNote", "API Note")) .addTag(JavadocTag("implSpec", "Implementation Specification")) .addTag(JavadocTag("implNote", "Implementation Note")) .build() - fun checkstyle() = + val checkstyle = CheckstyleConfiguration.Builder() .setVersion("10.12.2") .enable(CheckstyleReportFormat.HTML) diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/BuildConfiguration.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/BuildConfiguration.kt deleted file mode 100644 index e2076c7..0000000 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/BuildConfiguration.kt +++ /dev/null @@ -1,21 +0,0 @@ -package co.raccoons.local.gradle - -import org.gradle.api.Plugin -import org.gradle.api.Project - -class BuildConfiguration private constructor() { - - companion object { - fun of(project: Project): Builder { - return Builder(project) - } - } - - class Builder(private val project: Project) { - - fun use(plugin: Plugin): Builder { - plugin.apply(this.project) - return this - } - } -} diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/Defaults.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/Defaults.kt deleted file mode 100644 index d8c2d0c..0000000 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/Defaults.kt +++ /dev/null @@ -1,9 +0,0 @@ -package co.raccoons.local.gradle - -enum class Defaults(private val version: String) { - CHECKSTYLE("10.12.2"); - - fun version(): String { - return version - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/GradleBuild.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/GradleBuild.kt new file mode 100644 index 0000000..66a9f89 --- /dev/null +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/GradleBuild.kt @@ -0,0 +1,34 @@ +package co.raccoons.local.gradle + +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** + * Gradle build configurator. + */ +class GradleBuild private constructor() { + + companion object { + + /** + * Returns build configuration handler of this project. + */ + fun of(project: Project): ConfigurationHandler { + + /** + * Build configuration handler that applies configuration to project. + */ + class BuildConfigurationHandler(private val project: Project) : ConfigurationHandler { + override fun use(plugin: Plugin): ConfigurationHandler { + plugin.apply(this.project) + return this + } + } + return BuildConfigurationHandler(project) + } + } + + fun interface ConfigurationHandler { + fun use(plugin: Plugin): ConfigurationHandler + } +} diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/Presets.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/Presets.kt new file mode 100644 index 0000000..17d0211 --- /dev/null +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/Presets.kt @@ -0,0 +1,10 @@ +package co.raccoons.local.gradle + +/** + * Plugins version + */ +enum class Presets(private val version: String) { + CHECKSTYLE("10.12.2"); + + fun version() = version +} diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleConfiguration.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleConfiguration.kt index f8b5ee8..245bef2 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleConfiguration.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleConfiguration.kt @@ -1,11 +1,13 @@ package co.raccoons.local.gradle.checkstyle -import co.raccoons.local.gradle.Defaults +import co.raccoons.local.gradle.Presets import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.quality.Checkstyle import org.gradle.api.plugins.quality.CheckstyleExtension +private const val CHECKSTYLE_PLUGIN_ID = "checkstyle" + class CheckstyleConfiguration( private val toolVersion: String, private val reportFormats: List @@ -17,7 +19,7 @@ class CheckstyleConfiguration( } private fun setupPlugin(project: Project) { - project.plugins.apply("checkstyle") + project.plugins.apply(CHECKSTYLE_PLUGIN_ID) val checkstyleExtension = project.extensions.getByType(CheckstyleExtension::class.java) checkstyleExtension.toolVersion = this.toolVersion } @@ -34,7 +36,7 @@ class CheckstyleConfiguration( class Builder { - private var toolVersion = Defaults.CHECKSTYLE.version() + private var toolVersion = Presets.CHECKSTYLE.version() private val enabledFormats = mutableListOf() fun setVersion(version: String): Builder { @@ -47,8 +49,6 @@ class CheckstyleConfiguration( return this } - fun build(): CheckstyleConfiguration { - return CheckstyleConfiguration(this.toolVersion, this.enabledFormats) - } + fun build() = CheckstyleConfiguration(this.toolVersion, this.enabledFormats) } } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleReportFormat.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleReportFormat.kt index 4a6f875..f1a3f64 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleReportFormat.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/checkstyle/CheckstyleReportFormat.kt @@ -8,15 +8,13 @@ enum class CheckstyleReportFormat { override fun subscribeTo(checkstyleReports: CheckstyleReports) { checkstyleReports.html.required.set(true) } - }, XML { override fun subscribeTo(checkstyleReports: CheckstyleReports) { checkstyleReports.xml.required.set(true) } - }; abstract fun subscribeTo(checkstyleReports: CheckstyleReports) -} \ No newline at end of file +} diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/jacoco/JacocoConfiguration.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/jacoco/JacocoConfiguration.kt index 2f22bda..a2b82d8 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/jacoco/JacocoConfiguration.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/jacoco/JacocoConfiguration.kt @@ -4,6 +4,8 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.testing.jacoco.tasks.JacocoReport +private const val JACOCO_PLUGIN_ID = "jacoco" + class JacocoConfiguration(private val reportFormats: List) : Plugin { override fun apply(project: Project) { @@ -12,7 +14,7 @@ class JacocoConfiguration(private val reportFormats: List) : } private fun setupPlugin(project: Project) { - project.plugins.apply("jacoco") + project.plugins.apply(JACOCO_PLUGIN_ID) } private fun enableReports(project: Project) { @@ -36,9 +38,6 @@ class JacocoConfiguration(private val reportFormats: List) : return this } - fun build(): JacocoConfiguration { - return JacocoConfiguration(this.enabledFormats) - } + fun build() = JacocoConfiguration(this.enabledFormats) } - -} \ No newline at end of file +} diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/Dependency.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/Dependency.kt index bff8f22..fb25ff4 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/Dependency.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/Dependency.kt @@ -11,11 +11,8 @@ abstract class Dependency( private val dependencyNotation: DependencyNotation ) { - fun configurationName(): String { - return configurationName - } + fun configurationName() = configurationName + + fun dependencyNotation() = dependencyNotation +} - fun dependencyNotation(): String { - return dependencyNotation.toString() - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyNotation.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyNotation.kt index c28378f..ef65b09 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyNotation.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyNotation.kt @@ -12,7 +12,5 @@ class DependencyNotation( private val version: String ) { - override fun toString(): String { - return "${group}:${name}:${version}" - } + override fun toString() = "${group}:${name}:${version}" } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyScope.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyScope.kt index 349869c..04e3c50 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyScope.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/DependencyScope.kt @@ -11,7 +11,7 @@ import org.gradle.api.Project class DependencyScope : Plugin { - private val dependencies: MutableMap> = mutableMapOf() + private val dependencies = mutableMapOf>() override fun apply(project: Project) { for ((configurationName, configurationDependencies) in dependencies) { @@ -24,6 +24,6 @@ class DependencyScope : Plugin { fun add(dependency: Dependency) { this.dependencies.getOrPut(dependency.configurationName()) { mutableListOf() - }.add(dependency.dependencyNotation()) + }.add(dependency.dependencyNotation().toString()) } } diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/JavaLibraryConfiguration.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/JavaLibraryConfiguration.kt index 5f6840b..0f57cbb 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/JavaLibraryConfiguration.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/JavaLibraryConfiguration.kt @@ -3,13 +3,13 @@ package co.raccoons.local.gradle.java import org.gradle.api.Plugin import org.gradle.api.Project +private const val JAVA_LIBRARY = "java-library" + class JavaLibraryConfiguration(private val dependencyScope: DependencyScope) : Plugin { companion object { - public fun default(): JavaLibraryConfiguration { - return JavaLibraryConfiguration.Builder().build() - } + fun default() = Builder().build() } override fun apply(project: Project) { @@ -17,7 +17,7 @@ class JavaLibraryConfiguration(private val dependencyScope: DependencyScope) : } private fun setupPlugin(project: Project) { - project.plugins.apply("java-library") + project.plugins.apply(JAVA_LIBRARY) dependencyScope.apply(project) } @@ -30,8 +30,6 @@ class JavaLibraryConfiguration(private val dependencyScope: DependencyScope) : return this } - public fun build(): JavaLibraryConfiguration { - return JavaLibraryConfiguration(this.dependencyScope) - } + public fun build() = JavaLibraryConfiguration(this.dependencyScope) } -} \ No newline at end of file +} diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/TestImplementation.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/TestImplementation.kt index d9e6583..a81969a 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/TestImplementation.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/java/TestImplementation.kt @@ -6,10 +6,10 @@ package co.raccoons.local.gradle.java -class TestImplementation( - private val group: String, - private val name: String, - private val version: String, -) : Dependency("testImplementation", DependencyNotation(group, name, version)) { +private const val CONFIGURATION_NAME = "testImplementation" -} \ No newline at end of file +class TestImplementation( + group: String, + name: String, + version: String, +) : Dependency(CONFIGURATION_NAME, DependencyNotation(group, name, version)) diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javacompile/Version.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javacompile/Version.kt index bcb8b0f..6c97898 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javacompile/Version.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javacompile/Version.kt @@ -8,16 +8,13 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion enum class Version { JAVA { - override fun of(version: Int): Plugin { - return Plugin { project -> - + override fun of(version: Int) = Plugin { project -> project.tasks - .withType(JavaCompile::class.java) - .configureEach { javaCompile -> - javaCompile.sourceCompatibility = JavaLanguageVersion.of(version).toString() - javaCompile.targetCompatibility = JavaLanguageVersion.of(version).toString() - } - } + .withType(JavaCompile::class.java) + .configureEach { javaCompile -> + javaCompile.sourceCompatibility = JavaLanguageVersion.of(version).toString() + javaCompile.targetCompatibility = JavaLanguageVersion.of(version).toString() + } } }; diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocConfiguration.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocConfiguration.kt index 4dea73e..0fd01b5 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocConfiguration.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocConfiguration.kt @@ -28,8 +28,6 @@ class JavadocConfiguration(private val tags: List) : Plugin { return this } - fun build(): JavadocConfiguration { - return JavadocConfiguration(this.tags.toList()) - } + fun build() = JavadocConfiguration(this.tags.toList()) } } diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocTag.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocTag.kt index cf00f7f..0e9ace0 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocTag.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/javadoc/JavadocTag.kt @@ -2,7 +2,5 @@ package co.raccoons.local.gradle.javadoc class JavadocTag(private val name: String, private val head: String) { - override fun toString(): String { - return "${name}:a:${head}:" - } + override fun toString() = "${name}:a:${head}:" } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/repository/Repository.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/repository/Repository.kt index 51450c1..1669770 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/repository/Repository.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/repository/Repository.kt @@ -4,11 +4,13 @@ import org.gradle.api.Plugin import org.gradle.api.Project enum class Repository: Plugin { + MAVEN_CENTRAL { override fun apply(project: Project) { project.repositories.mavenCentral() } }, + MAVEN_LOCAL { override fun apply(project: Project) { project.repositories.mavenLocal() diff --git a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/test/TestNG.kt b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/test/TestNG.kt index bc5ba80..217a84b 100644 --- a/buildSrc/src/main/kotlin/co/raccoons/local/gradle/test/TestNG.kt +++ b/buildSrc/src/main/kotlin/co/raccoons/local/gradle/test/TestNG.kt @@ -31,8 +31,6 @@ class TestNG(private val dependencyScope: DependencyScope) : Plugin { return this } - fun build(): TestNG { - return TestNG(this.dependencyScope) - } + fun build() = TestNG(this.dependencyScope) } } \ No newline at end of file diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 8cfdf64..028c295 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -4,7 +4,7 @@ * @license MIT */ -import co.raccoons.local.gradle.BuildConfiguration +import co.raccoons.local.gradle.GradleBuild import co.raccoons.local.gradle.checkstyle.CheckstyleConfiguration import co.raccoons.local.gradle.checkstyle.CheckstyleReportFormat import co.raccoons.local.gradle.jacoco.JacocoConfiguration @@ -17,42 +17,42 @@ import co.raccoons.local.gradle.javadoc.JavadocTag import co.raccoons.local.gradle.repository.Repository import co.raccoons.local.gradle.test.TestNG -BuildConfiguration.of(project) +GradleBuild.of(project) .use(Repository.MAVEN_CENTRAL) .use(Repository.MAVEN_LOCAL) - .use(Configuration.javaLibrary()) + .use(Configuration.javaLibrary) .use(Version.JAVA.of(20)) - .use(Configuration.testNG()) - .use(Configuration.jacoco()) - .use(Configuration.javadoc()) - .use(Configuration.checkstyle()) + .use(Configuration.testNG) + .use(Configuration.jacoco) + .use(Configuration.javadoc) + .use(Configuration.checkstyle) -class Configuration { +internal class Configuration { companion object { - fun javaLibrary() = + val javaLibrary = JavaLibraryConfiguration.default() - fun testNG() = + val testNG = TestNG.Builder() .addDependency(TestImplementation("org.testng", "testng", "7.8.0")) .addDependency(TestImplementation("org.slf4j", "slf4j-simple", "2.0.7")) .build() - fun jacoco() = + val jacoco = JacocoConfiguration.Builder() .enable(JacocoReportFormat.HTML) .enable(JacocoReportFormat.XML) .build() - fun javadoc() = + val javadoc = JavadocConfiguration.Builder() .addTag(JavadocTag("apiNote", "API Note")) .addTag(JavadocTag("implSpec", "Implementation Specification")) .addTag(JavadocTag("implNote", "Implementation Note")) .build() - fun checkstyle() = + val checkstyle = CheckstyleConfiguration.Builder() .setVersion("10.12.2") .enable(CheckstyleReportFormat.HTML)