Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic actions #4

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import org.jetbrains.changelog.markdownToHTML
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

fun properties(key: String) = project.findProperty(key).toString()

plugins {
Expand Down Expand Up @@ -55,16 +53,7 @@ qodana {
}

tasks {
// Set the JVM compatibility versions
properties("javaVersion").let {
withType<JavaCompile> {
sourceCompatibility = it
targetCompatibility = it
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = it
}
}


runIde {
ideDir.set(file("/Applications/Android Studio.app/Contents"))
Expand Down Expand Up @@ -129,7 +118,4 @@ tasks {
channels.set(listOf(properties("pluginVersion").split('-').getOrElse(1) { "default" }.split('.').first()))
}
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
kotlin.jvmToolchain(11)
kotlin.jvmToolchain(11)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.burkclik.asplugin.actions

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import org.jetbrains.plugins.terminal.TerminalView
import java.io.IOException

class GradleTaskAction constructor(
val tabName: String,
taskName: String,
val gradleTerminalCommand: String,
): AnAction(taskName) {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val terminalView = TerminalView.getInstance(project)
try {
val terminalWidget = terminalView.createLocalShellWidget(e.project?.basePath, tabName)
terminalWidget.executeCommand(gradleTerminalCommand)
} catch (err: IOException) {
err.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.burkclik.asplugin.actions

import com.github.burkclik.asplugin.util.ConfigFileReader
import com.github.burkclik.asplugin.util.getModuleName
import com.github.burkclik.asplugin.util.getModuleTerminalCommand
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 ModuleLevelDynamicActionGroup : 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 { createModuleLevelGradleTaskAction(event, it) }
}

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

private fun createModuleLevelGradleTaskAction(event: AnActionEvent, task: String): GradleTaskAction {
val path: String = event.getData(CommonDataKeys.VIRTUAL_FILE)?.path.orEmpty()
val rootPath: String = event.getData(CommonDataKeys.PROJECT)?.basePath.orEmpty().split("/").last()
val gradleTerminalCommand = getModuleTerminalCommand(rootPath = rootPath, modulePath = path, gradleTaskName = task)
val moduleName = getModuleName(rootPath, path)
return GradleTaskAction(
tabName = moduleName,
taskName = task,
gradleTerminalCommand = gradleTerminalCommand
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.project.Project
import com.intellij.util.containers.map2Array

class RootLevelDynamicActionGroup : ActionGroup() {

override fun getChildren(e: AnActionEvent?): Array<AnAction> {
val project = e?.project ?: return emptyArray()
return runCatching { readActions(project) }
.onFailure { it.printStackTrace() }
.getOrNull()
.orEmpty()
.map2Array { taskName -> createRootGradleTaskAction(taskName) }
}

private fun createRootGradleTaskAction(taskName: String) = GradleTaskAction(
tabName = "root",
taskName = taskName,
gradleTerminalCommand = "./gradlew $taskName"
)

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


}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.burkclik.asplugin.util

import com.intellij.openapi.project.Project
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths

class ConfigFileReader constructor() {

fun readConfigFile(project: Project, path: String): List<String> {
val moduleTasksPath = getModuleTasksConfigAbsolutePath(
project,
path
)

return moduleTasksPath
.toFile()
.also { assertFileIsReadable(it) }
.readLines()
}

private fun getModuleTasksConfigAbsolutePath(project: Project, relativeConfigPath: String): Path {
return relativeConfigPath.replace('/', File.separatorChar)
.let { project.basePath?.let(Paths::get)?.resolve(it) ?: Paths.get(it) }
}

private fun assertFileIsReadable(it: File) {
require(it.canRead()) {
"Application couldn't read file on: ${it.path}. Make sure it exists and it's readable by application"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ fun getModuleTerminalCommand(
modulePath: String,
gradleTaskName: String
): String {
val moduleName = getModuleName(rootPath, modulePath)
return "./gradlew $moduleName:$gradleTaskName"
}

fun getModuleName(rootPath: String, modulePath: String): String {
val subString = modulePath.substringAfter(rootPath)
val formattedModulePath = subString
.split("/")
.plus(gradleTaskName)
.joinToString(":")
return "./gradlew $formattedModulePath"
return subString.split("/").joinToString(":") { it }
}
Loading