Skip to content

Commit 3040970

Browse files
committed
feat: blue-preprocessor
1 parent e1397e0 commit 3040970

4 files changed

Lines changed: 237 additions & 6 deletions

File tree

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ task exportBlueFiles {
7979
}
8080
}
8181

82+
jar {
83+
manifest {
84+
attributes(
85+
'Main-Class': 'blue.contract.packager.utils.BluePreprocessor'
86+
)
87+
}
88+
89+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
90+
91+
zip64 = true
92+
93+
from {
94+
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
95+
}
96+
97+
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
98+
}
99+
82100
test {
83101
useJUnitPlatform()
84102
reports {

src/main/java/blue/contract/packager/graphbuilder/FileSystemDependencyGraphBuilder.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
package blue.contract.packager.graphbuilder;
22

3+
import blue.contract.packager.BluePackageExporter;
34
import blue.contract.packager.model.DependencyGraph;
45
import blue.language.model.Node;
56

67
import java.io.IOException;
78
import java.nio.file.DirectoryStream;
89
import java.nio.file.Files;
910
import java.nio.file.Path;
11+
import java.util.HashSet;
12+
import java.util.Set;
1013

1114
import static blue.contract.packager.BluePackageExporter.EXTENDS_FILE_NAME;
1215
import static blue.language.utils.UncheckedObjectMapper.YAML_MAPPER;
1316

1417
public class FileSystemDependencyGraphBuilder implements DependencyGraphBuilder {
1518
private final Path rootPath;
1619

17-
public FileSystemDependencyGraphBuilder(Path rootPath) {
20+
private final Set<String> ignorePatterns;
21+
22+
public FileSystemDependencyGraphBuilder(Path rootPath, Set<String> ignorePatterns) {
1823
this.rootPath = rootPath;
24+
this.ignorePatterns = ignorePatterns;
25+
}
26+
27+
public FileSystemDependencyGraphBuilder(Path rootPath) {
28+
this(rootPath, new HashSet<>());
1929
}
2030

2131
@Override
@@ -29,18 +39,38 @@ public DependencyGraph buildDependencyGraph(String rootDir) throws IOException {
2939

3040
try (DirectoryStream<Path> stream = Files.newDirectoryStream(fullPath)) {
3141
for (Path path : stream) {
32-
if (Files.isDirectory(path)) {
42+
if (Files.isDirectory(path) && !shouldIgnore(path)) {
3343
String dirName = path.getFileName().toString();
34-
String dependency = readDependency(path.resolve(EXTENDS_FILE_NAME));
35-
graph.addDirectory(dirName, dependency);
36-
processFiles(path, dirName, graph);
44+
45+
// Check if this directory contains any .blue files
46+
if (containsBlueFiles(path)) {
47+
Path extendsFile = path.resolve(EXTENDS_FILE_NAME);
48+
String dependency = Files.exists(extendsFile) ?
49+
readDependency(extendsFile) :
50+
BluePackageExporter.ROOT_DEPENDENCY;
51+
52+
graph.addDirectory(dirName, dependency);
53+
processFiles(path, dirName, graph);
54+
}
3755
}
3856
}
3957
}
4058

4159
return graph;
4260
}
4361

62+
private boolean shouldIgnore(Path path) {
63+
String pathName = path.getFileName().toString();
64+
return ignorePatterns.stream().anyMatch(pattern ->
65+
pathName.matches(pattern.replace("*", ".*").replace("?", ".?")));
66+
}
67+
68+
private boolean containsBlueFiles(Path directory) throws IOException {
69+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, "*.blue")) {
70+
return stream.iterator().hasNext();
71+
}
72+
}
73+
4474
private String readDependency(Path path) throws IOException {
4575
if (!Files.exists(path)) {
4676
throw new IOException("Dependency file not found: " + path);

src/main/java/blue/contract/packager/model/BluePackage.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@ public Node getPackageContent() {
2828
}
2929

3030
public Map<String, String> getMappings() {
31-
return new HashMap<>(mappings);
31+
// Extract mappings from packageContent if they're not already in the mappings map
32+
if (mappings.isEmpty() && packageContent != null) {
33+
Node mappingsNode = packageContent.getItems()
34+
.get(1) // Second item contains mappings
35+
.getProperties()
36+
.get("mappings");
37+
38+
if (mappingsNode != null && mappingsNode.getProperties() != null) {
39+
mappingsNode.getProperties().forEach((key, valueNode) -> {
40+
mappings.put(key, valueNode.getValue().toString());
41+
});
42+
}
43+
}
44+
return mappings;
3245
}
3346

3447
public Map<String, Node> getPreprocessedNodes() {
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package blue.contract.packager.utils;
2+
3+
import blue.contract.packager.BluePackageExporter;
4+
import blue.contract.packager.graphbuilder.FileSystemDependencyGraphBuilder;
5+
import blue.contract.packager.model.BluePackage;
6+
import blue.language.model.Node;
7+
import blue.language.utils.BlueIdCalculator;
8+
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Map;
13+
import java.util.Set;
14+
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.List;
17+
import java.util.Arrays;
18+
19+
import static blue.language.utils.UncheckedObjectMapper.YAML_MAPPER;
20+
21+
public class BluePreprocessor {
22+
public static void main(String[] args) {
23+
if (args.length < 2 || args[0].equals("-h") || args[0].equals("--help")) {
24+
printUsage();
25+
System.exit(args.length < 2 ? 1 : 0);
26+
}
27+
28+
try {
29+
String inputDir = args[0];
30+
String outputDir = args[1];
31+
Set<String> ignorePatterns = new HashSet<>();
32+
boolean verbose = false;
33+
34+
// Parse additional arguments
35+
for (int i = 2; i < args.length; i++) {
36+
if (args[i].equals("--ignore") && i + 1 < args.length) {
37+
String[] patterns = args[i + 1].split(",");
38+
ignorePatterns.addAll(Arrays.asList(patterns));
39+
i++; // Skip the next argument as we've already processed it
40+
} else if (args[i].equals("--verbose")) {
41+
verbose = true;
42+
}
43+
}
44+
45+
// Add default ignore patterns
46+
ignorePatterns.addAll(Arrays.asList(
47+
".git",
48+
".github",
49+
"node_modules",
50+
"build",
51+
".gradle",
52+
"blue-preprocessed"
53+
));
54+
55+
if (verbose) {
56+
System.out.println("Input directory: " + inputDir);
57+
System.out.println("Output directory: " + outputDir);
58+
System.out.println("Ignored patterns: " + String.join(", ", ignorePatterns));
59+
}
60+
61+
Path inputPath = Paths.get(inputDir);
62+
Path outputPath = Paths.get(outputDir);
63+
64+
if (!Files.exists(inputPath)) {
65+
System.err.println("Input directory does not exist: " + inputPath);
66+
System.exit(1);
67+
}
68+
69+
// Create output directory if it doesn't exist
70+
Files.createDirectories(outputPath);
71+
72+
// Create builder with ignore patterns
73+
FileSystemDependencyGraphBuilder builder = new FileSystemDependencyGraphBuilder(inputPath, ignorePatterns);
74+
List<BluePackage> packages = BluePackageExporter.exportPackages("", builder);
75+
76+
writePreprocessedFiles(packages, outputPath);
77+
78+
System.out.println("Preprocessing completed successfully.");
79+
System.out.println("Preprocessed files written to: " + outputPath.toAbsolutePath());
80+
81+
} catch (Exception e) {
82+
System.err.println("Error during preprocessing: " + e.getMessage());
83+
e.printStackTrace();
84+
System.exit(1);
85+
}
86+
}
87+
88+
private static void printUsage() {
89+
System.out.println("Usage: blue-preprocessor <input-dir> <output-dir> [options]");
90+
System.out.println("Options:");
91+
System.out.println(" --ignore <patterns> Comma-separated list of directory patterns to ignore");
92+
System.out.println(" --verbose Print detailed processing information");
93+
System.out.println(" --help Show this help message");
94+
System.out.println("\nDefault ignored patterns: .git, .github, node_modules, build, .gradle");
95+
}
96+
97+
private static void writePreprocessedFiles(List<BluePackage> packages, Path outputPath) throws Exception {
98+
System.out.println("Writing preprocessed files...");
99+
100+
// Write package-specific files
101+
for (BluePackage pkg : packages) {
102+
String pkgName = pkg.getDirectoryName();
103+
System.out.println("Processing package: " + pkgName);
104+
105+
Path packageDir = outputPath.resolve(pkgName);
106+
Files.createDirectories(packageDir);
107+
108+
// Write preprocessed nodes
109+
for (Map.Entry<String, Node> entry : pkg.getPreprocessedNodes().entrySet()) {
110+
String fileName = entry.getKey() + ".blue";
111+
Path filePath = packageDir.resolve(fileName);
112+
String yaml = YAML_MAPPER.writeValueAsString(entry.getValue());
113+
Files.writeString(filePath, yaml);
114+
//System.out.println(" - Wrote: " + fileName);
115+
}
116+
117+
// Write package.blue
118+
Path packageBluePath = packageDir.resolve("package.blue");
119+
String packageBlueYaml = YAML_MAPPER.writeValueAsString(pkg.getPackageContent());
120+
Files.writeString(packageBluePath, packageBlueYaml);
121+
//System.out.println(" - Wrote: package.blue");
122+
123+
// Extract and write package-specific blue-ids.yaml
124+
Map<String, String> packageMappings = extractMappingsFromPackage(pkg);
125+
if (!packageMappings.isEmpty()) {
126+
Path packageBlueIdsPath = packageDir.resolve("blue-ids.yaml");
127+
String blueIdsYaml = YAML_MAPPER.writeValueAsString(packageMappings);
128+
Files.writeString(packageBlueIdsPath, blueIdsYaml);
129+
//System.out.println(" - Wrote: blue-ids.yaml with " + packageMappings.size() + " mappings");
130+
}
131+
}
132+
133+
// Write root blue-ids.yaml with package blueIds
134+
Map<String, String> packageBlueIds = new HashMap<>();
135+
for (BluePackage pkg : packages) {
136+
String packageBlueId = extractPackageBlueId(pkg);
137+
if (packageBlueId != null) {
138+
packageBlueIds.put(pkg.getDirectoryName(), packageBlueId);
139+
}
140+
}
141+
142+
Path rootBlueIdsPath = outputPath.resolve("blue-ids.yaml");
143+
String rootBlueIdsYaml = YAML_MAPPER.writeValueAsString(packageBlueIds);
144+
Files.writeString(rootBlueIdsPath, rootBlueIdsYaml);
145+
System.out.println("Wrote root blue-ids.yaml with " + packageBlueIds.size() + " package IDs");
146+
}
147+
148+
private static String extractPackageBlueId(BluePackage pkg) {
149+
Node packageContent = pkg.getPackageContent();
150+
return BlueIdCalculator.calculateBlueId(packageContent);
151+
}
152+
153+
private static Map<String, String> extractMappingsFromPackage(BluePackage pkg) {
154+
Node packageContent = pkg.getPackageContent();
155+
if (packageContent != null && packageContent.getItems() != null && packageContent.getItems().size() > 1) {
156+
Node mappingsContainer = packageContent.getItems().get(1);
157+
if (mappingsContainer.getProperties() != null) {
158+
Node mappingsNode = mappingsContainer.getProperties().get("mappings");
159+
if (mappingsNode != null && mappingsNode.getProperties() != null) {
160+
Map<String, String> mappings = new HashMap<>();
161+
mappingsNode.getProperties().forEach((key, valueNode) -> {
162+
mappings.put(key, valueNode.getValue().toString());
163+
});
164+
return mappings;
165+
}
166+
}
167+
}
168+
return new HashMap<>();
169+
}
170+
}

0 commit comments

Comments
 (0)