1818import org .apache .commons .io .filefilter .WildcardFileFilter ;
1919
2020import com .minecrafttas .discombobulator .Discombobulator ;
21+ import com .minecrafttas .discombobulator .tasks .TaskPreprocessWatch .CurrentFilePreprocessAction ;
2122import com .minecrafttas .discombobulator .utils .BetterFileWalker ;
2223import com .minecrafttas .discombobulator .utils .LineFeedHelper ;
2324import com .minecrafttas .discombobulator .utils .SafeFileOperations ;
24- import com .minecrafttas .discombobulator .utils .Triple ;
2525
2626public class FilePreprocessor {
2727
@@ -33,6 +33,15 @@ public FilePreprocessor(LinePreprocessor processor, WildcardFileFilter fileFilte
3333 this .fileFilter = fileFilter ;
3434 }
3535
36+ /**
37+ * Preprocesses a single file into the target version
38+ *
39+ * @param inFile The path to the file that will be preprocessed
40+ * @param outFile The path to the file where the preprocessed file will be stored stored
41+ * @param version The target version to preprocess to. null if the target is the base source
42+ * @param extension The file extension (e.g. ".java") of the file
43+ * @throws Exception If the preprocessing fails
44+ */
3645 public void preprocessFile (Path inFile , Path outFile , String version , String extension ) throws Exception {
3746
3847// System.out.println(inFile);
@@ -58,6 +67,13 @@ public void preprocessFile(Path inFile, Path outFile, String version, String ext
5867 return ;
5968 }
6069
70+ String targetVersion = version ;
71+ if (targetVersion == null ) {
72+ targetVersion = "Base" ;
73+ }
74+
75+ System .out .println (String .format ("into version %s%s%s" , CYAN , targetVersion , WHITE ));
76+
6177 if (fileFilter != null && fileFilter .accept (inFile .toFile ())) {
6278 System .out .println (String .format ("Ignoring %s%s%s" , YELLOW , inFile .getFileName ().toString (), WHITE ));
6379 Files .copy (inFile , outFile , StandardCopyOption .REPLACE_EXISTING );
@@ -69,20 +85,30 @@ public void preprocessFile(Path inFile, Path outFile, String version, String ext
6985 preprocessLines (linesToProcess , outFile , version , extension );
7086 }
7187
72- public Triple <List <String >, Path , Path > /*<- TODO Change to it's own class*/ preprocessVersions (Path inFile , Map <String , Path > versions , String extension , Path currentDir , boolean verbose ) throws Exception {
88+ /**
89+ * Preprocess a file into multiple target versions
90+ *
91+ * @param inFile The file to preprocess
92+ * @param versions The mapped versions to preprocess to.
93+ * @param extension The file extension (e.g. ".java") of the file
94+ * @param currentVersionDir The current version directory that where the file that is being worked on lies.<br>
95+ * Used for checking if you are trying to preprocess into the current directory, which can lead to issues otherwise.
96+ * @param verbose If more info should be printed to the console
97+ * @return The {@link CurrentFilePreprocessAction}
98+ * @throws Exception If the preprocessing fails
99+ */
100+ public CurrentFilePreprocessAction preprocessVersions (Path inFile , Map <String , Path > versions , String extension , Path currentVersionDir , boolean verbose ) throws Exception {
73101
74- Path relativeInFile = currentDir .relativize (inFile );
102+ Path relativeInFile = currentVersionDir .relativize (inFile );
75103 System .out .println (String .format ("Preprocessing %s%s%s%s%s" , relativeInFile .getParent (), File .separator , PURPLE , relativeInFile .getFileName ().toString (), WHITE ));
76104
77105 boolean ignored = fileFilter != null && fileFilter .accept (inFile .toFile ());
78106
79107 List <String > linesToProcess = null ;
80108 if (!ignored )
81109 linesToProcess = Files .readAllLines (inFile );
82- else
83- System .out .println (String .format ("Ignoring %s%s%s" , YELLOW , inFile .getFileName ().toString (), WHITE ));
84110
85- Triple < List < String >, Path , Path > out = null ;
111+ CurrentFilePreprocessAction out = null ;
86112
87113 // Iterate through all versions
88114 for (Entry <String , Path > versionPair : versions .entrySet ()) {
@@ -91,10 +117,15 @@ public void preprocessFile(Path inFile, Path outFile, String version, String ext
91117 Path targetSubSourceDir = targetProject .resolve ("src" );
92118 Path outFile = targetSubSourceDir .resolve (relativeInFile );
93119
120+ // Stop certain file types to be preprocessed (e.g. ".png")
94121 if (ignored ) {
122+ if (targetSubSourceDir .equals (currentVersionDir )) {
123+ continue ;
124+ }
95125 if (verbose ) {
96126 System .out .println (String .format ("into version %s%s%s" , CYAN , versionName , WHITE ));
97127 }
128+ System .out .println (String .format ("Ignoring %s%s%s" , YELLOW , inFile .getFileName ().toString (), WHITE ));
98129 Files .copy (inFile , outFile , StandardCopyOption .REPLACE_EXISTING );
99130 continue ;
100131 }
@@ -103,8 +134,8 @@ public void preprocessFile(Path inFile, Path outFile, String version, String ext
103134 List <String > outLines = processor .preprocess (versionName , linesToProcess , extension );
104135
105136 // If the version equals the original version, then skip it
106- if (targetSubSourceDir .equals (currentDir )) {
107- out = Triple . of (outLines , inFile , outFile );
137+ if (targetSubSourceDir .equals (currentVersionDir )) {
138+ out = new CurrentFilePreprocessAction (outLines , inFile , outFile );
108139 continue ;
109140 }
110141
@@ -127,22 +158,32 @@ public void preprocessFile(Path inFile, Path outFile, String version, String ext
127158 */
128159 public List <String > preprocessLines (List <String > inLines , Path outFile , String version , String extension ) throws Exception {
129160 List <String > lines = processor .preprocess (version , inLines , extension );
161+ writeLines (lines , outFile );
162+ return lines ;
163+ }
130164
165+ ///
166+ /// 1. Locks the file to stop the filewatcher from detecting it
167+ /// 2. Creates any missing directories
168+ /// 3. Writes the lines with the line feed specified by the `line.seperator` system property
169+ ///
170+ /// @param inLines
171+ /// @param outFile
172+ /// @throws Exception
173+ ///
174+ private void writeLines (List <String > inLines , Path outFile ) throws Exception {
131175 // Lock the file
132176 Discombobulator .pathLock .scheduleAndLock (outFile );
133-
134177 // Write file and update last modified date
135178 Files .createDirectories (outFile .getParent ());
136179
137180 StringBuilder stringBuilder = new StringBuilder ();
138181 String linefeed = LineFeedHelper .newLine ();
139- for (String line : lines ) {
182+ for (String line : inLines ) {
140183 stringBuilder .append (line );
141184 stringBuilder .append (linefeed );
142185 }
143186 Files .write (outFile , stringBuilder .toString ().getBytes ());
144-
145- return lines ;
146187 }
147188
148189 /**
@@ -183,4 +224,5 @@ public LinePreprocessor getLineProcessor() {
183224 public WildcardFileFilter getFileFilter () {
184225 return fileFilter ;
185226 }
227+
186228}
0 commit comments