-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectConfigLoader.java
More file actions
219 lines (202 loc) · 7.86 KB
/
Copy pathProjectConfigLoader.java
File metadata and controls
219 lines (202 loc) · 7.86 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package io.github.randomcodespace.iq.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Loads project-level configuration from .osscodeiq.yml or .osscodeiq.yaml
* found in the target directory, and applies overrides to {@link CodeIqConfig}.
* <p>
* Also produces a {@link ProjectConfig} with pipeline filter settings
* (languages, detector categories, detector includes, exclude patterns, etc.).
*/
public final class ProjectConfigLoader {
private static final Logger log = LoggerFactory.getLogger(ProjectConfigLoader.class);
private static final String[] CONFIG_FILE_NAMES = {".osscodeiq.yml", ".osscodeiq.yaml"};
private ProjectConfigLoader() {
// utility class
}
/**
* Look for .osscodeiq.yml or .osscodeiq.yaml in the given directory.
* If found, parse it and apply matching properties to the config.
*
* @param directory the project root directory to search
* @param config the config to apply overrides to
* @return true if a config file was found and applied
*/
@SuppressWarnings("unchecked")
public static boolean loadIfPresent(Path directory, CodeIqConfig config) {
for (String name : CONFIG_FILE_NAMES) {
Path configFile = directory.resolve(name);
if (Files.isRegularFile(configFile)) {
try {
String content = Files.readString(configFile, StandardCharsets.UTF_8);
Yaml yaml = new Yaml(new org.yaml.snakeyaml.constructor.SafeConstructor(new org.yaml.snakeyaml.LoaderOptions()));
Map<String, Object> data = yaml.load(content);
if (data != null) {
applyOverrides(data, config);
log.info("Loaded project config from {}", configFile);
return true;
}
} catch (IOException e) {
log.warn("Failed to read config file {}: {}", configFile, e.getMessage());
} catch (Exception e) {
log.warn("Failed to parse config file {}: {}", configFile, e.getMessage());
}
}
}
return false;
}
/**
* Load the full project configuration including pipeline filter settings.
*
* @param directory the project root directory to search
* @return parsed ProjectConfig, or {@link ProjectConfig#empty()} if no config found
*/
@SuppressWarnings("unchecked")
public static ProjectConfig loadProjectConfig(Path directory) {
for (String name : CONFIG_FILE_NAMES) {
Path configFile = directory.resolve(name);
if (Files.isRegularFile(configFile)) {
try {
String content = Files.readString(configFile, StandardCharsets.UTF_8);
Yaml yaml = new Yaml(new org.yaml.snakeyaml.constructor.SafeConstructor(new org.yaml.snakeyaml.LoaderOptions()));
Map<String, Object> data = yaml.load(content);
if (data != null) {
log.info("Loaded project config from {}", configFile);
return parseProjectConfig(data);
}
} catch (IOException e) {
log.warn("Failed to read config file {}: {}", configFile, e.getMessage());
} catch (Exception e) {
log.warn("Failed to parse config file {}: {}", configFile, e.getMessage());
}
}
}
return ProjectConfig.empty();
}
/**
* Parse a YAML data map into a structured ProjectConfig.
* Supports:
* <pre>
* languages:
* - java
* - python
* detectors:
* categories:
* - endpoints
* - entities
* include:
* - spring-rest-detector
* parsers:
* java: javaparser
* pipeline:
* parallelism: 4
* batch-size: 100
* exclude:
* - "*.generated.java"
* </pre>
*/
@SuppressWarnings("unchecked")
static ProjectConfig parseProjectConfig(Map<String, Object> data) {
List<String> languages = toStringList(data.get("languages"));
List<String> detectorCategories = null;
List<String> detectorInclude = null;
if (data.get("detectors") instanceof Map<?, ?> detectors) {
detectorCategories = toStringList(detectors.get("categories"));
detectorInclude = toStringList(detectors.get("include"));
}
List<String> exclude = toStringList(data.get("exclude"));
Map<String, String> parsers = null;
if (data.get("parsers") instanceof Map<?, ?> parsersMap) {
parsers = new LinkedHashMap<>();
for (var entry : parsersMap.entrySet()) {
parsers.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
}
Integer parallelism = null;
Integer batchSize = null;
if (data.get("pipeline") instanceof Map<?, ?> pipeline) {
parallelism = toInteger(pipeline.get("parallelism"));
batchSize = toInteger(pipeline.get("batch-size"));
}
return new ProjectConfig(
languages,
detectorCategories,
detectorInclude,
exclude,
parsers,
parallelism,
batchSize
);
}
@SuppressWarnings("unchecked")
private static void applyOverrides(Map<String, Object> data, CodeIqConfig config) {
if (data.containsKey("cache_dir")) {
config.setCacheDir(String.valueOf(data.get("cache_dir")));
}
if (data.containsKey("max_depth")) {
config.setMaxDepth(toInt(data.get("max_depth"), config.getMaxDepth()));
}
if (data.containsKey("max_radius")) {
config.setMaxRadius(toInt(data.get("max_radius"), config.getMaxRadius()));
}
// Nested analysis section (matches Python config structure)
if (data.get("analysis") instanceof Map<?, ?> analysis) {
if (analysis.containsKey("parallelism")) {
// Stored for CLI to pick up; not directly in CodeIqConfig
}
if (analysis.containsKey("incremental")) {
// Available for future use
}
}
// Nested output section
if (data.get("output") instanceof Map<?, ?> output) {
if (output.containsKey("max_nodes")) {
// Available for future use
}
}
}
private static int toInt(Object value, int defaultValue) {
if (value instanceof Number n) {
return n.intValue();
}
try {
return Integer.parseInt(String.valueOf(value));
} catch (NumberFormatException e) {
return defaultValue;
}
}
private static Integer toInteger(Object value) {
if (value == null) return null;
if (value instanceof Number n) {
return n.intValue();
}
try {
return Integer.parseInt(String.valueOf(value));
} catch (NumberFormatException e) {
return null;
}
}
@SuppressWarnings("unchecked")
private static List<String> toStringList(Object value) {
if (value == null) return null;
if (value instanceof List<?> list) {
List<String> result = new ArrayList<>();
for (Object item : list) {
if (item != null) {
result.add(String.valueOf(item));
}
}
return result.isEmpty() ? null : result;
}
return null;
}
}