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 ("\n Default 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