essential-actions
Clone or download
Modified Files
M
runexecutableaction/src/main/java/com/stream_pi/runexecutableaction/RunExecutableAction.java
+21
−1
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.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.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", "*.*")
);
);
addClientProperties(executablePathProperty);
addClientProperties(executablePathProperty);
if(getServerConnection().getPlatform() == Platform.WINDOWS)
{
BooleanProperty runAsAdminProperty = new BooleanProperty("run_as_admin");
runAsAdminProperty.setDisplayName("Run as Administrator");
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();
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
{
{
ProcessBuilder processBuilder = new ProcessBuilder();
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(executableFile.getParentFile());
processBuilder.directory(executableFile.getParentFile());
processBuilder.command(executableLocation);
String command = executableLocation;
if(getServerConnection().getPlatform()==Platform.WINDOWS)
{
boolean runAsAdmin = getClientProperties().getSingleProperty("run_as_admin").getBoolValue();
if(runAsAdmin)
{
command = "powershell -Command \"Start-Process '"+executableLocation+"' -Verb runAs\"";
}
}
processBuilder.command(command);
processBuilder.start();
processBuilder.start();
}
}
catch (IOException e)
catch (IOException e)
{
{
throw new MinorException(e.getMessage());
throw new MinorException(e.getMessage());
}
}
}
}
}
}