
com.github.tix320.nimble.api.shell.PowerShell Maven / Gradle / Ivy
The newest version!
package com.github.tix320.nimble.api.shell;
import java.io.*;
import java.util.concurrent.TimeoutException;
import com.github.tix320.nimble.internal.shell.Shell;
import com.github.tix320.nimble.internal.shell.stream.ShellFunctions;
/**
* @author : Tigran Sargsyan
* @since : 20.01.2021
**/
public final class PowerShell extends Shell {
private PowerShell() throws ShellNotAvailableException {
try {
executeCommand("$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8");
} catch (TimeoutException | IOException e) {
throw new ShellNotAvailableException("Unknown error", e);
}
}
public static PowerShell openSession() throws ShellNotAvailableException {
return new PowerShell();
}
@Override
protected ProcessBuilder builder() {
return new ProcessBuilder("powershell.exe", "-ExecutionPolicy", "Bypass", "-NoExit", "-NoProfile", "-Command",
"-");
}
@Override
protected ShellFunctions streams(Process process) {
PrintWriter commandWriter = new PrintWriter(
new OutputStreamWriter(new BufferedOutputStream(process.getOutputStream())), true);
BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
return new ShellFunctions(commandWriter::println, outputReader::readLine, errorReader::readLine);
}
@Override
protected String locationCommand() {
return "Get-Location | Foreach-Object { $_.Path }";
}
@Override
protected String echoToOutStream(String value) {
return "echo " + value;
}
@Override
protected String echoToErrorStream(String value) {
return "$host.ui.WriteErrorLine('" + value + "')";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy