Skip to content

Commit

Permalink
Merge pull request #5 from JakubNeukirch/spring-support
Browse files Browse the repository at this point in the history
Spring support
  • Loading branch information
JakubNeukirch authored Nov 20, 2020
2 parents e950486 + a9ee4e9 commit 99fd56b
Show file tree
Hide file tree
Showing 21 changed files with 613 additions and 22 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'org.example'
version '0.1.1'
version '0.2.0'

repositories {
mavenCentral()
Expand All @@ -24,7 +24,7 @@ dependencies {
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2020.2.1'
plugins = ['gradle']
plugins = ['gradle', 'java', 'org.jetbrains.kotlin']
}
patchPluginXml {
changeNotes """
Expand Down
21 changes: 13 additions & 8 deletions src/main/kotlin/tech/stonks/kvizard/KVisionModuleBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.stonks.kvizard

import com.intellij.execution.executors.DefaultRunExecutor
import com.intellij.ide.fileTemplates.FileTemplateManager
import com.intellij.ide.util.projectWizard.ModuleBuilder
import com.intellij.ide.util.projectWizard.ModuleWizardStep
import com.intellij.ide.util.projectWizard.WizardContext
Expand All @@ -14,13 +14,12 @@ import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import com.intellij.psi.impl.file.PsiDirectoryFactory
import org.jetbrains.plugins.gradle.action.GradleExecuteTaskAction
import tech.stonks.kvizard.generator.FrontendTreeGenerator
import tech.stonks.kvizard.generator.KtorTreeGenerator
import tech.stonks.kvizard.generator.SpringTreeGenerator
import tech.stonks.kvizard.generator.TreeGenerator
import tech.stonks.kvizard.step.library_choice.LibraryChoiceStep
import tech.stonks.kvizard.utils.backgroundTask
import tech.stonks.kvizard.utils.runGradle
import tech.stonks.kvizard.utils.*
import java.io.File

class KVisionModuleBuilder : ModuleBuilder() {
Expand All @@ -30,8 +29,9 @@ class KVisionModuleBuilder : ModuleBuilder() {
* Here add libraries that were newly supported
*/
val supportedBackendLibraries = arrayOf(
KVisionBackendLibrary.KTOR,
KVisionBackendLibrary.FRONTEND_ONLY
KVisionBackendLibrary.KTOR,
KVisionBackendLibrary.SPRING_BOOT,
KVisionBackendLibrary.FRONTEND_ONLY
)
}

Expand All @@ -46,8 +46,8 @@ class KVisionModuleBuilder : ModuleBuilder() {
ApplicationManager.getApplication().runWriteAction {
val manager = PsiManager.getInstance(modifiableRootModel.project)
manager.findFile(root)?.add(
PsiDirectoryFactory.getInstance(manager.project)
.createDirectory(root.createChildDirectory(null, "webpack"))
PsiDirectoryFactory.getInstance(manager.project)
.createDirectory(root.createChildDirectory(null, "webpack"))
)
}
} catch (ex: java.lang.Exception) {
Expand All @@ -61,6 +61,8 @@ class KVisionModuleBuilder : ModuleBuilder() {

}
runGradleTasks(modifiableRootModel.project)
RunConfigurationUtil.createConfiguration(modifiableRootModel.project)
KVisionDialogUtil.showNewsDialog()
}
}

Expand All @@ -72,6 +74,7 @@ class KVisionModuleBuilder : ModuleBuilder() {
return when (backendLibrary) {
KVisionBackendLibrary.KTOR -> KtorTreeGenerator()
KVisionBackendLibrary.FRONTEND_ONLY -> FrontendTreeGenerator()
KVisionBackendLibrary.SPRING_BOOT -> SpringTreeGenerator()
else -> throw IllegalStateException("${backendLibrary.name} is not supported yet.")
}
}
Expand All @@ -88,4 +91,6 @@ class KVisionModuleBuilder : ModuleBuilder() {
override fun getCustomOptionsStep(context: WizardContext?, parentDisposable: Disposable?): ModuleWizardStep? {
return LibraryChoiceStep(this)
}


}
11 changes: 11 additions & 0 deletions src/main/kotlin/tech/stonks/kvizard/action/StartupAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package tech.stonks.kvizard.action

import com.intellij.openapi.components.ProjectComponent
import tech.stonks.kvizard.utils.KVisionDialogUtil

@Suppress("DEPRECATION")
class StartupAction : ProjectComponent {
override fun initComponent() {
KVisionDialogUtil.showNewsDialog()
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/tech/stonks/kvizard/action/notification.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tech.stonks.kvizard.action

import com.intellij.ide.BrowserUtil
import com.intellij.notification.Notification
import com.intellij.notification.NotificationAction
import com.intellij.openapi.actionSystem.AnActionEvent
import tech.stonks.kvizard.settings.AppSettingsState

class NewsAction(text: String) : NotificationAction(text) {
override fun actionPerformed(e: AnActionEvent, notification: Notification) {
BrowserUtil.browse("https://kotlin.news")
notification.expire()
val appSettings = AppSettingsState.getInstance()
appSettings?.state?.isNotificationDisabled = true
}
}

class NotShowAction(text: String) : NotificationAction(text) {
override fun actionPerformed(e: AnActionEvent, notification: Notification) {
notification.expire()
val appSettings = AppSettingsState.getInstance()
appSettings?.state?.isNotificationDisabled = true

}
}
12 changes: 6 additions & 6 deletions src/main/kotlin/tech/stonks/kvizard/data/VersionApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ interface VersionApi {
fun getVersionData(): Single<VersionData>

companion object {
fun create() : VersionApi {
fun create(): VersionApi {
return Retrofit.Builder()
.baseUrl("https://raw.githubusercontent.com/rjaros/kvision/master/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.build()
.create(VersionApi::class.java)
.baseUrl("https://raw.githubusercontent.com/rjaros/kvision/master/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.build()
.create(VersionApi::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tech.stonks.kvizard.generator

class SpringTreeGenerator : TreeGenerator(
"spring",
false,
backendFiles = arrayOf("Main.kt", "Service.kt"),
backendResourcesFiles = arrayOf("application.yml", "logback.xml")
)
33 changes: 33 additions & 0 deletions src/main/kotlin/tech/stonks/kvizard/settings/AppSettingsState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tech.stonks.kvizard.settings

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.*
import com.intellij.openapi.project.ProjectManager
import com.intellij.serviceContainer.ComponentManagerImpl

@State(
name = "KVisionWizardSettings",
storages = [Storage("KVisionWizardSettings.xml", )]
)
class AppSettingsState : PersistentStateComponent<AppSettingsState.State> {
data class State(
var lastDisplayed: Long = 0L,
var isNotificationDisabled: Boolean = false
)

companion object {
fun getInstance(): AppSettingsState? {
return ApplicationManager.getApplication().getComponent(AppSettingsState::class.java)
}
}

private var _state: State = State()

override fun getState(): State? {
return _state
}

override fun loadState(state: State) {
_state = state
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tech.stonks.kvizard.step.library_choice

import com.intellij.ide.BrowserUtil
import com.intellij.openapi.ui.ComboBox
import tech.stonks.kvizard.KVisionBackendLibrary
import tech.stonks.kvizard.KVisionModuleBuilder
import tech.stonks.kvizard.utils.setOnTextChangedListener
import java.awt.Color
import java.awt.FlowLayout
import javax.swing.*

Expand Down Expand Up @@ -48,6 +50,13 @@ class LibraryChoiceView(
onChanged()
}
})
add(JButton("Check Kotlin.News").apply {
background = Color(0xffe017)

this.addActionListener {
BrowserUtil.browse("https://kotlin.news")
}
})
}
add(panel)
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/tech/stonks/kvizard/utils/KVisionDialogUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.stonks.kvizard.utils

import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.util.IconLoader
import tech.stonks.kvizard.action.NewsAction
import tech.stonks.kvizard.action.NotShowAction
import tech.stonks.kvizard.settings.AppSettingsState

object KVisionDialogUtil {
fun showNewsDialog() {
val appSettings = AppSettingsState.getInstance()
if (appSettings?.state?.isNotificationDisabled == false && appSettings.state?.wasPublishedRecently() == false) {

Notifications.Bus.notify(
Notification(
Notifications.SYSTEM_MESSAGES_GROUP_ID,
"Invitation",
"You have got invitation to join our community! Join Kotlin.News and become ninja developer in Kotlin.",
NotificationType.INFORMATION
).apply {
icon = IconLoader.getIcon("/images/email-black-18dp.svg")
this.addAction(NewsAction("Join Kotlin.News"))
this.addAction(NotShowAction("Do not show again"))
}
)
appSettings.state?.lastDisplayed = System.currentTimeMillis()
}
}

private fun AppSettingsState.State.wasPublishedRecently(): Boolean {
return (System.currentTimeMillis() - this.lastDisplayed) < 1000 * 60 * 60 * 12
}
}
61 changes: 61 additions & 0 deletions src/main/kotlin/tech/stonks/kvizard/utils/RunConfigurationUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tech.stonks.kvizard.utils

import com.intellij.execution.RunnerAndConfigurationSettings
import com.intellij.execution.configurations.ConfigurationFactory
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.impl.RunManagerImpl
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.IconLoader
import org.jetbrains.plugins.gradle.service.execution.GradleExternalTaskConfigurationType
import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration
import javax.swing.Icon

class KVisionConfigurationFactory(val task: String, private val args: String = "") :
ConfigurationFactory(GradleExternalTaskConfigurationType.getInstance()) {
override fun createTemplateConfiguration(project: Project): RunConfiguration {
val conf = GradleRunConfiguration(
project,
GradleExternalTaskConfigurationType.getInstance().factory,
"Run $task"
)
conf.settings.externalProjectPath = project.basePath
conf.settings.taskNames = listOf(task)
conf.settings.scriptParameters = args
return conf
}

override fun getName(): String = "Run $task"

override fun getIcon(): Icon = IconLoader.getIcon("/images/logo16.png")
}

class RunnerComparator : Comparator<RunnerAndConfigurationSettings> {
override fun compare(o1: RunnerAndConfigurationSettings?, o2: RunnerAndConfigurationSettings?): Int {
return when {
o1?.factory is KVisionConfigurationFactory -> 1
o2?.factory is KVisionConfigurationFactory -> -1
else -> 0
}
}
}

object RunConfigurationUtil {
fun createConfiguration(project: Project) {
val runManager = RunManagerImpl.getInstanceImpl(project)
runManager.addConfiguration(
RunnerAndConfigurationSettingsImpl(
RunManagerImpl.getInstanceImpl(project),
KVisionConfigurationFactory("backendRun").createTemplateConfiguration(project)
)
)
runManager.addConfiguration(
RunnerAndConfigurationSettingsImpl(
RunManagerImpl.getInstanceImpl(project),
KVisionConfigurationFactory("frontendRun", "-t").createTemplateConfiguration(project)
)
)
runManager.setOrder(RunnerComparator())
runManager.requestSort()
}
}
19 changes: 17 additions & 2 deletions src/main/kotlin/tech/stonks/kvizard/utils/file.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tech.stonks.kvizard.utils

import com.intellij.execution.executors.DefaultRunExecutor
import com.intellij.ide.fileTemplates.FileTemplateManager
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.plugins.gradle.action.GradleExecuteTaskAction
Expand Down Expand Up @@ -38,8 +39,8 @@ fun File.file(name: String, templateName: String, attributes: Map<String, String

private fun getTemplateData(templateName: String, attributes: Map<String, String> = emptyMap()): String {
val template = FileTemplateManager
.getDefaultInstance()
.getInternalTemplate(templateName)
.getDefaultInstance()
.getInternalTemplate(templateName)
return if (attributes.isEmpty()) {
template.text
} else {
Expand All @@ -55,6 +56,20 @@ fun Project.getRootFile(): VirtualFile? {
return projectFile?.parent?.parent
}

fun String.insertAfter(after: Regex, insert: String): String {
val last = after.find(this)?.range?.last
return if(last != null) {
buildString {
append(this.substring(0, last+1))
appendln(insert)
appendln(this.substring(last+1))
}
}else {
this
}

}

object TemplateAttributes {
const val GROUP_ID = "GROUP_ID"
const val ARTIFACT_ID = "ARTIFACT_ID"
Expand Down
14 changes: 10 additions & 4 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
<moduleType id="KVISION_WIZARD" implementationClass="tech.stonks.kvizard.KVisionModuleType" />
<moduleBuilder builderClass="tech.stonks.kvizard.KVisionModuleBuilder"/>
</extensions>

<actions>
<!-- Add your actions here -->
</actions>
<application-components>
<component>
<implementation-class>tech.stonks.kvizard.settings.AppSettingsState</implementation-class>
</component>
</application-components>
<project-components>
<component>
<implementation-class>tech.stonks.kvizard.action.StartupAction</implementation-class>
</component>
</project-components>
</idea-plugin>
Loading

0 comments on commit 99fd56b

Please sign in to comment.