-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
78 lines (64 loc) · 2.52 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import org._10ne.gradle.rest.RestTask
import org.jetbrains.kotlin.gradle.dsl.KotlinJsDce
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import java.util.*
plugins {
id("kotlin2js") version "1.3.31"
id("kotlin-dce-js") version "1.3.31"
id("org.tenne.rest") version "0.4.2"
}
repositories {
jcenter()
}
dependencies {
implementation("ch.delconte.screeps-kotlin:screeps-kotlin-types:1.3.0")
implementation(kotlin("stdlib-js"))
testImplementation(kotlin("test-js"))
}
val screepsUser: String? by project
val screepsPassword: String? by project
val screepsToken: String? by project
val screepsHost: String? by project
val screepsBranch: String? by project
val branch = screepsBranch ?: "kotlin-start"
val host = screepsHost ?: "https://screeps.com"
fun String.encodeBase64() = Base64.getEncoder().encodeToString(this.toByteArray())
tasks {
"compileKotlin2Js"(Kotlin2JsCompile::class) {
kotlinOptions {
moduleKind = "commonjs"
outputFile = "${buildDir}/screeps/main.js"
sourceMap = true
metaInfo = true
}
}
"runDceKotlinJs"(KotlinJsDce::class) {
keep("main.loop")
dceOptions.devMode = false
}
register<RestTask>("deploy") {
group = "screeps"
dependsOn("build")
val modules = mutableMapOf<String, String>()
val minifiedCodeLocation = File("$buildDir/kotlin-js-min/main")
httpMethod = "post"
uri = "$host/api/user/code"
requestHeaders = if (screepsToken != null)
mapOf("X-Token" to screepsToken)
else
mapOf("Authorization" to "Basic " + "$screepsUser:$screepsPassword".encodeBase64())
contentType = groovyx.net.http.ContentType.JSON
requestBody = mapOf("branch" to branch, "modules" to modules)
doFirst {
if (screepsUser == null && screepsPassword == null && screepsToken == null) {
throw InvalidUserDataException("you need to supply either screepsUser and screepsPassword or screepsToken before you can upload code")
}
if (!minifiedCodeLocation.isDirectory) {
throw InvalidUserDataException("found no code to upload at ${minifiedCodeLocation.path}")
}
val jsFiles = minifiedCodeLocation.listFiles { _, name -> name.endsWith(".js") }
modules.putAll(jsFiles.associate { it.nameWithoutExtension to it.readText() })
println("uploading ${jsFiles.count()} files to branch $branch on server $host")
}
}
}