Skip to content

Commit 321ab7e

Browse files
authored
Merge pull request #622 from dogboy21/cctest-backport
Backported changes from the CC: Tweaked Gametest framework present in 1.20.x
2 parents 4b49523 + 6227fd6 commit 321ab7e

File tree

26 files changed

+1293
-182
lines changed

26 files changed

+1293
-182
lines changed

build.gradle

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cc.tweaked.gradle.ClientJavaExec
12
import net.darkhax.curseforgegradle.TaskPublishCurseForge
23

34
import java.text.SimpleDateFormat
@@ -8,7 +9,7 @@ plugins {
89
id 'org.jetbrains.changelog' version '1.2.1'
910
id "com.modrinth.minotaur" version "2.+"
1011
id "org.jetbrains.kotlin.jvm" version "${kotlin_version}"
11-
id 'net.minecraftforge.gradle' version '[6.0.18,6.2)'
12+
id 'net.minecraftforge.gradle'
1213
id 'org.parchmentmc.librarian.forgegradle' version '1.+'
1314
id 'org.spongepowered.mixin' version '0.7.+'
1415
id "com.github.breadmoirai.github-release" version "2.5.2"
@@ -148,7 +149,7 @@ minecraft {
148149
}
149150
}
150151

151-
gameTestClient {
152+
testClient {
152153
workingDirectory project.file('test-files/client')
153154
parent runs.client
154155

@@ -664,3 +665,9 @@ publishing {
664665
}
665666
}
666667
}
668+
669+
tasks.register('runGameTestClient', ClientJavaExec, { task ->
670+
description "Runs client-side gametests with no mods"
671+
setRunConfig(minecraft.runs["testClient"])
672+
tags("client")
673+
})

buildSrc/build.gradle.kts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
`java-gradle-plugin`
3+
`kotlin-dsl`
4+
kotlin("jvm") version "1.9.23"
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
10+
maven("https://maven.minecraftforge.net") {
11+
name = "Forge"
12+
content {
13+
includeGroup("net.minecraftforge")
14+
includeGroup("net.minecraftforge.gradle")
15+
}
16+
}
17+
}
18+
19+
dependencies {
20+
implementation("net.minecraftforge.gradle:ForgeGradle:[6.0.18,6.2)")
21+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package cc.tweaked.gradle
6+
7+
import org.gradle.api.artifacts.dsl.DependencyHandler
8+
import org.gradle.api.file.FileSystemLocation
9+
import org.gradle.api.provider.Property
10+
import org.gradle.api.provider.Provider
11+
import org.gradle.api.tasks.JavaExec
12+
import org.gradle.process.BaseExecSpec
13+
import org.gradle.process.JavaExecSpec
14+
import org.gradle.process.ProcessForkOptions
15+
16+
/**
17+
* Add an annotation processor to all source sets.
18+
*/
19+
fun DependencyHandler.annotationProcessorEverywhere(dep: Any) {
20+
add("compileOnly", dep)
21+
add("annotationProcessor", dep)
22+
23+
add("clientCompileOnly", dep)
24+
add("clientAnnotationProcessor", dep)
25+
26+
add("testCompileOnly", dep)
27+
add("testAnnotationProcessor", dep)
28+
}
29+
30+
/**
31+
* A version of [JavaExecSpec.copyTo] which copies *all* properties.
32+
*/
33+
fun JavaExec.copyToFull(spec: JavaExec) {
34+
copyTo(spec)
35+
36+
// Additional Java options
37+
spec.jvmArgs = jvmArgs // Fabric overrides getJvmArgs so copyTo doesn't do the right thing.
38+
spec.args = args
39+
spec.argumentProviders.addAll(argumentProviders)
40+
spec.mainClass.set(mainClass)
41+
spec.classpath = classpath
42+
spec.javaLauncher.set(javaLauncher)
43+
if (executable != null) spec.setExecutable(executable!!)
44+
45+
// Additional ExecSpec options
46+
copyToExec(spec)
47+
}
48+
49+
/**
50+
* Copy additional [BaseExecSpec] options which aren't handled by [ProcessForkOptions.copyTo].
51+
*/
52+
fun BaseExecSpec.copyToExec(spec: BaseExecSpec) {
53+
spec.isIgnoreExitValue = isIgnoreExitValue
54+
if (standardInput != null) spec.standardInput = standardInput
55+
if (standardOutput != null) spec.standardOutput = standardOutput
56+
if (errorOutput != null) spec.errorOutput = errorOutput
57+
}
58+
59+
/**
60+
* An alternative to [Nothing] with a more descriptive name. Use to enforce calling a function with named arguments:
61+
*
62+
* ```kotlin
63+
* fun f(vararg unused: UseNamedArgs, arg1: Int, arg2: Int) {
64+
* // ...
65+
* }
66+
* ```
67+
*/
68+
class UseNamedArgs private constructor()
69+
70+
/**
71+
* An [AutoCloseable] implementation which can be used to combine other [AutoCloseable] instances.
72+
*
73+
* Values which implement [AutoCloseable] can be dynamically registered with [CloseScope.add]. When the scope is closed,
74+
* each value is closed in the opposite order.
75+
*
76+
* This is largely intended for cases where it's not appropriate to nest [AutoCloseable.use], for instance when nested
77+
* would be too deep.
78+
*/
79+
class CloseScope : AutoCloseable {
80+
private val toClose = ArrayDeque<AutoCloseable>()
81+
82+
/**
83+
* Add a value to be closed when this scope is closed.
84+
*/
85+
public fun add(value: AutoCloseable) {
86+
toClose.addLast(value)
87+
}
88+
89+
override fun close() {
90+
close(null)
91+
}
92+
93+
@PublishedApi
94+
internal fun close(baseException: Throwable?) {
95+
var exception = baseException
96+
97+
while (true) {
98+
var toClose = toClose.removeLastOrNull() ?: break
99+
try {
100+
toClose.close()
101+
} catch (e: Throwable) {
102+
if (exception == null) {
103+
exception = e
104+
} else {
105+
exception.addSuppressed(e)
106+
}
107+
}
108+
}
109+
110+
if (exception != null) throw exception
111+
}
112+
113+
inline fun <R> use(block: (CloseScope) -> R): R {
114+
var exception: Throwable? = null
115+
try {
116+
return block(this)
117+
} catch (e: Throwable) {
118+
exception = e
119+
throw e
120+
} finally {
121+
close(exception)
122+
}
123+
}
124+
}
125+
126+
/** Proxy method to avoid overload ambiguity. */
127+
fun <T> Property<T>.setProvider(provider: Provider<out T>) = set(provider)
128+
129+
/** Short-cut method to get the absolute path of a [FileSystemLocation] provider. */
130+
fun Provider<out FileSystemLocation>.getAbsolutePath(): String = get().asFile.absolutePath
131+
132+
/**
133+
* Get the version immediately after the provided version.
134+
*
135+
* For example, given "1.2.3", this will return "1.2.4".
136+
*/
137+
fun getNextVersion(version: String): String {
138+
// Split a version like x.y.z-SNAPSHOT into x.y.z and -SNAPSHOT
139+
val dashIndex = version.indexOf('-')
140+
val mainVersion = if (dashIndex < 0) version else version.substring(0, dashIndex)
141+
142+
// Find the last component in x.y.z and increment it.
143+
val lastIndex = mainVersion.lastIndexOf('.')
144+
if (lastIndex < 0) throw IllegalArgumentException("Cannot parse version format \"$version\"")
145+
val lastVersion = try {
146+
version.substring(lastIndex + 1).toInt()
147+
} catch (e: NumberFormatException) {
148+
throw IllegalArgumentException("Cannot parse version format \"$version\"", e)
149+
}
150+
151+
// Then append all components together.
152+
val out = StringBuilder()
153+
out.append(version, 0, lastIndex + 1)
154+
out.append(lastVersion + 1)
155+
if (dashIndex >= 0) out.append(version, dashIndex, version.length)
156+
return out.toString()
157+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package cc.tweaked.gradle
6+
7+
import net.minecraftforge.gradle.common.util.RunConfig
8+
import net.minecraftforge.gradle.common.util.runs.setRunConfigInternal
9+
import org.gradle.api.plugins.JavaPluginExtension
10+
import org.gradle.api.tasks.JavaExec
11+
import org.gradle.jvm.toolchain.JavaToolchainService
12+
import java.nio.file.Files
13+
14+
/**
15+
* Set [JavaExec] task to run a given [RunConfig].
16+
*/
17+
fun JavaExec.setRunConfig(config: RunConfig) {
18+
dependsOn("prepareRuns")
19+
setRunConfigInternal(project, this, config)
20+
doFirst("Create working directory") { Files.createDirectories(workingDir.toPath()) }
21+
22+
javaLauncher.set(
23+
project.extensions.getByType(JavaToolchainService::class.java)
24+
.launcherFor(project.extensions.getByType(JavaPluginExtension::class.java).toolchain),
25+
)
26+
}

0 commit comments

Comments
 (0)