Skip to content

Commit df1237e

Browse files
committed
update
1 parent 577f22b commit df1237e

File tree

5 files changed

+74
-57
lines changed

5 files changed

+74
-57
lines changed

src/main/kotlin/org/tabooproject/intellij/FunctionTemplate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ object FunctionTemplate {
1818
// 版本
1919
put("version", configProperty.version)
2020
// 模块列表
21-
put("modules", configProperty.modules.map { it.name })
21+
put("modules", configProperty.modules.map { it })
2222
// 从模块构建额外 imports
23-
put("extraPackages", configProperty.modules.map { "import io.izzel.taboolib.gradle.${it.name}" })
23+
put("extraPackages", configProperty.modules.map { "import io.izzel.taboolib.gradle.${it}" })
2424
val optionalProperty = OptionalPropertiesStep.property
2525
// 插件描述
2626
put("description", optionalProperty.description)

src/main/kotlin/org/tabooproject/intellij/Template.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import freemarker.cache.StringTemplateLoader
44
import freemarker.template.Configuration
55
import freemarker.template.Template
66
import okhttp3.OkHttpClient
7-
import okhttp3.Request
87
import org.tabooproject.intellij.step.ConfigurationPropertiesStep
9-
import java.io.*
8+
import org.tabooproject.intellij.step.TEMPLATE_DOWNLOAD_MIRROR
9+
import java.io.IOException
10+
import java.io.StringReader
11+
import java.io.StringWriter
1012
import java.nio.charset.StandardCharsets
1113
import java.nio.file.Files
1214
import java.util.zip.ZipInputStream
@@ -27,10 +29,11 @@ val TEMPLATE_FILES: Map<String, TemplateFile> = listOf(
2729

2830
object Template {
2931

30-
// GitHub 源
31-
private const val DOWNLOAD_URL = "https://github.com/TabooLib/taboolib-sdk/archive/refs/heads/idea-template.zip"
32+
private fun getTemplateDownloadUrl(): String {
33+
return TEMPLATE_DOWNLOAD_MIRROR.getValue(ConfigurationPropertiesStep.property.mirrorIndex)
34+
}
3235

33-
fun downloadAndUnzipFile(baseDir: String, url: String = DOWNLOAD_URL) {
36+
fun downloadAndUnzipFile(baseDir: String, url: String = getTemplateDownloadUrl()) {
3437
val response = OkHttpClient()
3538
.newCall(getRequest(url))
3639
.execute()
@@ -77,8 +80,4 @@ object Template {
7780
Files.write(file, finalContent.toByteArray(StandardCharsets.UTF_8))
7881
}
7982
}
80-
81-
private fun getRequest(url: String): Request {
82-
return Request.Builder().url(url).build()
83-
}
8483
}

src/main/kotlin/org/tabooproject/intellij/Utils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tabooproject.intellij
22

3+
import okhttp3.Request
34
import java.nio.file.Files
45
import java.nio.file.Path
56
import java.nio.file.Paths
@@ -16,4 +17,8 @@ fun createFileWithDirectories(baseDir: String, relativePath: String): Path? {
1617
return Files.createFile(fullPath)
1718
}
1819
return null
20+
}
21+
22+
fun getRequest(url: String): Request {
23+
return Request.Builder().url(url).build()
1924
}

src/main/kotlin/org/tabooproject/intellij/component/AddDeleteModuleListPanel.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package org.tabooproject.intellij.component
22

33
import com.intellij.ui.AddDeleteListPanel
44
import org.jetbrains.kotlin.utils.addToStdlib.ifFalse
5-
import org.tabooproject.intellij.step.Module
5+
import org.tabooproject.intellij.step.MODULES
66

77
class AddDeleteModuleListPanel(
88
title: String,
9-
initial: List<Module>,
10-
defaultHeight: Int = 175
11-
) : AddDeleteListPanel<Module>(title, initial) {
9+
initial: List<String>,
10+
defaultHeight: Int = 175,
11+
) : AddDeleteListPanel<String>(title, initial) {
1212

1313
init {
1414
preferredSize = preferredSize.apply {
@@ -17,15 +17,14 @@ class AddDeleteModuleListPanel(
1717
}
1818
}
1919

20-
override fun findItemToAdd(): Module? {
21-
val dialog = SelectBoxDialog("Add Module", "Choose a module:", Module.values()
20+
override fun findItemToAdd(): String? {
21+
val dialog = SelectBoxDialog("Add Module", "Choose a module:", MODULES
2222
.filter { it !in listItems }
23-
.map { it.name }
2423
.toTypedArray()
2524
)
2625
dialog.showAndGet().ifFalse { return null }
27-
return Module.valueOf(dialog.getSelectedOption()!!)
26+
return MODULES.firstOrNull { it == dialog.getSelectedOption() }
2827
}
2928

30-
fun export() = listItems.map { it as Module }
29+
fun export(): List<String> = listItems.map { it as String }
3130
}

src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,57 @@ package org.tabooproject.intellij.step
22

33
import com.intellij.ide.util.projectWizard.ModuleWizardStep
44
import com.intellij.ui.dsl.builder.panel
5+
import okhttp3.OkHttpClient
56
import org.tabooproject.intellij.component.AddDeleteModuleListPanel
7+
import org.tabooproject.intellij.getRequest
8+
import java.io.IOException
69
import javax.swing.JComponent
710

8-
enum class Module {
9-
AI,
10-
APPLICATION,
11-
BUKKIT,
12-
BUKKIT_ALL,
13-
BUKKIT_HOOK,
14-
BUKKIT_UTIL,
15-
BUKKIT_XSERIES,
16-
BUNGEE,
17-
CHAT,
18-
CONFIGURATION,
19-
DATABASE,
20-
EFFECT,
21-
EXPANSION_COMMAND_HELPER,
22-
EXPANSION_FOLIA,
23-
EXPANSION_GEEK_TOOL,
24-
EXPANSION_IOC,
25-
EXPANSION_JAVASCRIPT,
26-
EXPANSION_LANG_TOOL,
27-
EXPANSION_PLAYER_DATABASE,
28-
EXPANSION_PLAYER_FAKE_OP,
29-
EXPANSION_PTC,
30-
EXPANSION_PTC_OBJECT,
31-
EXPANSION_REDIS,
32-
EXPANSION_SUBMIT_CHAIN,
33-
KETHER,
34-
LANG,
35-
METRICS,
36-
NAVIGATION,
37-
NMS,
38-
NMS_UTIL,
39-
PORTICUS,
40-
UI,
41-
UNIVERSAL,
42-
UNIVERSAL_DATABASE,
43-
VELOCITY
11+
private fun fetchAndParseModules(
12+
url: String = "https://raw.githubusercontent.com/TabooLib/taboolib-gradle-plugin/master/src/main/kotlin/io/izzel/taboolib/gradle/Standards.kt",
13+
): List<String>? {
14+
val client = OkHttpClient()
15+
val request = getRequest(url)
16+
17+
return try {
18+
val response = client.newCall(request).execute()
19+
response.body?.string()?.let { responseBody ->
20+
parseModules(responseBody)
21+
}
22+
} catch (e: IOException) {
23+
e.printStackTrace()
24+
null
25+
}
4426
}
4527

28+
fun parseModules(content: String): List<String> {
29+
val pattern = """val (\w+) =""".toRegex()
30+
return pattern.findAll(content)
31+
.mapNotNull { matchResult ->
32+
val id = matchResult.groupValues[1]
33+
id.ifBlank { null }
34+
}
35+
.toList()
36+
}
37+
38+
val MODULES: List<String> by lazy {
39+
fetchAndParseModules() ?: error("Failed to fetch modules")
40+
}
41+
42+
val TEMPLATE_DOWNLOAD_MIRROR = mapOf(
43+
"GitHub" to "https://github.com/TabooLib/taboolib-sdk/archive/refs/heads/idea-template.zip",
44+
"Aliyun" to "https://template.tabooproject.org"
45+
)
46+
4647
data class ConfigurationProperty(
4748
var name: String = "untitled",
4849
var mainClass: String = "org.example.untitled.UntitledPlugin",
4950
var version: String = "1.0-SNAPSHOT",
50-
val modules: MutableList<Module> = mutableListOf(Module.UNIVERSAL, Module.BUKKIT_ALL),
51+
var mirrorIndex: String = "GitHub",
52+
val modules: MutableList<String> = mutableListOf<String>().apply {
53+
add("UNIVERSAL")
54+
add("BUKKIT_ALL")
55+
},
5156
)
5257

5358
class ConfigurationPropertiesStep : ModuleWizardStep() {
@@ -94,6 +99,15 @@ class ConfigurationPropertiesStep : ModuleWizardStep() {
9499
row {
95100
cell(modulePanel)
96101
}
102+
row { text("") }
103+
row("Select template download mirror:") {
104+
comboBox(TEMPLATE_DOWNLOAD_MIRROR.keys)
105+
.apply {
106+
component.selectedIndex = 0
107+
}.onChanged {
108+
property.mirrorIndex = it.selectedItem as String
109+
}
110+
}
97111
}
98112
}
99113
}

0 commit comments

Comments
 (0)