-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add module for a kotlin plugin #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theEvilReaper
wants to merge
6
commits into
master
Choose a base branch
from
feature/kotlinPlugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ee16d2
feat(module): add kotlin module
theEvilReaper d1fe350
feat(module): add base module to share logic between modules
theEvilReaper f15c524
feat(module): add the kotlin gralde plugin module
theEvilReaper d2c1f44
feat(build): add missing plugin apply
theEvilReaper 132424b
fix(build): add missing build.gradle.kts file
theEvilReaper 7d4f8d9
fix(tests): improve plugin apply to fix test execution
theEvilReaper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| plugins { | ||
| `java-gradle-plugin` | ||
| } | ||
|
|
93 changes: 93 additions & 0 deletions
93
lasius-base/src/main/java/net/onelitefeather/lasius/LasiusBasePlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package net.onelitefeather.lasius; | ||
|
|
||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| import org.gradle.api.publish.PublishingExtension; | ||
| import org.gradle.api.publish.maven.MavenPublication; | ||
| import org.gradle.api.tasks.testing.Test; | ||
| import org.gradle.api.tasks.testing.logging.TestLogEvent; | ||
| import org.gradle.testing.jacoco.tasks.JacocoReport; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Shared base Gradle plugin that configures common behavior for both Java and Kotlin projects. | ||
| */ | ||
| public abstract class LasiusBasePlugin implements Plugin<@NotNull Project> { | ||
|
|
||
| @Override | ||
| public void apply(@NotNull Project project) { | ||
| applyCommonPlugins(project); | ||
| configureTesting(project); | ||
| configurePublishing(project); | ||
| } | ||
|
|
||
| private void applyCommonPlugins(@NotNull Project project) { | ||
| project.getPluginManager().apply("jacoco"); | ||
| project.getPluginManager().apply("maven-publish"); | ||
| } | ||
|
|
||
| /** | ||
| * Configures JUnit 5, Jacoco, and test logging. | ||
| */ | ||
| private void configureTesting(@NotNull Project project) { | ||
| project.getTasks().withType(Test.class).configureEach(test -> { | ||
| test.useJUnitPlatform(); | ||
| test.getTestLogging().setEvents(Set.of( | ||
| TestLogEvent.PASSED, | ||
| TestLogEvent.SKIPPED, | ||
| TestLogEvent.FAILED | ||
| )); | ||
| test.finalizedBy(project.getTasks().named("jacocoTestReport")); | ||
| }); | ||
|
|
||
| project.getTasks().withType(JacocoReport.class).configureEach(jacoco -> { | ||
| jacoco.dependsOn(project.getTasks().withType(Test.class)); | ||
| jacoco.getReports().getXml().getRequired().set(true); | ||
| jacoco.getReports().getHtml().getRequired().set(true); | ||
| jacoco.getReports().getCsv().getRequired().set(false); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Configures Maven publishing with credentials and repository selection based on version. | ||
| */ | ||
| private void configurePublishing(@NotNull Project project) { | ||
| project.afterEvaluate(p -> { | ||
| var publishing = p.getExtensions().findByType(PublishingExtension.class); | ||
| if (publishing == null) { | ||
| return; | ||
| } | ||
|
|
||
| publishing.publications(publications -> | ||
| publications.create("maven", MavenPublication.class, publication -> { | ||
| var component = p.getComponents().findByName("java"); | ||
| if (component != null) { | ||
| publication.from(component); | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| publishing.repositories(repos -> repos.maven(maven -> { | ||
| String version = p.getVersion().toString(); | ||
| String baseUrl = version.contains("SNAPSHOT") | ||
| ? "https://repo.onelitefeather.dev/onelitefeather-snapshots" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a property for gradle or system property There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be a separate pull request and isn’t really related to this one |
||
| : "https://repo.onelitefeather.dev/onelitefeather-releases"; | ||
|
|
||
| maven.setName("OneLiteFeatherRepository"); | ||
| maven.setUrl(p.uri(baseUrl)); | ||
|
|
||
| String username = System.getenv("ONELITEFEATHER_MAVEN_USERNAME"); | ||
| String password = System.getenv("ONELITEFEATHER_MAVEN_PASSWORD"); | ||
|
|
||
| maven.credentials(passwordCredentials -> { | ||
| passwordCredentials.setUsername(username); | ||
| passwordCredentials.setPassword(password); | ||
| }); | ||
| })); | ||
|
|
||
| project.getLogger().lifecycle("Configured publishing for {}", project.getName()); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 8 additions & 107 deletions
115
lasius-java/src/main/java/net/onelitefeather/lasius/LasiusJavaPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,131 +1,32 @@ | ||
| package net.onelitefeather.lasius; | ||
|
|
||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| import org.gradle.api.plugins.JavaPluginExtension; | ||
| import org.gradle.api.publish.PublishingExtension; | ||
| import org.gradle.api.publish.maven.MavenPublication; | ||
| import org.gradle.api.tasks.compile.JavaCompile; | ||
| import org.gradle.api.tasks.testing.Test; | ||
| import org.gradle.api.tasks.testing.logging.TestLogEvent; | ||
| import org.gradle.jvm.toolchain.JavaLanguageVersion; | ||
| import org.gradle.testing.jacoco.plugins.JacocoPlugin; | ||
| import org.gradle.testing.jacoco.tasks.JacocoReport; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class LasiusJavaPlugin implements Plugin<@NotNull Project> { | ||
| public class LasiusJavaPlugin extends LasiusBasePlugin { | ||
|
|
||
| @Override | ||
| public void apply(@NotNull Project project) { | ||
| applyPlugins(project); | ||
| super.apply(project); | ||
| configureJava(project); | ||
| configureTasks(project); | ||
| configurePublishing(project); | ||
| } | ||
|
|
||
| private void applyPlugins(@NotNull Project project) { | ||
| project.getPluginManager().apply("java-library"); | ||
| project.getPluginManager().apply("maven-publish"); | ||
| project.getPluginManager().apply(JacocoPlugin.class); | ||
| } | ||
|
|
||
| private void configureJava(@NotNull Project project) { | ||
| project.getPluginManager().withPlugin("java", java -> { | ||
| project.getPluginManager().withPlugin("java-library", _ -> { | ||
| var javaExt = project.getExtensions().getByType(JavaPluginExtension.class); | ||
| javaExt.getToolchain().getLanguageVersion().set(JavaLanguageVersion.of(25)); | ||
| javaExt.withJavadocJar(); | ||
| javaExt.withSourcesJar(); | ||
|
|
||
| project.getLogger().lifecycle("Set Java toolchain to Java {} for project {}", 25, project.getName()); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Configures tasks for the project. | ||
| * @param project the project to configure tasks for | ||
| */ | ||
| private void configureTasks(@NotNull Project project) { | ||
| project.getTasks().withType(JavaCompile.class).configureEach(compileTask -> { | ||
| compileTask.getOptions().setEncoding(StandardCharsets.UTF_8.displayName()); | ||
| compileTask.getOptions().getRelease().set(25); | ||
| }); | ||
|
|
||
| project.getTasks().withType(Test.class).configureEach(testTask -> { | ||
| testTask.useJUnitPlatform(); | ||
| if (hasMinestomDependency(project)) { | ||
| testTask.jvmArgs("-Dminestom.inside-test=true"); | ||
| } | ||
|
|
||
| testTask.getTestLogging().setEvents(java.util.Set.of( | ||
| TestLogEvent.PASSED, | ||
| TestLogEvent.SKIPPED, | ||
| TestLogEvent.FAILED | ||
| )); | ||
|
|
||
| testTask.finalizedBy(project.getTasks().named("jacocoTestReport")); | ||
| }); | ||
|
|
||
| project.getTasks().withType(JacocoReport.class).configureEach(jacocoTask -> { | ||
| jacocoTask.dependsOn(project.getTasks().withType(Test.class)); | ||
| jacocoTask.getReports().getXml().getRequired().set(true); | ||
| jacocoTask.getReports().getCsv().getRequired().set(true); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Configures publishing for the project. | ||
| * | ||
| * @param project the project to configure publishing for. | ||
| */ | ||
| private void configurePublishing(@NotNull Project project) { | ||
| project.getPluginManager().apply("maven-publish"); | ||
|
|
||
| project.afterEvaluate(p -> { | ||
| var publishing = p.getExtensions().findByType(PublishingExtension.class); | ||
| if (publishing == null) return; | ||
|
|
||
| publishing.publications(publications -> | ||
| publications.create("maven", MavenPublication.class, publication -> | ||
| publication.from(p.getComponents().getByName("java")) | ||
| ) | ||
| ); | ||
|
|
||
| publishing.repositories(repos -> repos.maven(maven -> { | ||
| maven.setName("OneLiteFeatherRepository"); | ||
|
|
||
| String username = System.getenv("ONELITEFEATHER_MAVEN_USERNAME"); | ||
| String password = System.getenv("ONELITEFEATHER_MAVEN_PASSWORD"); | ||
| project.getTasks().withType(JavaCompile.class).configureEach(task -> { | ||
| task.getOptions().setEncoding("UTF-8"); | ||
| task.getOptions().getRelease().set(25); | ||
| }); | ||
|
|
||
| maven.credentials(passwordCredentials -> { | ||
| passwordCredentials.setUsername(username); | ||
| passwordCredentials.setPassword(password); | ||
| }); | ||
|
|
||
| String version = p.getVersion().toString(); | ||
| String url = version.contains("SNAPSHOT") | ||
| ? "https://repo.onelitefeather.dev/onelitefeather-snapshots" | ||
| : "https://repo.onelitefeather.dev/onelitefeather-releases"; | ||
|
|
||
| maven.setUrl(p.uri(url)); | ||
| })); | ||
|
|
||
| project.getLogger().lifecycle("Configured publishing for {}", project.getName()); | ||
| project.getLogger().lifecycle("Set Java toolchain to Java {} for project {}", 25, project.getName()); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the project has a dependency on Minestom. | ||
| * | ||
| * @param project the project to check | ||
| * @return true if the project has a dependency on Minestom, false otherwise | ||
| */ | ||
| private boolean hasMinestomDependency(@NotNull Project project) { | ||
| return project.getConfigurations().stream() | ||
| .anyMatch(config -> config.getDependencies().stream() | ||
| .anyMatch(dep -> dep.getName().toLowerCase().contains("minestom"))); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| plugins { | ||
| `kotlin-dsl` | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(project(":lasius-base")) | ||
| // Needed because your plugin applies and configures the Kotlin plugin | ||
| implementation("org.jetbrains.kotlin:kotlin-gradle-plugin") | ||
| testImplementation(platform(libs.junit.bom)) | ||
| testImplementation(libs.junit.jupiter) | ||
| testRuntimeOnly(libs.junit.launcher) | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| } | ||
|
|
||
| gradlePlugin { | ||
| plugins { | ||
| register("lasiusKotlinPlugin") { | ||
| id = "${group}.lasius-kotlin" | ||
| implementationClass = "net.onelitefeather.lasius.LasiusKotlinPlugin" | ||
| displayName = "Lasius Kotlin Plugin" | ||
| description = "A gradle plugin to apply default settings to kotlin projects" | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
lasius-kotlin/src/main/kotlin/net/onelitefeather/lasius/LasiusKotlinPlugin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package net.onelitefeather.lasius | ||
|
|
||
| import org.gradle.api.Project | ||
| import org.gradle.kotlin.dsl.findByType | ||
| import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension | ||
|
|
||
| class LasiusKotlinPlugin : LasiusBasePlugin() { | ||
| override fun apply(project: Project) { | ||
| configureKotlin(project) | ||
| } | ||
|
|
||
| private fun configureKotlin(project: Project) { | ||
| project.extensions.findByType(KotlinJvmProjectExtension::class)?.apply { | ||
| jvmToolchain(21) | ||
| project.logger.lifecycle("Configured Kotlin JVM toolchain to Java 21 for ${project.name}") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add conditions here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you design such conditions? Just using properties? I’m not very experienced in writing plugins, so I can’t suggest the best solution