-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLUGIN-0 | Added file command support for maestro commands
- Loading branch information
burak.ozturk1
committed
Aug 6, 2024
1 parent
2797298
commit a48eeee
Showing
4 changed files
with
110 additions
and
7 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/com/github/burkclik/asplugin/actions/FileLevelDynamicActionGroup.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,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 | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/com/github/burkclik/asplugin/actions/FileTaskAction.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,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() } | ||
} | ||
} |
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