Skip to content

Commit

Permalink
Initial Private Beta
Browse files Browse the repository at this point in the history
  • Loading branch information
pronskiy committed Mar 22, 2024
1 parent 19b9431 commit a77d731
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 187 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- Plugin description -->
This plugin adds support for using Pest PHP inside PhpStorm.
This plugin adds debugging tool window for Trap inside PhpStorm.
<!-- Plugin description end -->
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.intellij.plugins.phpstorm_dd

/**
* Constants for notifications.
*/
object NotificationConstants {
const val NOTIFICATION_GROUP_ID = "TrapPlugin"
const val NOTIFICATION_TITLE = "Trap plugin"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.intellij.plugins.phpstorm_dd

/**
* Constants for preferences.
*/
object Preferences {
const val HTTP_PORT_DEFAULT = 8888
const val HTTP_PORT_KEY = "com.intellij.plugins.phpstorm_dd.http_port"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextField

/**
* Preferences component.
*/
class PreferencesDialog(
project: Project?
) : DialogWrapper(project) {
Expand All @@ -22,7 +19,7 @@ class PreferencesDialog(

override fun createCenterPanel(): JComponent? {
val panel = JPanel(FlowLayout())
panel.add(JLabel("Http Port: "))
panel.add(JLabel("Port: "))
val propertiesComponent = PropertiesComponent.getInstance()
val httpPort = propertiesComponent.getInt(Preferences.HTTP_PORT_KEY, Preferences.HTTP_PORT_DEFAULT)
portTextField = JTextField(httpPort.toString(), 6)
Expand All @@ -49,6 +46,6 @@ class PreferencesDialog(

init {
init()
title = "Proxy Preferences"
title = "Preferences"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
import kotlinx.coroutines.isActive
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class StartTrapServerAction()
: AnAction("Start Server", "", AllIcons.Actions.Execute) {

override fun actionPerformed(event: AnActionEvent) {

event.presentation.isEnabled = false

val project = event.dataContext.getData(CommonDataKeys.PROJECT)

val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
Expand All @@ -24,12 +28,19 @@ class StartTrapServerAction()
}

val trapServerService = event.project!!.getService(TrapServerService::class.java)
trapServerService.startTrapServer(interpretersManager.interpreters[1])
CoroutineScope(Dispatchers.IO).launch {
trapServerService.startTrapServer(interpretersManager.interpreters[1])
}
}

override fun update(event: AnActionEvent) {
val trapServerService = event.project!!.getService(TrapServerService::class.java)
event.presentation.isEnabled = trapServerService.trapDaemon?.isActive == null || trapServerService.trapDaemon?.isActive == false
val oldValue = event.presentation.isEnabled
event.presentation.isEnabled = trapServerService.trapDaemon == null || trapServerService.trapDaemon?.isAlive == false

if (oldValue != event.presentation.isEnabled) {
trapServerService.statusChanged()
}
}

override fun getActionUpdateThread(): ActionUpdateThread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import com.intellij.notification.Notifications
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


class StopTrapServerAction : AnAction("Stop Server", "", AllIcons.Actions.Suspend) {
class StopTrapServerAction()
: AnAction("Stop Server", "", AllIcons.Actions.Suspend) {

override fun actionPerformed(event: AnActionEvent) {
notifyProxyShutdown()
event.presentation.isEnabled = false
// notifyProxyShutdown()
val trapServerService = event.project!!.getService(TrapServerService::class.java)
GlobalScope.launch {
trapServerService.stopTrapServer()
}

trapServerService.stopTrapServer()
}

override fun update(event: AnActionEvent) {
val trapServerService = event.project!!.getService(TrapServerService::class.java)
event.presentation.isEnabled = trapServerService.trapDaemon?.isActive !== null && trapServerService.trapDaemon?.isActive == true
val oldValue = event.presentation.isEnabled
event.presentation.isEnabled = trapServerService.trapDaemon !== null && trapServerService.trapDaemon?.isAlive == true

if (oldValue != event.presentation.isEnabled) {
trapServerService.statusChanged()
}
}

private fun notifyProxyShutdown() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,82 +1,89 @@
package com.intellij.plugins.phpstorm_dd

import com.intellij.openapi.Disposable
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.externalSystem.service.ui.completion.collector.TextCompletionCollector.Companion.async
import com.intellij.openapi.project.Project
import com.intellij.platform.ijent.IjentProcessWatcher.Companion.launch
import com.intellij.ui.jcef.JBCefBrowser
import java.io.File
import com.jetbrains.php.config.interpreters.PhpInterpreter
import com.jetbrains.rd.generator.nova.PredefinedType
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import java.io.InputStream
import java.util.concurrent.TimeUnit

class TrapServerService(
private var project: Project?
) : Disposable {

var trapDaemon: Job? = null
var webview: JBCefBrowser? = null
var trapDaemon: Process? = null

override fun dispose() {
if (trapDaemon != null) {
trapDaemon!!.cancel()
trapDaemon!!.destroy()
trapDaemon = null
}
}

fun startTrapServer(interpreter: PhpInterpreter) {

// private var processChannel: Channel<Process?>? = null
private var processChannel = Channel<Process?>()

suspend fun startTrapServer(interpreter: PhpInterpreter) {

if (trapDaemon?.isActive == true) {
if (trapDaemon?.isAlive == true) {
return
}

val cmdString = "%s %s/vendor/bin/trap --ui".format(interpreter.pathToPhpExecutable, project?.basePath)
val cmdMap = cmdString.split(" ")
val workingDir = File(project?.basePath!!)

// trapDaemon = ProcessBuilder(cmdMap)
// .directory(workingDir)
// .redirectOutput(ProcessBuilder.Redirect.INHERIT)
// .redirectError(ProcessBuilder.Redirect.INHERIT)
// .start()


trapDaemon = CoroutineScope(Dispatchers.IO).launch {
CoroutineScope(Dispatchers.IO).launch {
executeCommand(cmdMap, workingDir)
}

trapDaemon = processChannel.receive()

println("here")
}

suspend fun stopTrapServer() {
fun stopTrapServer() {

if (trapDaemon == null || !trapDaemon!!.isActive) {
if (trapDaemon == null || !trapDaemon!!.isAlive) {
return
}

trapDaemon!!.cancel()
trapDaemon!!.join()

trapDaemon?.destroy()
}

fun statusChanged() {
if (trapDaemon == null || !trapDaemon!!.isAlive) {
webview?.loadHTML("Not Running");
} else {
webview?.loadURL("http://127.0.0.1:8000")
}
}

private suspend fun executeCommand(commandArgs: List<String>, workingDir: File): CoroutineScope = withContext(
Dispatchers.IO
) {
var process: Process? = null
runCatching {
val process = ProcessBuilder(commandArgs)
process = ProcessBuilder(commandArgs)
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()

processChannel.send(process)

val outputStream = async {
println("Context for output stream -> $coroutineContext -> Thread -> ${Thread.currentThread()}")
readStream(process.inputStream) }
readStream(process!!.inputStream) }
val errorStream = async {
println("Context for error stream -> $coroutineContext -> Thread -> ${Thread.currentThread()}")
readStream(process.errorStream)
readStream(process!!.errorStream)
}
println("Context for exit code -> $coroutineContext -> Thread -> ${Thread.currentThread()}")
val exitCode = process.waitFor()
val exitCode = process?.waitFor()

ProcessResult(
exitCode = exitCode,
exitCode = exitCode!!,
message = outputStream.await(),
errorMessage = errorStream.await()
)
Expand All @@ -92,14 +99,8 @@ class TrapServerService(
return@withContext this
}

private fun readStream(inputStream: InputStream) =
inputStream.bufferedReader().use { reader -> reader.readText() }

data class ProcessResult(val exitCode: Int, val message: String, val errorMessage: String)

companion object {
fun getInstance(project: Project): TrapServerService {
return ServiceManager.getService(project, TrapServerService::class.java)
}
}
private fun readStream(inputStream: InputStream) =
inputStream.bufferedReader().use { reader -> reader.readText() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class TrapToolWindowFactory : ToolWindowFactory {
vertical = false,
borderless = true
)
val trapServerService = project.getService(TrapServerService::class.java)
trapServerService.webview = trapToolWindowPanel.webview
val contentFactory = ContentFactory.SERVICE.getInstance()
val content = contentFactory.createContent(trapToolWindowPanel, "", false)
toolWindow.contentManager.addContent(content)
Expand Down
Loading

0 comments on commit a77d731

Please sign in to comment.