Skip to content

Commit

Permalink
Correctly handle IntelliJ runs for root project names with spaces (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Jun 21, 2024
1 parent e456622 commit c2f3fea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.jetbrains.gradle.ext.Application;
import org.jetbrains.gradle.ext.IdeaExtPlugin;
import org.jetbrains.gradle.ext.JUnit;
import org.jetbrains.gradle.ext.ModuleRef;
import org.jetbrains.gradle.ext.ProjectSettings;
import org.jetbrains.gradle.ext.RunConfigurationContainer;
import org.slf4j.event.Level;
Expand Down Expand Up @@ -739,7 +738,7 @@ private static void addIntelliJRunConfiguration(Project project,
if (!sourceSets.contains(sourceSet)) {
throw new GradleException("Cannot use source set from another project for run " + run.getName());
}
appRun.setModuleRef(new ModuleRef(project, sourceSet));
appRun.setModuleName(RunUtils.getIntellijModuleName(project, sourceSet));
appRun.setWorkingDirectory(run.getGameDirectory().get().getAsFile().getAbsolutePath());
appRun.setEnvs(run.getEnvironment().get());

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/neoforged/moddevgradle/internal/RunUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.gradle.process.CommandLineArgumentProvider;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.gradle.ext.ModuleRef;
import org.slf4j.event.Level;
import org.xml.sax.InputSource;

Expand Down Expand Up @@ -299,6 +300,25 @@ private static String evaluateXPath(File file, @Language("xpath") String express
throw new UncheckedIOException("Failed to evaluate xpath " + expression + " on file " + file, e);
}
}

/**
* Convert a project and source set to an IntelliJ module name.
* Do not use {@link ModuleRef} as it does not correctly handle projects with a space in their name!
*/
public static String getIntellijModuleName(Project project, SourceSet sourceSet) {
var moduleName = new StringBuilder();
// The `replace` call here is our bug fix compared to ModuleRef!
// The actual IDEA logic is more complicated, but this should cover the majority of use cases.
// See https://github.com/JetBrains/intellij-community/blob/a32fd0c588a6da11fd6d5d2fb0362308da3206f3/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/GradleProjectResolverUtil.java#L205
// which calls https://github.com/JetBrains/intellij-community/blob/a32fd0c588a6da11fd6d5d2fb0362308da3206f3/platform/util-rt/src/com/intellij/util/PathUtilRt.java#L120
moduleName.append(project.getRootProject().getName().replace(" ", "_"));
if (project != project.getRootProject()) {
moduleName.append(project.getPath().replaceAll(":", "."));
}
moduleName.append(".");
moduleName.append(sourceSet.getName());
return moduleName.toString();
}
}

record AssetProperties(String assetIndex, String assetsRoot) {
Expand Down

0 comments on commit c2f3fea

Please sign in to comment.