-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/scala/ru/dgolubets/sbt/npmassets/util/OsInfo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ru.dgolubets.sbt.npmassets.util | ||
|
||
import ru.dgolubets.sbt.npmassets.util.OsKind.OsKind | ||
|
||
case class OsInfo(kind: OsKind) | ||
|
||
object OsInfo { | ||
def current: OsInfo = { | ||
val os = sys.props("os.name").toLowerCase | ||
if(os.contains("windows")){ | ||
OsInfo(OsKind.Windows) | ||
} else { | ||
OsInfo(OsKind.Linux) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ru.dgolubets.sbt.npmassets.util | ||
|
||
object OsKind extends Enumeration { | ||
type OsKind = Value | ||
val Linux, Windows = Value | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/scala/ru/dgolubets/sbt/npmassets/util/ProcessUtil.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ru.dgolubets.sbt.npmassets.util | ||
|
||
import java.io.File | ||
|
||
import com.sun.jna.Pointer | ||
import com.sun.jna.platform.win32.{Kernel32, WinNT} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
object ProcessUtil { | ||
|
||
private def getProcessId(p: Process): Int = { | ||
p.getClass.getName match { | ||
case "java.lang.UNIXProcess" => | ||
val f = p.getClass.getDeclaredField("pid") | ||
try { | ||
f.setAccessible(true) | ||
f.getInt(p) | ||
} finally { | ||
f.setAccessible(false) | ||
} | ||
case "java.lang.Win32Process" | "java.lang.ProcessImpl" => | ||
val f = p.getClass.getDeclaredField("handle") | ||
try { | ||
f.setAccessible(true) | ||
val handle = f.getLong(p) | ||
val kernel = Kernel32.INSTANCE | ||
val hand = new WinNT.HANDLE() | ||
hand.setPointer(Pointer.createConstant(handle)) | ||
kernel.GetProcessId(hand) | ||
} finally { | ||
f.setAccessible(false) | ||
} | ||
case _ => | ||
-1 | ||
} | ||
} | ||
|
||
def exec(command: String, | ||
args: Seq[String] = Seq.empty, | ||
envVars: Map[String, String] = Map.empty, | ||
cwd: Option[File] = None): Process = { | ||
val commands = OsInfo.current.kind match { | ||
case OsKind.Linux => | ||
// start process with new session id to kill it all later | ||
"setsid" :: command :: args.toList | ||
case OsKind.Windows => | ||
"cmd" :: "/C" :: command :: args.toList | ||
} | ||
|
||
val processBuilder = new ProcessBuilder(commands.asJava) | ||
for (dir <- cwd) { | ||
processBuilder.directory(dir) | ||
} | ||
processBuilder.environment().putAll(envVars.asJava) | ||
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT) | ||
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT) | ||
processBuilder.start() | ||
} | ||
|
||
def kill(p: Process): Unit = { | ||
val pid = getProcessId(p) | ||
OsInfo.current.kind match { | ||
case OsKind.Linux => | ||
Runtime.getRuntime.exec(s"pkill -s $pid") | ||
case OsKind.Windows => | ||
Runtime.getRuntime.exec(s"taskkill /pid $pid /t /f") | ||
} | ||
} | ||
} |