-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add settings page to allow configuration of rpm executable path
Currently if the path is changed the IDE needs to be restarted to pick up the changes.
- Loading branch information
Showing
5 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/kotlin/com/carbonblack/intellij/rpmspec/RpmSpecSettingsConfigurable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.carbonblack.intellij.rpmspec | ||
|
||
import com.carbonblack.intellij.rpmmacro.RpmMacroUtil | ||
import com.intellij.openapi.options.Configurable | ||
import com.intellij.ui.IdeBorderFactory | ||
import com.intellij.ui.components.JBList | ||
import com.intellij.ui.components.JBScrollPane | ||
import com.intellij.ui.components.JBTextField | ||
import com.intellij.util.ui.FormBuilder | ||
import java.awt.BorderLayout | ||
import java.awt.Insets | ||
import javax.swing.* | ||
import kotlin.reflect.KProperty | ||
|
||
private class TextFieldDelegate(private val textField: JBTextField) { | ||
operator fun getValue(thisRef: Any?, property: KProperty<*>): String { | ||
return textField.text | ||
} | ||
|
||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { | ||
textField.text = value | ||
} | ||
} | ||
|
||
class RpmSpecSettingsConfigurable : Configurable { | ||
override fun getDisplayName() = "RPM SPEC File" | ||
override fun getPreferredFocusedComponent(): JComponent = rpmCommandPathBox | ||
|
||
private val rpmCommandPathBox = JBTextField("rpm") | ||
private var rpmCommandPath: String by TextFieldDelegate(rpmCommandPathBox) | ||
|
||
private val detectedPathsList = JBList<String>().apply { | ||
selectionMode = ListSelectionModel.SINGLE_SELECTION | ||
border = IdeBorderFactory.createEmptyBorder(Insets(0, 3, 0, 0)) | ||
isFocusable = false | ||
} | ||
|
||
override fun createComponent(): JComponent { | ||
val detectedMacrosPanel = JPanel(BorderLayout()).apply { | ||
// This is for if we decide to make the list of macros editable | ||
/* val toolbarDecorator = ToolbarDecorator.createDecorator(detectedPathsList) | ||
.setScrollPaneBorder(JBUI.Borders.empty()) | ||
.setPanelBorder(JBUI.Borders.customLine(JBColor.border(), 0, 1, 0, 1)) | ||
add(toolbarDecorator.createPanel(), BorderLayout.NORTH) */ | ||
add(JBScrollPane(detectedPathsList), BorderLayout.CENTER) | ||
border = IdeBorderFactory.createTitledBorder("Detected Macro Files:", false).setShowLine(false) | ||
} | ||
|
||
return FormBuilder.createFormBuilder() | ||
.addLabeledComponent("Path to rpm executable:", rpmCommandPathBox) | ||
.addComponentFillVertically(detectedMacrosPanel, 12) | ||
.panel | ||
} | ||
|
||
override fun isModified(): Boolean { | ||
return rpmCommandPath != RpmSpecSettingsState.instance.rpmCommandPath | ||
} | ||
|
||
override fun apply() { | ||
RpmSpecSettingsState.instance.rpmCommandPath = rpmCommandPath | ||
} | ||
|
||
override fun reset() { | ||
rpmCommandPath = RpmSpecSettingsState.instance.rpmCommandPath | ||
detectedPathsList.setListData(RpmMacroUtil.macroPathFiles.mapNotNull { it.canonicalPath }.toTypedArray()) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/carbonblack/intellij/rpmspec/RpmSpecSettingsState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.carbonblack.intellij.rpmspec | ||
|
||
import com.carbonblack.intellij.rpmmacro.RpmMacroUtil | ||
import com.intellij.openapi.components.* | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.util.io.exists | ||
import com.intellij.util.xmlb.XmlSerializerUtil | ||
import java.io.File | ||
import java.nio.file.Paths | ||
|
||
private val log = Logger.getInstance(RpmMacroUtil::class.java) | ||
|
||
@State(name = "RpmSpecSettings", storages = [Storage("rpmSpec.xml")]) | ||
class RpmSpecSettingsState : PersistentStateComponent<RpmSpecSettingsState> { | ||
var rpmCommandPath = findRpmPath() ?: "rpm" | ||
|
||
override fun getState(): RpmSpecSettingsState = this | ||
override fun loadState(state: RpmSpecSettingsState) { | ||
XmlSerializerUtil.copyBean(state, this) | ||
} | ||
|
||
companion object { | ||
val instance: RpmSpecSettingsState | ||
get() = ServiceManager.getService(RpmSpecSettingsState::class.java) | ||
|
||
private fun findRpmPath(): String? { | ||
return try { | ||
System.getenv().getOrDefault("PATH", "") | ||
.split(File.pathSeparator) | ||
.map { Paths.get(it).resolve("rpm") } | ||
.find { it.exists() } | ||
?.toAbsolutePath() | ||
?.toString() | ||
} catch (e: Exception) { | ||
log.warn("Error trying to find rpm executable.", e) | ||
null | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters