Skip to content

Commit

Permalink
Dynamically load configuration type
Browse files Browse the repository at this point in the history
  • Loading branch information
raniejade committed Aug 1, 2017
1 parent 936a525 commit d2a1de0
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 59 deletions.
12 changes: 0 additions & 12 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ apply plugin: "kotlin"

def isAndroidStudio = findProperty("as") != null

if (isAndroidStudio) {
version = "$version-as"
}

intellij {
pluginName = 'spek-idea-plugin'
updateSinceUntilBuild = false
Expand All @@ -23,14 +19,6 @@ intellij {
}
}

patchPluginXml {
if (isAndroidStudio) {
pluginXmlFiles = ['src/pluginDescriptor/android/plugin.xml']
} else {
pluginXmlFiles = ['src/pluginDescriptor/jvm/plugin.xml']
}
}

dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compileOnly "org.jetbrains.kotlin:kotlin-runtime:$kotlinVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jetbrains.spek.idea

import com.intellij.execution.configurations.ConfigurationFactory
import com.intellij.execution.configurations.ConfigurationType
import javax.swing.Icon

class DynamicConfigurationType: ConfigurationType {
val manager by lazy { SpekPluginManager() }
val extensionFactory by lazy { manager.extensionFactory }
val type by lazy { extensionFactory.createConfigurationType() }

override fun getIcon(): Icon {
return type.icon
}

override fun getConfigurationTypeDescription(): String {
return type.configurationTypeDescription
}

override fun getId(): String {
return type.id
}

override fun getDisplayName(): String {
return type.displayName
}

override fun getConfigurationFactories(): Array<ConfigurationFactory> {
return type.configurationFactories
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jetbrains.spek.idea

import com.intellij.execution.configurations.ConfigurationType

interface ExtensionFactory {
fun createConfigurationType(): ConfigurationType
}
11 changes: 11 additions & 0 deletions plugin/src/main/kotlin/org/jetbrains/spek/idea/PluginOverride.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.spek.idea

import com.intellij.openapi.extensions.ExtensionPointName

interface PluginOverride {
fun createExtensionFactory(): ExtensionFactory

companion object {
val PLUGIN_OVERRIDE_EP = ExtensionPointName.create<PluginOverride>("org.jetbrains.spek.spek-idea-plugin.pluginOverride")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import com.intellij.icons.AllIcons
/**
* @author Ranie Jade Ramiso
*/
abstract class SpekRunConfigurationType(id: String, displayName: String): ConfigurationTypeBase(
abstract class SpekConfigurationType(id: String, displayName: String): ConfigurationTypeBase(
id,
displayName,
"Run Spek tests",
AllIcons.RunConfigurations.Junit
) {
protected abstract fun createConfigurationFactory(type: SpekRunConfigurationType): SpekConfigurationFactory
protected abstract fun createConfigurationFactory(type: SpekConfigurationType): SpekConfigurationFactory

init {
addFactory(createConfigurationFactory(this))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jetbrains.spek.idea

class SpekJvmRunConfigurationType: SpekRunConfigurationType(
class SpekJvmRunConfigurationType: SpekConfigurationType(
"org.spekframework.spek-jvm",
"Spek"
) {
override fun createConfigurationFactory(type: SpekRunConfigurationType): SpekConfigurationFactory {
override fun createConfigurationFactory(type: SpekConfigurationType): SpekConfigurationFactory {
return SpekJvmConfigurationFactory(type)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.spek.idea

import com.intellij.execution.configurations.ConfigurationType
import com.intellij.openapi.extensions.Extensions

class SpekPluginManager {
val extensionFactory by lazy {
val override = Extensions.getExtensions(PluginOverride.PLUGIN_OVERRIDE_EP)
.firstOrNull()

override?.createExtensionFactory() ?: createDefaultExtensionFactory()
}

private fun createDefaultExtensionFactory(): ExtensionFactory {
return object: ExtensionFactory {
override fun createConfigurationType(): ConfigurationType {
return SpekJvmRunConfigurationType()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.jetbrains.spek.tooling.Target
* @author Ranie Jade Ramiso
*/
class SpekRunConfigurationProducer: JavaRunConfigurationProducerBase<SpekRunConfiguration>(
ConfigurationTypeUtil.findConfigurationType(SpekRunConfigurationType::class.java)
ConfigurationTypeUtil.findConfigurationType(DynamicConfigurationType::class.java)
) {
override fun setupConfigurationFromContext(configuration: SpekRunConfiguration, context: ConfigurationContext,
sourceElement: Ref<PsiElement>): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jetbrains.spek.idea.androidstudio

import com.intellij.execution.configurations.ConfigurationType
import org.jetbrains.spek.idea.ExtensionFactory
import org.jetbrains.spek.idea.PluginOverride

class PluginAndroidOverride: PluginOverride {
override fun createExtensionFactory(): ExtensionFactory {
return object: ExtensionFactory {
override fun createConfigurationType(): ConfigurationType {
return SpekAndroidConfigurationType()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.jetbrains.spek.idea.androidstudio

import org.jetbrains.spek.idea.SpekConfigurationFactory
import org.jetbrains.spek.idea.SpekRunConfigurationType
import org.jetbrains.spek.idea.SpekConfigurationType

class SpekAndroidConfigurationType: SpekRunConfigurationType(
class SpekAndroidConfigurationType: SpekConfigurationType(
"SpecsRunConfiguration", // hardcoded :(
"Spek - Android"
"Spek"
) {
override fun createConfigurationFactory(type: SpekRunConfigurationType): SpekConfigurationFactory {
override fun createConfigurationFactory(type: SpekConfigurationType): SpekConfigurationFactory {
return SpekAndroidConfigurationFactory(type)
}
}
5 changes: 5 additions & 0 deletions plugin/src/main/resources/META-INF/android.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<idea-plugin>
<extensions defaultExtensionNs="org.jetbrains.spek.spek-idea-plugin">
<pluginOverride implementation="org.jetbrains.spek.idea.androidstudio.PluginAndroidOverride"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
-->
<depends>org.jetbrains.kotlin</depends>
<depends>com.intellij.modules.java</depends>
<depends optional="true" config-file="android.xml">com.intellij.modules.androidstudio</depends>

<extensions defaultExtensionNs="com.intellij">
<configurationType implementation="org.jetbrains.spek.idea.SpekJvmRunConfigurationType"/>
<configurationType implementation="org.jetbrains.spek.idea.DynamicConfigurationType"/>
<runConfigurationProducer implementation="org.jetbrains.spek.idea.SpekRunConfigurationProducer"/>
<implicitUsageProvider implementation="org.jetbrains.spek.idea.SpekImplicitUsageProvider"/>
<runLineMarkerContributor language="kotlin" implementationClass="org.jetbrains.spek.idea.SpekLineMarkerProvider"/>
</extensions>

<extensionPoints>
<extensionPoint name="pluginOverride" interface="org.jetbrains.spek.idea.PluginOverride"/>
</extensionPoints>

<actions>
<!-- Add your actions here -->
</actions>
Expand Down
37 changes: 0 additions & 37 deletions plugin/src/pluginDescriptor/android/plugin.xml

This file was deleted.

0 comments on commit d2a1de0

Please sign in to comment.