Skip to content

Commit bb11d46

Browse files
committed
Update tasks to support gradle config cache
1 parent 3ed8e99 commit bb11d46

File tree

5 files changed

+97
-56
lines changed

5 files changed

+97
-56
lines changed

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

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.nio.file.Files;
44
import java.nio.file.Path;
55
import java.util.ArrayList;
6+
import java.util.HashMap;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
@@ -33,21 +34,50 @@
3334
*/
3435
public class Discombobulator implements Plugin<Project> {
3536

36-
/**
37-
* Which port to lock
38-
*/
37+
///
38+
/// Which port to lock
39+
///
3940
public static int PORT_LOCK = 8762;
4041

42+
///
43+
/// The config set in the build.gradle file of the discombobulator
44+
///
4145
public static PreprocessingConfiguration config;
4246

47+
///
48+
/// If true: Disables color output in the console
49+
///
4350
public static boolean DISABLE_ANSI = false;
4451

52+
///
53+
/// Whether to use Windows or Unix style line feeds
54+
///
4555
public static String DEFAULT_LINE_FEED = System.lineSeparator();
4656

57+
///
58+
/// The base dir of the project
59+
///
60+
public static Path BASE_PROJECT_DIR;
61+
62+
///
63+
/// The base build directory, usually {@link #BASE_PROJECT_DIR}/build
64+
///
65+
public static Path BUILD_DIR;
66+
67+
///
68+
/// The {@link FilePreprocessor}
69+
///
4770
public static FilePreprocessor fileProcessor;
4871

72+
///
73+
/// The {@link PathLock}
74+
///
4975
public static PathLock pathLock;
5076

77+
///
78+
/// The version of Discombobulator,
79+
/// used for the splash in the console
80+
///
5181
private static String discoVersion;
5282

5383
/**
@@ -61,27 +91,42 @@ public void apply(Project project) {
6191
pathLock = new PathLock();
6292

6393
// Register tasks
94+
95+
/// preprocessBase ///
6496
TaskPreprocessBase baseTask = project.getTasks().register("preprocessBase", TaskPreprocessBase.class).get();
6597
baseTask.setGroup("discombobulator");
6698
baseTask.setDescription("Split base source into seperate version folders");
6799

100+
/// preprocessWatch ///
68101
TaskPreprocessWatch watchTask = project.getTasks().register("preprocessWatch", TaskPreprocessWatch.class).get();
69102
watchTask.setGroup("discombobulator");
70103
watchTask.setDescription("Starts a watch session. Preprocesses files into other versions on file change.");
104+
watchTask.setProperty("projectName", project.getName());
71105

106+
/// collectBuilds ///
72107
TaskCollectBuilds collectBuilds = project.getTasks().register("collectBuilds", TaskCollectBuilds.class).get();
73108
collectBuilds.setGroup("discombobulator");
74109
collectBuilds.setDescription("Builds, then collects all versions in root/build");
75110

76-
List<Task> compileTasks = new ArrayList<>();
111+
List<Task> compileTaskList = new ArrayList<>();
112+
Map<String, Path> buildDirs = new HashMap<>();
113+
for (Project subProject : project.getSubprojects()) {
114+
Task compileTask = subProject.getTasksByName("remapJar", false).iterator().next();
115+
compileTaskList.add(compileTask);
116+
117+
buildDirs.put(subProject.getName(), getBuildDir(subProject).resolve("libs"));
118+
}
119+
collectBuilds.setDependsOn(compileTaskList);
120+
collectBuilds.setProperty("buildDirectories", buildDirs);
121+
122+
/// preprocessVersion ///
77123
for (Project subProject : project.getSubprojects()) {
78-
compileTasks.add(subProject.getTasksByName("remapJar", false).iterator().next());
79124
// Register preprocessVersion task in subProjects
80125
TaskPreprocessVersion versionTask = subProject.getTasks().register("preprocessVersion", TaskPreprocessVersion.class).get();
126+
versionTask.setProperty("versionDirectory", subProject.getProjectDir());
81127
versionTask.setGroup("discombobulator");
82128
versionTask.setDescription("Preprocesses this version back to the base folder and to versions other than this one");
83129
}
84-
collectBuilds.updateCompileTasks(compileTasks);
85130

86131
// Register preprocessVersion task in root
87132
TaskPreprocessVersionError versionTaskRoot = project.getTasks().register("preprocessVersion", TaskPreprocessVersionError.class).get();
@@ -93,11 +138,12 @@ public void apply(Project project) {
93138
PORT_LOCK = config.getPort().getOrElse(8762);
94139
DISABLE_ANSI = config.getDisableAnsi().getOrElse(false);
95140
DEFAULT_LINE_FEED = config.getDefaultLineFeed().getOrElse(System.lineSeparator());
141+
BASE_PROJECT_DIR = _project.getProjectDir().toPath();
142+
BUILD_DIR = getBuildDir(_project);
96143

97144
Map<String, Path> versionPairs = null;
98-
Path projectDir = _project.getProjectDir().toPath();
99145
try {
100-
versionPairs = getVersionPairs(projectDir);
146+
versionPairs = getVersionPairs(BASE_PROJECT_DIR);
101147
} catch (Exception e) {
102148
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
103149
printError(e.getMessage());
@@ -126,8 +172,9 @@ public void apply(Project project) {
126172

127173
public static String getSplash() {
128174
return "\n" + (DISABLE_ANSI ? getColorLessSplash() : getColoredSplash()) + "\n\n"
129-
+ getCenterText(String.format("Now using %sGradle 9", Colors.PURPLE), 9) + "\n"
130-
+ " Created by Pancake and Scribble\n" + getCenterText(discoVersion) + "\n\n";
175+
+ getCenterText(String.format("Enable configuration cache today!")) + "\n"
176+
+ getCenterText("Created by Pancake and Scribble") + "\n"
177+
+ getCenterText(discoVersion) + "\n\n";
131178

132179
}
133180

@@ -162,6 +209,14 @@ private static String getCenterText(String text) {
162209
return getCenterText(text, length);
163210
}
164211

212+
/**
213+
* @param project The project to use
214+
* @return The build directory from the project
215+
*/
216+
public static Path getBuildDir(Project project) {
217+
return project.getLayout().getBuildDirectory().get().getAsFile().toPath();
218+
}
219+
165220
/**
166221
* Centers any given text
167222
*

src/main/java/com/minecrafttas/discombobulator/tasks/TaskCollectBuilds.java

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44
import java.nio.file.Files;
55
import java.nio.file.Path;
66
import java.nio.file.StandardCopyOption;
7-
import java.util.HashMap;
8-
import java.util.List;
9-
import java.util.Map;
107
import java.util.Map.Entry;
118
import java.util.stream.Stream;
129

1310
import org.gradle.api.DefaultTask;
14-
import org.gradle.api.Project;
15-
import org.gradle.api.Task;
11+
import org.gradle.api.provider.MapProperty;
12+
import org.gradle.api.tasks.Input;
1613
import org.gradle.api.tasks.TaskAction;
1714

15+
import com.minecrafttas.discombobulator.Discombobulator;
16+
1817
/**
1918
* Builds and moves all built versions into one directory for easier access
2019
*
2120
* @author Pancake
2221
*/
23-
public class TaskCollectBuilds extends DefaultTask {
22+
public abstract class TaskCollectBuilds extends DefaultTask {
2423

2524
/**
2625
* List of all build dirs
2726
*/
28-
private Map<String, Path> buildDirs = new HashMap<>();
27+
@Input
28+
abstract MapProperty<String, Path> getBuildDirectories();
2929

3030
/**
3131
* The main task component
3232
*/
3333
@TaskAction
3434
public void collectBuilds() {
35-
Path collectDir = getBuildDir(getProject());
35+
Path collectDir = Discombobulator.BUILD_DIR;
3636

3737
try {
3838
if (!Files.exists(collectDir))
@@ -42,7 +42,7 @@ public void collectBuilds() {
4242
return;
4343
}
4444

45-
for (Entry<String, Path> entry : buildDirs.entrySet()) {
45+
for (Entry<String, Path> entry : getBuildDirectories().get().entrySet()) {
4646
Path buildDir = entry.getValue();
4747
Stream<Path> stream;
4848

@@ -67,25 +67,4 @@ public void collectBuilds() {
6767
stream.close();
6868
}
6969
}
70-
71-
/**
72-
* Updates this task with all compile tasks
73-
*
74-
* @param compileTasks List of compile tasks
75-
*/
76-
public void updateCompileTasks(List<Task> compileTasks) {
77-
for (Task task : compileTasks) {
78-
Project project = task.getProject();
79-
this.buildDirs.put(project.getName(), getBuildDir(project).resolve("libs"));
80-
}
81-
this.setDependsOn(compileTasks);
82-
}
83-
84-
/**
85-
* @param project The project to use
86-
* @return The build directory from the project
87-
*/
88-
private Path getBuildDir(Project project) {
89-
return project.getLayout().getBuildDirectory().get().getAsFile().toPath();
90-
}
9170
}

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void preprocessBase() throws Exception {
4242
lock.tryLock();
4343

4444
// Prepare list of physical version folders
45-
Path baseProjectDir = this.getProject().getProjectDir().toPath();
45+
Path baseProjectDir = Discombobulator.BASE_PROJECT_DIR;
4646
Map<String, Path> versionsConfig;
4747
try {
4848
versionsConfig = Discombobulator.getVersionPairs(baseProjectDir);

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessVersion.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.apache.commons.io.FilenameUtils;
1313
import org.gradle.api.DefaultTask;
14+
import org.gradle.api.file.DirectoryProperty;
15+
import org.gradle.api.tasks.InputDirectory;
1416
import org.gradle.api.tasks.TaskAction;
1517

1618
import com.minecrafttas.discombobulator.Discombobulator;
@@ -33,7 +35,10 @@
3335
*
3436
* @author Scribble
3537
*/
36-
public class TaskPreprocessVersion extends DefaultTask {
38+
public abstract class TaskPreprocessVersion extends DefaultTask {
39+
40+
@InputDirectory
41+
abstract DirectoryProperty getVersionDirectory();
3742

3843
@TaskAction
3944
public void preprocessVersion() throws Exception {
@@ -44,7 +49,7 @@ public void preprocessVersion() throws Exception {
4449
lock.tryLock();
4550

4651
// Prepare list of physical version folders
47-
Path baseProjectDir = this.getProject().getParent().getProjectDir().toPath();
52+
Path baseProjectDir = Discombobulator.BASE_PROJECT_DIR;
4853
Path baseSourceDir = baseProjectDir.resolve("src");
4954

5055
Map<String, Path> versionsConfig;
@@ -59,7 +64,7 @@ public void preprocessVersion() throws Exception {
5964
return;
6065
}
6166

62-
Path versionProjectDir = this.getProject().getProjectDir().toPath();
67+
Path versionProjectDir = getVersionDirectory().get().getAsFile().toPath();
6368
Pair<String, Path> masterVersion = null;
6469
// Find current version in version config
6570

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessWatch.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import org.apache.commons.io.FilenameUtils;
2626
import org.gradle.api.DefaultTask;
27+
import org.gradle.api.provider.Property;
28+
import org.gradle.api.tasks.Input;
2729
import org.gradle.api.tasks.TaskAction;
2830

2931
import com.minecrafttas.discombobulator.Discombobulator;
@@ -40,14 +42,17 @@
4042
*
4143
* @author Pancake, Scribble
4244
*/
43-
public class TaskPreprocessWatch extends DefaultTask {
45+
public abstract class TaskPreprocessWatch extends DefaultTask {
4446

4547
private List<FileWatcherThread> threads = new ArrayList<>();
4648

4749
private CurrentFilePreprocessAction currentFileAction = null;
4850

4951
private boolean msgSeen = false;
5052

53+
@Input
54+
abstract Property<String> getProjectName();
55+
5156
@TaskAction
5257
public void preprocessWatch() throws Exception {
5358
System.out.println(Discombobulator.getSplash());
@@ -56,7 +61,7 @@ public void preprocessWatch() throws Exception {
5661
lock.tryLock();
5762

5863
// Prepare list of physical version folders
59-
Path baseProjectDir = this.getProject().getProjectDir().toPath();
64+
Path baseProjectDir = Discombobulator.BASE_PROJECT_DIR;
6065

6166
LineFeedHelper.printMessage();
6267

@@ -122,7 +127,7 @@ private void watchVersion(Path subSourceDir, Map<String, Path> targetSet) {
122127
e.printStackTrace();
123128
}
124129

125-
if (version.equals(this.getProject().getName())) {
130+
if (version.equals(getProjectName().get())) {
126131
version = "Base";
127132
}
128133

@@ -259,25 +264,22 @@ public void close() {
259264
}
260265
}
261266

262-
///
263267
/// Stores data used for preprocessing the file that was edited most recently.
264268
///
265-
/// This fixes an issue where the IDE will behave weirdly, when trying to preprocess and replace a file,
266-
/// that is currently being worked on. So when saving a file,
267-
/// The file watcher would also replace the file that you just saved, leading to discrepancies and annoyances.
269+
/// This fixes an issue where the IDE will behave weirdly, when trying to
270+
/// preprocess and replace a file, that is currently being worked on. So when
271+
/// saving a file, The file watcher would also replace the file that you just
272+
/// saved, leading to discrepancies and annoyances.
268273
///
269274
/// With this, you can execute the preprocessing at a later time.
270275
///
271-
/// @author Scribble
272-
///
276+
/// @author Scribble ///
273277
public static record CurrentFilePreprocessAction(List<String> outLines, Path inFile, Path outFile) {
274278
}
275279

276-
///
277280
/// Runs the {@link CurrentFilePreprocessAction}
278281
///
279-
/// @param currentFileAction The {@link CurrentFilePreprocessAction} to run
280-
///
282+
/// @param currentFileAction The {@link CurrentFilePreprocessAction} to run ///
281283
public static void runFileAction(CurrentFilePreprocessAction currentFileAction) throws IOException {
282284
Path outFile = currentFileAction.outFile();
283285
List<String> outLines = currentFileAction.outLines();

0 commit comments

Comments
 (0)