-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.gradle.kts
218 lines (181 loc) · 7.11 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.extensions.authentication
import com.github.kittinunf.fuel.core.extensions.jsonBody
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
// TODO: https://github.com/JetBrains/gradle-grammar-kit-plugin/issues/23
plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "1.5.31"
id("org.jetbrains.intellij") version "1.2.0"
}
repositories {
mavenCentral()
jcenter()
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.google.code.gson:gson:2.8.6")
classpath("com.github.kittinunf.fuel:fuel:2.2.3")
classpath("com.github.kittinunf.fuel:fuel-gson:2.2.3")
classpath("org.jsoup:jsoup:1.14.3")
classpath("net.coobird:thumbnailator:0.4.11")
}
}
val sourceCompatibility = JavaVersion.VERSION_11
val targetCompatibility = JavaVersion.VERSION_11
group = "com.github.syuchan1005"
val compileJava: JavaCompile by tasks
compileJava.apply {
sourceCompatibility = sourceCompatibility.toString()
targetCompatibility = targetCompatibility.toString()
options.apply {
compilerArgs = listOf("-Xlint:deprecation")
encoding = "UTF-8"
}
}
val compileKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = targetCompatibility.toString()
// val intellij: org.jetbrains.intellij.tasks.IntelliJInstrumentCodeTask by tasks
intellij {
pluginName.set("GitPrefix")
// https://www.jetbrains.com/intellij-repository/releases
type.set("IC")
plugins.set(listOf("git4idea"))
updateSinceUntilBuild.set(false)
}
tasks.register<UpdateEmojiTask>("updateEmoji") {
group = "gitprefix"
description = "Update EmojiUtil from submodule"
}
compileKotlin.dependsOn("updateEmoji")
open class UpdateEmojiTask : DefaultTask() {
@org.gradle.api.tasks.TaskAction
fun update() {
val config = Config.get(project)
if (!config.createClass && !config.convertIcon) {
return
}
// TODO: no output = no update = return logic
runCommand("git submodule update --recursive")
val emojiNames = File(config.srcEmojiFolderPath).listFiles()
?.map { it.name.replace(".png", "") } ?: return
if (config.convertIcon) {
File(config.distEmojiFolderPath).mkdirs()
emojiNames.forEach {
logger.info("Processing: ${it}.png")
net.coobird.thumbnailator.Thumbnails.of("${config.srcEmojiFolderPath}${it}.png")
.size(16, 16)
.toFile("${config.distEmojiFolderPath}${it}.png")
net.coobird.thumbnailator.Thumbnails.of("${config.srcEmojiFolderPath}${it}.png")
.size(32, 32)
.toFile("${config.distEmojiFolderPath}${it}@2x.png")
}
}
if (config.createClass) {
File("./src/main/java/" +
config.classPackageName.replace(".", "/") +
"/${config.className}.kt")
.writeText(generateClassContent(fetchGists(emojiNames)))
}
}
private fun runCommand(command: String) {
val process = Runtime.getRuntime().exec(command)
process.inputStream.copyTo(System.out)
process.errorStream.copyTo(System.out)
process.waitFor()
}
private fun fetchGists(emojiNames: List<String>): Map<String, String> {
val config = Config.get(project)
val gistToken = if (config.gistToken.isEmpty()) {
System.getenv("GITHUB_TOKEN")
} else {
config.gistToken
}
val (_, _, createData) = Fuel
.post("https://api.github.com/gists")
.jsonBody("{\"files\":{\"test.md\":{\"content\":\"${emojiNames.map { ":$it:" }
.joinToString(":", ":", ":")}\"}}}")
.authentication().bearer(gistToken)
.responseObject(gsonDeserializerOf(GistResponse.Create::class.java))
val (createResult, _) = createData
if (createResult == null) {
throw IllegalStateException("create failed.")
}
var readIndex = 0
val resultMap = org.jsoup.Jsoup.connect(createResult.html_url).get()
.select(".markdown-body.entry-content > p").first()
?.childNodes()
?.mapNotNull { node ->
when (node) {
is org.jsoup.nodes.TextNode -> {
if (node.text() != ":") {
readIndex++
}
}
is org.jsoup.nodes.Element -> {
val pair = emojiNames[readIndex] to node.text()
readIndex++
return@mapNotNull pair
}
}
return@mapNotNull null
}?.toMap().orEmpty()
val deleteRes = Fuel
.delete("https://api.github.com/gists/${createResult.id}")
.authentication().bearer(gistToken)
.response()
if (deleteRes.second.statusCode != 204) {
logger.error("Gist remove failed.\nURL: ${createResult.html_url}\n ID: ${createResult.id}")
logger.error(deleteRes.first.toString())
logger.error(deleteRes.second.toString())
}
return resultMap
}
private fun generateClassContent(emojiList: Map<String, String>): String {
val config = Config.get(project)
return """package ${config.classPackageName}
import com.intellij.openapi.util.IconLoader
import com.intellij.util.castSafelyTo
import javax.swing.Icon
class ${config.className} {
data class EmojiData(val code: String, val icon: Icon)
companion object {
@JvmStatic
val ${config.classMapName}: Map<String, EmojiData> = mapOf(
${emojiList.map { (k, v) -> " \"$k\" to EmojiData(\"$v\", IconLoader.getIcon(\"/emojis/$k.png\", ${config.className}::class.java))" }
.joinToString(",\n")}
)
}
}
"""
}
sealed class GistResponse {
data class Create(val id: String, val html_url: String) : GistResponse()
}
data class Config(
val createClass: Boolean,
val convertIcon: Boolean,
val srcEmojiFolderPath: String,
val distEmojiFolderPath: String,
val classPackageName: String,
val className: String,
val classMapName: String,
val gistToken: String
) {
companion object {
private val gson = com.google.gson.Gson()
private var config: Config? = null
fun get(p: Project): Config {
if (config == null) {
var jsonFile = p.file("EmojiUpdate.json")
if (!jsonFile.exists()) jsonFile = p.file("./EmojiUpdate.template.json")
config = gson.fromJson(jsonFile.reader(), Config::class.java)
}
return config!!
}
}
}
}