WIP grab once per day korge-templates.xml and use it

This commit is contained in:
soywiz
2019-12-15 11:33:24 +01:00
parent 270baba5fb
commit e6a2b35663
7 changed files with 88 additions and 36 deletions

View File

@@ -1,22 +1,20 @@
package com.soywiz.korge.intellij
import com.intellij.codeInsight.completion.*
import com.intellij.facet.FacetManager
import com.intellij.facet.*
import com.intellij.ide.util.*
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.*
import com.intellij.openapi.components.*
import com.intellij.openapi.module.Module
import com.intellij.openapi.progress.PerformInBackgroundOption
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.module.*
import com.intellij.openapi.progress.*
import com.intellij.openapi.project.*
import com.intellij.openapi.roots.*
import com.intellij.openapi.roots.libraries.*
import com.intellij.openapi.util.*
import com.intellij.util.*
import java.util.concurrent.Semaphore
import java.awt.*
import java.util.concurrent.*
fun <T> runReadAction(callback: () -> T): T {
return ApplicationManager.getApplication().runReadAction(Computable {
@@ -48,6 +46,18 @@ fun OrderEnumerator.toLibrarySequence(): Sequence<Library> = sequence {
yieldAll(items)
}
fun runInUiThread(callback: () -> Unit) {
EventQueue.invokeLater(Runnable {
callback()
})
}
fun runBackgroundTaskGlobal(callback: () -> Unit) {
Thread {
callback()
}.start()
}
fun Project.runBackgroundTaskWithProgress(callback: (ProgressIndicator) -> Unit) {
var error: Throwable? = null
val sema = Semaphore(1)

View File

@@ -2,19 +2,35 @@ package com.soywiz.korge.intellij.config
import com.intellij.openapi.components.*
import com.intellij.util.xmlb.*
import com.soywiz.klock.*
import com.soywiz.korge.intellij.*
import com.soywiz.korge.intellij.module.*
import java.net.*
@State(
name = "KorgeGlobalSettings",
storages = [Storage("korge.xml")]
)
open class KorgeGlobalSettings : PersistentStateComponent<KorgeGlobalSettings> {
var hello: String = "world"
var cachedTemplateLastRefreshTime: Long = 0L
var cachedTemplateString: String? = null
init {
println("KorgeGlobalSettings.init")
}
fun getCachedTemplate(): String {
val now = System.currentTimeMillis()
if (cachedTemplateString == null || (now - cachedTemplateLastRefreshTime).milliseconds >= 1.days) {
cachedTemplateLastRefreshTime = now
cachedTemplateString =
runCatching { URL("https://raw.githubusercontent.com/korlibs/korge-intellij-plugin/master/src/main/resources/com/soywiz/korge/intellij/korge-templates.xml").readText() }.getOrNull()
?: runCatching { KorgeProjectTemplate::class.java.getResource("/com/soywiz/korge/intellij/korge-templates.xml")?.readText() }.getOrNull()
?: error("Can't get a valid 'korge-templates.xml' file from any source")
}
return cachedTemplateString!!
}
override fun getState() = this
override fun loadState(state: KorgeGlobalSettings) {

View File

@@ -25,6 +25,7 @@ class KorgeModuleBuilder() : JavaModuleBuilder() {
override fun getBuilderId() = "KORGE_MODULE"
override fun isSuitableSdkType(sdk: SdkTypeId?) = sdk === JavaSdk.getInstance()
val korgeProjectTemplateProvider = KorgeProjectTemplate.provider()
var config = KorgeModuleConfig()
override fun getModuleType(): ModuleType<*> = KorgeModuleType.INSTANCE
@@ -78,12 +79,14 @@ class KorgeModuleBuilder() : JavaModuleBuilder() {
override fun getParentGroup() = KorgeModuleType.NAME
override fun createWizardSteps(wizardContext: WizardContext, modulesProvider: ModulesProvider) = arrayOf(
KorgeArtifactWizardStep(config)
)
// 1st screen
override fun getCustomOptionsStep(context: WizardContext, parentDisposable: Disposable?) =
KorgeModuleWizardStep(config)
KorgeModuleWizardStep(korgeProjectTemplateProvider, config)
// 2nd+ screen(s)
override fun createWizardSteps(wizardContext: WizardContext, modulesProvider: ModulesProvider) = arrayOf(
KorgeArtifactWizardStep(korgeProjectTemplateProvider, config)
)
override fun getSourcePaths(): MutableList<Pair<String, String>> {
return super.getSourcePaths()

View File

@@ -9,7 +9,7 @@ class KorgeModuleConfig {
var artifactVersion = "0.0.1"
var projectType = ProjectType.Gradle
var featuresToInstall = listOf(Features.core)
var korgeVersion = Versions.LAST
var korgeVersion = KorgeProjectTemplate.Versions.Version("1.5.0d")
fun generate(): Map<String, ByteArray> = LinkedHashMap<String, ByteArray>().apply {
fun putTextFile(name: String, file: Indenter.() -> Unit) {
@@ -156,18 +156,6 @@ enum class ProjectType(val id: String) {
}
}
object Versions {
val V150 = KorgeVersion(version = "1.5.0c", kotlinVersion = "1.3.61")
val V143 = KorgeVersion(version = "1.4.3", kotlinVersion = "1.3.61")
val ALL = arrayOf(V143, V150)
val LAST = V150
val LAST_EAP = V150
private val VMAP = ALL.associateBy { it.version }
fun fromString(version: String): KorgeVersion? = VMAP[version]
}
data class KorgeVersion(
val version: String,
val kotlinVersion: String,

View File

@@ -1,5 +1,6 @@
package com.soywiz.korge.intellij.module
import com.soywiz.korge.intellij.config.*
import org.intellij.lang.annotations.*
import javax.xml.bind.*
import javax.xml.bind.annotation.*
@@ -20,9 +21,11 @@ open class KorgeProjectTemplate {
@set:XmlElement(name = "version")
var versions = arrayListOf<Version>()
class Version {
data class Version @JvmOverloads constructor(
@set:XmlValue
var text: String = ""
) {
override fun toString(): String = text
}
}
@@ -61,9 +64,24 @@ open class KorgeProjectTemplate {
}
}
interface Provider {
val template: KorgeProjectTemplate
}
companion object {
fun fromXml(@Language("XML") xml: String): KorgeProjectTemplate = JAXBContext.newInstance(KorgeProjectTemplate::class.java).createUnmarshaller().unmarshal(xml.reader()) as KorgeProjectTemplate
fun fromXml(@Language("XML") xml: String): KorgeProjectTemplate =
JAXBContext.newInstance(KorgeProjectTemplate::class.java).createUnmarshaller().unmarshal(xml.reader()) as KorgeProjectTemplate
fun fromEmbeddedResource(): KorgeProjectTemplate =
fromXml(KorgeProjectTemplate::class.java.getResource("/com/soywiz/korge/intellij/korge-templates.xml")?.readText() ?: error("Can't find ¡korge-templates.xml' from esources"))
fromXml(
KorgeProjectTemplate::class.java.getResource("/com/soywiz/korge/intellij/korge-templates.xml")?.readText()
?: error("Can't find ¡korge-templates.xml' from esources")
)
fun fromUpToDateTemplate(): KorgeProjectTemplate = fromXml(korgeGlobalSettings.getCachedTemplate())
fun provider() = object : Provider {
override val template by lazy { fromUpToDateTemplate() }
}
}
}

View File

@@ -6,7 +6,7 @@ import com.soywiz.korge.intellij.module.*
import com.soywiz.korge.intellij.util.*
import javax.swing.*
class KorgeArtifactWizardStep(val config: KorgeModuleConfig) : ModuleWizardStep() {
class KorgeArtifactWizardStep(val korgeProjectTemplateProvider: KorgeProjectTemplate.Provider, val config: KorgeModuleConfig) : ModuleWizardStep() {
val groupId: JTextField
val artifactId: JTextField
val version: JTextField

View File

@@ -4,6 +4,7 @@ import com.intellij.ide.util.projectWizard.*
import com.intellij.openapi.ui.*
import com.intellij.ui.*
import com.intellij.util.ui.*
import com.soywiz.korge.intellij.*
import com.soywiz.korge.intellij.module.*
import com.soywiz.korge.intellij.util.*
import java.awt.*
@@ -11,7 +12,7 @@ import java.net.*
import javax.swing.*
import javax.swing.tree.*
class KorgeModuleWizardStep(val config: KorgeModuleConfig) : ModuleWizardStep() {
class KorgeModuleWizardStep(val korgeProjectTemplateProvider: KorgeProjectTemplate.Provider, val config: KorgeModuleConfig) : ModuleWizardStep() {
override fun updateDataModel() {
config.projectType = projectTypeCB.selectedItem as ProjectType
config.featuresToInstall = featuresToCheckbox.keys.filter { it.selected }
@@ -20,7 +21,7 @@ class KorgeModuleWizardStep(val config: KorgeModuleConfig) : ModuleWizardStep()
lateinit var projectTypeCB: JComboBox<ProjectType>
lateinit var versionCB: JComboBox<KorgeVersion>
lateinit var versionCB: JComboBox<KorgeProjectTemplate.Versions.Version>
lateinit var wrapperCheckBox: JCheckBox
@@ -28,7 +29,12 @@ class KorgeModuleWizardStep(val config: KorgeModuleConfig) : ModuleWizardStep()
val featuresToCheckbox = LinkedHashMap<Feature, ThreeStateCheckedTreeNode>()
init {
println("Created KorgeModuleWizardStep")
}
val panel by lazy {
println("Created KorgeModuleWizardStep.panel")
JPanel().apply {
val description = JPanel().apply {
layout = BoxLayout(this, BoxLayout.Y_AXIS)
@@ -70,9 +76,10 @@ class KorgeModuleWizardStep(val config: KorgeModuleConfig) : ModuleWizardStep()
td(JComboBox(ProjectType.values()).apply { projectTypeCB = this })
td(JCheckBox("Wrapper", true).apply { wrapperCheckBox = this })
td(JLabel("Korge Version:"))
td(JComboBox(Versions.ALL).apply {
td(JComboBox<KorgeProjectTemplate.Versions.Version>(arrayOf()).apply {
versionCB = this
selectedItem = Versions.LAST
isEnabled = false
//selectedIndex = 0
})
}
}, BorderLayout.NORTH)
@@ -94,6 +101,16 @@ class KorgeModuleWizardStep(val config: KorgeModuleConfig) : ModuleWizardStep()
}
this.secondComponent = description
}, BorderLayout.CENTER)
}.also {
runBackgroundTaskGlobal {
// Required since this is blocking
val korgeProjectTemplate = korgeProjectTemplateProvider.template
runInUiThread {
versionCB.model = DefaultComboBoxModel(korgeProjectTemplate.versions.versions.toTypedArray())
versionCB.isEnabled = true
}
}
}
}