From cc28f83856de827e26bebf609aa9a3e96b97bfca Mon Sep 17 00:00:00 2001 From: WarningImHack3r <43064022+WarningImHack3r@users.noreply.github.com> Date: Fri, 21 Jun 2024 00:05:32 +0200 Subject: [PATCH] Improve shell runner & others --- .../backend/helpers/DependencyManager.kt | 6 ++--- .../backend/helpers/ShellRunner.kt | 27 +++++++++++-------- .../backend/sources/Source.kt | 8 +++--- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt index 9492b1e..44fd4bc 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt @@ -34,9 +34,8 @@ class DependencyManager(private val project: Project) { if (installationType == InstallationType.DEV) "-D" else null, *dependencyNames.toTypedArray() ).toTypedArray() - val res = ShellRunner(project).execute(command) // check if the installation was successful - if (res == null) { + if (ShellRunner(project).execute(command) == null) { NotificationManager(project).sendNotification( "Failed to install dependencies", "Failed to install dependencies: ${dependencyNames.joinToString { ", " }} (${command.joinToString(" ")}). Please install it manually.", @@ -54,9 +53,8 @@ class DependencyManager(private val project: Project) { "remove", *dependencyNames.toTypedArray() ).toTypedArray() - val res = ShellRunner(project).execute(command) // check if the uninstallation was successful - if (res == null) { + if (ShellRunner(project).execute(command) == null) { NotificationManager(project).sendNotification( "Failed to uninstall dependencies", "Failed to uninstall dependencies (${command.joinToString(" ")}). Please uninstall them manually.", diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/ShellRunner.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/ShellRunner.kt index 03d61fa..4337538 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/ShellRunner.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/ShellRunner.kt @@ -9,16 +9,17 @@ class ShellRunner(private val project: Project? = null) { private val log = logger() } - private val failedCommands = mutableSetOf() + private val failedWindowsPrograms = mutableSetOf() private fun isWindows() = System.getProperty("os.name").lowercase().contains("win") fun execute(command: Array): String? { - val commandName = command.firstOrNull() ?: return null.also { + val program = command.firstOrNull() ?: return null.also { log.warn("No command name provided") } - if (isWindows() && failedCommands.contains(commandName)) { - command[0] = "$commandName.cmd" + if (isWindows() && failedWindowsPrograms.contains(program)) { + command[0] = "$program.cmd" + log.warn("(Re)trying command with .cmd extension: \"${command.joinToString(" ")}\"") } return try { val platformCommand = if (isWindows()) { @@ -28,21 +29,25 @@ class ShellRunner(private val project: Project? = null) { } + command log.debug("Executing command: \"${platformCommand.joinToString(" ")}\"") val process = ProcessBuilder(*platformCommand) - .redirectOutput(ProcessBuilder.Redirect.PIPE) .directory(project?.basePath?.let { File(it) }) .start() process.waitFor() - process.inputStream.bufferedReader().readText().also { - log.debug("Successfully executed \"${platformCommand.joinToString(" ")}\": $it") + val output = process.inputStream?.bufferedReader()?.readText()?.also { + log.debug("Successfully executed \"${platformCommand.joinToString(" ")}\" with output:\n$it") } + val error = process.errorStream?.bufferedReader()?.readText() + if (output.isNullOrBlank() && !error.isNullOrBlank()) { + log.warn("Error while executing \"${platformCommand.joinToString(" ")}\":\n${error.take(150)}") + } + output } catch (e: Exception) { - if (isWindows() && !commandName.endsWith(".cmd")) { + if (isWindows() && !program.endsWith(".cmd")) { log.warn( - "Failed to execute \"${command.joinToString(" ")}\". Trying to execute \"$commandName.cmd\" instead", + "Failed to execute \"${command.joinToString(" ")}\". Trying to execute \"$program.cmd\" instead", e ) - failedCommands.add(commandName) - return execute(arrayOf("$commandName.cmd") + command.drop(1).toTypedArray()) + failedWindowsPrograms.add(program) + return execute(arrayOf("$program.cmd") + command.drop(1).toTypedArray()) } log.warn("Error while executing \"${command.joinToString(" ")}\"", e) null diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt index 51640f7..53672ad 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt @@ -152,7 +152,7 @@ abstract class Source(val project: Project, private val serializer: } // Public methods - open fun fetchAllComponents(): List { + fun fetchAllComponents(): List { return RequestSender.sendRequest("$domain/registry/index.json").ok { Json.decodeFromString>(it.body) }?.also { @@ -162,7 +162,7 @@ abstract class Source(val project: Project, private val serializer: } } - open fun getInstalledComponents(): List { + fun getInstalledComponents(): List { return FileManager(project).getFileAtPath( "${resolveAlias(getLocalPathForComponents())}/ui" )?.children?.map { file -> @@ -174,7 +174,7 @@ abstract class Source(val project: Project, private val serializer: } } - open fun addComponent(componentName: String) { + fun addComponent(componentName: String) { val componentsPath = resolveAlias(getLocalPathForComponents()) // Install component val component = fetchComponent(componentName) @@ -275,7 +275,7 @@ abstract class Source(val project: Project, private val serializer: } } - open fun isComponentUpToDate(componentName: String): Boolean { + fun isComponentUpToDate(componentName: String): Boolean { val remoteComponent = fetchComponent(componentName) val componentPath = "${resolveAlias(getLocalPathForComponents())}/${remoteComponent.type.substringAfterLast(":")}${