Skip to content

Commit

Permalink
Inject default settings automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Jul 19, 2024
1 parent d1a5aa3 commit 7367e43
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.metaborg.convention

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.jvm.toolchain.JavaLanguageVersion
import javax.inject.Inject

/** Configuration for the Java convention. */
interface JavaConventionExtension {
open class JavaConventionExtension @Inject constructor(
/** The Gradle object factory. */
objects: ObjectFactory,
) {
/** The Java version to compile to. */
val javaVersion: Property<JavaLanguageVersion>

/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
javaVersion.convention(JavaLanguageVersion.of(11))
}
val javaVersion: Property<JavaLanguageVersion> = objects.property(JavaLanguageVersion::class.java)
.convention(JavaLanguageVersion.of(11))
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class JavaConventionPlugin: Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
// Add the configuration extension
val extension = extensions.create<JavaConventionExtension>("javaConvention")
extension.setConvention()

// Apply the Java base plugin
plugins.apply("java-base")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.newInstance
import javax.inject.Inject

/**
* Configuration for the Maven Publish convention.
*
* Create an instance like this:
*
* ```kotlin
* extensions.create<MavenPublishConventionExtension>("mavenPublishConvention")
* extension.setConvention()
* ```
*/
/** Configuration for the Maven Publish convention. */
open class MavenPublishConventionExtension @Inject constructor(
/** The Gradle object factory. */
objects: ObjectFactory,
Expand All @@ -26,11 +17,6 @@ open class MavenPublishConventionExtension @Inject constructor(
/** The name of the repository. */
val repoName: Property<String> = objects.property(String::class.java)

/** Whether to publish to GitHub packages. */
val publishToGitHubPackages: Property<Boolean> = objects.property(Boolean::class.java)
/** Whether to publish to Metaborg Artifacts. */
val publishToMetaborgArtifacts: Property<Boolean> = objects.property(Boolean::class.java)

/** The publication metadata (dot notation). */
val metadata: MetadataHandler = objects.newInstance<MetadataHandler>()

Expand All @@ -39,39 +25,34 @@ open class MavenPublishConventionExtension @Inject constructor(
action.execute(metadata)
}

/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
publishToGitHubPackages.convention(metadata.scm.map { it == SCM.GitHub })
publishToMetaborgArtifacts.convention(true)
metadata.setConvention()
}
/** Whether to publish to GitHub packages. */
val publishToGitHubPackages: Property<Boolean> = objects.property(Boolean::class.java)
.convention(metadata.scm.map { it == SCM.GitHub })
/** Whether to publish to Metaborg Artifacts. */
val publishToMetaborgArtifacts: Property<Boolean> = objects.property(Boolean::class.java)
.convention(true)

/** Configuration of publication metadata. */
interface MetadataHandler {
open class MetadataHandler @Inject constructor(
/** The Gradle object factory. */
objects: ObjectFactory,
) {
/** The year the project was started. */
val inceptionYear: Property<String>
val inceptionYear: Property<String> = objects.property(String::class.java)
/** The developers of the project. */
val developers: ListProperty<Developer>
val developers: ListProperty<Developer> = objects.listProperty(Developer::class.java)
/** The source control management system. */
val scm: Property<SCM>
val scm: Property<SCM> = objects.property(SCM::class.java)
.convention(SCM.GitHub)
/** The open source license that the project is published under. */
val license: Property<OpenSourceLicense>
val license: Property<OpenSourceLicense> = objects.property(OpenSourceLicense::class.java)
.convention(OpenSourceLicense.Apache2)
/** The HTTP SCM URL format. */
val httpUrlFormat: Property<String>
val httpUrlFormat: Property<String> = objects.property(String::class.java)
.convention(scm.map { it.httpUrlFormat })
/** The SCM URL format. */
val scmUrlFormat: Property<String>

/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
scm.convention(SCM.GitHub)
license.convention(OpenSourceLicense.Apache2)
httpUrlFormat.convention(scm.map { it.httpUrlFormat })
scmUrlFormat.convention(scm.map { it.scmUrlFormat })
}
val scmUrlFormat: Property<String> = objects.property(String::class.java)
.convention(scm.map { it.scmUrlFormat })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class MavenPublishConventionPlugin: Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
// Add the configuration extension
val extension = extensions.create<MavenPublishConventionExtension>("mavenPublishConvention")
extension.setConvention()

// Apply the Maven Publish plugin
plugins.apply("maven-publish")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
package org.metaborg.convention

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import javax.inject.Inject

/** Configuration for the root project convention. */
interface RootProjectConventionExtension {

open class RootProjectConventionExtension @Inject constructor(
/** The Gradle object factory. */
objects: ObjectFactory,
) {
/** Suffix for all task names. */
val taskNameSuffix: Property<String>
val taskNameSuffix: Property<String> = objects.property(String::class.java)
.convention("")

/** The name of the `assemble` task. */
val assembleTaskName: Property<String>
val assembleTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "assemble$it"} )
/** The name of the `build` task. */
val buildTaskName: Property<String>
val buildTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "build$it"} )
/** The name of the `clean` task. */
val cleanTaskName: Property<String>
val cleanTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "clean$it"} )
/** The name of the `publish` task. */
val publishTaskName: Property<String>
val publishTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "publish$it"} )
/** The name of the `publishToMavenLocal` task. */
val publishToMavenLocalTaskName: Property<String>
val publishToMavenLocalTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "publish${it}ToMavenLocal"} )
/** The name of the `check` task. */
val checkTaskName: Property<String>
val checkTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "check$it"} )
/** The name of the `test` task. */
val testTaskName: Property<String>
val testTaskName: Property<String> = objects.property(String::class.java)
.convention(taskNameSuffix.map { "test$it"} )
/** The name of the `tasks` task. */
val tasksTaskName: Property<String>

/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
taskNameSuffix.convention("")
assembleTaskName.convention(taskNameSuffix.map { "assemble$it"} )
buildTaskName.convention(taskNameSuffix.map { "build$it"} )
cleanTaskName.convention(taskNameSuffix.map { "clean$it"} )
publishTaskName.convention(taskNameSuffix.map { "publish$it"} )
publishToMavenLocalTaskName.convention(taskNameSuffix.map { "publish${it}ToMavenLocal"} )
checkTaskName.convention(taskNameSuffix.map { "check$it"} )
testTaskName.convention(taskNameSuffix.map { "test$it"} )
tasksTaskName.convention("allTasks")
}
val tasksTaskName: Property<String> = objects.property(String::class.java)
.convention("allTasks")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class RootProjectConventionPlugin: Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
// Add the configuration extension
val extension = extensions.create<RootProjectConventionExtension>("rootProjectConvention")
extension.setConvention()

afterEvaluate {
// Build tasks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.metaborg.convention

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import javax.inject.Inject

/** Configuration for the settings build convention. */
interface SettingsConventionExtension {
open class SettingsConventionExtension @Inject constructor(
/** The Gradle object factory. */
objects: ObjectFactory,
) {

/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SettingsConventionPlugin: Plugin<Settings> {
override fun apply(settings: Settings): Unit = with(settings) {
// Add the configuration extension
val extension = extensions.create<SettingsConventionExtension>("settingsConvention")
extension.setConvention()

// This allows us to use the catalog in dependencies
dependencyResolutionManagement {
Expand Down

0 comments on commit 7367e43

Please sign in to comment.