use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlRunUtil method getPerlCommandLine.
/**
* Builds non-patched perl command line (without patching by version manager)
*
* @return new perl command line or null if sdk is missing or corrupted
*/
@Nullable
public static PerlCommandLine getPerlCommandLine(@NotNull Project project, @Nullable Sdk perlSdk, @Nullable String localScriptPath, @NotNull List<String> perlParameters, @NotNull List<String> scriptParameters) {
if (perlSdk == null) {
perlSdk = PerlProjectManager.getSdk(project);
}
if (perlSdk == null) {
LOG.error("No sdk provided or available in project " + project);
return null;
}
String interpreterPath = PerlProjectManager.getInterpreterPath(perlSdk);
if (StringUtil.isEmpty(interpreterPath)) {
LOG.warn("Empty interpreter path in " + perlSdk + " while building command line for " + localScriptPath);
return null;
}
PerlCommandLine commandLine = new PerlCommandLine(perlSdk).withProject(project);
commandLine.setExePath(interpreterPath);
PerlHostData<?, ?> hostData = PerlHostData.notNullFrom(perlSdk);
for (VirtualFile libRoot : PerlProjectManager.getInstance(project).getModulesLibraryRoots()) {
commandLine.addParameter(PERL_I + hostData.getRemotePath(libRoot.getCanonicalPath()));
}
commandLine.addParameters(perlParameters);
if (StringUtil.isNotEmpty(localScriptPath)) {
String remoteScriptPath = hostData.getRemotePath(localScriptPath);
if (remoteScriptPath != null) {
commandLine.addParameter(remoteScriptPath);
}
}
commandLine.addParameters(scriptParameters);
return commandLine;
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlDockerAdapter method createProcess.
/**
* Wrapping a {@code commandLine} to a script and runs it using {@code docker} command, returning it's process
*/
public Process createProcess(@NotNull PerlCommandLine commandLine) throws ExecutionException {
PerlCommandLine dockerCommandLine = buildBaseProcessCommandLine(commandLine);
// mounting helpers
dockerCommandLine.withParameters(WITH_VOLUME, PerlPluginUtil.getPluginHelpersRoot() + ':' + myData.getHelpersRootPath());
if (!commandLine.isUserCommandLine()) {
dockerCommandLine.withParameters(WITH_ENTRYPOINT, "");
}
Project project = commandLine.getEffectiveProject();
if (project != null) {
// mounting modules roots
Set<VirtualFile> roots = new HashSet<>();
for (Module module : ModuleManager.getInstance(project).getModules()) {
roots.addAll(Arrays.asList(ModuleRootManager.getInstance(module).getContentRoots()));
}
roots.addAll(PerlProjectManager.getInstance(project).getExternalLibraryRoots());
for (VirtualFile rootToMount : VfsUtil.getCommonAncestors(roots.toArray(VirtualFile.EMPTY_ARRAY))) {
String localPath = rootToMount.getPath();
String remotePath = myData.getRemotePath(localPath);
dockerCommandLine.withParameters(WITH_VOLUME, localPath + ':' + remotePath);
}
// adding project settings if possible
dockerCommandLine.withParameters(StringUtil.split(PerlDockerProjectSettings.getInstance(project).getAdditionalDockerParameters(), " "));
}
// working directory
File remoteWorkingDirectory = myData.getRemotePath(commandLine.getWorkDirectory());
if (remoteWorkingDirectory != null) {
dockerCommandLine.withParameters(WORKING_DIRECTORY + "=" + StringUtil.escapeChar(FileUtil.toSystemIndependentName(remoteWorkingDirectory.toString()), ' '));
}
// required by coverage, probably we should have a getter for this; Also contains a temp path
String localSystemPath = PathManager.getSystemPath();
dockerCommandLine.withParameters(WITH_VOLUME, localSystemPath + ':' + myData.getRemotePath(localSystemPath));
// we sure that command script is under system dir
File script = createCommandScript(commandLine);
String dockerScriptPath = myData.getRemotePath(script.getPath());
if (StringUtil.isEmpty(dockerScriptPath)) {
throw new ExecutionException("Unable to map path for " + script.getPath() + " in " + myData);
}
return dockerCommandLine.withParameters(myData.getImageName(), "sh", dockerScriptPath).createProcess();
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlDockerProjectSettingsConfigurable method updatePreview.
private void updatePreview() {
PerlCommandLine dockerCommandLine = PerlDockerAdapter.buildBaseProcessCommandLine(new PerlCommandLine());
dockerCommandLine.addParameters(StringUtil.split(myArgumentsEditor.getText(), " "));
dockerCommandLine.addParameters("<project_and_helpers_volumes>", "<image_name>", "sh", "<shell_script_with_commands>");
myPreviewEditor.setText(dockerCommandLine.getCommandLineString());
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlRunAnythingProvider method execute.
@Override
public void execute(@NotNull DataContext dataContext, @NotNull List<CommandElement> value) {
LOG.info("Running " + getCommand(value));
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null) {
LOG.warn("No project in context");
return;
}
if (!PerlProjectManager.isPerlEnabled(project)) {
LOG.warn("Perl is not configured for project " + project);
return;
}
PerlRunUtil.runInConsole(new PerlCommandLine(getCommand(value)).withProject(project));
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlFormatWithPerlTidyAction method actionPerformed.
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
if (isEnabled(event)) {
final PsiFile file = PerlActionUtil.getPsiFileFromEvent(event);
if (file == null) {
return;
}
final Project project = file.getProject();
final Document document = file.getViewProvider().getDocument();
if (document == null) {
return;
}
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) {
return;
}
FileDocumentManager.getInstance().saveDocument(document);
new Task.Backgroundable(project, PerlBundle.message("perl.tidy.formatting"), false) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
try {
PerlCommandLine perlTidyCommandLine = getPerlTidyCommandLine(project);
if (perlTidyCommandLine == null) {
return;
}
BaseProcessHandler<?> processHandler = PerlHostData.createProcessHandler(perlTidyCommandLine.withCharset(virtualFile.getCharset()));
final OutputStream outputStream = Objects.requireNonNull(processHandler.getProcessInput());
try {
final byte[] sourceBytes = virtualFile.contentsToByteArray();
outputStream.write(sourceBytes);
outputStream.close();
} catch (IOException e) {
LOG.error(e);
Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.formatting.error.title"), e.getMessage(), NotificationType.ERROR));
return;
}
ProcessOutput processOutput = PerlHostData.getOutput(processHandler);
final List<String> stdoutLines = processOutput.getStdoutLines(false);
List<String> stderrLines = processOutput.getStderrLines();
if (stderrLines.isEmpty()) {
WriteCommandAction.runWriteCommandAction(project, () -> {
document.setText(StringUtil.join(stdoutLines, "\n"));
PsiDocumentManager.getInstance(project).commitDocument(document);
});
} else {
LOG.warn("Non-empty stderr: " + processOutput.getStderr());
Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.formatting.error.title"), StringUtil.join(stderrLines, "<br>"), NotificationType.ERROR));
}
} catch (ExecutionException e) {
LOG.error(e);
Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.running.error.title"), e.getMessage(), NotificationType.ERROR));
}
}
}.queue();
}
}
Aggregations