Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
[6/?] Improved task support: Eclipse source lookup path editing support
Browse files Browse the repository at this point in the history
One step closer towards the ultimate galimulator debugging experience
  • Loading branch information
Geolykt committed Jul 18, 2022
1 parent 95b9c81 commit 5a5ecd0
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 16 deletions.
2 changes: 1 addition & 1 deletion brachyura/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.coolcrabs</groupId>
<artifactId>brachyura</artifactId>
<version>0.90.4</version>
<version>0.90.5</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public Path[] getResourceDirs() {
}

@Override
@NotNull
public String getModuleName() {
return getModId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gson.stream.JsonWriter;

import io.github.coolcrabs.brachyura.dependency.JavaJarDependency;
import io.github.coolcrabs.brachyura.ide.source.SourceLookupEntry;
import io.github.coolcrabs.brachyura.project.Task;
import io.github.coolcrabs.brachyura.util.AtomicFile;
import io.github.coolcrabs.brachyura.util.JvmUtil;
Expand All @@ -30,8 +31,8 @@
public enum Eclipse implements Ide {
INSTANCE;

private static final String JDT_JRE_CONTAINER_KEY = "org.eclipse.jdt.launching.JRE_CONTAINER";
private static final String JDT_JRE_CONTAINER_JVM = JDT_JRE_CONTAINER_KEY + "/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-";
public static final String JDT_JRE_CONTAINER_KEY = "org.eclipse.jdt.launching.JRE_CONTAINER";
public static final String JDT_JRE_CONTAINER_JVM = JDT_JRE_CONTAINER_KEY + "/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-";

@Override
@NotNull
Expand Down Expand Up @@ -223,6 +224,10 @@ void writeLaunchConfigs(Path projectDir, IdeModule ideProject) throws IOExceptio
w.newline();
w.writeEndElement();
w.newline();
stringAttribute(w, "org.eclipse.debug.core.source_locator_id", "org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector");
w.newline();
stringAttribute(w, "org.eclipse.debug.core.source_locator_memento", sourceLookupValue(task.getIdeDebugConfigSourceLookupEntries()));
w.newline();
booleanAttribute(w, "org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE", false);
w.newline();
booleanAttribute(w, "org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES", false);
Expand Down Expand Up @@ -320,6 +325,29 @@ String projectValue(String project) throws XMLStreamException {
return writer.toString();
}

String sourceLookupValue(@NotNull List<SourceLookupEntry> sources) throws XMLStreamException {
StringWriter writer = new StringWriter();
try (FormattedXMLStreamWriter w = XmlUtil.newStreamWriter(writer)) {
w.writeStartDocument("UTF-8", "1.0");
w.writeStartElement("sourceLookupDirector");
w.writeStartElement("sourceContainers");
w.writeAttribute("duplicates", "false");
for (SourceLookupEntry sourceEntry : sources) {
w.writeEmptyElement("container");
w.writeAttribute("memento", sourceEntry.getEclipseJDTValue());
w.writeAttribute("typeId", sourceEntry.getEclipseJDTType());
}
w.writeEmptyElement("container");
w.writeAttribute("memento", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><default/>");
w.writeAttribute("typeId", "org.eclipse.debug.core.containerType.default");
w.unindent();
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
}
return writer.toString();
}

void booleanAttribute(FormattedXMLStreamWriter w, String key, boolean value) throws XMLStreamException {
w.writeEmptyElement("booleanAttribute");
w.writeAttribute("key", key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.coolcrabs.brachyura.ide.source;

import java.io.StringWriter;
import java.nio.file.Path;

import javax.xml.stream.XMLStreamException;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import io.github.coolcrabs.brachyura.dependency.JavaJarDependency;
import io.github.coolcrabs.brachyura.util.XmlUtil.FormattedXMLStreamWriter;

public class JarDepSourceLookupEntry implements SourceLookupEntry {

@NotNull
private final Path sourcesJar;

public JarDepSourceLookupEntry(@NotNull JavaJarDependency dep) {
Path sourcesJar = dep.sourcesJar;
if (sourcesJar == null) {
throw new IllegalArgumentException("JavaJarDependency has no sources jar attached.");
}
this.sourcesJar = sourcesJar;
}

@Override
@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTType() {
return "org.eclipse.debug.core.containerType.externalArchive";
}

@Override
@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTValue() {
StringWriter writer = new StringWriter();
try (FormattedXMLStreamWriter xmlWriter = new FormattedXMLStreamWriter(writer)) {
xmlWriter.writeStartDocument("UTF-8", "1.0");
xmlWriter.writeEmptyElement("archive");
xmlWriter.writeAttribute("detectRoot", "true"); // I have no idea what this does
xmlWriter.writeAttribute("path", sourcesJar.toAbsolutePath().toString());
xmlWriter.writeEndDocument();
} catch (XMLStreamException e) {
throw new IllegalStateException(e);
}
return writer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.coolcrabs.brachyura.ide.source;

import java.io.StringWriter;

import javax.xml.stream.XMLStreamException;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import io.github.coolcrabs.brachyura.ide.Eclipse;
import io.github.coolcrabs.brachyura.util.XmlUtil.FormattedXMLStreamWriter;

public class JavaJRESourceLookupEntry implements SourceLookupEntry {

private int javaVersion;

public JavaJRESourceLookupEntry(int javaVersion) {
this.javaVersion = javaVersion;
}

@Override
@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTType() {
return "org.eclipse.jdt.launching.sourceContainer.classpathContainer";
}

@Override
@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTValue() {
StringWriter writer = new StringWriter();
try (FormattedXMLStreamWriter xmlWriter = new FormattedXMLStreamWriter(writer)) {
xmlWriter.writeStartDocument("UTF-8", "1.0");
xmlWriter.writeEmptyElement("classpathContainer");
xmlWriter.writeAttribute("path", Eclipse.JDT_JRE_CONTAINER_JVM + getJavaVersion());
xmlWriter.writeEndDocument();
} catch (XMLStreamException e) {
throw new IllegalStateException(e);
}
return writer.toString();
}

@Contract(pure = true)
public int getJavaVersion() {
return javaVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.github.coolcrabs.brachyura.ide.source;

import java.io.StringWriter;

import javax.xml.stream.XMLStreamException;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import io.github.coolcrabs.brachyura.project.java.BuildModule;
import io.github.coolcrabs.brachyura.project.java.SimpleJavaProject;
import io.github.coolcrabs.brachyura.util.XmlUtil.FormattedXMLStreamWriter;

/**
* Creates a debug source lookup entry that provides the source of another java project.
* Usually it will be another brachyura build module, but really can be any java project under a given name
* however doing that increases the risk of the project not running on other computers.
*
* <p>Dependencies of that project are excluded and may need to be added manually using
* {@link JarDepSourceLookupEntry}.
*
* <p>It is generally preferable to use this class over {@link JarDepSourceLookupEntry} if possible
* as those two projects will more or less be linked, making hotswapping easier.
*
* @author Geolykt
* @since 0.90.5
*/
public class JavaProjectSourceLookupEntry implements SourceLookupEntry {

@NotNull
private final String projectName;

public JavaProjectSourceLookupEntry(@NotNull BuildModule module) {
this.projectName = module.getModuleName();
}

public JavaProjectSourceLookupEntry(@NotNull SimpleJavaProject project) {
this(project.projectModule.get());
}

public JavaProjectSourceLookupEntry(@NotNull String projectName) {
this.projectName = projectName;
}

@Override
@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTType() {
return "org.eclipse.jdt.launching.sourceContainer.javaProject";
}

@Override
@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTValue() {
StringWriter writer = new StringWriter();
try (FormattedXMLStreamWriter xmlWriter = new FormattedXMLStreamWriter(writer)) {
xmlWriter.writeStartDocument("UTF-8", "1.0");
xmlWriter.writeEmptyElement("javaProject");
xmlWriter.writeAttribute("name", getProjectName());
xmlWriter.writeEndDocument();
} catch (XMLStreamException e) {
throw new IllegalStateException(e);
}
return writer.toString();
}

@NotNull
@Contract(pure = true)
public String getProjectName() {
return projectName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.coolcrabs.brachyura.ide.source;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public interface SourceLookupEntry {

@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTType();

@NotNull
@Contract(value = "-> !null", pure = true)
public String getEclipseJDTValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jetbrains.annotations.NotNull;

import io.github.coolcrabs.brachyura.exception.TaskFailedException;
import io.github.coolcrabs.brachyura.ide.source.SourceLookupEntry;
import io.github.coolcrabs.brachyura.util.ThrowingRunnable;

/**
Expand All @@ -21,6 +22,16 @@
*/
public abstract class Task {

/**
* The list of source lookup entries that are used in debug runs.
* As of now, this feature is only supported for the eclipse IDE.
* Contributions to make it work on other IDEs are welcome.
*
* @since 0.90.5
*/
@NotNull
private final List<SourceLookupEntry> ideDebugConfigSourceLookupEntries;

@NotNull
public final String name;

Expand Down Expand Up @@ -49,6 +60,18 @@ public abstract class Task {
@NotNull
private static final String DEFAULT_MAIN_CLASS = "io.github.coolcrabs.brachyura.project.BuildscriptDevEntry";

/**
* Obtains the list of source lookup entries that are used in debug runs.
* As of now, this feature is only supported for the eclipse IDE.
* Contributions to make it work on other IDEs are welcome.
*
* @since 0.90.5
*/
@NotNull
public List<SourceLookupEntry> getIdeDebugConfigSourceLookupEntries() {
return ideDebugConfigSourceLookupEntries;
}

/**
* Obtains the name of the task. Used in the CLI as the task name and used in IDE run configuration files for
* the name of the run configuration. The task name should ideally not contain any spaces to simplify the process
Expand Down Expand Up @@ -245,10 +268,10 @@ protected Task(@NotNull String name, @NotNull Path workingDirectory) {
* @param projectPath The path to the project directory
*/
Task(@NotNull String name, @NotNull Path workingDirectory, @NotNull Path projectPath) {
this(name, DEFAULT_JAVA_VERSION, DEFAULT_MAIN_CLASS, workingDirectory, new ArrayList<>(), getDefaultArgs(name, projectPath), new ArrayList<>(), getDefaultClasspath());
this(name, DEFAULT_JAVA_VERSION, DEFAULT_MAIN_CLASS, workingDirectory, new ArrayList<>(), getDefaultArgs(name, projectPath), new ArrayList<>(), getDefaultClasspath(), new ArrayList<>());
}

Task(@NotNull String name, int javaVer, @NotNull String mainClass, @NotNull Path workingDir, @NotNull List<String> vmArgs, @NotNull List<String> args, @NotNull List<Path> resourcePath, @NotNull List<Path> classPath) {
Task(@NotNull String name, int javaVer, @NotNull String mainClass, @NotNull Path workingDir, @NotNull List<String> vmArgs, @NotNull List<String> args, @NotNull List<Path> resourcePath, @NotNull List<Path> classPath, @NotNull List<SourceLookupEntry> ideDebugConfigSourceLookupEntries) {
this.name = name;
this.ideRunConfigJavaVersion = javaVer;
this.ideRunConfigMainClass = mainClass;
Expand All @@ -257,6 +280,7 @@ protected Task(@NotNull String name, @NotNull Path workingDirectory) {
this.ideRunConfigClasspath = classPath;
this.ideRunConfigResourcepath = resourcePath;
this.ideRunConfigWorkingDir = workingDir;
this.ideDebugConfigSourceLookupEntries = ideDebugConfigSourceLookupEntries;
}

@NotNull
Expand Down
Loading

0 comments on commit 5a5ecd0

Please sign in to comment.