Skip to content

Commit

Permalink
feat(run-code): add CLI execution support for Python, JavaScript, and…
Browse files Browse the repository at this point in the history
… Ruby files #24

The update introduces a new function `runInCli` in `FileRunService.kt` that allows Python, JavaScript, and Ruby files to be executed directly from the command line. This function is then utilized in `RunCodeProcessor.kt` to provide an alternative execution method when no file run service is found.
  • Loading branch information
phodal committed Jun 29, 2024
1 parent 1ae3647 commit 0619ecb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class RunCodeProcessor : PostProcessor {
) {
val fileRunService = FileRunService.provider(project, file)
if (fileRunService == null) {
FileRunService.runInCli(project, psiFile)?.let {
console?.print(it, NORMAL_OUTPUT)
return
}

console?.print("No run service found\n", ERROR_OUTPUT)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package com.phodal.shirecore.provider.shire
import com.intellij.execution.RunManager
import com.intellij.execution.RunnerAndConfigurationSettings
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.configurations.RunProfile
import com.intellij.execution.util.ExecUtil
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.phodal.shirecore.runner.RunServiceTask

interface FileRunService {
Expand Down Expand Up @@ -126,5 +129,23 @@ interface FileRunService {
it.createConfiguration(project, file) != null
}
}

fun runInCli(project: Project, psiFile: PsiFile): String? {
val commandLine = when (psiFile.language.displayName.lowercase()) {
"python" -> GeneralCommandLine("python", psiFile.virtualFile.path)
"javascript" -> GeneralCommandLine("node", psiFile.virtualFile.path)
"ruby" -> GeneralCommandLine("ruby", psiFile.virtualFile.path)
else -> return null
}

commandLine.setWorkDirectory(project.basePath)
return try {
val output = ExecUtil.execAndGetOutput(commandLine)
output.stdout
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
}

0 comments on commit 0619ecb

Please sign in to comment.