Skip to content

Commit

Permalink
Improve shell runner & others
Browse files Browse the repository at this point in the history
  • Loading branch information
WarningImHack3r committed Jun 20, 2024
1 parent f362269 commit cc28f83
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand All @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ class ShellRunner(private val project: Project? = null) {
private val log = logger<ShellRunner>()
}

private val failedCommands = mutableSetOf<String>()
private val failedWindowsPrograms = mutableSetOf<String>()

private fun isWindows() = System.getProperty("os.name").lowercase().contains("win")

fun execute(command: Array<String>): 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()) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ abstract class Source<C : Config>(val project: Project, private val serializer:
}

// Public methods
open fun fetchAllComponents(): List<Component> {
fun fetchAllComponents(): List<Component> {
return RequestSender.sendRequest("$domain/registry/index.json").ok {
Json.decodeFromString<List<Component>>(it.body)
}?.also {
Expand All @@ -162,7 +162,7 @@ abstract class Source<C : Config>(val project: Project, private val serializer:
}
}

open fun getInstalledComponents(): List<String> {
fun getInstalledComponents(): List<String> {
return FileManager(project).getFileAtPath(
"${resolveAlias(getLocalPathForComponents())}/ui"
)?.children?.map { file ->
Expand All @@ -174,7 +174,7 @@ abstract class Source<C : Config>(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)
Expand Down Expand Up @@ -275,7 +275,7 @@ abstract class Source<C : Config>(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(":")}${
Expand Down

0 comments on commit cc28f83

Please sign in to comment.