Skip to content

Commit 1348b4b

Browse files
authored
Add task preprocessVersion (#26)
- Add preprocessVersion - Add disableAnsi - Fix errors - Improved messages
2 parents fc95e53 + bae93f6 commit 1348b4b

File tree

13 files changed

+236
-38
lines changed

13 files changed

+236
-38
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v4
14-
- name: Set up JDK 22 for x64
14+
- name: Set up JDK 23 for x64
1515
uses: actions/setup-java@v4
1616
with:
17-
java-version: '22'
17+
java-version: '23'
1818
distribution: 'temurin'
1919
architecture: x64
2020
- name: Setup Gradle
21-
uses: gradle/actions/setup-gradle@v3
21+
uses: gradle/actions/setup-gradle@v4
2222
with:
23-
gradle-version: 8.10.2
23+
gradle-version: 8.12.1
2424
- name: Build
2525
run: gradle build
2626
- name: Upload Test Report

Test/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ discombobulator {
99
]
1010

1111
ignoredFileFormats = ["*.png"]
12+
13+
disableAnsi = false
1214
}

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ plugins {
44
id 'maven-publish'
55
}
66

7-
// Change java compatibility level to 22
8-
sourceCompatibility = targetCompatibility = 22
7+
// Change java compatibility level to 23
8+
sourceCompatibility = targetCompatibility = 23
99

1010
// Name, version and group for the project
1111
version = "1.3-SNAPSHOT"

src/main/java/com/minecrafttas/discombobulator/Discombobulator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.minecrafttas.discombobulator.tasks.TaskCollectBuilds;
3333
import com.minecrafttas.discombobulator.tasks.TaskPreprocessBase;
3434
import com.minecrafttas.discombobulator.tasks.TaskPreprocessVersion;
35+
import com.minecrafttas.discombobulator.tasks.TaskPreprocessVersionError;
3536
import com.minecrafttas.discombobulator.tasks.TaskPreprocessWatch;
3637
import com.minecrafttas.discombobulator.utils.Colors;
3738
import com.minecrafttas.discombobulator.utils.PathLock;
@@ -47,6 +48,8 @@ public class Discombobulator implements Plugin<Project> {
4748

4849
public static PreprocessingConfiguration config;
4950

51+
public static boolean DISABLE_ANSI = false;
52+
5053
public static FilePreprocessor fileProcessor;
5154

5255
public static PathLock pathLock;
@@ -79,16 +82,22 @@ public void apply(Project project) {
7982
List<Task> compileTasks = new ArrayList<>();
8083
for (Project subProject : project.getSubprojects()) {
8184
compileTasks.add(subProject.getTasksByName("remapJar", false).iterator().next());
82-
85+
// Register preprocessVersion task in subProjects
8386
TaskPreprocessVersion versionTask = subProject.getTasks().register("preprocessVersion", TaskPreprocessVersion.class).get();
8487
versionTask.setGroup("discombobulator");
8588
versionTask.setDescription("Preprocesses this version back to the base folder and to versions other than this one");
8689
}
8790
collectBuilds.updateCompileTasks(compileTasks);
8891

92+
// Register preprocessVersion task in root
93+
TaskPreprocessVersionError versionTaskRoot = project.getTasks().register("preprocessVersion", TaskPreprocessVersionError.class).get();
94+
versionTaskRoot.setGroup("discombobulator");
95+
versionTaskRoot.setDescription("Do not use this task! Use it in subprojects!");
96+
8997
project.afterEvaluate(_project -> {
9098
boolean inverted = config.getInverted().getOrElse(false);
9199
PORT_LOCK = config.getPort().getOrElse(8762);
100+
DISABLE_ANSI = config.getDisableAnsi().getOrElse(false);
92101

93102
Map<String, Path> versionPairs = null;
94103
Path projectDir = _project.getProjectDir().toPath();

src/main/java/com/minecrafttas/discombobulator/extensions/PreprocessingConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public abstract class PreprocessingConfiguration {
4848
*/
4949
public abstract Property<Boolean> getInverted();
5050

51+
/**
52+
* If true, disables ANSI colors
53+
* @return
54+
*/
55+
public abstract Property<Boolean> getDisableAnsi();
56+
5157
/**
5258
* The port for the port lock
5359
* @return The port number

src/main/java/com/minecrafttas/discombobulator/processor/FilePreprocessor.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import org.apache.commons.io.filefilter.WildcardFileFilter;
1919

2020
import com.minecrafttas.discombobulator.Discombobulator;
21+
import com.minecrafttas.discombobulator.tasks.TaskPreprocessWatch.CurrentFilePreprocessAction;
2122
import com.minecrafttas.discombobulator.utils.BetterFileWalker;
2223
import com.minecrafttas.discombobulator.utils.LineFeedHelper;
2324
import com.minecrafttas.discombobulator.utils.SafeFileOperations;
24-
import com.minecrafttas.discombobulator.utils.Triple;
2525

2626
public 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
}
Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,96 @@
11
package com.minecrafttas.discombobulator.tasks;
22

3+
import java.nio.charset.MalformedInputException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.util.Map;
7+
import java.util.Map.Entry;
8+
9+
import org.apache.commons.io.FilenameUtils;
310
import org.gradle.api.DefaultTask;
411
import org.gradle.api.tasks.TaskAction;
512

13+
import com.minecrafttas.discombobulator.Discombobulator;
14+
import com.minecrafttas.discombobulator.tasks.TaskPreprocessWatch.CurrentFilePreprocessAction;
15+
import com.minecrafttas.discombobulator.utils.BetterFileWalker;
16+
import com.minecrafttas.discombobulator.utils.LineFeedHelper;
17+
import com.minecrafttas.discombobulator.utils.Pair;
18+
import com.minecrafttas.discombobulator.utils.SocketLock;
19+
20+
/**
21+
* Task for preprocessing one version into all other versions including the base source
22+
*
23+
* @author Scribble
24+
*/
625
public class TaskPreprocessVersion extends DefaultTask {
726

827
@TaskAction
9-
public void preprocessVersion() {
28+
public void preprocessVersion() throws Exception {
29+
System.out.println(Discombobulator.getSplash());
30+
31+
// Lock port
32+
SocketLock lock = new SocketLock(Discombobulator.PORT_LOCK);
33+
lock.tryLock();
34+
35+
// Prepare list of physical version folders
36+
Path baseProjectDir = this.getProject().getParent().getProjectDir().toPath();
37+
Path baseSourceDir = baseProjectDir.resolve("src");
38+
39+
Map<String, Path> versionsConfig;
40+
try {
41+
versionsConfig = Discombobulator.getVersionPairs(baseProjectDir);
42+
} catch (Exception e) {
43+
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
44+
Discombobulator.printError(e.getMessage());
45+
} else {
46+
e.printStackTrace();
47+
}
48+
return;
49+
}
50+
51+
Path versionProjectDir = this.getProject().getProjectDir().toPath();
52+
Pair<String, Path> masterVersion = null;
53+
// Find current version in version config
54+
55+
for (Entry<String, Path> entry : versionsConfig.entrySet()) {
56+
if (entry.getValue().equals(versionProjectDir)) {
57+
masterVersion = Pair.of(entry.getKey(), entry.getValue());
58+
}
59+
}
60+
61+
if (masterVersion == null) {
62+
throw new Exception("Version could not be found in build.gradle");
63+
}
64+
65+
LineFeedHelper.printMessage();
66+
67+
System.out.println(String.format("Preprocessing version %s...", masterVersion.left()));
68+
69+
Path versionSourceDir = versionProjectDir.resolve("src");
70+
if (!Files.exists(versionSourceDir))
71+
throw new RuntimeException("Base source folder not found");
72+
73+
BetterFileWalker.walk(versionSourceDir, path -> {
74+
Path inFile = versionSourceDir.resolve(path);
75+
String extension = FilenameUtils.getExtension(path.getFileName().toString());
76+
77+
try {
78+
// Preprocess version dir
79+
CurrentFilePreprocessAction action = Discombobulator.fileProcessor.preprocessVersions(inFile, versionsConfig, extension, versionSourceDir, true);
80+
TaskPreprocessWatch.runFileAction(action);
81+
82+
// Preprocess in base dir
83+
Path outFile = baseSourceDir.resolve(path);
84+
Discombobulator.fileProcessor.preprocessFile(inFile, outFile, null, extension);
85+
} catch (MalformedInputException e) {
86+
Discombobulator.printError(String.format("Can't process file, probably not a text file...\n Maybe add ignoredFileFormats = [\"*.%s\"] to the build.gradle?", extension), path.getFileName().toString());
87+
e.printStackTrace();
88+
return;
89+
} catch (Exception e) {
90+
Discombobulator.printError(e.getMessage(), path.getFileName().toString());
91+
e.printStackTrace();
92+
return;
93+
}
94+
});
1095
}
1196
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.minecrafttas.discombobulator.tasks;
2+
3+
import org.gradle.api.DefaultTask;
4+
import org.gradle.api.tasks.TaskAction;
5+
6+
/**
7+
* <p>Task class for stopping a task.
8+
* <p>{@link TaskPreprocessVersion} is intended for <strong>subprojects only!</strong><br>
9+
* And since gradle is such a cool program, it automatically creates a task in the root project as well,<br>
10+
* that you can not disable apparently.
11+
* <p>But you can register separate tasks to the root project, that will execute before everything else, so to stop it,<br>
12+
* we just need to throw an exception and it will stop all following tasks. Thanks Gradle!
13+
*/
14+
public class TaskPreprocessVersionError extends DefaultTask {
15+
16+
@TaskAction
17+
public void preprocessVersion() throws Exception {
18+
throw new Exception("\n\n!!!!!!!!!! Do not use this task on the root project !!!!!!!!!!\n\n"
19+
+ "This task is intended for subprojects only,\n"
20+
+ "so this exception will stop it from running on all subprojects.\n"
21+
+ "This was the only way I could find to disable the task for the root project, thanks Gradle!\n");
22+
}
23+
}

0 commit comments

Comments
 (0)