Skip to content

Commit

Permalink
Merge pull request #1223 from KronicDeth/1218
Browse files Browse the repository at this point in the history
Convert mix and mix test run configurations to new format
  • Loading branch information
KronicDeth authored Aug 17, 2018
2 parents 88733a7 + 2ae4d42 commit 5b4b01b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
3 changes: 3 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<fileTypeFactory implementation="org.elixir_lang.ElixirFileTypeFactory"/>
<filePropertyPusher implementation="org.elixir_lang.file.LevelPropertyPusher"/>

<!-- Run Configurations -->
<project.converterProvider implementation="org.elixir_lang.run.conversion.Provider"/>

<!-- Facet -->
<applicationConfigurable id="language.elixir.sdks.elixir"
parentId="language.elixir"
Expand Down
2 changes: 1 addition & 1 deletion src/org/elixir_lang/exunit/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@ class Configuration(name: String, project: Project) :
}
}

private const val MIX_TEST = "mix-test"
const val MIX_TEST = "mix-test"
13 changes: 13 additions & 0 deletions src/org/elixir_lang/run/conversion/Converter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.elixir_lang.run.conversion

import com.intellij.conversion.ConversionProcessor
import com.intellij.conversion.ProjectConverter
import com.intellij.conversion.RunManagerSettings

class Converter : ProjectConverter() {
override fun createRunConfigurationsConverter(): ConversionProcessor<RunManagerSettings> =
RunConfiguration()

// All conversion done by [createRunConfigurationsConverter]
override fun isConversionNeeded(): Boolean = false
}
17 changes: 17 additions & 0 deletions src/org/elixir_lang/run/conversion/Provider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.elixir_lang.run.conversion

import com.intellij.conversion.ConversionContext
import com.intellij.conversion.ConverterProvider
import com.intellij.conversion.ProjectConverter

class Provider : ConverterProvider("elixir-v8.0.0-run-configurations") {
override fun getConversionDescription(): String =
"""
Mix and Mix ExUnit run configurations now allow more command line options to be set.
Mix Program Arguments is renamed to "`mix` arguments" and gains "`elixir` arguments" and "`erl` arguments".
Mix ExUnit Programs Arguments is renamed to "`mix test` arguments" and gains "`elixir` arguments" and "`erl` arguments".
""".trimIndent()

override fun createConverter(conversionContext: ConversionContext): ProjectConverter = Converter()
}
67 changes: 67 additions & 0 deletions src/org/elixir_lang/run/conversion/RunConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.elixir_lang.run.conversion

import com.intellij.conversion.ConversionProcessor
import com.intellij.conversion.RunManagerSettings
import org.elixir_lang.exunit.MIX_TEST
import org.elixir_lang.run.*
import org.jdom.Element

class RunConfiguration : ConversionProcessor<RunManagerSettings>() {
override fun isConversionNeeded(settings: RunManagerSettings): Boolean =
settings.runConfigurations.conversionNeeded().isNotEmpty()

override fun process(settings: RunManagerSettings) {
settings.runConfigurations.conversionNeeded().process()
}
}

private val TYPES = arrayOf("MixRunConfigurationType", "MixExUnitRunConfigurationType")
private val OLD_OPTION_NAMES = arrayOf("programParameters", "workingDirectory")

private fun Element.getOption(name: String): Element? = getChildren("option").first { it.getAttributeValue("name") == name }
private fun Element.hasAnyOption(names: Array<String>): Boolean = getChildren("option").any { it.getAttributeValue("name") in names }
private fun Element.hasOldOptions(): Boolean = hasAnyOption(OLD_OPTION_NAMES)
private fun Element.correctType(): Boolean = getAttributeValue("type") in TYPES
private fun Collection<Element>.conversionNeeded(): Collection<Element> = filter(Element::correctType).filter(Element::hasOldOptions)
private fun Collection<Element>.process() = forEach(Element::process)

private fun Element.process() {
processProgramParametersOption()
processWorkingDirectoryOption()
putNewModuleFilters()
}

fun Element.processProgramParametersOption() {
getOption("programParameters")?.let { programParametersOption ->
val command = when {
getAttributeValue("type") == "MixRunConfigurationType" -> MIX
getAttributeValue("type") == "MixExUnitRunConfigurationType" -> MIX_TEST
else -> null
}

if (command != null) {
val programParameters = programParametersOption.getAttributeValue("value")

val argumentList = mutableListOf<String>()
argumentList.fromArguments(programParameters)

writeExternalArgumentList(command, argumentList)
removeContent(programParametersOption)
}
}
}

fun Element.processWorkingDirectoryOption() {
getOption("workingDirectory")?.let { workingDirectoryOption ->
val oldValue = workingDirectoryOption.getAttributeValue("value")
writeExternalWorkingDirectory("file://$oldValue")
removeContent(workingDirectoryOption)
}
}

fun Element.putNewModuleFilters() {
if (getChild("module-filters") == null) {
writeModuleFilters(emptyList(), true)
}
}

0 comments on commit 5b4b01b

Please sign in to comment.