Allow Specifying Custom TypeScript Root
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit

This commit is contained in:
TheBrokenRail 2020-05-29 17:19:14 -04:00
parent 9f68375dc6
commit d8b0a1ddbe
3 changed files with 113 additions and 91 deletions

View File

@ -28,39 +28,17 @@ jni {
addPlatform("windows-x86", ".dll")
}
tasks.register<Exec>("eslint") {
tasks.register<TypeScriptPlugin.NPMTask>("eslint") {
group = "verification"
workingDir(typescript.root)
executable("npm")
args("run", "eslint")
dependsOn(tasks["npmInstall"])
taskName = "eslint"
}
tasks.register<Exec>("typedoc") {
tasks.register<TypeScriptPlugin.NPMTask>("typedoc") {
group = "documentation"
val typedocOut = File(typescript.root, "build/typedoc")
inputs.dir(typescript.root)
outputs.dir(typedocOut)
workingDir(typescript.root)
executable("npm")
args("run", "typedoc")
doFirst {
project.delete {
delete(typedocOut)
}
}
dependsOn(tasks["npmInstall"])
taskName = "typedoc"
outputDir = "build/typedoc"
}
tasks.named<Copy>("processResources") {

View File

@ -1,8 +1,7 @@
import org.gradle.api.DefaultTask
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.*
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.*
import java.io.File
@ -10,113 +9,158 @@ 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")
var root: File = File(rootDir, "src/main/ts")
}
fun getDeclarationDir(): File {
return File(root, "build/dts")
open class NPMTask : DefaultTask() {
@get:Input
lateinit var taskName: String
@get:Input
lateinit var outputDir: String
@TaskAction
fun exec() {
project.delete {
delete(getOutput())
}
project.exec {
workingDir(getInput())
executable("npm")
args("run", taskName)
}
}
fun getDependenciesDir(): File {
return File(root, "build/dependencies")
@InputDirectory
fun getInput(): Callable<File> {
return Callable {
project.the<TypeScriptConfig>().root
}
}
@OutputDirectory
fun getOutput(): Callable<File>? {
return if (this::outputDir.isInitialized) {
Callable {
if (File(outputDir).isAbsolute) {
File(outputDir)
} else {
File(project.the<TypeScriptConfig>().root, outputDir)
}
}
} else {
null
}
}
}
open class NPMInstallTask : DefaultTask() {
@TaskAction
fun exec() {
project.delete {
delete(getOutput())
}
project.exec {
workingDir(Callable {
project.the<TypeScriptConfig>().root
})
executable("npm")
args("install")
}
}
@InputFile
fun getInput(): Callable<File> {
return Callable {
File(project.the<TypeScriptConfig>().root, "package.json")
}
}
@OutputDirectory
fun getOutput(): Callable<File> {
return Callable {
File(project.the<TypeScriptConfig>().root, "node_modules")
}
}
}
override fun apply(project: Project): Unit = project.run {
val extension = project.extensions.create<TypeScriptConfig>("typescript", projectDir)
configurations {
create("typescript") {
val typescript = configurations.create("typescript") {
isTransitive = false
}
val npmInstallTask = tasks.register<NPMInstallTask>("npmInstall") {
group = "typescript"
}
tasks.register<Sync>("extractTypescriptDependencies") {
val extractTypescriptDependenciesTask = tasks.register<Sync>("extractTypescriptDependencies") {
group = "typescript"
dependsOn(configurations["typescript"])
dependsOn(typescript)
from(Callable {
configurations["typescript"].map {
typescript.map {
zipTree(it).matching {
exclude("META-INF", "META-INF/**")
}
}
})
into(extension.getDependenciesDir())
into(File(extension.root, "build/dependencies/api"))
}
tasks.register<Exec>("npmInstall") {
project.afterEvaluate {
tasks.forEach {
if (it is NPMTask) {
it.dependsOn(npmInstallTask)
it.dependsOn(extractTypescriptDependenciesTask)
}
}
}
val typescriptTask = tasks.register<NPMTask>("typescript") {
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"))
}
taskName = "build"
outputDir = "build/ts"
}
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") {
val apiJarTask = tasks.register<Jar>("apiJar") {
group = "typescript"
into("/types") {
from(File(extension.root, "types"))
from(Callable {
File(extension.root, "types")
})
}
into("/src") {
from(extension.getDeclarationDir())
from(Callable {
File(extension.root, "build/dts")
})
}
archiveClassifier.set("api")
dependsOn(tasks["typescript"])
dependsOn(typescriptTask)
}
artifacts {
add("archives", tasks["apiJar"])
add("archives", apiJarTask)
}
tasks.named<Copy>("processResources") {
dependsOn(tasks["typescript"])
dependsOn(typescriptTask)
from(Callable {
File(extension.root, "build/ts")
})
from(extension.getOutDir()) {
into("scriptcraft")
}
}
}
}

View File

@ -10,12 +10,12 @@ This is an example of a Minecraft mod made in TypeScript using ScriptCraft.
- NPM dependencies are not bundled
- API JARs are not bundled
## ```typescript``` Gradle Configuration
The ```typescript``` gradle configuration will extract the specified API JAR into ```src/main/ts/build/dependencies```.
## API JARs
An API JAR can be built with ```./gradlew apiJar```.
### ```typescript``` Gradle Configuration
The ```typescript``` gradle configuration will extract the specified API JAR into ```src/main/ts/build/dependencies```. It will not be bundled with the mod.
### File Structure
```
- API JAR Root