essential-actions
Clone or download
Modified Files
M
runexecutableaction/src/main/java/com/stream_pi/runexecutableaction/RunExecutableAction.java
+2
−2
package com.stream_pi.runexecutableaction;
package com.stream_pi.runexecutableaction;
import com.stream_pi.action_api.actionproperty.property.*;
import com.stream_pi.action_api.actionproperty.property.*;
import com.stream_pi.action_api.externalplugin.NormalAction;
import com.stream_pi.action_api.externalplugin.NormalAction;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Arrays;
import java.util.logging.Logger;
import java.util.logging.Logger;
public class RunExecutableAction extends NormalAction
public class RunExecutableAction extends NormalAction
{
{
public RunExecutableAction()
public RunExecutableAction()
{
{
setName("Run Executable");
setName("Run Executable");
setCategory("Essentials");
setCategory("Essentials");
setAuthor("rnayabed");
setAuthor("rnayabed");
setServerButtonGraphic("fas-terminal");
setServerButtonGraphic("fas-terminal");
setHelpLink("https://github.com/stream-pi/essentialactions");
setHelpLink("https://github.com/stream-pi/essentialactions");
setVersion(new Version(1,0,0));
setVersion(new Version(1,0,0));
}
}
@Override
@Override
public void initProperties() throws MinorException
public void initProperties() throws MinorException
{
{
StringProperty executablePathProperty = new StringProperty("executable_location");
StringProperty executablePathProperty = new StringProperty("executable_location");
executablePathProperty.setDisplayName("Executable Location");
executablePathProperty.setDisplayName("Executable Location");
executablePathProperty.setControlType(ControlType.FILE_PATH);
executablePathProperty.setControlType(ControlType.FILE_PATH);
executablePathProperty.setExtensionFilters(
executablePathProperty.setExtensionFilters(
new FileExtensionFilter("Executable", "*.*")
new FileExtensionFilter("Executable", "*.*")
);
);
StringProperty argumentsProperty = new StringProperty("executable_arguments");
StringProperty argumentsProperty = new StringProperty("executable_arguments");
argumentsProperty.setDisplayName("Arguments");
argumentsProperty.setDisplayName("Arguments");
addClientProperties(executablePathProperty, argumentsProperty);
addClientProperties(executablePathProperty, argumentsProperty);
if(getServerConnection().getPlatform() == Platform.WINDOWS)
if(getServerConnection().getPlatform() == Platform.WINDOWS)
{
{
BooleanProperty runAsAdminProperty = new BooleanProperty("run_as_admin");
BooleanProperty runAsAdminProperty = new BooleanProperty("run_as_admin");
runAsAdminProperty.setDisplayName("Run as Administrator");
runAsAdminProperty.setDisplayName("Run as Administrator");
addClientProperties(runAsAdminProperty);
addClientProperties(runAsAdminProperty);
}
}
}
}
@Override
@Override
public void onActionClicked() throws MinorException
public void onActionClicked() throws MinorException
{
{
String executableLocation = getClientProperties().getSingleProperty("executable_location").getStringValue();
String executableLocation = getClientProperties().getSingleProperty("executable_location").getStringValue();
String arguments = getClientProperties().getSingleProperty("executable_arguments").getStringValue();
String arguments = getClientProperties().getSingleProperty("executable_arguments").getStringValue();
if(executableLocation.isBlank())
if(executableLocation.isBlank())
{
{
throw new MinorException("Executable File location is empty");
throw new MinorException("Executable File location is empty");
}
}
File executableFile = new File(executableLocation);
File executableFile = new File(executableLocation);
if(!executableFile.exists())
if(!executableFile.exists())
{
{
throw new MinorException("The File at given path '"+executableLocation+"' does not exist.");
throw new MinorException("The File at given path '"+executableLocation+"' does not exist.");
}
}
try
try
{
{
if(getServerConnection().getPlatform() == Platform.WINDOWS)
if(getServerConnection().getPlatform() == Platform.WINDOWS)
{
{
String command = "powershell -Command \"cd "+executableFile.getParentFile().toString()+
String command = "powershell -Command \"cd '"+executableFile.getParentFile().toString()+
"; Start-Process -FilePath '"+executableLocation+"' -ArgumentList '"+arguments+"' ";
"'; Start-Process '"+executableLocation+"' -ArgumentList '"+arguments+"' ";
getLogger().info("Full command : "+ command);
getLogger().info("Full command : "+ command);
boolean runAsAdmin = getClientProperties().getSingleProperty("run_as_admin").getBoolValue();
boolean runAsAdmin = getClientProperties().getSingleProperty("run_as_admin").getBoolValue();
if(runAsAdmin)
if(runAsAdmin)
{
{
command +="-Verb runAs\"";
command +="-Verb runAs\"";
}
}
else
else
{
{
command +="\"";
command +="\"";
}
}
getLogger().info("Running command : "+command);
getLogger().info("Running command : "+command);
runCommand(command);
runCommand(command);
}
}
else
else
{
{
ProcessBuilder processBuilder = new ProcessBuilder();
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(executableFile.getParentFile());
processBuilder.directory(executableFile.getParentFile());
String[] args = arguments.split(" ");
String[] args = arguments.split(" ");
String[] finalArray = new String[args.length + 1];
String[] finalArray = new String[args.length + 1];
finalArray[0] = executableLocation;
finalArray[0] = executableLocation;
System.arraycopy(args, 0, finalArray, 1, args.length);
System.arraycopy(args, 0, finalArray, 1, args.length);
getLogger().info("Full array : "+ Arrays.toString(finalArray));
getLogger().info("Full array : "+ Arrays.toString(finalArray));
processBuilder.command(finalArray);
processBuilder.command(finalArray);
processBuilder.start();
processBuilder.start();
}
}
}
}
catch (IOException e)
catch (IOException e)
{
{
throw new MinorException(e.getMessage());
throw new MinorException(e.getMessage());
}
}
}
}
private void runCommand(String command) throws IOException
private void runCommand(String command) throws IOException
{
{
Runtime rt = Runtime.getRuntime();
Runtime rt = Runtime.getRuntime();
rt.exec(command);
rt.exec(command);
}
}
}
}