Unconventional

- Use convention plugins for common build logic
- Convention plugins get to be applied in the plugins block and can load
  other plugins
- Move GeneratePackageInfosTask to its own file in buildSrc
- Apply GeneratePackageInfosTask to every sourceSet
This commit is contained in:
Jozufozu 2024-04-25 10:37:12 -07:00
parent 82a098626b
commit e1b0cebc2c
11 changed files with 106 additions and 105 deletions

View file

@ -5,8 +5,6 @@ plugins {
id 'dev.architectury.loom' apply false
}
apply from: rootProject.file('buildSrc/simple-java.gradle')
println("Java: ${System.getProperty('java.version')}, JVM: ${System.getProperty('java.vm.version')} (${System.getProperty('java.vendor')}), Arch: ${System.getProperty('os.arch')}")
idea {

View file

@ -1,2 +1,3 @@
// TODO: port stuff to convention plugins
plugins {
id 'groovy-gradle-plugin'
}

View file

@ -1,74 +0,0 @@
// Adapted from https://github.com/FabricMC/fabric/blob/31787236d242247e0b6c4ae806b1cfaa7042a62c/gradle/package-info.gradle, which is licensed under Apache 2.0.
import java.nio.file.Files
setupGeneratePackageInfos(sourceSets.main)
def setupGeneratePackageInfos(SourceSet sourceSet) {
// We have to capture the source set name for the lazy string literals,
// otherwise it'll just be whatever the last source set is in the list.
def sourceSetName = sourceSet.name
def taskName = sourceSet.getTaskName('generate', 'PackageInfos')
def task = tasks.register(taskName, GeneratePackageInfosTask) {
group = 'flywheel'
description = "Generates package-info files for $sourceSetName packages."
// Only apply to default source directory since we also add the generated
// sources to the source set.
sourceRoot = file("src/$sourceSetName/java")
outputDir = file("src/$sourceSetName/generatedPackageInfos")
}
sourceSet.java.srcDir task
tasks.named('ideaSyncTask').configure {
finalizedBy task
}
def cleanTask = tasks.register(sourceSet.getTaskName('clean', 'PackageInfos'), Delete) {
group = 'flywheel'
delete file("src/$sourceSetName/generatedPackageInfos")
}
clean.dependsOn cleanTask
}
class GeneratePackageInfosTask extends DefaultTask {
@SkipWhenEmpty
@InputDirectory
final DirectoryProperty sourceRoot = project.objects.directoryProperty()
@OutputDirectory
final DirectoryProperty outputDir = project.objects.directoryProperty()
@TaskAction
def run() {
def output = outputDir.get().asFile.toPath()
output.deleteDir()
def root = sourceRoot.get().asFile.toPath()
root.eachDirRecurse {
def containsJava = Files.list(it).any {
Files.isRegularFile(it) && it.fileName.toString().endsWith('.java')
}
if (containsJava && Files.notExists(it.resolve('package-info.java'))) {
def relativePath = root.relativize(it)
def target = output.resolve(relativePath)
Files.createDirectories(target)
target.resolve('package-info.java').withWriter {
def packageName = relativePath.toString().replace(File.separator, '.')
it.write("""@ParametersAreNonnullByDefault
|@FieldsAreNonnullByDefault
|@MethodsReturnNonnullByDefault
|package $packageName;
|
|import javax.annotation.ParametersAreNonnullByDefault;
|
|import net.minecraft.FieldsAreNonnullByDefault;
|import net.minecraft.MethodsReturnNonnullByDefault;
|""".stripMargin())
}
}
}
}
}

View file

@ -0,0 +1,53 @@
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import java.nio.file.Files
// Adapted from https://github.com/FabricMC/fabric/blob/31787236d242247e0b6c4ae806b1cfaa7042a62c/gradle/package-info.gradle, which is licensed under Apache 2.0.
class GeneratePackageInfosTask extends DefaultTask {
@SkipWhenEmpty
@InputDirectory
final DirectoryProperty sourceRoot = project.objects.directoryProperty()
@OutputDirectory
final DirectoryProperty outputDir = project.objects.directoryProperty()
@TaskAction
def run() {
def output = outputDir.get().asFile.toPath()
output.deleteDir()
def root = sourceRoot.get().asFile.toPath()
root.eachDirRecurse {
def containsJava = Files.list(it).any {
Files.isRegularFile(it) && it.fileName.toString().endsWith('.java')
}
if (containsJava && Files.notExists(it.resolve('package-info.java'))) {
def relativePath = root.relativize(it)
def target = output.resolve(relativePath)
Files.createDirectories(target)
target.resolve('package-info.java').withWriter {
def packageName = relativePath.toString().replace(File.separator, '.')
it.write("""@ParametersAreNonnullByDefault
|@FieldsAreNonnullByDefault
|@MethodsReturnNonnullByDefault
|package $packageName;
|
|import javax.annotation.ParametersAreNonnullByDefault;
|
|import net.minecraft.FieldsAreNonnullByDefault;
|import net.minecraft.MethodsReturnNonnullByDefault;
|""".stripMargin())
}
}
}
}
}

View file

@ -1,3 +1,7 @@
plugins {
id 'java'
}
boolean dev = System.getenv('RELEASE') == null || System.getenv('RELEASE').equalsIgnoreCase('false')
String buildNumber = System.getenv('BUILD_NUMBER')

View file

@ -0,0 +1,31 @@
sourceSets.configureEach {
setupGeneratePackageInfos(it)
}
def setupGeneratePackageInfos(SourceSet sourceSet) {
// We have to capture the source set name for the lazy string literals,
// otherwise it'll just be whatever the last source set is in the list.
def sourceSetName = sourceSet.name
def taskName = sourceSet.getTaskName('generate', 'PackageInfos')
def task = tasks.register(taskName, GeneratePackageInfosTask) {
group = 'flywheel'
description = "Generates package-info files for $sourceSetName packages."
// Only apply to default source directory since we also add the generated
// sources to the source set.
sourceRoot = file("src/$sourceSetName/java")
outputDir = file("src/$sourceSetName/generatedPackageInfos")
}
sourceSet.java.srcDir task
tasks.named('ideaSyncTask').configure {
finalizedBy task
}
def cleanTask = tasks.register(sourceSet.getTaskName('clean', 'PackageInfos'), Delete) {
group = 'flywheel'
delete file("src/$sourceSetName/generatedPackageInfos")
}
clean.dependsOn cleanTask
}

View file

@ -1,3 +1,7 @@
plugins {
id 'flywheel.subproject'
}
evaluationDependsOn(':common')
loom {

View file

@ -1,3 +1,11 @@
plugins {
id 'idea'
id 'flywheel.java'
id 'maven-publish'
id 'dev.architectury.loom'
id 'flywheel.package-infos'
}
loom {
silentMojangMappingsLicense()

View file

@ -1,15 +1,7 @@
plugins {
id 'idea'
id 'java'
id 'maven-publish'
id 'dev.architectury.loom'
id 'flywheel.subproject'
}
apply from: rootProject.file('buildSrc/simple-java.gradle')
apply from: rootProject.file('buildSrc/subprojects.gradle')
// TODO: package-infos specifically targets the main source set
// apply from: rootProject.file('buildSrc/package-infos.gradle')
sourceSets {
// Loom only populates mc stuff to the main source set,
// so grab that here and use it for the others.

View file

@ -1,15 +1,7 @@
plugins {
id 'idea'
id 'java'
id 'maven-publish'
id 'dev.architectury.loom'
id 'flywheel.platform'
}
apply from: rootProject.file('buildSrc/simple-java.gradle')
apply from: rootProject.file('buildSrc/subprojects.gradle')
apply from: rootProject.file('buildSrc/platforms.gradle')
apply from: rootProject.file('buildSrc/package-infos.gradle')
dependencies {
modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
modApi "net.fabricmc.fabric-api:fabric-api:${fabric_api_version}"

View file

@ -1,15 +1,7 @@
plugins {
id 'idea'
id 'java'
id 'maven-publish'
id 'dev.architectury.loom'
id 'flywheel.platform'
}
apply from: rootProject.file('buildSrc/simple-java.gradle')
apply from: rootProject.file('buildSrc/subprojects.gradle')
apply from: rootProject.file('buildSrc/platforms.gradle')
apply from: rootProject.file('buildSrc/package-infos.gradle')
loom {
forge {
mixinConfig 'flywheel.backend.mixins.json'