use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class MojoGenerateAction method actionPerformed.
@Override
public final void actionPerformed(@NotNull AnActionEvent e) {
var project = getEventProject(e);
var perlSdk = PerlProjectManager.getSdk(e);
VirtualFile mojoScript = MojoUtil.getMojoScript(e);
if (mojoScript == null) {
LOG.warn("No mojo script; project: " + project + " sdk: " + perlSdk);
return;
}
List<String> generationParameters = computeGenerationParameters(e, mojoScript);
if (generationParameters == null) {
return;
}
var localScriptPath = mojoScript.getPath();
if (perlSdk == null) {
LOG.warn("No perl sdk for " + project);
return;
}
PerlHostData<?, ?> hostData = PerlHostData.notNullFrom(perlSdk);
var remoteScriptPath = hostData.getRemotePath(localScriptPath);
if (remoteScriptPath == null) {
LOG.warn("No remote path for " + localScriptPath + " in " + perlSdk);
return;
}
List<String> fullArguments = ContainerUtil.newArrayList(remoteScriptPath, GENERATE_COMMAND);
fullArguments.addAll(generationParameters);
VirtualFile targetDirectory = getTargetDirectory(e);
var targetDirectoryPath = targetDirectory.getPath();
var callbackTestSemaphore = myCallbackTestSemaphore;
PerlRunUtil.runInConsole(new PerlCommandLine(fullArguments).withProject(project).withConsoleIcon(MojoIcons.MOJO_LOGO).withWorkDirectory(targetDirectoryPath).withProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(@NotNull ProcessEvent event) {
targetDirectory.refresh(true, false);
try {
hostData.fixPermissionsRecursively(targetDirectoryPath, project);
} catch (ExecutionException ex) {
LOG.warn("Error fixing permissions for " + targetDirectoryPath + ": " + ex.getMessage());
} finally {
if (callbackTestSemaphore != null) {
callbackTestSemaphore.up();
}
}
}
}));
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PackageManagerAdapter method doInstall.
/**
* Installs a package with {@code packageName} with output in console
*/
private void doInstall(@NotNull Collection<String> packageNames) {
ApplicationManager.getApplication().assertIsDispatchThread();
if (myProject != null && myProject.isDisposed()) {
return;
}
VirtualFile script = PerlRunUtil.findLibraryScriptWithNotification(getSdk(), getProject(), getManagerScriptName(), getManagerPackageName());
if (script == null) {
return;
}
String remotePath = PerlHostData.notNullFrom(mySdk).getRemotePath(script.getPath());
if (remotePath == null) {
return;
}
PerlRunUtil.runInConsole(new PerlCommandLine(remotePath).withParameters(getInstallParameters(packageNames)).withSdk(getSdk()).withProject(getProject()).withConsoleTitle(PerlBundle.message("perl.cpan.console.installing", StringUtil.join(packageNames, ", "))).withProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(@NotNull ProcessEvent event) {
PerlRunUtil.refreshSdkDirs(mySdk, myProject);
}
}));
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlCriticAnnotator method doAnnotate.
@Override
@Nullable
public List<PerlCriticErrorDescriptor> doAnnotate(final PerlFile sourcePsiFile) {
if (sourcePsiFile == null) {
return null;
}
VirtualFile virtualFile = ReadAction.compute(() -> PsiUtilCore.getVirtualFile(sourcePsiFile));
if (virtualFile == null) {
return null;
}
byte[] sourceBytes = ReadAction.compute(sourcePsiFile::getPerlContentInBytes);
if (sourceBytes == null) {
return null;
}
try {
PerlCommandLine criticCommandLine = getPerlCriticCommandLine(sourcePsiFile);
if (criticCommandLine == null) {
PerlSharedSettings.getInstance(sourcePsiFile.getProject()).PERL_CRITIC_ENABLED = false;
return null;
}
BaseProcessHandler<?> processHandler = PerlHostData.createProcessHandler(criticCommandLine.withCharset(virtualFile.getCharset()));
OutputStream outputStream = Objects.requireNonNull(processHandler.getProcessInput());
outputStream.write(sourceBytes);
outputStream.close();
List<PerlCriticErrorDescriptor> errors = new ArrayList<>();
PerlCriticErrorDescriptor lastDescriptor = null;
for (String output : PerlHostData.getOutput(processHandler).getStdoutLines()) {
PerlCriticErrorDescriptor fromString = PerlCriticErrorDescriptor.getFromString(output);
if (fromString != null) {
errors.add(lastDescriptor = fromString);
} else if (lastDescriptor != null) {
lastDescriptor.append(" " + output);
} else if (!StringUtil.equals(output, "source OK")) {
LOG.warn("Could not parse line: " + output);
}
}
return errors;
} catch (Exception e) {
LOG.warn("Error running perlcritic", e);
Notifications.Bus.notify(new Notification(PerlBundle.message("perl.critic.notification.group"), PerlBundle.message("perl.critic.execution.error.title"), PerlBundle.message("perl.critic.execution.error.message", e.getMessage()), NotificationType.ERROR));
PerlSharedSettings.getInstance(sourcePsiFile.getProject()).PERL_CRITIC_ENABLED = false;
}
return null;
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlCriticAnnotator method getPerlCriticCommandLine.
@Nullable
protected PerlCommandLine getPerlCriticCommandLine(@NotNull PsiFile fileToLint) {
var project = fileToLint.getProject();
PerlSharedSettings sharedSettings = PerlSharedSettings.getInstance(project);
VirtualFile perlCriticScript = ReadAction.compute(() -> PerlRunUtil.findLibraryScriptWithNotification(project, SCRIPT_NAME, PACKAGE_NAME));
if (perlCriticScript == null) {
return null;
}
PerlCommandLine commandLine = PerlRunUtil.getPerlCommandLine(project, perlCriticScript);
if (commandLine == null) {
return null;
}
var virtualFileToLint = PsiUtilCore.getVirtualFile(fileToLint);
var contentRoot = PerlFileUtil.getContentRoot(project, virtualFileToLint);
if (contentRoot != null) {
commandLine.withWorkDirectory(contentRoot.getPath());
} else {
LOG.warn("No content root for perlcritic lint: " + virtualFileToLint + " - " + fileToLint + " in " + project);
}
if (StringUtil.isNotEmpty(sharedSettings.PERL_CRITIC_ARGS)) {
commandLine.addParameters(StringUtil.split(sharedSettings.PERL_CRITIC_ARGS, " "));
}
return commandLine;
}
use of com.perl5.lang.perl.idea.execution.PerlCommandLine in project Perl5-IDEA by Camelcade.
the class PerlDeparseFileAction method actionPerformed.
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
PsiFile file = PerlActionUtil.getPsiFileFromEvent(event);
if (file == null) {
return;
}
final Document document = file.getViewProvider().getDocument();
if (document == null) {
return;
}
final Project project = file.getProject();
String deparseArgument = "-MO=Deparse";
PerlSharedSettings perl5Settings = PerlSharedSettings.getInstance(project);
if (StringUtil.isNotEmpty(perl5Settings.PERL_DEPARSE_ARGUMENTS)) {
deparseArgument += "," + perl5Settings.PERL_DEPARSE_ARGUMENTS;
}
PerlCommandLine commandLine = PerlRunUtil.getPerlCommandLine(project, file.getVirtualFile(), deparseArgument);
if (commandLine == null) {
return;
}
commandLine.withWorkDirectory(project.getBasePath());
FileDocumentManager.getInstance().saveDocument(document);
new Task.Backgroundable(file.getProject(), PerlBundle.message("perl.action.deparsing.progress", file.getName()), true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
try {
ProcessOutput processOutput = PerlHostData.execAndGetOutput(commandLine);
String deparsed = processOutput.getStdout();
String error = processOutput.getStderr();
if (StringUtil.isNotEmpty(error) && !StringUtil.contains(error, "syntax OK")) {
if (StringUtil.isEmpty(deparsed)) {
Notifications.Bus.notify(new Notification(PERL_DEPARSE_GROUP, PerlBundle.message("perl.action.error.notification.title"), error.replaceAll("\\n", "<br/>"), NotificationType.ERROR));
} else {
Notifications.Bus.notify(new Notification(PERL_DEPARSE_GROUP, PerlBundle.message("perl.action.success.notification.title"), PerlBundle.message("perl.action.success.notification.message", error.replaceAll("\\n", "<br/>")), NotificationType.INFORMATION));
}
}
if (StringUtil.isNotEmpty(deparsed)) {
ApplicationManager.getApplication().invokeLater(() -> WriteAction.run(() -> {
VirtualFile newFile = new LightVirtualFile("Deparsed " + file.getName(), file.getFileType(), deparsed);
OpenFileAction.openFile(newFile, project);
}));
}
} catch (ExecutionException e) {
Notifications.Bus.notify(new Notification(PERL_DEPARSE_GROUP, PerlBundle.message("perl.execution.error.notification.title"), e.getMessage().replaceAll("\\n", "<br/>"), NotificationType.ERROR));
}
}
}.queue();
}
Aggregations