Skip to content

Commit

Permalink
Add a bunch more configuration options and allow running only a singl…
Browse files Browse the repository at this point in the history
…e file of specs
  • Loading branch information
Gregg Van Hove committed Feb 13, 2018
1 parent fdeb901 commit eb08975
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 41 deletions.
66 changes: 54 additions & 12 deletions src/io/pivotal/intellij/jasmine/JasmineConfigurationEditor.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,85 @@
package io.pivotal.intellij.jasmine

import com.intellij.execution.configuration.EnvironmentVariablesComponent
import com.intellij.execution.configuration.EnvironmentVariablesTextFieldWithBrowseButton
import com.intellij.javascript.nodejs.interpreter.NodeJsInterpreterField
import com.intellij.javascript.nodejs.util.NodePackageField
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.options.SettingsEditor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.util.io.FileUtil
import com.intellij.ui.RawCommandLineEditor
import com.intellij.ui.components.fields.ExpandableTextField
import com.intellij.util.ui.ComponentWithEmptyText
import com.intellij.util.ui.FormBuilder
import com.intellij.util.ui.SwingHelper
import javax.swing.JComponent
import javax.swing.JPanel


class JasmineConfigurationEditor(project: Project) : SettingsEditor<JasmineRunConfiguration>() {
class JasmineConfigurationEditor(private var project: Project) : SettingsEditor<JasmineRunConfiguration>() {
private var nodeJsInterpreterField: NodeJsInterpreterField = NodeJsInterpreterField(project, false)
private var nodeOptionsField: RawCommandLineEditor = RawCommandLineEditor()
private var workingDirectoryField = createWorkingDirectoryField()
private var envVars: EnvironmentVariablesTextFieldWithBrowseButton = EnvironmentVariablesTextFieldWithBrowseButton()
private var jasminePackageField: NodePackageField = NodePackageField(nodeJsInterpreterField, "jasmine")
private var envVars: EnvironmentVariablesComponent = EnvironmentVariablesComponent()
private var jasmineOptionsField = createJasmineOptionsField()
private var rootForm: JPanel

init {
nodeOptionsField.dialogCaption = "Node Options"
rootForm = FormBuilder()
.setAlignLabelOnRight(false)
.addLabeledComponent("Node interpreter", nodeJsInterpreterField)
.addLabeledComponent("Jasmine package", jasminePackageField)
.addComponent(envVars)
.addLabeledComponent("Node &interpreter", nodeJsInterpreterField)
.addLabeledComponent("Node &options", nodeOptionsField)
.addLabeledComponent("&Working directory", workingDirectoryField)
.addLabeledComponent("&Environment variables", envVars)
.addLabeledComponent("&Jasmine package", jasminePackageField)
.addLabeledComponent("E&xtra Jasmine options", jasmineOptionsField)
.panel
}

private fun createWorkingDirectoryField(): TextFieldWithBrowseButton {
val field = TextFieldWithBrowseButton()
SwingHelper.installFileCompletionAndBrowseDialog(project, field, "Jasmine Working Directory",
FileChooserDescriptorFactory.createSingleFolderDescriptor())
return field
}

private fun createJasmineOptionsField(): RawCommandLineEditor {
val editor = RawCommandLineEditor()
editor.dialogCaption = "Extra Jasmine Options"
val field = editor.textField
if (field is ExpandableTextField) {
field.putClientProperty("monospaced", false)
}

if (field is ComponentWithEmptyText) {
(field as ComponentWithEmptyText).emptyText.text = "CLI options, e.g. --fail-fast=true"
}

return editor
}

override fun createEditor(): JComponent = rootForm

override fun applyEditorTo(config: JasmineRunConfiguration) {
config.nodeJs = nodeJsInterpreterField.interpreterRef
config.jasmineRunSettings = config.jasmineRunSettings.copy(
nodeJs = nodeJsInterpreterField.interpreterRef,
nodeOptions = nodeOptionsField.text,
workingDir = workingDirectoryField.text,
envData = envVars.data,
extraJasmineOptions = jasmineOptionsField.text)
config.setJasminePackage(jasminePackageField.selected)
config.envData = envVars.envData
}

override fun resetEditorFrom(config: JasmineRunConfiguration) {
nodeJsInterpreterField.interpreterRef = config.nodeJs
val runSettings = config.jasmineRunSettings
nodeJsInterpreterField.interpreterRef = runSettings.nodeJs
nodeOptionsField.text = runSettings.nodeOptions
workingDirectoryField.text = FileUtil.toSystemDependentName(runSettings.workingDir)
envVars.data = runSettings.envData
jasminePackageField.selected = config.selectedJasminePackage()
if (config.envData != null) {
envVars.envData = config.envData!!
}
jasmineOptionsField.text = runSettings.extraJasmineOptions
}
}
5 changes: 0 additions & 5 deletions src/io/pivotal/intellij/jasmine/JasmineConfigurationType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class JasmineConfigurationType : ConfigurationTypeBase("JavascriptTestRunnerJasm
fun getInstance(): JasmineConfigurationType {
return Holder.INSTANCE
}

fun getFactory(): ConfigurationFactory {
val type = getInstance()
return type.getConfigurationFactories()[0]
}
}


Expand Down
7 changes: 2 additions & 5 deletions src/io/pivotal/intellij/jasmine/JasmineRunConfiguration.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package io.pivotal.intellij.jasmine

import com.intellij.execution.Executor
import com.intellij.execution.configuration.EnvironmentVariablesData
import com.intellij.execution.configurations.ConfigurationFactory
import com.intellij.execution.configurations.LocatableConfigurationBase
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.javascript.nodejs.interpreter.NodeJsInterpreterRef
import com.intellij.javascript.nodejs.interpreter.local.NodeJsLocalInterpreter
import com.intellij.javascript.nodejs.util.NodePackage
import com.intellij.openapi.project.Project


class JasmineRunConfiguration(project: Project, factory: ConfigurationFactory, name: String) : LocatableConfigurationBase(project, factory, name) {
var nodeJs = NodeJsInterpreterRef.createProjectRef()
var jasmineRunSettings = JasmineRunSettings()
private var _jasminePackage: NodePackage? = null
var envData: EnvironmentVariablesData? = null

override fun getConfigurationEditor() = JasmineConfigurationEditor(project)

override fun getState(executor: Executor, environment: ExecutionEnvironment) = JasmineRunProfileState(project, this, executor, environment)

fun selectedJasminePackage(): NodePackage {
if (_jasminePackage == null) {
val interpreter = NodeJsLocalInterpreter.tryCast(nodeJs.resolve(project))
val interpreter = NodeJsLocalInterpreter.tryCast(jasmineRunSettings.nodeJs.resolve(project))
val pkg = NodePackage.findPreferredPackage(project, "Jasmine", interpreter)
_jasminePackage = pkg
return pkg
Expand Down
63 changes: 52 additions & 11 deletions src/io/pivotal/intellij/jasmine/JasmineRunConfigurationProducer.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
package io.pivotal.intellij.jasmine;
package io.pivotal.intellij.jasmine

import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.actions.RunConfigurationProducer;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiElement;
import com.google.common.collect.ImmutableList
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.javascript.testing.JsTestRunConfigurationProducer
import com.intellij.lang.javascript.psi.JSFile
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFileSystemItem
import com.intellij.psi.util.PsiUtilCore
import com.intellij.util.ObjectUtils.tryCast

class JasmineRunConfigurationProducer : RunConfigurationProducer<JasmineRunConfiguration>(JasmineConfigurationType.getInstance()) {
class JasmineRunConfigurationProducer : JsTestRunConfigurationProducer<JasmineRunConfiguration>(JasmineConfigurationType.getInstance(), ImmutableList.of("jasmine")) {
override fun isConfigurationFromCompatibleContext(runConfig: JasmineRunConfiguration, context: ConfigurationContext): Boolean {
val element = context.psiLocation ?: return false
val currentFile = PsiUtilCore.getVirtualFile(element) ?: return false
if (currentFile.isDirectory) {
// not sure the right way to run a dir
return false
}

override fun setupConfigurationFromContext(runConfig: JasmineRunConfiguration,
config: ConfigurationContext,
sourceElement: Ref<PsiElement>) = true
val psiFile = tryCast(element.containingFile, JSFile::class.java) ?: return false
psiFile.testFileType ?: return false

override fun isConfigurationFromContext(p0: JasmineRunConfiguration?,
p1: ConfigurationContext?) = true
// if (element is PsiFileSystemItem) {
runConfig.jasmineRunSettings = runConfig.jasmineRunSettings.copy(
specFile = currentFile.path
)
// } else return false

return true
}

override fun setupConfigurationFromCompatibleContext(runConfig: JasmineRunConfiguration, context: ConfigurationContext, sourceElement: Ref<PsiElement>): Boolean {
val element = context.psiLocation ?: return false
val currentFile = PsiUtilCore.getVirtualFile(element) ?: return false
if (currentFile.isDirectory) {
// not sure the right way to run a dir
return false
}

val psiFile = tryCast(element.containingFile, JSFile::class.java) ?: return false

if (element is PsiFileSystemItem) {
psiFile.testFileType ?: return false

runConfig.jasmineRunSettings = runConfig.jasmineRunSettings.copy(
specFile = currentFile.path
)
sourceElement.set(psiFile)
runConfig.setGeneratedName()
} else return false


return true
}
}
30 changes: 26 additions & 4 deletions src/io/pivotal/intellij/jasmine/JasmineRunProfileState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.intellij.execution.ui.ConsoleView
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.util.PathUtil
import com.intellij.util.execution.ParametersListUtil
import org.apache.commons.lang.StringUtils
import java.io.File
import java.nio.file.Paths

Expand All @@ -23,16 +25,36 @@ class JasmineRunProfileState(private var project: Project,
environment: ExecutionEnvironment) : CommandLineState(environment) {

override fun startProcess(): ProcessHandler {
val interpreter = runConfig.nodeJs.resolveAsLocal(project)
val runSettings = runConfig.jasmineRunSettings
val interpreter = runSettings.nodeJs.resolveAsLocal(project)
val commandLine = GeneralCommandLine()
commandLine.workDirectory = File(project.baseDir.path)

if (StringUtils.isBlank(runSettings.workingDir)) {
commandLine.withWorkDirectory(project.baseDir.path)
} else {
commandLine.withWorkDirectory(runSettings.workingDir)
}

commandLine.exePath = interpreter.interpreterSystemDependentPath

runSettings.envData.configureCommandLine(commandLine, true)

val nodeOptionsList = ParametersListUtil.parse(runSettings.nodeOptions.trim())
commandLine.addParameters(nodeOptionsList)

commandLine.addParameter(jasminePath(runConfig))

val jasmineOptionsList = ParametersListUtil.parse(runSettings.extraJasmineOptions.trim())
commandLine.addParameters(jasmineOptionsList)

if (!StringUtils.isBlank(runSettings.jasmineConfigFile)) {
commandLine.addParameter("--config=${runSettings.jasmineConfigFile}")
}

commandLine.addParameter("--reporter=${findReporterPath()}")
if (runConfig.envData != null) {
commandLine.environment.putAll(runConfig.envData!!.envs)

if (!StringUtils.isBlank(runSettings.specFile)) {
commandLine.addParameter(runSettings.specFile)
}

val processHandler = KillableColoredProcessHandler(commandLine)
Expand Down
6 changes: 2 additions & 4 deletions src/io/pivotal/intellij/jasmine/JasmineRunProgramRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.intellij.execution.configurations.RunProfile
import com.intellij.execution.configurations.RunProfileState
import com.intellij.execution.configurations.RunnerSettings
import com.intellij.execution.executors.DefaultRunExecutor
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.execution.runners.GenericProgramRunner
import com.intellij.execution.runners.RerunTestsAction
import com.intellij.execution.runners.RunContentBuilder
import com.intellij.execution.runners.*
import com.intellij.execution.ui.RunContentDescriptor
import com.intellij.openapi.fileEditor.FileDocumentManager

Expand All @@ -24,6 +21,7 @@ object JasmineRunProgramRunner : GenericProgramRunner<RunnerSettings>() {
val builder = RunContentBuilder(result, environment)
val descriptor = builder.showRunContent(environment.contentToReuse)

RerunTestsNotification.showRerunNotification(environment.contentToReuse, result.executionConsole)
RerunTestsAction.register(descriptor)
return descriptor
}
Expand Down
14 changes: 14 additions & 0 deletions src/io/pivotal/intellij/jasmine/JasmineRunSettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.pivotal.intellij.jasmine

import com.intellij.execution.configuration.EnvironmentVariablesData
import com.intellij.javascript.nodejs.interpreter.NodeJsInterpreterRef

data class JasmineRunSettings(
var nodeJs : NodeJsInterpreterRef = NodeJsInterpreterRef.createProjectRef(),
var nodeOptions : String = "",
var workingDir : String = "",
var envData : EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT,
var extraJasmineOptions : String = "",
var jasmineConfigFile : String = "",
var specFile : String = ""
)

0 comments on commit eb08975

Please sign in to comment.