This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/plugin/src/main/kotlin/ScriptCraftPlugin.kt

169 lines
4.2 KiB
Kotlin
Raw Permalink Normal View History

import org.gradle.api.DefaultTask
2020-05-26 19:48:44 +00:00
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.*
2020-05-26 19:48:44 +00:00
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*
import java.io.File
import java.util.concurrent.Callable
2020-05-31 17:25:16 +00:00
class ScriptCraftPlugin : Plugin<Project> {
2020-06-01 21:32:20 +00:00
open class Config(rootDir: File) {
var root: File = File(rootDir, "src/main/ts")
}
open class NPMTask : DefaultTask() {
@get:Input
lateinit var taskName: String
2020-05-31 02:15:53 +00:00
@get:Optional
@get:Input
2020-05-31 02:15:53 +00:00
var outputDir: String? = null
@TaskAction
fun exec() {
project.delete {
delete(getOutput())
}
project.exec {
workingDir(getInput())
executable("npm")
2020-05-26 19:48:44 +00:00
args("run", taskName)
}
2020-05-26 19:48:44 +00:00
}
@InputDirectory
fun getInput(): Callable<File> {
return Callable {
2020-06-01 21:32:20 +00:00
project.the<Config>().root
}
2020-05-26 19:48:44 +00:00
}
2020-05-31 02:15:53 +00:00
@Optional
@OutputDirectory
fun getOutput(): Callable<File>? {
2020-05-31 02:15:53 +00:00
val dir = outputDir
return if (dir != null) {
Callable {
2020-05-31 02:15:53 +00:00
if (File(dir).isAbsolute) {
File(dir)
} else {
2020-06-01 21:32:20 +00:00
File(project.the<Config>().root, dir)
}
}
} else {
null
}
}
}
open class NPMInstallTask : DefaultTask() {
@TaskAction
fun exec() {
project.delete {
delete(getOutput())
}
project.exec {
workingDir(Callable {
2020-06-01 21:32:20 +00:00
project.the<Config>().root
})
executable("npm")
args("install")
}
}
@InputFile
fun getInput(): Callable<File> {
return Callable {
2020-06-01 21:32:20 +00:00
File(project.the<Config>().root, "package.json")
}
}
@OutputDirectory
fun getOutput(): Callable<File> {
return Callable {
2020-06-01 21:32:20 +00:00
File(project.the<Config>().root, "node_modules")
}
2020-05-26 19:48:44 +00:00
}
}
override fun apply(project: Project): Unit = project.run {
2020-06-01 21:32:20 +00:00
val extension = project.extensions.create<Config>("scriptcraft", projectDir)
2020-05-26 19:48:44 +00:00
val typescript = configurations.create("typescript") {
isTransitive = false
}
val npmInstallTask = tasks.register<NPMInstallTask>("npmInstall") {
2020-06-07 21:58:49 +00:00
group = "scriptcraft"
2020-05-26 19:48:44 +00:00
}
val extractTypescriptDependenciesTask = tasks.register<Sync>("extractTypescriptDependencies") {
2020-06-07 21:58:49 +00:00
group = "scriptcraft"
2020-05-26 19:48:44 +00:00
dependsOn(typescript)
2020-05-26 19:48:44 +00:00
from(Callable {
typescript.map {
2020-05-26 19:48:44 +00:00
zipTree(it).matching {
exclude("META-INF", "META-INF/**")
}
}
})
2020-05-31 20:04:45 +00:00
into(File(extension.root, "build/dependencies"))
2020-05-26 19:48:44 +00:00
}
project.afterEvaluate {
tasks.forEach {
if (it is NPMTask) {
it.dependsOn(npmInstallTask)
it.dependsOn(extractTypescriptDependenciesTask)
2020-05-26 19:48:44 +00:00
}
}
}
val typescriptTask = tasks.register<NPMTask>("typescript") {
2020-06-07 21:58:49 +00:00
group = "scriptcraft"
2020-05-26 19:48:44 +00:00
taskName = "build"
outputDir = "build/ts"
2020-05-26 19:48:44 +00:00
}
val apiJarTask = tasks.register<Jar>("apiJar") {
2020-06-07 21:58:49 +00:00
group = "scriptcraft"
2020-05-26 19:48:44 +00:00
into("/types") {
from(Callable {
File(extension.root, "types")
})
2020-05-26 19:48:44 +00:00
}
into("/src") {
from(Callable {
File(extension.root, "build/dts")
})
2020-05-26 19:48:44 +00:00
}
archiveClassifier.set("api")
dependsOn(typescriptTask)
2020-05-26 19:48:44 +00:00
}
artifacts {
add("archives", apiJarTask)
2020-05-26 19:48:44 +00:00
}
tasks.named<Copy>("processResources") {
dependsOn(typescriptTask)
2020-05-26 19:48:44 +00:00
2020-06-01 23:07:01 +00:00
into("scriptcraft") {
from(Callable {
File(extension.root, "build/ts")
})
}
2020-05-26 19:48:44 +00:00
}
}
}