Skip to content

Commit

Permalink
Slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed May 25, 2024
1 parent da2b0bc commit 44888f3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ sourceSets {

dependencies {
java17CompileOnly gradleApi()
java17CompileOnly "com.intellij:annotations:9.0.4"
java17Implementation "com.google.code.gson:gson:2.10.1"
java17Implementation "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:1.1.8"
java17Implementation "net.fabricmc:fabric-loom-native:0.2.1"
java17Implementation "net.neoforged:JarJarMetadata:0.4.1"
java17Implementation "com.intellij:annotations:9.0.4"
java17Implementation "net.neoforged:EclipseLaunchConfigs:0.1.11"
api "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:1.1.8"
api "net.neoforged:JarJarMetadata:0.4.1"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,59 +362,14 @@ public void apply(Project project) {
});
});

// IDEA Sync has no real notion of tasks or providers or similar
project.afterEvaluate(ignored -> {
var settings = getIntelliJProjectSettings(project);
if (settings != null) {
var taskTriggers = ((ExtensionAware) settings).getExtensions().getByType(TaskTriggersConfig.class);
// Careful, this will overwrite on intellij (and append on eclipse, but we aren't there yet!)
taskTriggers.afterSync(idePostSyncTask);
}

var runConfigurations = getIntelliJRunConfigurations(project); // TODO: Consider making this a value source

if (runConfigurations == null) {
project.getLogger().debug("Failed to find IntelliJ run configuration container. Not adding run configurations.");
} else {
var outputDirectory = RunUtils.getIntellijOutputDirectory(project);

extension.getRuns().forEach(run -> {
var prepareTask = prepareRunTasks.get(run).get();
if (!prepareTask.getEnabled()) {
project.getLogger().lifecycle("Not creating IntelliJ run {} since its prepare task {} is disabled", run, prepareTask);
return;
}
addIntelliJRunConfiguration(project, runConfigurations, outputDirectory, run, prepareTask);
});
}
});

setupJarJar(project);

setupTesting(project, userDevConfigOnly, neoForgeModDevModules, downloadAssets, idePostSyncTask, createArtifacts, neoForgeModDevLibrariesDependency);

// Set up stuff for Eclipse
var eclipseModel = ExtensionUtils.findExtension(project, "eclipse", EclipseModel.class);
if (eclipseModel != null) {
// Make sure our post-sync task runs on Eclipse
eclipseModel.synchronizationTasks(idePostSyncTask);

// When using separate artifacts for classes and sources, link them
if (!shouldUseCombinedSourcesAndClassesArtifact()) {
var fileClasspath = eclipseModel.getClasspath().getFile();
fileClasspath.whenMerged((org.gradle.plugins.ide.eclipse.model.Classpath classpath) -> {
var classesPath = createArtifacts.get().getCompiledArtifact().get().getAsFile();
var sourcesPath = createArtifacts.get().getSourcesArtifact().get().getAsFile();

for (var entry : classpath.getEntries()) {
System.out.println(entry);
if (entry instanceof Library library && classesPath.equals(new File(library.getPath()))) {
library.setSourcePath(classpath.fileReference(sourcesPath));
}
}
});
}
}
configureIntelliJModel(project, idePostSyncTask, extension, prepareRunTasks);

configureEclipseModel(project, idePostSyncTask, createArtifacts);
}

private static boolean shouldUseCombinedSourcesAndClassesArtifact() {
Expand Down Expand Up @@ -565,6 +520,35 @@ private static void addIntelliJRunConfiguration(Project project,
runConfigurations.add(a);
}

private static void configureIntelliJModel(Project project, TaskProvider<Task> idePostSyncTask, NeoForgeExtension extension, Map<RunModel, TaskProvider<PrepareRunForIde>> prepareRunTasks) {
// IDEA Sync has no real notion of tasks or providers or similar
project.afterEvaluate(ignored -> {
var settings = getIntelliJProjectSettings(project);
if (settings != null) {
var taskTriggers = ((ExtensionAware) settings).getExtensions().getByType(TaskTriggersConfig.class);
// Careful, this will overwrite on intellij (and append on eclipse, but we aren't there yet!)
taskTriggers.afterSync(idePostSyncTask);
}

var runConfigurations = getIntelliJRunConfigurations(project); // TODO: Consider making this a value source

if (runConfigurations == null) {
project.getLogger().debug("Failed to find IntelliJ run configuration container. Not adding run configurations.");
} else {
var outputDirectory = RunUtils.getIntellijOutputDirectory(project);

extension.getRuns().forEach(run -> {
var prepareTask = prepareRunTasks.get(run).get();
if (!prepareTask.getEnabled()) {
project.getLogger().lifecycle("Not creating IntelliJ run {} since its prepare task {} is disabled", run, prepareTask);
return;
}
addIntelliJRunConfiguration(project, runConfigurations, outputDirectory, run, prepareTask);
});
}
});
}

@Nullable
private static IdeaProject getIntelliJProject(Project project) {
var ideaModel = ExtensionUtils.findExtension(project, "idea", IdeaModel.class);
Expand Down Expand Up @@ -624,5 +608,36 @@ static String guessMavenGav(ResolvedArtifactResult result) {
}
return gav;
}

private static void configureEclipseModel(Project project,
TaskProvider<Task> idePostSyncTask,
TaskProvider<CreateMinecraftArtifactsTask> createArtifacts) {
// Set up stuff for Eclipse
var eclipseModel = ExtensionUtils.findExtension(project, "eclipse", EclipseModel.class);
if (eclipseModel == null) {
return;
}

// Make sure our post-sync task runs on Eclipse
eclipseModel.synchronizationTasks(idePostSyncTask);

// When using separate artifacts for classes and sources, link them
if (!shouldUseCombinedSourcesAndClassesArtifact()) {
var fileClasspath = eclipseModel.getClasspath().getFile();
fileClasspath.whenMerged((org.gradle.plugins.ide.eclipse.model.Classpath classpath) -> {
var classesPath = createArtifacts.get().getCompiledArtifact().get().getAsFile();
var sourcesPath = createArtifacts.get().getSourcesArtifact().get().getAsFile();

for (var entry : classpath.getEntries()) {
System.out.println(entry);
if (entry instanceof Library library && classesPath.equals(new File(library.getPath()))) {
library.setSourcePath(classpath.fileReference(sourcesPath));
}
}
});
}

// Set up runs
}
}

0 comments on commit 44888f3

Please sign in to comment.