-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a repository management plugin (#27)
- Loading branch information
Showing
5 changed files
with
114 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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
9 changes: 9 additions & 0 deletions
9
src/java8/java/net/neoforged/moddevgradle/boot/RepositoryPlugin.java
This file contains 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,9 @@ | ||
package net.neoforged.moddevgradle.boot; | ||
|
||
import org.gradle.api.Project; | ||
|
||
public class RepositoryPlugin extends TrampolinePlugin<Project> { | ||
public RepositoryPlugin() { | ||
super("net.neoforged.moddevgradle.internal.RepositoryPlugin"); | ||
} | ||
} |
This file contains 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
67 changes: 67 additions & 0 deletions
67
src/main/java/net/neoforged/moddevgradle/internal/RepositoryPlugin.java
This file contains 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,67 @@ | ||
package net.neoforged.moddevgradle.internal; | ||
|
||
import net.neoforged.moddevgradle.internal.generated.MojangRepositoryFilter; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.dsl.RepositoryHandler; | ||
import org.gradle.api.artifacts.repositories.MavenArtifactRepository; | ||
import org.gradle.api.initialization.Settings; | ||
import org.gradle.api.invocation.Gradle; | ||
import org.gradle.api.plugins.PluginAware; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* This plugin acts in different roles depending on where it is applied: | ||
* <ul> | ||
* <li>At the project-level, it will add the required repositories for moddev.</li> | ||
* <li>At the settings-level, it will add the required repositories to the dependency management block, and add a marker plugin to the Gradle instance to prevent the | ||
* repositories from being added again at the project-level.</li> | ||
* </ul> | ||
*/ | ||
public class RepositoryPlugin implements Plugin<PluginAware> { | ||
@Override | ||
public void apply(PluginAware target) { | ||
if (target instanceof Project project) { | ||
applyRepositories(project.getRepositories()); | ||
} else if (target instanceof Settings settings) { | ||
applyRepositories(settings.getDependencyResolutionManagement().getRepositories()); | ||
settings.getGradle().getPlugins().apply(getClass()); // Add a marker to Gradle | ||
} else if (target instanceof Gradle gradle) { | ||
// Do nothing | ||
} else { | ||
throw new GradleException("This plugin does not support being applied to " + target); | ||
} | ||
} | ||
|
||
private void applyRepositories(RepositoryHandler repositories) { | ||
var mojangMaven = repositories.maven(repo -> { | ||
repo.setName("Mojang Minecraft Libraries"); | ||
repo.setUrl(URI.create("https://libraries.minecraft.net/")); | ||
repo.metadataSources(sources -> sources.mavenPom()); | ||
repo.content(MojangRepositoryFilter::filter); | ||
}); | ||
sortFirst(repositories, mojangMaven); | ||
|
||
var mojangMetaMaven = repositories.maven(repo -> { | ||
repo.setName("Mojang Meta"); | ||
repo.setUrl("https://maven.neoforged.net/mojang-meta/"); | ||
repo.metadataSources(sources -> sources.gradleMetadata()); | ||
repo.content(content -> { | ||
content.includeModule("net.neoforged", "minecraft-dependencies"); | ||
}); | ||
}); | ||
sortFirst(repositories, mojangMetaMaven); | ||
|
||
repositories.maven(repo -> { | ||
repo.setName("NeoForged Releases"); | ||
repo.setUrl(URI.create("https://maven.neoforged.net/releases/")); | ||
}); | ||
} | ||
|
||
private static void sortFirst(RepositoryHandler repositories, MavenArtifactRepository repo) { | ||
repositories.remove(repo); | ||
repositories.add(0, repo); | ||
} | ||
} |