|
| 1 | +// This file is responsible for reconfiguring repositories to use EngineHub's mirrors |
| 2 | +// In addition, it configures content filtering to speed up dependency resolution |
| 3 | + |
| 4 | +import org.gradle.api.artifacts.dsl.RepositoryHandler |
| 5 | +import org.gradle.api.artifacts.repositories.UrlArtifactRepository |
| 6 | +import org.gradle.api.logging.Logging |
| 7 | +import java.net.URI |
| 8 | + |
| 9 | +data class RepositoryReconfiguration( |
| 10 | + val newUri: URI, |
| 11 | + val contentConfiguration: (MavenRepositoryContentDescriptor.() -> Unit)? = null, |
| 12 | +) { |
| 13 | + constructor(newUri: String, contentConfiguration: (MavenRepositoryContentDescriptor.() -> Unit)? = null) : |
| 14 | + this(URI.create(newUri), contentConfiguration) |
| 15 | +} |
| 16 | + |
| 17 | +data class ModuleDeclaration( |
| 18 | + val group: String, |
| 19 | + val name: String, |
| 20 | + val version: String? = null, |
| 21 | +) |
| 22 | + |
| 23 | +// Isolate everything from the buildscript so it's serializable |
| 24 | +object Isolated { |
| 25 | + private val ALLOWED_PREFIXES = listOf( |
| 26 | + "https://repo.enginehub.org", |
| 27 | + "file:", |
| 28 | + ) |
| 29 | + |
| 30 | + // Until https://github.com/gradle/gradle/issues/31888 is fixed in 9.4.0 |
| 31 | + // we need to explicitly exclude unique snapshots |
| 32 | + private val UNIQUE_SNAPSHOT_MODULES = listOf( |
| 33 | + ModuleDeclaration("org.spongepowered", "spongeapi"), |
| 34 | + ModuleDeclaration("org.spongepowered", "vanillagradle"), |
| 35 | + ModuleDeclaration("me.lucko", "spark-api"), |
| 36 | + ) |
| 37 | + |
| 38 | + private fun MavenRepositoryContentDescriptor.enhancedReleasesOnly() { |
| 39 | + releasesOnly() |
| 40 | + UNIQUE_SNAPSHOT_MODULES.forEach { |
| 41 | + if (it.version != null) { |
| 42 | + excludeVersion(it.group, it.name, it.version) |
| 43 | + } else { |
| 44 | + excludeModule(it.group, it.name) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private val REPO_RECONFIGURATIONS = listOf( |
| 50 | + "https://repo.maven.apache.org/maven2/" to |
| 51 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/maven-central-proxy/") { |
| 52 | + enhancedReleasesOnly() |
| 53 | + }, |
| 54 | + "https://plugins.gradle.org/m2" to |
| 55 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/plugin-portal-proxy/") { |
| 56 | + enhancedReleasesOnly() |
| 57 | + }, |
| 58 | + "https://libraries.minecraft.net/" to |
| 59 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/minecraft/") { |
| 60 | + enhancedReleasesOnly() |
| 61 | + }, |
| 62 | + "https://maven.neoforged.net/releases/" to |
| 63 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/neoforged/") { |
| 64 | + enhancedReleasesOnly() |
| 65 | + includeGroupAndSubgroups("net.minecraftforge") |
| 66 | + includeGroupAndSubgroups("net.neoforged") |
| 67 | + }, |
| 68 | + "https://maven.minecraftforge.net/" to |
| 69 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/forge/") { |
| 70 | + enhancedReleasesOnly() |
| 71 | + includeGroupAndSubgroups("net.minecraftforge") |
| 72 | + }, |
| 73 | + "https://maven.parchmentmc.org/" to |
| 74 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/parchment/") { |
| 75 | + enhancedReleasesOnly() |
| 76 | + includeGroup("org.parchmentmc.data") |
| 77 | + }, |
| 78 | + "https://repo.papermc.io/repository/maven-public/" to |
| 79 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/papermc-proxy/") { |
| 80 | + includeGroupAndSubgroups("io.papermc") |
| 81 | + includeGroupAndSubgroups("com.velocitypowered") |
| 82 | + includeGroupAndSubgroups("ca.spottedleaf") |
| 83 | + includeGroupAndSubgroups("me.lucko") |
| 84 | + includeModule("net.md-5", "bungeecord-chat") |
| 85 | + }, |
| 86 | + "https://maven.fabricmc.net/" to |
| 87 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/fabricmc/") { |
| 88 | + enhancedReleasesOnly() |
| 89 | + includeGroupAndSubgroups("fabric-loom") |
| 90 | + includeGroupAndSubgroups("net.fabricmc") |
| 91 | + excludeModule("net.fabricmc", "yarn") |
| 92 | + }, |
| 93 | + "https://maven.fabricmc.net/#yarn-only" to |
| 94 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/fabricmc-yarn/") { |
| 95 | + enhancedReleasesOnly() |
| 96 | + includeModule("net.fabricmc", "yarn") |
| 97 | + }, |
| 98 | + "https://repo.spongepowered.org/repository/maven-releases/" to |
| 99 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/spongepowered-releases/") { |
| 100 | + enhancedReleasesOnly() |
| 101 | + includeGroupAndSubgroups("org.spongepowered") |
| 102 | + }, |
| 103 | + "https://repo.spongepowered.org/repository/maven-snapshots/" to |
| 104 | + RepositoryReconfiguration("https://repo.enginehub.org/internal/spongepowered-snapshots/") { |
| 105 | + // Cannot actually call snapshotsOnly() because it excludes unique snapshots right now |
| 106 | + includeGroupAndSubgroups("org.spongepowered") |
| 107 | + }, |
| 108 | + "https://repo.enginehub.org/libs-release/" to |
| 109 | + RepositoryReconfiguration("https://repo.enginehub.org/libs-release/") { |
| 110 | + enhancedReleasesOnly() |
| 111 | + includeGroupAndSubgroups("com.sk89q") |
| 112 | + includeGroupAndSubgroups("org.enginehub") |
| 113 | + }, |
| 114 | + ).associate { (k, v) -> URI.create(k) to v } |
| 115 | + private val LOGGER = Logging.getLogger("enginehub-reconfiguring-repositories") |
| 116 | + |
| 117 | + fun RepositoryHandler.mirrorNonEngineHubRepositories() { |
| 118 | + configureEach { |
| 119 | + val repo = this |
| 120 | + if (!(repo is UrlArtifactRepository)) { |
| 121 | + return@configureEach |
| 122 | + } |
| 123 | + val reconfiguration = REPO_RECONFIGURATIONS[repo.url] |
| 124 | + val mustReplaceUrl = !ALLOWED_PREFIXES.any { repo.url.toString().startsWith(it) } |
| 125 | + if (mustReplaceUrl) { |
| 126 | + check(reconfiguration != null) { |
| 127 | + "No replacement found for non-EngineHub repository: ${repo.name} ${repo.url}" |
| 128 | + } |
| 129 | + LOGGER.info( |
| 130 | + "Replacing non-EngineHub repository: {} {} -> {}", |
| 131 | + repo.name, |
| 132 | + repo.url, |
| 133 | + reconfiguration.newUri |
| 134 | + ) |
| 135 | + repo.url = reconfiguration.newUri |
| 136 | + } |
| 137 | + if (reconfiguration?.contentConfiguration != null) { |
| 138 | + if (!(repo is MavenArtifactRepository)) { |
| 139 | + error("Cannot configure content on non-Maven repository: ${repo.name} ${repo.url}") |
| 140 | + } |
| 141 | + repo.mavenContent { |
| 142 | + reconfiguration.contentConfiguration.invoke(this) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +with(Isolated) { |
| 150 | + gradle.lifecycle.beforeProject { |
| 151 | + buildscript.repositories.mirrorNonEngineHubRepositories() |
| 152 | + repositories.mirrorNonEngineHubRepositories() |
| 153 | + } |
| 154 | + |
| 155 | + settings.buildscript.repositories.mirrorNonEngineHubRepositories() |
| 156 | + settings.pluginManagement.repositories.mirrorNonEngineHubRepositories() |
| 157 | + settings.dependencyResolutionManagement.repositories.mirrorNonEngineHubRepositories() |
| 158 | +} |
0 commit comments