Improve Gradle Plugins
All checks were successful
ScriptCraft/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-05-26 15:48:44 -04:00
parent 4a46eec067
commit 9f68375dc6
6 changed files with 225 additions and 188 deletions

View File

@ -19,10 +19,19 @@ dependencies {
modImplementation("net.fabricmc.fabric-api:fabric-api:${project.property("fabric_api_version")}")
}
jni {
addPlatform("linux-x86_64", ".so")
addPlatform("linux-x86", ".so")
addPlatform("linux-armhf", ".so")
addPlatform("linux-arm64", ".so")
addPlatform("windows-x86_64", ".dll")
addPlatform("windows-x86", ".dll")
}
tasks.register<Exec>("eslint") {
group = "verification"
workingDir(project.extra.get("typescriptRoot") as File)
workingDir(typescript.root)
executable("npm")
@ -34,12 +43,12 @@ tasks.register<Exec>("eslint") {
tasks.register<Exec>("typedoc") {
group = "documentation"
val typedocOut = File(project.extra.get("typescriptRoot") as File, "build/typedoc")
val typedocOut = File(typescript.root, "build/typedoc")
inputs.dir(project.extra.get("typescriptRoot") as File)
inputs.dir(typescript.root)
outputs.dir(typedocOut)
workingDir(project.extra.get("typescriptRoot") as File)
workingDir(typescript.root)
executable("npm")

View File

@ -4,4 +4,17 @@ repositories {
plugins {
`kotlin-dsl`
}
gradlePlugin {
plugins {
register("jni-plugin") {
id = "jni"
implementationClass = "JNIPlugin"
}
register("typescript-plugin") {
id = "typescript"
implementationClass = "TypeScriptPlugin"
}
}
}

View File

@ -0,0 +1,77 @@
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.*
import java.io.File
class JNIPlugin : Plugin<Project> {
open class JNIConfig(private val project: Project) {
fun addPlatform(name: String, libExtension: String) = project.run {
val cDir = File(projectDir.absolutePath, "src/main/c")
val buildDir = File(cDir, "build-${name}")
tasks.register<Exec>("cmake-${name}") {
doFirst {
if (!buildDir.exists()) {
buildDir.mkdir()
}
}
group = "jni"
workingDir(buildDir)
executable("cmake")
args("-DCMAKE_TOOLCHAIN_FILE=${projectDir.absolutePath}/cmake/${name}-toolchain.cmake", "..")
}
tasks.register<Exec>("compileJNI-${name}") {
group = "jni"
workingDir(buildDir)
executable("make")
dependsOn(tasks["cmake-${name}"])
}
tasks.named("compileJNI") {
dependsOn(tasks["compileJNI-${name}"])
}
tasks.register<Delete>("cleanJNI-${name}") {
group = "jni"
delete(buildDir)
}
tasks.named("cleanJNI") {
dependsOn(tasks["cleanJNI-${name}"])
}
tasks.named<Copy>("processResources") {
from(buildDir) {
include("*${libExtension}")
into(File("natives", name).path)
}
}
}
}
override fun apply(project: Project): Unit = project.run {
tasks.register("cleanJNI") {
group = "jni"
}
tasks.named("clean") {
dependsOn(tasks["cleanJNI"])
}
tasks.register("compileJNI") {
group = "jni"
}
tasks.named<Copy>("processResources") {
dependsOn(tasks["compileJNI"])
}
project.extensions.create<JNIConfig>("jni", project)
}
}

View File

@ -0,0 +1,122 @@
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*
import java.io.File
import java.util.concurrent.Callable
class TypeScriptPlugin : Plugin<Project> {
open class TypeScriptConfig(rootDir: File) {
val root: File = File(rootDir, "src/main/ts")
fun getOutDir(): File {
return File(root, "build/ts")
}
fun getDeclarationDir(): File {
return File(root, "build/dts")
}
fun getDependenciesDir(): File {
return File(root, "build/dependencies")
}
}
override fun apply(project: Project): Unit = project.run {
val extension = project.extensions.create<TypeScriptConfig>("typescript", projectDir)
configurations {
create("typescript") {
isTransitive = false
}
}
tasks.register<Sync>("extractTypescriptDependencies") {
group = "typescript"
dependsOn(configurations["typescript"])
from(Callable {
configurations["typescript"].map {
zipTree(it).matching {
exclude("META-INF", "META-INF/**")
}
}
})
into(extension.getDependenciesDir())
}
tasks.register<Exec>("npmInstall") {
group = "typescript"
inputs.file(File(extension.root, "package.json"))
outputs.dir(File(extension.root, "node_modules"))
workingDir(extension.root)
executable("npm")
args("install")
doFirst {
project.delete {
delete(File(extension.root, "node_modules"))
}
}
dependsOn(tasks["extractTypescriptDependencies"])
}
tasks.register<Exec>("typescript") {
group = "typescript"
inputs.dir(extension.root)
outputs.dirs(extension.getOutDir())
workingDir(extension.root)
executable("npm")
args("run", "build")
doFirst {
project.delete {
delete(extension.getOutDir())
}
}
dependsOn(tasks["npmInstall"])
}
tasks.register<Jar>("apiJar") {
group = "typescript"
into("/types") {
from(File(extension.root, "types"))
}
into("/src") {
from(extension.getDeclarationDir())
}
archiveClassifier.set("api")
dependsOn(tasks["typescript"])
}
artifacts {
add("archives", tasks["apiJar"])
}
tasks.named<Copy>("processResources") {
dependsOn(tasks["typescript"])
from(extension.getOutDir()) {
into("scriptcraft")
}
}
}
}

View File

@ -1,83 +0,0 @@
val cDir = File(rootProject.projectDir.absolutePath, "src/main/c")
class JNIPlatform(val name: String, val libExtension: String, rootDir: File) {
val cmakeArgs: Array<String> = arrayOf("-DCMAKE_TOOLCHAIN_FILE=${rootDir.absolutePath}/cmake/${name}-toolchain.cmake")
}
val jniPlatforms = arrayOf(
JNIPlatform("linux-x86_64", ".so", rootDir),
JNIPlatform("linux-x86", ".so", rootDir),
JNIPlatform("linux-armhf", ".so", rootDir),
JNIPlatform("linux-arm64", ".so", rootDir),
JNIPlatform("windows-x86_64", ".dll", rootDir),
JNIPlatform("windows-x86", ".dll", rootDir)
)
tasks.register("cleanJNI") {
group = "jni"
}
tasks.register("compileJNI") {
group = "jni"
}
jniPlatforms.forEach {
val buildDir = File(cDir, "build-${it.name}")
tasks.register<Exec>("cmake-${it.name}") {
doFirst {
if (!buildDir.exists()) {
buildDir.mkdir()
}
}
group = "jni"
workingDir(buildDir)
executable("cmake")
args(it.cmakeArgs, "..")
}
tasks.register<Exec>("compileJNI-${it.name}") {
group = "jni"
workingDir(buildDir)
executable("make")
dependsOn(tasks["cmake-${it.name}"])
}
tasks.named("compileJNI") {
dependsOn(tasks["compileJNI-${it.name}"])
}
tasks.register<Delete>("cleanJNI-${it.name}") {
group = "jni"
delete(buildDir)
}
tasks.named("cleanJNI") {
dependsOn(tasks["cleanJNI-${it.name}"])
}
}
tasks.named("clean") {
dependsOn(tasks["cleanJNI"])
}
tasks.named<Copy>("processResources") {
dependsOn(tasks["compileJNI"])
jniPlatforms.forEach {
val buildDir = File(cDir, "build-${it.name}")
if (!buildDir.exists()) {
buildDir.mkdir()
}
val file = File(buildDir, "libscriptcraft${it.libExtension}")
inputs.files(file)
from(file.absolutePath) {
into(File("natives", it.name).path)
}
}
}

View File

@ -1,101 +0,0 @@
val typescriptRoot = File(rootDir.absolutePath, "src/main/ts")
extra.set("typescriptRoot", typescriptRoot)
val typescriptOut = File(typescriptRoot, "build/ts")
val nodeModules = File(typescriptRoot, "node_modules")
val typescriptDependencies = File(typescriptRoot, "build/dependencies")
configurations {
create("typescript") {
isTransitive = false
}
}
tasks.register<Sync>("extractTypescriptDependencies") {
group = "typescript"
dependsOn(configurations["typescript"])
from(Callable {
configurations["typescript"].map {
zipTree(it).matching {
exclude("META-INF", "META-INF/**")
}
}
})
into(typescriptDependencies)
}
tasks.register<Exec>("npmInstall") {
group = "typescript"
inputs.file(File(typescriptRoot, "package.json"))
outputs.dir(nodeModules)
workingDir(typescriptRoot)
executable("npm")
args("install")
doFirst {
project.delete {
delete(nodeModules)
}
}
dependsOn(tasks["extractTypescriptDependencies"])
}
tasks.register<Exec>("typescript") {
group = "typescript"
inputs.dir(typescriptRoot)
outputs.dirs(typescriptOut)
workingDir(typescriptRoot)
executable("npm")
args("run", "build")
doFirst {
project.delete {
delete(typescriptOut)
}
}
dependsOn(tasks["npmInstall"])
}
tasks.register<Jar>("apiJar") {
group = "typescript"
val dtsOut = File(typescriptRoot, "build/dts")
into("/types") {
from(File(typescriptRoot, "types"))
}
into("/src") {
from(dtsOut)
}
archiveClassifier.set("api")
dependsOn(tasks["typescript"])
}
artifacts {
add("archives", tasks["apiJar"])
}
tasks.named<Copy>("processResources") {
dependsOn(tasks["typescript"])
from(typescriptOut.absolutePath) {
into("scriptcraft")
}
}