Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
iselo committed Aug 20, 2023
1 parent 3531251 commit 4e2983d
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 115 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

This file was deleted.

9 changes: 0 additions & 9 deletions buildSrc/src/main/kotlin/co/raccoons/local/gradle/Defaults.kt

This file was deleted.

34 changes: 34 additions & 0 deletions buildSrc/src/main/kotlin/co/raccoons/local/gradle/GradleBuild.kt
Original file line number Diff line number Diff line change
@@ -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<Project>): ConfigurationHandler {
plugin.apply(this.project)
return this
}
}
return BuildConfigurationHandler(project)
}
}

fun interface ConfigurationHandler {
fun use(plugin: Plugin<Project>): ConfigurationHandler
}
}
10 changes: 10 additions & 0 deletions buildSrc/src/main/kotlin/co/raccoons/local/gradle/Presets.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<CheckstyleReportFormat>
Expand All @@ -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
}
Expand All @@ -34,7 +36,7 @@ class CheckstyleConfiguration(

class Builder {

private var toolVersion = Defaults.CHECKSTYLE.version()
private var toolVersion = Presets.CHECKSTYLE.version()
private val enabledFormats = mutableListOf<CheckstyleReportFormat>()

fun setVersion(version: String): Builder {
Expand All @@ -47,8 +49,6 @@ class CheckstyleConfiguration(
return this
}

fun build(): CheckstyleConfiguration {
return CheckstyleConfiguration(this.toolVersion, this.enabledFormats)
}
fun build() = CheckstyleConfiguration(this.toolVersion, this.enabledFormats)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<JacocoReportFormat>) : Plugin<Project> {

override fun apply(project: Project) {
Expand All @@ -12,7 +14,7 @@ class JacocoConfiguration(private val reportFormats: List<JacocoReportFormat>) :
}

private fun setupPlugin(project: Project) {
project.plugins.apply("jacoco")
project.plugins.apply(JACOCO_PLUGIN_ID)
}

private fun enableReports(project: Project) {
Expand All @@ -36,9 +38,6 @@ class JacocoConfiguration(private val reportFormats: List<JacocoReportFormat>) :
return this
}

fun build(): JacocoConfiguration {
return JacocoConfiguration(this.enabledFormats)
}
fun build() = JacocoConfiguration(this.enabledFormats)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ class DependencyNotation(
private val version: String
) {

override fun toString(): String {
return "${group}:${name}:${version}"
}
override fun toString() = "${group}:${name}:${version}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.gradle.api.Project

class DependencyScope : Plugin<Project> {

private val dependencies: MutableMap<String, MutableList<String>> = mutableMapOf()
private val dependencies = mutableMapOf<String, MutableList<String>>()

override fun apply(project: Project) {
for ((configurationName, configurationDependencies) in dependencies) {
Expand All @@ -24,6 +24,6 @@ class DependencyScope : Plugin<Project> {
fun add(dependency: Dependency) {
this.dependencies.getOrPut(dependency.configurationName()) {
mutableListOf()
}.add(dependency.dependencyNotation())
}.add(dependency.dependencyNotation().toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ 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<Project> {

companion object {
public fun default(): JavaLibraryConfiguration {
return JavaLibraryConfiguration.Builder().build()
}
fun default() = Builder().build()
}

override fun apply(project: Project) {
this.setupPlugin(project)
}

private fun setupPlugin(project: Project) {
project.plugins.apply("java-library")
project.plugins.apply(JAVA_LIBRARY)
dependencyScope.apply(project)
}

Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}
class TestImplementation(
group: String,
name: String,
version: String,
) : Dependency(CONFIGURATION_NAME, DependencyNotation(group, name, version))
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
enum class Version {

JAVA {
override fun of(version: Int): Plugin<Project> {
return Plugin<Project> { project ->

override fun of(version: Int) = Plugin<Project> { 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()
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class JavadocConfiguration(private val tags: List<String>) : Plugin<Project> {
return this
}

fun build(): JavadocConfiguration {
return JavadocConfiguration(this.tags.toList())
}
fun build() = JavadocConfiguration(this.tags.toList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}:"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import org.gradle.api.Plugin
import org.gradle.api.Project

enum class Repository: Plugin<Project> {

MAVEN_CENTRAL {
override fun apply(project: Project) {
project.repositories.mavenCentral()
}
},

MAVEN_LOCAL {
override fun apply(project: Project) {
project.repositories.mavenLocal()
Expand Down
Loading

0 comments on commit 4e2983d

Please sign in to comment.