Skip to content

Commit

Permalink
PLUGIN-0 | Added file command support for maestro commands
Browse files Browse the repository at this point in the history
  • Loading branch information
burak.ozturk1 committed Aug 6, 2024
1 parent 2797298 commit a48eeee
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.burkclik.asplugin.actions

import com.github.burkclik.asplugin.util.ConfigFileReader
import com.intellij.openapi.actionSystem.ActionGroup
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.project.Project
import com.intellij.util.containers.map2Array

class FileLevelDynamicActionGroup : ActionGroup() {
override fun getChildren(e: AnActionEvent?): Array<AnAction> {
val event = e ?: return emptyArray()
val project = e.project ?: return emptyArray()
return runCatching { readActions(project) }
.onFailure { it.printStackTrace() }
.getOrNull()
.orEmpty()
.map2Array { task ->
val (taskName, command) = splitTaskName(task)
createFileLevelGradleTaskAction(event, taskName, command)
}
}

private fun readActions(project: Project): List<String> {
return ConfigFileReader()
.readConfigFile(project, "config/Commander/file-tasks.txt")
}

private fun createFileLevelGradleTaskAction(
event: AnActionEvent,
taskName: String,
command: String
): AnAction = FileTaskAction(
taskName = taskName,
command = command,
filePaths = event.getSelectedPaths()
)


private fun AnActionEvent.getSelectedPaths(): List<String> {
val basePath = project?.basePath.orEmpty()
return getData(CommonDataKeys.VIRTUAL_FILE_ARRAY)?.map {
it.path.removePrefix("$basePath/")
}.orEmpty()
}

private fun splitTaskName(task: String): Pair<String, String> = task
.split(" ", limit = MAX_SUBSTRING)
.run { first() to last() }

companion object {
private const val MAX_SUBSTRING = 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.burkclik.asplugin.actions

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import org.jetbrains.plugins.terminal.TerminalToolWindowManager

class FileTaskAction(
taskName: String,
private val command: String,
private val filePaths: List<String>
) : AnAction(taskName) {
override fun actionPerformed(e: AnActionEvent) {
e.project?.let { project ->
filePaths.forEach { filePath ->
runTerminal(
project = project,
tabName = filePath,
command = "$command $filePath"
)
}
}
}

private fun runTerminal(
project: Project,
tabName: String,
command: String,
) {
runCatching {
TerminalToolWindowManager
.getInstance(project)
.createLocalShellWidget(project.basePath, tabName)
.executeCommand(command)
}.onFailure { it.printStackTrace() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProjectSettingsConfigurable(private val project: Project) : Configurable,
private val commandName: JBTextField = JBTextField()
private val rootCheckbox: JBCheckBox = JBCheckBox()
private val moduleCheckbox: JBCheckBox = JBCheckBox()
private val fileCheckbox: JBCheckBox = JBCheckBox()

private var modified = false

Expand All @@ -44,13 +45,15 @@ class ProjectSettingsConfigurable(private val project: Project) : Configurable,
row("Command Type:") {
cell(rootCheckbox.apply { text = "Root" })
cell(moduleCheckbox.apply { text = "Module" })
cell(fileCheckbox.apply { text = "File" })
}.topGap(TopGap.MEDIUM)
}

return panel
}

override fun isModified(): Boolean = modified && (rootCheckbox.isSelected || moduleCheckbox.isSelected)
override fun isModified(): Boolean = modified && (
rootCheckbox.isSelected || moduleCheckbox.isSelected || fileCheckbox.isSelected)

override fun apply() {
writeToFile()
Expand All @@ -72,14 +75,17 @@ class ProjectSettingsConfigurable(private val project: Project) : Configurable,
}

private fun writeToFile() {
if (rootCheckbox.isSelected && moduleCheckbox.isSelected) {
writeFile("config/Commander/module-tasks.txt")
writeFile("config/Commander/root-level-tasks.txt")
} else if (rootCheckbox.isSelected) {
if (rootCheckbox.isSelected) {
writeFile("config/Commander/root-level-tasks.txt")
} else {
}

if (moduleCheckbox.isSelected) {
writeFile("config/Commander/module-tasks.txt")
}

if (fileCheckbox.isSelected) {
writeFile("config/Commander/file-tasks.txt")
}
}

private fun writeFile(path: String) {
Expand All @@ -88,6 +94,6 @@ class ProjectSettingsConfigurable(private val project: Project) : Configurable,
}

companion object {
private const val PLUGIN_NAME = "Commande"
private const val PLUGIN_NAME = "Commander"
}
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
popup="true">
>
</group>
<group
class="com.github.burkclik.asplugin.actions.FileLevelDynamicActionGroup"
text="File Tasks"
popup="true">
</group>
</group>

<group
Expand Down

0 comments on commit a48eeee

Please sign in to comment.