Skip to content

Commit 5ba8f7c

Browse files
authored
fix: use generic line separator (#169)
## Description Use a generic line separator regEx for Yarn and Go **Related issue (if any):** fixes #168 ## Checklist - [x] I have followed this repository's contributing guidelines. - [x] I will adhere to the project's code of conduct. Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 22bc155 commit 5ba8f7c

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/main/java/com/redhat/exhort/providers/GoModulesProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
138138
boolean matchManifestVersions =
139139
Environment.getBoolean(Provider.PROP_MATCH_MANIFEST_VERSIONS, false);
140140
if (matchManifestVersions) {
141-
String[] goModGraphLines = goModulesResult.split(System.lineSeparator());
141+
String[] goModGraphLines = goModulesResult.split(Operations.GENERIC_LINE_SEPARATOR);
142142
performManifestVersionsCheck(goModGraphLines, manifestPath);
143143
}
144144
if (!buildTree) {
@@ -152,7 +152,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
152152
private void performManifestVersionsCheck(String[] goModGraphLines, Path manifestPath) {
153153
try {
154154
String goModLines = Files.readString(manifestPath);
155-
String[] lines = goModLines.split(System.lineSeparator());
155+
String[] lines = goModLines.split(Operations.GENERIC_LINE_SEPARATOR);
156156
String root = getParentVertex(goModGraphLines[0]);
157157
List<String> comparisonLines =
158158
Arrays.stream(goModGraphLines)
@@ -271,7 +271,8 @@ private Sbom buildSbomFromGraph(
271271
// iterate over go mod graph line by line and create map , with each entry to contain module as
272272
// a key , and
273273
// value of list of that module' dependencies.
274-
List<String> linesList = Arrays.asList(goModulesResult.split(System.lineSeparator()));
274+
List<String> linesList =
275+
Arrays.asList(goModulesResult.split(Operations.GENERIC_LINE_SEPARATOR));
275276

276277
int startingIndex = 0;
277278
for (String line : linesList) {
@@ -325,7 +326,7 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(
325326
String finalVersionsForAllModules =
326327
Operations.runProcessGetOutput(manifestPath.getParent(), "go", "list", "-m", "all");
327328
Map<String, String> finalModulesVersions =
328-
Arrays.stream(finalVersionsForAllModules.split(System.lineSeparator()))
329+
Arrays.stream(finalVersionsForAllModules.split(Operations.GENERIC_LINE_SEPARATOR))
329330
.filter(string -> string.trim().split(" ").length == 2)
330331
.collect(
331332
Collectors.toMap(
@@ -437,7 +438,7 @@ private String buildGoModulesDependencies(Path manifestPath) {
437438
}
438439

439440
private Sbom buildSbomFromList(String golangDeps, List<PackageURL> ignoredDeps) {
440-
String[] allModulesFlat = golangDeps.split(System.lineSeparator());
441+
String[] allModulesFlat = golangDeps.split(Operations.GENERIC_LINE_SEPARATOR);
441442
String parentVertex = getParentVertex(allModulesFlat[0]);
442443
PackageURL root = toPurl(parentVertex, "@", this.goEnvironmentVariableForPurl);
443444
// Get only direct dependencies of root package/module, and that's it.

src/main/java/com/redhat/exhort/providers/YarnBerryProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.github.packageurl.PackageURL;
2121
import com.redhat.exhort.providers.javascript.model.Manifest;
2222
import com.redhat.exhort.sbom.Sbom;
23+
import com.redhat.exhort.tools.Operations;
2324
import java.nio.file.Path;
2425
import java.util.Map;
2526
import java.util.TreeMap;
@@ -36,6 +37,7 @@ public YarnBerryProcessor(String packageManager, Manifest manifest) {
3637
super(packageManager, manifest);
3738
}
3839

40+
@Override
3941
public String[] installCmd(Path manifestDir) {
4042
if (manifestDir != null) {
4143
return new String[] {
@@ -89,7 +91,9 @@ private boolean isRoot(String name) {
8991

9092
@Override
9193
public String parseDepTreeOutput(String output) {
92-
return "[" + output.trim().replace(System.lineSeparator(), "").replace("}{", "},{") + "]";
94+
return "["
95+
+ output.trim().replaceAll(Operations.GENERIC_LINE_SEPARATOR, "").replace("}{", "},{")
96+
+ "]";
9397
}
9498

9599
private PackageURL purlFromNode(String normalizedLocator, JsonNode node) {

src/main/java/com/redhat/exhort/tools/Operations.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
/** Utility class used for executing process on the operating system. * */
3636
public final class Operations {
3737

38+
// Some package providers might return Unix output in Windows, so we need to use a generic line
39+
// separator
40+
public static final String GENERIC_LINE_SEPARATOR = "\\r?\\n";
3841
private static final Logger log = LoggersFactory.getLogger(Operations.class.getName());
3942

4043
private Operations() {

0 commit comments

Comments
 (0)