Skip to content
This repository has been archived by the owner on Mar 2, 2023. It is now read-only.

Commit

Permalink
[1.3.0] Configure Spotless for Kotlin Gradle, fix tests, misc. updates
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Feb 20, 2023
1 parent eaef693 commit e0f0773
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 107 deletions.
25 changes: 25 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* SOFTWARE.
*/

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.noelware.infra.gradle.*
import dev.floofy.utils.gradle.*

Expand Down Expand Up @@ -124,6 +126,29 @@ tasks {
)
}
}

withType<Test>().configureEach {
useJUnitPlatform()
outputs.upToDateWhen { false }

maxParallelForks = Runtime.getRuntime().availableProcessors()
failFast = true
testLogging {
events(
TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STARTED
)

exceptionFormat = TestExceptionFormat.FULL
showStackTraces = true
showExceptions = true
showCauses = true
}
}
}

publishing {
Expand Down
32 changes: 32 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 🐻‍❄️🐘 gradle-infra: Gradle plugin to configure sane defaults for Noelware's Gradle projects
* Copyright (c) 2023 Noelware, LLC. <team@noelware.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

pluginManagement {
repositories {
gradlePluginPortal()
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0"
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Metadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ package org.noelware.infra.gradle
import org.gradle.api.JavaVersion
import dev.floofy.utils.gradle.*

val VERSION = Version(1, 2, 0, showPatchNumber = true)
val VERSION = Version(1, 3, 0, showPatchNumber = true)
val JAVA_VERSION = JavaVersion.VERSION_17
20 changes: 2 additions & 18 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
* SOFTWARE.
*/

@file:Suppress("UnstableApiUsage")

import org.gradle.toolchains.foojay.FoojayToolchainResolver
import org.noelware.infra.gradle.toolchains.NoelwareJvmToolchainResolver

rootProject.name = "gradle-infra"

pluginManagement {
Expand All @@ -40,22 +35,11 @@ pluginManagement {

buildscript {
dependencies {
classpath("org.noelware.gradle:gradle-infra:1.1.1")
classpath("org.noelware.gradle:gradle-infra-plugin:1.2.0")
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver") version "0.4.0"
id("org.noelware.gradle.settings") version "1.1.1"
id("org.noelware.gradle.settings") version "1.2.0"
id("com.gradle.enterprise") version "3.12.3"
}

toolchainManagement {
jvm {
javaRepositories {
repository("noelware") {
resolverClass.set(NoelwareJvmToolchainResolver::class.java)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.testing.Test;
import org.gradle.api.tasks.testing.logging.TestExceptionFormat;
Expand All @@ -44,12 +43,10 @@
public class JavaModulePlugin implements Plugin<Project> {
@Override
public void apply(@NotNull Project project) {
final Logger log = project.getLogger();
final NoelwareModuleExtension ext = project.getExtensions().findByType(NoelwareModuleExtension.class) != null
? project.getExtensions().findByType(NoelwareModuleExtension.class)
: project.getExtensions().create("noelware", NoelwareModuleExtension.class);

log.info("Initializing Java module...");
project.getPlugins().apply("java");
project.getPlugins().apply("com.diffplug.spotless");

Expand Down Expand Up @@ -88,27 +85,25 @@ public void apply(@NotNull Project project) {
});

// configure junit tests if needed
if (ext.getUnitTests().getOrElse(false)) {
project.getTasks().withType(Test.class).configureEach((test) -> {
test.useJUnitPlatform();
test.getOutputs().upToDateWhen((a) -> false);
test.setMaxParallelForks(Runtime.getRuntime().availableProcessors());
test.setFailFast(true);
test.testLogging((logging) -> {
logging.events(
TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STARTED);
project.getTasks().withType(Test.class).configureEach((test) -> {
test.useJUnitPlatform();
test.getOutputs().upToDateWhen((a) -> false);
test.setMaxParallelForks(Runtime.getRuntime().availableProcessors());
test.setFailFast(true);
test.testLogging((logging) -> {
logging.events(
TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STARTED);

logging.setShowCauses(true);
logging.setShowStandardStreams(true);
logging.setShowExceptions(true);
logging.setExceptionFormat(TestExceptionFormat.FULL);
});
logging.setShowCauses(true);
logging.setShowStandardStreams(true);
logging.setShowExceptions(true);
logging.setExceptionFormat(TestExceptionFormat.FULL);
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.*;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.testing.Test;
import org.gradle.api.tasks.testing.logging.TestExceptionFormat;
Expand All @@ -49,52 +45,68 @@
public class KotlinModulePlugin implements Plugin<Project> {
@Override
public void apply(@NotNull Project project) {
final Logger log = project.getLogger();
final NoelwareModuleExtension ext = project.getExtensions().findByType(NoelwareModuleExtension.class) != null
? project.getExtensions().findByType(NoelwareModuleExtension.class)
: project.getExtensions().create("noelware", NoelwareModuleExtension.class);

assert ext != null : "extension couldn't be created";

final JavaVersion javaVersion = ext.getMinimumJavaVersion().getOrElse(JavaVersion.VERSION_17);

log.info("Initializing Kotlin module...");
project.getPlugins().apply("org.jetbrains.kotlin.jvm");
project.getPlugins().apply("com.diffplug.spotless");

if (project.getPlugins().hasPlugin("org.jetbrains.kotlin.plugin.serialization")) {
project.getPlugins().apply("org.jetbrains.kotlin.plugin.serialization");
}
project.getPlugins().apply("org.jetbrains.kotlin.jvm");

// Configure Spotless
project.getExtensions().configure(SpotlessExtension.class, (spotless) -> {
spotless.kotlin((kotlin) -> {
String license;
try {
license = ext.getLicense()
.convention(Licenses.MIT)
.get()
.getTemplate(
ext.getProjectName()
.getOrElse(project.getRootProject().getName()),
ext.getProjectDescription()
.getOrElse(
project.getDescription() != null
? project.getDescription()
: "fill this out"),
ext.getCurrentYear()
.getOrElse(String.valueOf(
Calendar.getInstance().get(Calendar.YEAR))),
ext.getProjectEmoji().getOrElse(""));
} catch (IOException e) {
throw new GradleException("Unable to generate license", e);
}

spotless.kotlin(kotlin -> {
kotlin.licenseHeader(license);
kotlin.endWithNewline();
kotlin.encoding("UTF-8");
kotlin.target("**/*.kt");

try {
kotlin.licenseHeader(ext.getLicense()
.convention(Licenses.MIT)
.get()
.getTemplate(
ext.getProjectName()
.getOrElse(project.getRootProject().getName()),
ext.getProjectDescription()
.getOrElse(
project.getDescription() != null
? project.getDescription()
: "fill this out"),
ext.getCurrentYear()
.getOrElse(String.valueOf(
Calendar.getInstance().get(Calendar.YEAR))),
ext.getProjectEmoji().getOrElse("")));
kotlin.ktlint()
.setUseExperimental(true)
.setEditorConfigPath(
new File(project.getRootProject().getProjectDir(), ".editorconfig"));
} catch (IOException e) {
throw new GradleException("Unable to apply Ktlint to Spotless", e);
}
});

spotless.kotlinGradle(kotlin -> {
kotlin.licenseHeader(license, "(@file|import |pluginManagement|plugins)");
kotlin.endWithNewline();
kotlin.encoding("UTF-8");
kotlin.target("**/*.gradle.kts");

kotlin.trimTrailingWhitespace();
kotlin.endWithNewline();
kotlin.encoding("UTF-8");
try {
kotlin.ktlint()
.setUseExperimental(true)
.setEditorConfigPath(
new File(project.getRootProject().getProjectDir(), ".editorconfig"));
} catch (IOException e) {
throw new GradleException("unable to generate license template", e);
throw new GradleException("Unable to apply Ktlint to Spotless", e);
}
});
});
Expand All @@ -115,26 +127,24 @@ public void apply(@NotNull Project project) {
});

// configure junit tests if needed
if (ext.getUnitTests().getOrElse(false)) {
project.getTasks().withType(Test.class).configureEach((test) -> {
test.useJUnitPlatform();
test.getOutputs().upToDateWhen((a) -> false);
test.setMaxParallelForks(Runtime.getRuntime().availableProcessors());
test.setFailFast(true);
test.testLogging((logging) -> {
logging.events(
TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STARTED);
project.getTasks().withType(Test.class).configureEach((test) -> {
test.useJUnitPlatform();
test.getOutputs().upToDateWhen((a) -> false);
test.setMaxParallelForks(Runtime.getRuntime().availableProcessors());
test.setFailFast(true);
test.testLogging((logging) -> {
logging.events(
TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STARTED);

logging.setShowCauses(true);
logging.setShowExceptions(true);
logging.setExceptionFormat(TestExceptionFormat.FULL);
});
logging.setShowCauses(true);
logging.setShowExceptions(true);
logging.setExceptionFormat(TestExceptionFormat.FULL);
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public abstract class NoelwareModuleExtension {

/**
* @return {@link Property<Boolean>} if we should include unit tests with JUnit5
* @deprecated Tests are automatically configured with JUnit5, so this doesn't do anything.
*/
@Deprecated(since = "1.3.0", forRemoval = true)
public abstract Property<Boolean> getUnitTests();
}
13 changes: 0 additions & 13 deletions src/test/java/org/noelware/infra/gradle/SettingsPluginTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,12 @@ public void test_settingsPluginKotlin() throws IOException {
writeFile(
settingsKtsFile,
"""
import org.noelware.infra.gradle.toolchains.NoelwareJvmToolchainResolver
plugins {
id("org.noelware.gradle.settings")
`jvm-toolchain-management`
}
logger.lifecycle("hi")
rootProject.name = "test"
toolchainManagement {
jvm {
javaRepositories {
repository("noelware") {
resolverClass.set(NoelwareJvmToolchainResolver::class.java)
}
}
}
}
""");

assertDoesNotThrow(() -> GradleRunner.create()
Expand Down

0 comments on commit e0f0773

Please sign in to comment.