Skip to content

Commit

Permalink
No longer manipulating System.out and System.err streams, instead wri…
Browse files Browse the repository at this point in the history
…ting maven classpath to disk
  • Loading branch information
rodinaarssen committed Aug 20, 2024
1 parent c9fc81e commit d5c655b
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions src/org/rascalmpl/library/util/PathConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.rascalmpl.library.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -661,35 +659,26 @@ private static IList getPomXmlCompilerClasspath(ISourceLocation manifestRoot) {
}

var maven = new MavenCli();
try (var output = new ByteArrayOutputStream()) {
var oldOut = System.out;
var oldErr = System.err;
try (var out = new PrintStream(output, false, StandardCharsets.UTF_8)) {
System.setOut(out);
System.setErr(out);
maven.doMain(buildRequest(new String[] {"-o", "dependency:build-classpath", "-DincludeScope=compile"}, manifestRoot));
}
finally {
System.setOut(oldOut);
System.setErr(oldErr);
}
var mavenOutput = new String(output.toByteArray(), StandardCharsets.UTF_8);
var match = FIND_CLASS_PATH.matcher(mavenOutput);
var foundClassPath = match.find() ? match.group(1) : "";

return Arrays.stream(foundClassPath.split(File.pathSeparator))
.filter(fileName -> new File(fileName).exists())
.map(elem -> {
try {
return URIUtil.createFileLocation(elem);
}
catch (URISyntaxException e) {
return null;
}
})
.filter(Objects::nonNull)
.collect(vf.listWriter());
}
var tempFile = Files.createTempFile("rascal-classpath-", ".tmp");

maven.doMain(buildRequest(new String[] {"-o", "dependency:build-classpath", "-DincludeScope=compile", "-Dmdep.outputFile=" + tempFile.toString()}, manifestRoot));

var mavenOutput = Files.readAllLines(tempFile);
var match = FIND_CLASS_PATH.matcher(mavenOutput.get(0));

Check warning on line 667 in src/org/rascalmpl/library/util/PathConfig.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/PathConfig.java#L667

Added line #L667 was not covered by tests
var foundClassPath = match.find() ? match.group(1) : "";

return Arrays.stream(foundClassPath.split(File.pathSeparator))
.filter(fileName -> new File(fileName).exists())
.map(elem -> {

Check warning on line 672 in src/org/rascalmpl/library/util/PathConfig.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/PathConfig.java#L670-L672

Added lines #L670 - L672 were not covered by tests
try {
return URIUtil.createFileLocation(elem);

Check warning on line 674 in src/org/rascalmpl/library/util/PathConfig.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/PathConfig.java#L674

Added line #L674 was not covered by tests
}
catch (URISyntaxException e) {
return null;

Check warning on line 677 in src/org/rascalmpl/library/util/PathConfig.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/PathConfig.java#L676-L677

Added lines #L676 - L677 were not covered by tests
}
})
.filter(Objects::nonNull)
.collect(vf.listWriter());

Check warning on line 681 in src/org/rascalmpl/library/util/PathConfig.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/PathConfig.java#L680-L681

Added lines #L680 - L681 were not covered by tests
}
catch (IOException | RuntimeException | ReflectiveOperationException e) {
return vf.list();
Expand Down

0 comments on commit d5c655b

Please sign in to comment.