Search in sources :

Example 1 with PerlHostFileTransfer

use of com.perl5.lang.perl.idea.sdk.host.PerlHostFileTransfer in project Perl5-IDEA by Camelcade.

the class PerlSdkType method setupSdkPaths.

@Override
public void setupSdkPaths(@NotNull Sdk sdk) {
    if (ApplicationManager.getApplication().isDispatchThread() && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
        throw new RuntimeException("Do not call from EDT, refreshes FS");
    }
    String oldText = PerlRunUtil.setProgressText(PerlBundle.message("perl.progress.refreshing.inc", sdk.getName()));
    LOG.info("Refreshing @INC for " + sdk);
    PerlHostData<?, ?> hostData = PerlHostData.notNullFrom(sdk);
    List<String> pathsToRefresh = new ArrayList<>();
    // syncing data if necessary
    List<String> incPaths = computeIncPaths(sdk);
    List<Exception> exceptions = new ArrayList<>();
    try (PerlHostFileTransfer<?> fileTransfer = hostData.getFileTransfer()) {
        Consumer<File> downloader = it -> syncAndCollectException(fileTransfer, it, pathsToRefresh, exceptions, false);
        Consumer<File> binStubber = it -> syncAndCollectException(fileTransfer, it, pathsToRefresh, exceptions, true);
        for (String hostPath : incPaths) {
            downloader.accept(new File(hostPath));
            binStubber.accept(PerlRunUtil.findLibsBin(new File(hostPath)));
        }
        // additional bin dirs from version manager
        PerlVersionManagerData.notNullFrom(sdk).getBinDirsPath().forEach(binStubber);
        // sdk home path
        File interpreterPath = new File(Objects.requireNonNull(PerlProjectManager.getInterpreterPath(sdk)));
        binStubber.accept(interpreterPath.getParentFile());
        List<VirtualFile> filesToRefresh = pathsToRefresh.stream().map(it -> VfsUtil.findFileByIoFile(new File(it), true)).filter(Objects::nonNull).collect(Collectors.toList());
        if (!filesToRefresh.isEmpty()) {
            PerlRunUtil.setProgressText(PerlBundle.message("perl.progress.refreshing.filesystem"));
            VfsUtil.markDirtyAndRefresh(false, true, true, filesToRefresh.toArray(VirtualFile.EMPTY_ARRAY));
        }
        try {
            fileTransfer.syncHelpers();
        } catch (IOException e) {
            exceptions.add(e);
        }
        if (!exceptions.isEmpty()) {
            int copiedFiles = filesToRefresh.size();
            int errorsNumber = exceptions.size();
            Notifications.Bus.notify(new Notification(PerlBundle.message("perl.sync.notification.group"), PerlBundle.message("perl.sync.notification.title"), PerlBundle.message("perl.sync.notification.body", copiedFiles, copiedFiles + errorsNumber, errorsNumber, StringUtil.pluralize(PerlBundle.message("perl.sync.notification.pluralize"), errorsNumber)), NotificationType.ERROR));
            exceptions.forEach(LOG::warn);
        }
    } catch (IOException e) {
        LOG.warn("Error closing transfer for " + sdk, e);
    }
    // updating sdk
    SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.removeAllRoots();
    for (String hostPath : incPaths) {
        String localPath = hostData.getLocalPath(hostPath);
        if (localPath == null) {
            continue;
        }
        File libDir = new File(localPath);
        if (libDir.exists() && libDir.isDirectory()) {
            VirtualFile virtualDir = VfsUtil.findFileByIoFile(libDir, true);
            if (virtualDir != null) {
                sdkModificator.addRoot(virtualDir, OrderRootType.CLASSES);
            }
        }
    }
    ApplicationManager.getApplication().invokeAndWait(sdkModificator::commitChanges);
    PerlRunUtil.setProgressText(oldText);
}
Also used : PerlProjectManager(com.perl5.lang.perl.idea.project.PerlProjectManager) PerlVersionManagerData(com.perl5.lang.perl.idea.sdk.versionManager.PerlVersionManagerData) PerlBundle(com.perl5.PerlBundle) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PerlHostFileTransfer(com.perl5.lang.perl.idea.sdk.host.PerlHostFileTransfer) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) PerlIcons(com.perl5.PerlIcons) com.intellij.openapi.projectRoots(com.intellij.openapi.projectRoots) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) PerlRunUtil(com.perl5.lang.perl.util.PerlRunUtil) Logger(com.intellij.openapi.diagnostic.Logger) PerlSdkTable(com.intellij.openapi.projectRoots.impl.PerlSdkTable) PerlHostData(com.perl5.lang.perl.idea.sdk.host.PerlHostData) ProjectJdkImpl(com.intellij.openapi.projectRoots.impl.ProjectJdkImpl) Notifications(com.intellij.notification.Notifications) OrderRootType(com.intellij.openapi.roots.OrderRootType) SdkConfigurationUtil(com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil) StringUtil(com.intellij.openapi.util.text.StringUtil) PerlImplementationHandler(com.perl5.lang.perl.idea.sdk.implementation.PerlImplementationHandler) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SystemInfo(com.intellij.openapi.util.SystemInfo) File(java.io.File) NotificationType(com.intellij.notification.NotificationType) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) List(java.util.List) PerlImplementationData(com.perl5.lang.perl.idea.sdk.implementation.PerlImplementationData) ApplicationManager(com.intellij.openapi.application.ApplicationManager) VfsUtil(com.intellij.openapi.vfs.VfsUtil) ObjectUtils(com.intellij.util.ObjectUtils) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) javax.swing(javax.swing) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) Notification(com.intellij.notification.Notification) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

Notification (com.intellij.notification.Notification)1 NotificationType (com.intellij.notification.NotificationType)1 Notifications (com.intellij.notification.Notifications)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Project (com.intellij.openapi.project.Project)1 com.intellij.openapi.projectRoots (com.intellij.openapi.projectRoots)1 PerlSdkTable (com.intellij.openapi.projectRoots.impl.PerlSdkTable)1 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)1 SdkConfigurationUtil (com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil)1 OrderRootType (com.intellij.openapi.roots.OrderRootType)1 Messages (com.intellij.openapi.ui.Messages)1 SystemInfo (com.intellij.openapi.util.SystemInfo)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 VfsUtil (com.intellij.openapi.vfs.VfsUtil)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ObjectUtils (com.intellij.util.ObjectUtils)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 PerlBundle (com.perl5.PerlBundle)1 PerlIcons (com.perl5.PerlIcons)1