Skip to content

Commit 544cf39

Browse files
committed
增加进度条显示, 以及下载sha1和modulelist走proxied http client
1 parent 2f98904 commit 544cf39

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import com.intellij.ide.starters.local.StandardAssetsProvider
55
import com.intellij.ide.util.projectWizard.ModuleWizardStep
66
import com.intellij.ide.util.projectWizard.WizardContext
77
import com.intellij.ide.wizard.*
8+
import com.intellij.openapi.progress.ProgressManager
89
import com.intellij.openapi.project.Project
910
import com.intellij.openapi.roots.ui.configuration.ModulesProvider
11+
import com.intellij.openapi.util.ThrowableComputable
1012
import org.tabooproject.intellij.step.ConfigurationPropertiesStep
1113
import org.tabooproject.intellij.step.OptionalPropertiesStep
1214
import org.tabooproject.intellij.util.Assets
@@ -37,11 +39,16 @@ class ProjectBuilder : AbstractNewProjectWizardBuilder() {
3739
// 资源填充
3840
.nextStep {
3941
object : AssetsNewProjectWizardStep(it) {
40-
4142
override fun setupAssets(project: Project) {
4243
addAssets(StandardAssetsProvider().getGradlewAssets())
43-
val directory = project.basePath!!
44-
Template.downloadAndUnzipFile(directory)
44+
45+
ProgressManager.getInstance().runProcessWithProgressSynchronously(
46+
ThrowableComputable {
47+
val directory = project.basePath!!
48+
Template.downloadAndUnzipFile(directory)
49+
},
50+
"Downloading template files", true, project
51+
)
4552
}
4653
}
4754
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package org.tabooproject.intellij
22

3-
import com.intellij.psi.*
3+
import com.intellij.psi.PsiDirectory
4+
import com.intellij.psi.PsiElement
5+
import com.intellij.psi.PsiFile
6+
import com.intellij.psi.PsiReference
47
import okhttp3.OkHttpClient
58
import okhttp3.Request
69
import org.jetbrains.kotlin.psi.KtAnnotated
10+
import java.io.IOException
711
import java.net.InetSocketAddress
812
import java.net.Proxy
913
import java.nio.file.Files
1014
import java.nio.file.Path
1115
import java.nio.file.Paths
16+
import java.util.concurrent.TimeUnit
1217

1318
fun createFileWithDirectories(baseDir: String, relativePath: String): Path? {
1419
val fullPath = Paths.get(baseDir, relativePath)
@@ -28,6 +33,8 @@ fun createOkHttpClientWithSystemProxy(block: OkHttpClient.Builder.() -> Unit = {
2833
val proxyHost = System.getProperty("http.proxyHost")
2934
val proxyPort = System.getProperty("http.proxyPort")
3035

36+
println("proxy host: $proxyHost, proxyPort: $proxyPort")
37+
3138
val clientBuilder = OkHttpClient.Builder().apply {
3239
block(this)
3340
}
@@ -70,4 +77,18 @@ private inline fun <reified T : PsiElement> PsiElement.findParent(
7077

7178
el = el.parent ?: return null
7279
}
80+
}
81+
82+
fun readFromUrl(url: String): String? {
83+
val client = createOkHttpClientWithSystemProxy {
84+
connectTimeout(10, TimeUnit.SECONDS)
85+
readTimeout(10, TimeUnit.SECONDS)
86+
}
87+
88+
val response = client
89+
.newCall(getRequest(url))
90+
.execute()
91+
.takeIf { it.isSuccessful } ?: throw IOException("Failed to get $url")
92+
93+
return response.body?.string()
7394
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package org.tabooproject.intellij.step
33
import ai.grazie.utils.capitalize
44
import com.intellij.ide.util.projectWizard.ModuleWizardStep
55
import com.intellij.ide.util.projectWizard.WizardContext
6+
import com.intellij.openapi.progress.ProgressManager
7+
import com.intellij.openapi.util.ThrowableComputable
68
import com.intellij.ui.dsl.builder.columns
79
import com.intellij.ui.dsl.builder.panel
810
import kotlinx.coroutines.DelicateCoroutinesApi
@@ -11,6 +13,7 @@ import kotlinx.coroutines.coroutineScope
1113
import kotlinx.coroutines.launch
1214
import org.tabooproject.intellij.component.CheckModulePanel
1315
import org.tabooproject.intellij.util.ResourceLoader
16+
import org.tabooproject.intellij.util.ResourceLoader.loadModules
1417
import javax.swing.JComponent
1518
import javax.swing.JTextField
1619

@@ -88,7 +91,7 @@ class ConfigurationPropertiesStep(val context: WizardContext) : ModuleWizardStep
8891
row("Plugin name:") {
8992
textField()
9093
.apply {
91-
property.mainClass = "org.example.${property.name?.lowercase()}.${property.name?.capitalize()}Plugin"
94+
property.mainClass = "org.example.${property.name?.lowercase()}.${property.name?.capitalize()}"
9295
component.text = property.name
9396
component.columns = 30
9497
}.onChanged {
@@ -132,6 +135,14 @@ class ConfigurationPropertiesStep(val context: WizardContext) : ModuleWizardStep
132135
@OptIn(DelicateCoroutinesApi::class)
133136
override fun _init() {
134137
if (inited) return
138+
139+
ProgressManager.getInstance().runProcessWithProgressSynchronously(
140+
ThrowableComputable {
141+
loadModules()
142+
},
143+
"Downloading modules list", true, context.project
144+
)
145+
135146
GlobalScope.launch {
136147
coroutineScope {
137148
checkModulePanel.setModules(ResourceLoader.getModules())

src/main/kotlin/org/tabooproject/intellij/util/ResourceLoader.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import com.jetbrains.rd.util.info
66
import groovy.util.logging.Slf4j
77
import kotlinx.serialization.json.*
88
import org.jetbrains.annotations.NonNls
9+
import org.tabooproject.intellij.readFromUrl
910
import org.tabooproject.intellij.step.Module
10-
import java.net.URL
11+
import java.io.IOException
1112
import java.security.MessageDigest
1213

1314
/**
@@ -26,22 +27,19 @@ object ResourceLoader {
2627
}
2728

2829
fun loadModules() {
29-
val moduleSha1URL = URL(url.plus("Modules.json.sha1"))
30-
3130
val loadLocalModulesToJson = loadLocalModulesToJson()
3231

32+
val remoteSha1 = readFromUrl(url.plus("Modules.json.sha1")) ?: throw IOException("Failed to get remote sha1")
33+
3334
if (cacheJson != null) {
3435
val cacheJsonSha1 = sha1(cacheJson.toString())
35-
val remoteSha1 = moduleSha1URL.openStream().use { it.bufferedReader().use { it.readText() } }
3636
if (remoteSha1.trim() == cacheJsonSha1.trim()) {
3737
logger.info { "缓存数据正确,不进行更新" }
3838
return
3939
}
4040
}
4141

42-
4342
sha1(loadLocalModulesToJson.toString()).also {
44-
val remoteSha1 = moduleSha1URL.openStream().use { it.bufferedReader().use { it.readText() } }
4543
if (remoteSha1.trim() == it.trim()) {
4644
logger.info { "校验通过,本地文件与远程文件一致,无需更新" }
4745
if (cacheJson == null) {
@@ -71,14 +69,11 @@ object ResourceLoader {
7169
}
7270

7371
fun downloadRemoteJson(): JsonObject {
74-
val moduleURL = URL(url.plus("Modules.json"))
75-
return moduleURL.openStream().use {
76-
it.bufferedReader().use { it.readText() }.let { Json.parseToJsonElement(it).jsonObject }
77-
}
72+
val jsonString = readFromUrl(url.plus("Modules.json")) ?: throw IOException("Failed to get modules")
73+
return Json.parseToJsonElement(jsonString).jsonObject
7874
}
7975

8076
fun getModules(): Map<String, List<Module>> {
81-
loadModules()
8277
return parseModules(cacheJson ?: error("加载失败"))
8378
}
8479

0 commit comments

Comments
 (0)