diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Eclipse.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Eclipse.java index cb90cbbf..e7e3b46b 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Eclipse.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Eclipse.java @@ -1,5 +1,6 @@ package io.github.coolcrabs.brachyura.ide; +import java.io.BufferedWriter; import java.io.IOException; import java.io.StringWriter; import java.nio.file.FileVisitResult; @@ -47,6 +48,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO }); for (IdeModule module : ideModules) { if (Files.exists(module.root.resolve(".brachyura").resolve("eclipseout"))) PathUtil.deleteDirectoryChildren(module.root.resolve(".brachyura").resolve("eclipseout")); + if (Files.exists(module.root.resolve(".brachyura").resolve("eclipsetestout"))) PathUtil.deleteDirectoryChildren(module.root.resolve(".brachyura").resolve("eclipsetestout")); writeModule(module); writeClasspath(module); writeLaunchConfigs(projectRoot, module); @@ -108,6 +110,13 @@ void writeModule(IdeModule module) throws IOException, XMLStreamException { w.newline(); w.writeEndDocument(); } + try (BufferedWriter prefs = Files.newBufferedWriter(PathUtil.resolveAndCreateDir(module.root, ".settings").resolve("org.eclipse.jdt.core.prefs"))) { + prefs.write("eclipse.preferences.version=1\n"); + String j = JvmUtil.javaVersionString(module.javaVersion); + prefs.write("org.eclipse.jdt.core.compiler.codegen.targetPlatform="); prefs.write(j); prefs.write('\n'); + prefs.write("org.eclipse.jdt.core.compiler.compliance="); prefs.write(j); prefs.write('\n'); + prefs.write("org.eclipse.jdt.core.compiler.source="); prefs.write(j); prefs.write('\n'); + } } void writeClasspath(IdeModule project) throws IOException, XMLStreamException { @@ -121,8 +130,10 @@ void writeClasspath(IdeModule project) throws IOException, XMLStreamException { w.writeEmptyElement("classpathentry"); w.writeAttribute("kind", "con"); w.writeAttribute("path", "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-" + JvmUtil.javaVersionString(project.javaVersion)); - sourceClasspathEntryAttributes(w, project.root, project.sourcePaths); - sourceClasspathEntryAttributes(w, project.root, project.resourcePaths); + sourceClasspathEntryAttributes(w, project.root, project.sourcePaths, false); + sourceClasspathEntryAttributes(w, project.root, project.resourcePaths, false); + sourceClasspathEntryAttributes(w, project.root, project.testSourcePaths, true); + sourceClasspathEntryAttributes(w, project.root, project.testResourcePaths, true); moduleDepClasspathEntries(w, project); for (JavaJarDependency dep : project.dependencies.get()) { w.newline(); @@ -280,12 +291,33 @@ void stringAttribute(FormattedXMLStreamWriter w, String key, String value) throw w.writeAttribute("value", value); } - void sourceClasspathEntryAttributes(FormattedXMLStreamWriter w, Path projectDir, List paths) throws XMLStreamException { + void sourceClasspathEntryAttributes(FormattedXMLStreamWriter w, Path projectDir, List paths, boolean isTest) throws XMLStreamException { for (Path src : paths) { w.newline(); - w.writeEmptyElement("classpathentry"); + if (isTest) { + w.writeStartElement("classpathentry"); + } else { + w.writeEmptyElement("classpathentry"); + } w.writeAttribute("kind", "src"); w.writeAttribute("path", projectDir.relativize(src).toString()); + if (isTest) { + w.writeAttribute("output", ".brachyura/eclipsetestout"); + w.indent(); + w.newline(); + w.writeStartElement("attributes"); + w.indent(); + w.newline(); + w.writeEmptyElement("attribute"); + w.writeAttribute("name", "test"); + w.writeAttribute("value", "true"); + w.unindent(); + w.newline(); + w.writeEndElement(); + w.unindent(); + w.newline(); + w.writeEndElement(); + } } } diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/IdeModule.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/IdeModule.java index a1f82209..39662965 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/IdeModule.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/IdeModule.java @@ -19,9 +19,11 @@ public class IdeModule { public final List runConfigs; public final List sourcePaths; public final List resourcePaths; + public final List testSourcePaths; + public final List testResourcePaths; public final int javaVersion; - IdeModule(String name, Path root, Supplier> dependencies, List dependencyModules, List runConfigs, List sourcePaths, List resourcePaths, int javaVersion) { + IdeModule(String name, Path root, Supplier> dependencies, List dependencyModules, List runConfigs, List sourcePaths, List resourcePaths, List testSourcePaths, List testResourcePaths, int javaVersion) { this.name = name; this.root = root; this.dependencies = new Lazy<>(dependencies); @@ -32,6 +34,8 @@ public class IdeModule { } this.sourcePaths = sourcePaths; this.resourcePaths = resourcePaths; + this.testSourcePaths = testSourcePaths; + this.testResourcePaths = testResourcePaths; this.javaVersion = javaVersion; } @@ -43,6 +47,8 @@ public static class IdeModuleBuilder { private List runConfigs = Collections.emptyList(); private List sourcePaths = Collections.emptyList(); private List resourcePaths = Collections.emptyList(); + private List testSourcePaths = Collections.emptyList(); + private List testResourcePaths = Collections.emptyList(); private int javaVersion = 8; public IdeModuleBuilder name(String name) { @@ -115,6 +121,33 @@ public IdeModuleBuilder resourcePaths(Path... resourcePaths) { this.resourcePaths = Arrays.asList(resourcePaths); return this; } + + public IdeModuleBuilder testSourcePaths(Path... testSourcePaths) { + this.testSourcePaths = Arrays.asList(testSourcePaths); + return this; + } + + public IdeModuleBuilder testSourcePath(Path testSourcePath) { + this.testSourcePaths = new ArrayList<>(); + testSourcePaths.add(testSourcePath); + return this; + } + + public IdeModuleBuilder testResourcePaths(List testResourcePaths) { + this.testResourcePaths = testResourcePaths; + return this; + } + + public IdeModuleBuilder testResourcePaths(Path... testResourcePaths) { + this.testResourcePaths = Arrays.asList(testResourcePaths); + return this; + } + + public IdeModuleBuilder testResourcePath(Path testResourcePath) { + this.testResourcePaths = new ArrayList<>(); + testResourcePaths.add(testResourcePath); + return this; + } public IdeModuleBuilder javaVersion(int javaVersion) { this.javaVersion = javaVersion; @@ -124,7 +157,7 @@ public IdeModuleBuilder javaVersion(int javaVersion) { public IdeModule build() { Objects.requireNonNull(name, "IdeModule missing name"); Objects.requireNonNull(root, "IdeModule missing root"); - return new IdeModule(name, root, dependencies, dependencyModules, runConfigs, sourcePaths, resourcePaths, javaVersion); + return new IdeModule(name, root, dependencies, dependencyModules, runConfigs, sourcePaths, resourcePaths, testSourcePaths, testResourcePaths, javaVersion); } } diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Intellijank.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Intellijank.java index 5f61bb79..4b636e18 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Intellijank.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/ide/Intellijank.java @@ -162,6 +162,18 @@ void writeModule(IdeModule ideProject) throws IOException, XMLStreamException { w.writeAttribute("url", jankFilePath(resourceDir)); w.writeAttribute("type", "java-resource"); } + for (Path sourceDir : ideProject.testSourcePaths) { + w.newline(); + w.writeEmptyElement("sourceFolder"); + w.writeAttribute("url", jankFilePath(sourceDir)); + w.writeAttribute("isTestSource", "true"); + } + for (Path resourceDir : ideProject.testResourcePaths) { + w.newline(); + w.writeEmptyElement("sourceFolder"); + w.writeAttribute("url", jankFilePath(resourceDir)); + w.writeAttribute("type", "java-test-resource"); + } w.unindent(); w.newline(); w.writeEndElement(); diff --git a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaModule.java b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaModule.java index a6c11b4f..6c23e253 100644 --- a/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaModule.java +++ b/brachyura/src/main/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaModule.java @@ -29,7 +29,7 @@ public List getCompileDependencies() { List deps = dependencies.get(); ArrayList result = new ArrayList<>(deps.size()); for (int i = 0; i < deps.size(); i++) { - result.set(i, deps.get(i).jar); + result.add(deps.get(i).jar); } return result; } diff --git a/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java b/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java index 37bb2404..be25074f 100644 --- a/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java +++ b/brachyura/src/test/java/io/github/coolcrabs/brachyura/project/java/SimpleJavaProjectTest.java @@ -1,8 +1,16 @@ package io.github.coolcrabs.brachyura.project.java; +import io.github.coolcrabs.brachyura.dependency.JavaJarDependency; +import io.github.coolcrabs.brachyura.ide.IdeModule; +import io.github.coolcrabs.brachyura.ide.IdeModule.RunConfigBuilder; +import io.github.coolcrabs.brachyura.maven.Maven; import io.github.coolcrabs.brachyura.maven.MavenId; import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import org.junit.jupiter.api.Test; @@ -27,11 +35,45 @@ public int getJavaVersion() { public Path getProjectDir() { return PathUtil.CWD.getParent().resolve("testprogram"); } + + @Override + public List createDependencies() { + return Arrays.asList( + Maven.getMavenJarDep(Maven.MAVEN_CENTRAL, new MavenId("org.junit.platform:junit-platform-console-standalone:1.8.2")) + ); + } + + @Override + public SimpleJavaModule createProjectModule() { + return new SimpleJavaProjectModule() { + @Override + public IdeModule ideModule() { + return new IdeModule.IdeModuleBuilder() + .name(getModuleName()) + .root(getModuleRoot()) + .javaVersion(getJavaVersion()) + .sourcePaths(getSrcDirs()) + .resourcePaths(getResourceDirs()) + .testSourcePath(getModuleRoot().resolve("src").resolve("test").resolve("java")) + .testResourcePath(getModuleRoot().resolve("src").resolve("test").resolve("resources")) + .dependencies(dependencies.get()) + .dependencyModules(getModuleDependencies().stream().map(BuildModule::ideModule).collect(Collectors.toList())) + .runConfigs( + new RunConfigBuilder() + .name("bruh") + .cwd(getModuleRoot()) + .mainClass("io.github.coolcrabs.testprogram.TestProgram") + ) + .build(); + } + }; + } }; //Todo better api for this? project.getTasks(p -> { - if (p.name.equals("vscode")) p.doTask(new String[]{}); + if (p.name.equals("jdt")) p.doTask(new String[]{}); if (p.name.equals("netbeans")) p.doTask(new String[]{}); + if (p.name.equals("idea")) p.doTask(new String[]{}); }); Assertions.assertNotNull(project.build()); project.getTasks(p -> { diff --git a/testprogram/.gitignore b/testprogram/.gitignore index 46998a5e..44d02ab2 100644 --- a/testprogram/.gitignore +++ b/testprogram/.gitignore @@ -3,3 +3,9 @@ build .brachyura run netbeans +*.iml +.idea +*.launch +.project +.classpath +.settings diff --git a/testprogram/src/main/java/io/github/coolcrabs/testprogram/TestProgram.java b/testprogram/src/main/java/io/github/coolcrabs/testprogram/TestProgram.java index a488cd18..3b679c89 100644 --- a/testprogram/src/main/java/io/github/coolcrabs/testprogram/TestProgram.java +++ b/testprogram/src/main/java/io/github/coolcrabs/testprogram/TestProgram.java @@ -1,4 +1,4 @@ -package io.github.coolcrabs.testprogram; +package io.github.coolcrabs.testprogram; public class TestProgram { public static void main(String[] args) { diff --git a/testprogram/src/test/java/io/github/coolcrabs/testprogram/BruhTest.java b/testprogram/src/test/java/io/github/coolcrabs/testprogram/BruhTest.java new file mode 100644 index 00000000..bccf2399 --- /dev/null +++ b/testprogram/src/test/java/io/github/coolcrabs/testprogram/BruhTest.java @@ -0,0 +1,10 @@ +package io.github.coolcrabs.testprogram; + +import org.junit.jupiter.api.Test; + +public class BruhTest { + @Test + void ugg() { + TestProgram.main(new String[0]); + } +} diff --git a/testprogram/src/test/java/io/github/coolcrabs/testprogram/TestProgramTest.java b/testprogram/src/test/java/io/github/coolcrabs/testprogram/TestProgramTest.java new file mode 100644 index 00000000..de59f595 --- /dev/null +++ b/testprogram/src/test/java/io/github/coolcrabs/testprogram/TestProgramTest.java @@ -0,0 +1,10 @@ +package io.github.coolcrabs.testprogram; + +import org.junit.jupiter.api.Test; + +public class TestProgramTest { + @Test + void bruh() { + TestProgram.HelloWorldProducerSingleton.INSTANCE.sayHello2World(); + } +} diff --git a/testprogram/src/test/resources/yeet.txt b/testprogram/src/test/resources/yeet.txt new file mode 100644 index 00000000..399f9bfe --- /dev/null +++ b/testprogram/src/test/resources/yeet.txt @@ -0,0 +1 @@ +yeet \ No newline at end of file