Skip to content

Commit

Permalink
fix(core/patcher): Download Spigot dependencies using DetachedResolver (
Browse files Browse the repository at this point in the history
#233)

* Download Spigot sources using DetachedResolver; fixes #232

* Create resolver during task init

* Use DependencyFactory instead of project

* Remove redundant casts

* remove unused import
  • Loading branch information
jpenilla authored Mar 27, 2024
1 parent 0d47c66 commit bf78951
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/config-kotlin.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ repositories {
}
}
mavenCentral()
gradlePluginPortal()
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ import javax.inject.Inject
import javax.xml.parsers.DocumentBuilderFactory
import kotlin.io.path.*
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.dsl.DependencyFactory
import org.gradle.api.attributes.java.TargetJvmEnvironment
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.api.internal.project.ProjectInternal.DetachedResolver
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.*
import org.gradle.work.DisableCachingByDefault
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkQueue
Expand Down Expand Up @@ -140,7 +146,7 @@ fun downloadMinecraftLibraries(
return queue
}

@CacheableTask
@DisableCachingByDefault(because = "Gradle handles caching")
abstract class DownloadSpigotDependencies : BaseTask() {

@get:InputFile
Expand All @@ -167,19 +173,22 @@ abstract class DownloadSpigotDependencies : BaseTask() {
@get:Inject
abstract val workerExecutor: WorkerExecutor

@get:Inject
abstract val dependencyFactory: DependencyFactory

private val detachedResolver: DetachedResolver = (project as ProjectInternal).newDetachedResolver()

@TaskAction
fun run() {
val apiSetup = parsePom(apiPom.path)
val serverSetup = parsePom(serverPom.path)
val mcLibraries = mcLibrariesFile.path.readLines()

val out = outputDir.path
val excludes = listOf(out.fileSystem.getPathMatcher("glob:*.etag"))
out.deleteRecursive(excludes)
out.deleteRecursive()

val outSources = outputSourcesDir.path
val excludesSources = listOf(outSources.fileSystem.getPathMatcher("glob:*.etag"))
outSources.deleteRecursive(excludesSources)
outSources.deleteRecursive()

val spigotRepos = mutableSetOf<String>()
spigotRepos += apiSetup.repos
Expand All @@ -189,23 +198,74 @@ abstract class DownloadSpigotDependencies : BaseTask() {
artifacts += apiSetup.artifacts
artifacts += serverSetup.artifacts

val queue = workerExecutor.noIsolation()
for (art in artifacts) {
queue.submit(DownloadWorker::class) {
repos.set(spigotRepos)
artifact.set(art.toString())
target.set(out)
downloadToDir.set(true)
downloader.set(this@DownloadSpigotDependencies.downloader)
val resolver = detachedResolver
for (repo in spigotRepos) {
resolver.repositories.maven(repo)
}
val config = resolver.configurations.create("spigotDependencies") {
attributes {
attribute(TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objects.named(TargetJvmEnvironment.STANDARD_JVM))
}
if (!mcLibraries.contains(art.toString())) {
queue.submit(DownloadSourcesToDirAction::class) {
repos.set(spigotRepos)
artifact.set(art.toString())
target.set(outSources)
downloader.set(this@DownloadSpigotDependencies.downloader)
}
for (artifact in artifacts) {
val gav = artifact.gav.let {
if (it == "com.google.guava:guava:32.1.2-jre") {
// https://github.com/google/guava/issues/6657
"com.google.guava:guava:32.1.3-jre"
} else {
it
}
}
config.dependencies.add(
dependencyFactory.create(gav).also {
it.artifact {
artifact.classifier?.let { s -> classifier = s }
artifact.extension?.let { s -> extension = s }
}
}
)
}

// The source variants don't have transitives
val flatComponents = mutableSetOf<ComponentIdentifier>()

for (artifact in config.incoming.artifacts.artifacts) {
artifact.file.toPath().copyTo(outputDir.path.resolve(artifact.file.name).also { it.parent.createDirectories() }, true)
flatComponents += artifact.id.componentIdentifier
}

val sourcesConfig = resolver.configurations.create("spigotDependenciesSources") {
attributes {
// Mojang libs & Guava don't resolve metadata correctly, so we set the classifier below instead...

// attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
// attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
// attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
// attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION))

// Needed since we set the classifier instead of using above attributes
attribute(TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objects.named(TargetJvmEnvironment.STANDARD_JVM))
}
}
for (component in flatComponents) {
sourcesConfig.dependencies.add(
dependencyFactory.create(component.displayName).also {
it.artifact {
classifier = "sources"
}
}
)
}
val sourcesView = sourcesConfig.incoming.artifactView {
componentFilter {
mcLibraries.none { l -> l == it.displayName } &&
// This is only needed since we don't use variant-aware resolution properly
it.displayName != "com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava"
}
}

for (artifact in sourcesView.artifacts.artifacts) {
artifact.file.toPath().copyTo(outputSourcesDir.path.resolve(artifact.file.name).also { it.parent.createDirectories() }, true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ data class MavenArtifact(
private val group: String,
private val artifact: String,
private val version: String,
private val classifier: String? = null,
private val extension: String? = null
val classifier: String? = null,
val extension: String? = null
) {

private val classifierText: String
Expand All @@ -46,6 +46,9 @@ data class MavenArtifact(
val file: String
get() = "$artifact-$version$classifierText.$ext"

val gav: String
get() = "$group:$artifact:$version"

fun downloadToFile(downloadService: DownloadService, targetFile: Path, repos: List<String>) {
targetFile.parent.createDirectories()

Expand Down

0 comments on commit bf78951

Please sign in to comment.