Skip to content

Commit

Permalink
Add support for DevLogin
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed May 29, 2024
1 parent e8c6d92 commit c71abfc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement;
import net.neoforged.gradle.dsl.common.extensions.repository.Repository;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Conventions;
import net.neoforged.gradle.dsl.common.extensions.subsystems.DevLogin;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Tools;
import net.neoforged.gradle.dsl.common.extensions.subsystems.conventions.Configurations;
import net.neoforged.gradle.dsl.common.extensions.subsystems.conventions.IDE;
import net.neoforged.gradle.dsl.common.extensions.subsystems.conventions.Runs;
Expand All @@ -36,17 +38,15 @@
import net.neoforged.gradle.util.UrlConstants;
import org.gradle.StartParameter;
import org.gradle.TaskExecutionRequest;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.*;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.Category;
import org.gradle.api.component.AdhocComponentWithVariants;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.Delete;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.internal.DefaultTaskExecutionRequest;
Expand Down Expand Up @@ -346,6 +346,10 @@ private void applyAfterEvaluate(final Project project) {
}
}

if (run.getModSources().get().isEmpty()) {
throw new GradleException("Run: " + run.getName() + " has no source sets configured. Please configure at least one source set.");
}

if (run.getConfigureFromDependencies().get()) {
final RunImpl runImpl = (RunImpl) run;

Expand Down Expand Up @@ -374,6 +378,28 @@ private void applyAfterEvaluate(final Project project) {
definitionSet.forEach((identifier, definition) -> {
definition.configureRun(runImpl);
});

//Handle dev login.
final DevLogin devLogin = project.getExtensions().getByType(Subsystems.class).getDevLogin();
final Tools tools = project.getExtensions().getByType(Subsystems.class).getTools();
if (devLogin.getEnabled().get()) {
//Dev login is only supported on the client side
if (runImpl.getIsClient().get()) {
final String mainClass = runImpl.getMainClass().get();

//We add the dev login tool to the runtime only configuration, of the first source set, this should suffice
final SourceSet defaultSourceSet = runImpl.getModSources().get().get(0);
final Configuration defaultRuntimeOnlyConfiguration = project.getConfigurations().maybeCreate(defaultSourceSet.getRuntimeOnlyConfigurationName());
defaultRuntimeOnlyConfiguration.getDependencies().add(project.getDependencies().create(tools.getDevLogin().get()));

//Update the program arguments to properly launch the dev login tool
run.getProgramArguments().add("--launch_target");
run.getProgramArguments().add(mainClass);

//Set the main class to the dev login tool
run.getMainClass().set("net.covers1624.devlogin.DevLogin");
}
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
import java.util.Collections;
import java.util.Locale;

import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_ARTIFACT_PREFIX;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_GROUP;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_PARCHMENT_MAVEN_URL;
import static net.neoforged.gradle.dsl.common.util.Constants.JST_TOOL_ARTIFACT;
import static net.neoforged.gradle.dsl.common.util.Constants.DEFAULT_RECOMPILER_MAX_MEMORY;
import static net.neoforged.gradle.dsl.common.util.Constants.*;

public abstract class SubsystemsExtension extends WithPropertyLookup implements ConfigurableDSLElement<Subsystems>, Subsystems {

Expand All @@ -33,13 +29,42 @@ public SubsystemsExtension(Project project) {
configureRecompilerDefaults();
configureParchmentDefaults();
configureToolsDefaults();
configureDevLoginDefaults();
}

private void configureDevLoginDefaults() {
DevLogin devLogin = getDevLogin();
devLogin.getEnabled().convention(
getBooleanProperty("devLogin.enabled").orElse(true)
);
devLogin.getAddRepository().convention(
getBooleanProperty("devLogin.addRepository").orElse(true)
);

// Add a filtered dev login repository automatically if enabled
project.afterEvaluate(p -> {
if (!devLogin.getEnabled().get() || !devLogin.getAddRepository().get()) {
return;
}
MavenArtifactRepository repo = p.getRepositories().maven(m -> {
m.setName("DevLogin Tool");
m.setUrl(URI.create(DEFAULT_DEVLOGIN_MAVEN_URL));
m.mavenContent(mavenContent -> mavenContent.includeGroup(DEFAULT_DEVLOGIN_GROUP));
});
// Make sure it comes first due to its filtered group, that should speed up resolution
p.getRepositories().remove(repo);
p.getRepositories().addFirst(repo);
});
}

private void configureToolsDefaults() {
Tools tools = getTools();
tools.getJST().convention(
getStringProperty("tools.jst").orElse(JST_TOOL_ARTIFACT)
);
tools.getDevLogin().convention(
getStringProperty("tools.devlogin").orElse(DEVLOGIN_TOOL_ARTIFACT)
);
}

private void configureDecompilerDefaults() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.neoforged.gradle.dsl.common.extensions.subsystems

import groovy.transform.CompileStatic
import net.minecraftforge.gdi.ConfigurableDSLElement
import net.minecraftforge.gdi.annotations.DSLProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

/**
* Allows configuration of the dev login system.
*/
@CompileStatic
interface DevLogin extends ConfigurableDSLElement<DevLogin> {

/**
* @return Whether or not dev login is enabled on launch.
*/
@Input
@Optional
@DSLProperty
Property<Boolean> getEnabled();

/**
* @return Whether or not to add the dev login repository to the project.
*/
@Input
@Optional
@DSLProperty
Property<Boolean> getAddRepository();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ interface Subsystems extends BaseDSLElement<Subsystems> {
@Nested
@DSLProperty
Tools getTools();

/**
* @return settings for the dev login subsystem
*/
@Nested
@DSLProperty
DevLogin getDevLogin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ interface Tools extends ConfigurableDSLElement<Parchment> {
@DSLProperty
Property<String> getJST();

/**
* Artifact coordinates for the NeoGradle decompiler.
* Used by the runs subsystem to allow login to the dev environment.
*/
@Input
@Optional
@DSLProperty
Property<String> getDevLogin();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Constants {
public static final String DEFAULT_PARCHMENT_ARTIFACT_PREFIX = "parchment-"
public static final String DEFAULT_PARCHMENT_MAVEN_URL = "https://maven.parchmentmc.org/"
public static final String JST_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.36"
public static final String DEFAULT_DEVLOGIN_GROUP = "net.covers1624"
public static final String DEFAULT_DEVLOGIN_ARTIFACT = "DevLogin"
public static final String DEFAULT_DEVLOGIN_MAVEN_URL = "https://maven.covers1624.net/"
public static final String DEVLOGIN_TOOL_ARTIFACT = "net.covers1624:DevLogin:0.1.0.3"
public static final String DEVLOGIN_MAIN_CLASS = "net.covers1624.devlogin.DevLogin"

public static final String DEFAULT_RECOMPILER_MAX_MEMORY = "1g"

Expand Down

0 comments on commit c71abfc

Please sign in to comment.