-
Notifications
You must be signed in to change notification settings - Fork 70
/
PackratLockFileParser.java
121 lines (102 loc) · 5.24 KB
/
PackratLockFileParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* detectable
*
* Copyright (c) 2019 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectables.cran.parse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import com.synopsys.integration.bdio.graph.DependencyGraph;
import com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder;
import com.synopsys.integration.bdio.graph.builder.MissingExternalIdException;
import com.synopsys.integration.bdio.model.Forge;
import com.synopsys.integration.bdio.model.dependencyid.DependencyId;
import com.synopsys.integration.bdio.model.dependencyid.NameDependencyId;
import com.synopsys.integration.bdio.model.dependencyid.NameVersionDependencyId;
import com.synopsys.integration.bdio.model.externalid.ExternalId;
import com.synopsys.integration.bdio.model.externalid.ExternalIdFactory;
public class PackratLockFileParser {
private static final String PACKAGE_TOKEN = "Package";
private static final String VERSION_TOKEN = "Version";
private static final String REQUIRES_TOKEN = "Requires";
private static final String INDENTATION_TOKEN = " ";
private final ExternalIdFactory externalIdFactory;
public PackratLockFileParser(final ExternalIdFactory externalIdFactory) {
this.externalIdFactory = externalIdFactory;
}
public DependencyGraph parseProjectDependencies(final List<String> packratLockContents) throws MissingExternalIdException {
final LazyExternalIdDependencyGraphBuilder graphBuilder = new LazyExternalIdDependencyGraphBuilder();
DependencyId currentParent = null;
String name = null;
boolean requiresSection = false;
for (final String line : packratLockContents) {
if (StringUtils.isBlank(line)) {
currentParent = null;
name = null;
requiresSection = false;
continue;
}
if (!(line.startsWith(PACKAGE_TOKEN) || line.startsWith(VERSION_TOKEN) || line.startsWith(REQUIRES_TOKEN) || line.startsWith(INDENTATION_TOKEN))) {
continue;
}
if (line.startsWith(PACKAGE_TOKEN)) {
name = getValueFromLine(line);
currentParent = new NameDependencyId(name);
graphBuilder.setDependencyName(currentParent, name);
graphBuilder.addChildToRoot(currentParent);
requiresSection = false;
} else if (line.startsWith(VERSION_TOKEN)) {
final String version = getValueFromLine(line);
graphBuilder.setDependencyVersion(currentParent, version);
final DependencyId realId = new NameVersionDependencyId(name, version);
final ExternalId externalId = this.externalIdFactory.createNameVersionExternalId(Forge.CRAN, name, version);
graphBuilder.setDependencyAsAlias(realId, currentParent);
graphBuilder.setDependencyInfo(realId, name, version, externalId);
currentParent = realId;
} else if (line.startsWith(REQUIRES_TOKEN)) {
requiresSection = true;
final String cleanLine = getValueFromLine(line);
final List<DependencyId> children = getChildrenNames(cleanLine).stream()
.map(NameDependencyId::new)
.collect(Collectors.toList());
graphBuilder.addParentWithChildren(currentParent, children);
} else if (requiresSection && line.startsWith(INDENTATION_TOKEN)) {
final List<DependencyId> children = getChildrenNames(line).stream()
.map(NameDependencyId::new)
.collect(Collectors.toList());
graphBuilder.addParentWithChildren(currentParent, children);
}
}
return graphBuilder.build();
}
private String getValueFromLine(final String line) {
final int separatorIndex = line.indexOf(':');
return line.substring(separatorIndex + 1).trim();
}
private List<String> getChildrenNames(final String line) {
final String[] parts = line.split(",");
return Arrays.stream(parts)
.map(String::trim)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toList());
}
}