-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluePreprocessor.java
More file actions
170 lines (144 loc) · 7.23 KB
/
BluePreprocessor.java
File metadata and controls
170 lines (144 loc) · 7.23 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
package blue.contract.packager.utils;
import blue.contract.packager.BluePackageExporter;
import blue.contract.packager.graphbuilder.FileSystemDependencyGraphBuilder;
import blue.contract.packager.model.BluePackage;
import blue.language.model.Node;
import blue.language.utils.BlueIdCalculator;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Arrays;
import static blue.language.utils.UncheckedObjectMapper.YAML_MAPPER;
public class BluePreprocessor {
public static void main(String[] args) {
if (args.length < 2 || args[0].equals("-h") || args[0].equals("--help")) {
printUsage();
System.exit(args.length < 2 ? 1 : 0);
}
try {
String inputDir = args[0];
String outputDir = args[1];
Set<String> ignorePatterns = new HashSet<>();
boolean verbose = false;
// Parse additional arguments
for (int i = 2; i < args.length; i++) {
if (args[i].equals("--ignore") && i + 1 < args.length) {
String[] patterns = args[i + 1].split(",");
ignorePatterns.addAll(Arrays.asList(patterns));
i++; // Skip the next argument as we've already processed it
} else if (args[i].equals("--verbose")) {
verbose = true;
}
}
// Add default ignore patterns
ignorePatterns.addAll(Arrays.asList(
".git",
".github",
"node_modules",
"build",
".gradle",
"blue-preprocessed"
));
if (verbose) {
System.out.println("Input directory: " + inputDir);
System.out.println("Output directory: " + outputDir);
System.out.println("Ignored patterns: " + String.join(", ", ignorePatterns));
}
Path inputPath = Paths.get(inputDir);
Path outputPath = Paths.get(outputDir);
if (!Files.exists(inputPath)) {
System.err.println("Input directory does not exist: " + inputPath);
System.exit(1);
}
// Create output directory if it doesn't exist
Files.createDirectories(outputPath);
// Create builder with ignore patterns
FileSystemDependencyGraphBuilder builder = new FileSystemDependencyGraphBuilder(inputPath, ignorePatterns);
List<BluePackage> packages = BluePackageExporter.exportPackages("", builder);
writePreprocessedFiles(packages, outputPath);
System.out.println("Preprocessing completed successfully.");
System.out.println("Preprocessed files written to: " + outputPath.toAbsolutePath());
} catch (Exception e) {
System.err.println("Error during preprocessing: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
private static void printUsage() {
System.out.println("Usage: blue-preprocessor <input-dir> <output-dir> [options]");
System.out.println("Options:");
System.out.println(" --ignore <patterns> Comma-separated list of directory patterns to ignore");
System.out.println(" --verbose Print detailed processing information");
System.out.println(" --help Show this help message");
System.out.println("\nDefault ignored patterns: .git, .github, node_modules, build, .gradle");
}
private static void writePreprocessedFiles(List<BluePackage> packages, Path outputPath) throws Exception {
System.out.println("Writing preprocessed files...");
// Write package-specific files
for (BluePackage pkg : packages) {
String pkgName = pkg.getDirectoryName();
System.out.println("Processing package: " + pkgName);
Path packageDir = outputPath.resolve(pkgName);
Files.createDirectories(packageDir);
// Write preprocessed nodes
for (Map.Entry<String, Node> entry : pkg.getPreprocessedNodes().entrySet()) {
String fileName = entry.getKey() + ".blue";
Path filePath = packageDir.resolve(fileName);
String yaml = YAML_MAPPER.writeValueAsString(entry.getValue());
Files.writeString(filePath, yaml);
//System.out.println(" - Wrote: " + fileName);
}
// Write package.blue
Path packageBluePath = packageDir.resolve("package.blue");
String packageBlueYaml = YAML_MAPPER.writeValueAsString(pkg.getPackageContent());
Files.writeString(packageBluePath, packageBlueYaml);
//System.out.println(" - Wrote: package.blue");
// Extract and write package-specific blue-ids.yaml
Map<String, String> packageMappings = extractMappingsFromPackage(pkg);
if (!packageMappings.isEmpty()) {
Path packageBlueIdsPath = packageDir.resolve("blue-ids.yaml");
String blueIdsYaml = YAML_MAPPER.writeValueAsString(packageMappings);
Files.writeString(packageBlueIdsPath, blueIdsYaml);
//System.out.println(" - Wrote: blue-ids.yaml with " + packageMappings.size() + " mappings");
}
}
// Write root blue-ids.yaml with package blueIds
Map<String, String> packageBlueIds = new HashMap<>();
for (BluePackage pkg : packages) {
String packageBlueId = extractPackageBlueId(pkg);
if (packageBlueId != null) {
packageBlueIds.put(pkg.getDirectoryName(), packageBlueId);
}
}
Path rootBlueIdsPath = outputPath.resolve("blue-ids.yaml");
String rootBlueIdsYaml = YAML_MAPPER.writeValueAsString(packageBlueIds);
Files.writeString(rootBlueIdsPath, rootBlueIdsYaml);
System.out.println("Wrote root blue-ids.yaml with " + packageBlueIds.size() + " package IDs");
}
private static String extractPackageBlueId(BluePackage pkg) {
Node packageContent = pkg.getPackageContent();
return BlueIdCalculator.calculateBlueId(packageContent);
}
private static Map<String, String> extractMappingsFromPackage(BluePackage pkg) {
Node packageContent = pkg.getPackageContent();
if (packageContent != null && packageContent.getItems() != null && packageContent.getItems().size() > 1) {
Node mappingsContainer = packageContent.getItems().get(1);
if (mappingsContainer.getProperties() != null) {
Node mappingsNode = mappingsContainer.getProperties().get("mappings");
if (mappingsNode != null && mappingsNode.getProperties() != null) {
Map<String, String> mappings = new HashMap<>();
mappingsNode.getProperties().forEach((key, valueNode) -> {
mappings.put(key, valueNode.getValue().toString());
});
return mappings;
}
}
}
return new HashMap<>();
}
}