Skip to content

Commit

Permalink
add options for api sources and maven publish
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Sep 17, 2024
1 parent 3bf8f5a commit b153068
Showing 1 changed file with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import org.gradle.api.plugins.BasePluginExtension;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Property;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.SourceSet;
import org.gradle.jvm.tasks.Jar;
import org.gradle.language.jvm.tasks.ProcessResources;

import javax.inject.Inject;
Expand All @@ -19,6 +22,7 @@

public abstract class AlmostGradleExtension {
public static final String NAME = "almostgradle";
public static final String MAVEN = "mavenJava";

private final Project project;
private final RecipeViewers recipeViewers;
Expand All @@ -32,6 +36,8 @@ public AlmostGradleExtension(Project project) {
var providers = project.getProviders();

getTestMod().convention(false);
getApiSourceSet().convention(false);
getMavenPublish().convention(false);
getDataGen().set(providers.gradleProperty("datagen").map(s -> {
if (s.equals("true")) return true;
if (s.equals("false")) return false;
Expand All @@ -53,6 +59,10 @@ public AlmostGradleExtension(Project project) {

public abstract Property<Object> getDataGen();

public abstract Property<Boolean> getApiSourceSet();

public abstract Property<Boolean> getMavenPublish();

public RecipeViewers getRecipeViewers() {
return recipeViewers;
}
Expand Down Expand Up @@ -103,6 +113,7 @@ public void setup(Action<AlmostGradleExtension> onSetup) {

createProcessResourcesTask();
applyBuildConfig();
applyApiSourceSet();
applyBasicMod();
applyTestMod();
getRecipeViewers().createRuns();
Expand All @@ -127,6 +138,51 @@ private void onPostRunConfigs() {
});
}

private void applyApiSourceSet() {
if (!getApiSourceSet().get()) {
return;
}

var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class);
var main = javaPlugin.getSourceSets().getByName("main");
var api = javaPlugin.getSourceSets().create("api");
var neoForge = project.getExtensions().getByType(NeoForgeExtension.class);
neoForge.addModdingDependenciesTo(api);
project.getDependencies().add(main.getImplementationConfigurationName(), api.getOutput());

var apiJar = project.getTasks().register("apiJar", Jar.class, jar -> {
jar.getArchiveClassifier().set("api");
jar.from(api.getOutput());
});

var apiSources = project.getTasks().register("apiSources", Jar.class, jar -> {
jar.getArchiveClassifier().set("api-sources");
jar.from(api.getAllJava());
});

project.getTasks().named("jar", Jar.class, jar -> {
jar.dependsOn(apiJar);
jar.from(api.getOutput());
});

project.artifacts(a -> {
a.add("archives", apiJar);
a.add("archives", apiSources);
});

var maven = project.getExtensions().getByType(PublishingExtension.class);
maven.getPublications().withType(MavenPublication.class).configureEach(pub -> {
pub.artifact(apiJar);
pub.artifact(apiSources);
});

if (getWithSourcesJar().get()) {
project.getTasks().named("sourcesJar", Jar.class, jar -> {
jar.from(api.getAllJava());
});
}
}

private void applyBasicMod() {
var neoForge = project.getExtensions().getByType(NeoForgeExtension.class);
var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class);
Expand All @@ -135,6 +191,10 @@ private void applyBasicMod() {
var mainSourceSet = javaPlugin.getSourceSets().getByName("main");

mainMod.sourceSet(mainSourceSet);
if (getApiSourceSet().get()) {
mainMod.sourceSet(javaPlugin.getSourceSets().getByName("api"));
}

neoForge.getRuns().create("client", (run) -> {
run.client();
run.getLoadedMods().set(Set.of(mainMod));
Expand Down Expand Up @@ -167,12 +227,15 @@ private void applyDataGen(NeoForgeExtension neoForge, ModModel mainMod, SourceSe
neoForge.getRuns().create("datagen", (run) -> {
run.data();
run.getLoadedMods().set(Set.of(mainMod));
run.getProgramArguments().addAll(
"--mod", getModId(),
"--all",
"--output", project.file(generatedPath).getAbsolutePath(),
"--existing", project.file("src/main/resources").getAbsolutePath()
);
run
.getProgramArguments()
.addAll("--mod",
getModId(),
"--all",
"--output",
project.file(generatedPath).getAbsolutePath(),
"--existing",
project.file("src/main/resources").getAbsolutePath());
});

log("📕Applied datagen output under: " + generatedPath.replace('/', '.'));
Expand All @@ -195,6 +258,13 @@ private void applyBasics() {
var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class);
javaPlugin.withSourcesJar();
}

if (getMavenPublish().get()) {
project.getPlugins().apply("maven-publish");
var maven = project.getExtensions().getByType(PublishingExtension.class);
var pub = maven.getPublications().create(MAVEN, MavenPublication.class);
pub.from(project.getComponents().getByName("java"));
}
}

private void applyBuildConfig() {
Expand Down

0 comments on commit b153068

Please sign in to comment.