Skip to content

Commit

Permalink
ide test sources + vscode fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolMineman committed Jun 13, 2022
1 parent 690be55 commit 6fb9b56
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -280,12 +291,33 @@ void stringAttribute(FormattedXMLStreamWriter w, String key, String value) throw
w.writeAttribute("value", value);
}

void sourceClasspathEntryAttributes(FormattedXMLStreamWriter w, Path projectDir, List<Path> paths) throws XMLStreamException {
void sourceClasspathEntryAttributes(FormattedXMLStreamWriter w, Path projectDir, List<Path> 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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public class IdeModule {
public final List<RunConfig> runConfigs;
public final List<Path> sourcePaths;
public final List<Path> resourcePaths;
public final List<Path> testSourcePaths;
public final List<Path> testResourcePaths;
public final int javaVersion;

IdeModule(String name, Path root, Supplier<List<JavaJarDependency>> dependencies, List<IdeModule> dependencyModules, List<RunConfigBuilder> runConfigs, List<Path> sourcePaths, List<Path> resourcePaths, int javaVersion) {
IdeModule(String name, Path root, Supplier<List<JavaJarDependency>> dependencies, List<IdeModule> dependencyModules, List<RunConfigBuilder> runConfigs, List<Path> sourcePaths, List<Path> resourcePaths, List<Path> testSourcePaths, List<Path> testResourcePaths, int javaVersion) {
this.name = name;
this.root = root;
this.dependencies = new Lazy<>(dependencies);
Expand All @@ -32,6 +34,8 @@ public class IdeModule {
}
this.sourcePaths = sourcePaths;
this.resourcePaths = resourcePaths;
this.testSourcePaths = testSourcePaths;
this.testResourcePaths = testResourcePaths;
this.javaVersion = javaVersion;
}

Expand All @@ -43,6 +47,8 @@ public static class IdeModuleBuilder {
private List<RunConfigBuilder> runConfigs = Collections.emptyList();
private List<Path> sourcePaths = Collections.emptyList();
private List<Path> resourcePaths = Collections.emptyList();
private List<Path> testSourcePaths = Collections.emptyList();
private List<Path> testResourcePaths = Collections.emptyList();
private int javaVersion = 8;

public IdeModuleBuilder name(String name) {
Expand Down Expand Up @@ -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<Path> 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;
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<Path> getCompileDependencies() {
List<JavaJarDependency> deps = dependencies.get();
ArrayList<Path> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -27,11 +35,45 @@ public int getJavaVersion() {
public Path getProjectDir() {
return PathUtil.CWD.getParent().resolve("testprogram");
}

@Override
public List<JavaJarDependency> 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 -> {
Expand Down
6 changes: 6 additions & 0 deletions testprogram/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ build
.brachyura
run
netbeans
*.iml
.idea
*.launch
.project
.classpath
.settings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.coolcrabs.testprogram;
package io.github.coolcrabs.testprogram;

public class TestProgram {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions testprogram/src/test/resources/yeet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yeet

0 comments on commit 6fb9b56

Please sign in to comment.