<commit_before>package com.blocklatency.embeddedlinuxjvm.protocol.ssh;

import com.blocklatency.embeddedlinuxjvm.commandline.JavaStatusChecker;
import com.blocklatency.embeddedlinuxjvm.commandline.LinuxCommand;
import com.blocklatency.embeddedlinuxjvm.console.EmbeddedLinuxJVMConsoleView;
import com.blocklatency.embeddedlinuxjvm.localization.EmbeddedLinuxJVMBundle;
import com.blocklatency.embeddedlinuxjvm.protocol.ssh.jsch.EmbeddedSSHClient;
import com.blocklatency.embeddedlinuxjvm.protocol.ssh.jsch.SFTPHandler;
import com.blocklatency.embeddedlinuxjvm.runner.data.EmbeddedLinuxJVMRunConfigurationRunnerParameters;
import com.blocklatency.embeddedlinuxjvm.utils.FileUtilities;
import com.intellij.execution.configurations.RuntimeConfigurationException;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.Builder;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

@Builder
public class SSHHandlerTarget {
    public static final String NEW_LINE = System.getProperty("line.separator");
    private static final String OUTPUT_LOCATION = "IdeaProjects";
    private static final String EMBEDDED_LINUX_HOME = "home";
    private EmbeddedLinuxJVMRunConfigurationRunnerParameters params;
    private EmbeddedLinuxJVMConsoleView consoleView;
    private EmbeddedSSHClient ssh;

    /**
     * Uploads Java application output folders
     *
     * @param compileOutput Output directory folder where to store the java application
     * @param cmd           The command to execute on the java files
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws RuntimeConfigurationException
     */
    public void uploadAndRunJavaApp(@NotNull final File compileOutput, @NotNull final String cmd)
            throws IOException, ClassNotFoundException, RuntimeConfigurationException {
        final String remoteDir = FileUtilities.SEPARATOR + EMBEDDED_LINUX_HOME + FileUtilities.SEPARATOR
                + params.getUsername() + FileUtilities.SEPARATOR + OUTPUT_LOCATION;
        String deploymentPath = remoteDir + FileUtilities.SEPARATOR + consoleView.getProject().getName();
        genericUpload(deploymentPath, compileOutput);
        runJavaApp(deploymentPath, cmd);
    }

    /**
     * Generic SSh Ftp uploader
     *
     * @param deploymentPath the remote location storing the compressed file
     * @param fileToUpload   files to upload
     * @throws IOException
     * @throws RuntimeConfigurationException
     */
    public void genericUpload(@NotNull final String deploymentPath, @NotNull final File fileToUpload) throws IOException, RuntimeConfigurationException {
        forceCreateDirectories(deploymentPath);
        Session session = connect(ssh.get());
        try {
            SFTPHandler sftpHandler = new SFTPHandler(consoleView);
            sftpHandler.upload(session, fileToUpload, deploymentPath);
        } catch (Exception e) {
            setErrorOnUI(e.getMessage());
        }
    }


    /**
     * Force create directories, if it exists it won't do anything
     *
     * @param path
     * @throws IOException
     * @throws RuntimeConfigurationException
     */
    private void forceCreateDirectories(@NotNull final String path) throws IOException, RuntimeConfigurationException {
        Session session = connect(ssh.get());
        try {
            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
            List<String> commands = Arrays.asList(
                    String.format("mkdir -p %s", path),
                    String.format("cd %s", path),
                    String.format("mkdir -p %s", FileUtilities.CLASSES),
                    String.format("mkdir -p %s", FileUtilities.LIB),
                    String.format("cd %s", path + FileUtilities.SEPARATOR + FileUtilities.CLASSES),
                    "rm -rf *"
            );
            for (String command : commands) {
                consoleView.print(EmbeddedLinuxJVMBundle.getString("pi.deployment.command") + command + NEW_LINE, ConsoleViewContentType.SYSTEM_OUTPUT);
            }
            channelExec.setCommand(LinuxCommand.builder().commands(commands).build().toString());
            channelExec.connect();
            channelExec.disconnect();
        } catch (JSchException e) {
            setErrorOnUI(e.getMessage());
        }
    }

    /**<commit_msg>Remove old librairies from target with new build<commit_after>package com.blocklatency.embeddedlinuxjvm.protocol.ssh;

import com.blocklatency.embeddedlinuxjvm.commandline.JavaStatusChecker;
import com.blocklatency.embeddedlinuxjvm.commandline.LinuxCommand;
import com.blocklatency.embeddedlinuxjvm.console.EmbeddedLinuxJVMConsoleView;
import com.blocklatency.embeddedlinuxjvm.localization.EmbeddedLinuxJVMBundle;
import com.blocklatency.embeddedlinuxjvm.protocol.ssh.jsch.EmbeddedSSHClient;
import com.blocklatency.embeddedlinuxjvm.protocol.ssh.jsch.SFTPHandler;
import com.blocklatency.embeddedlinuxjvm.runner.data.EmbeddedLinuxJVMRunConfigurationRunnerParameters;
import com.blocklatency.embeddedlinuxjvm.utils.FileUtilities;
import com.intellij.execution.configurations.RuntimeConfigurationException;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.Builder;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

@Builder
public class SSHHandlerTarget {
    public static final String NEW_LINE = System.getProperty("line.separator");
    private static final String OUTPUT_LOCATION = "IdeaProjects";
    private static final String EMBEDDED_LINUX_HOME = "home";
    private EmbeddedLinuxJVMRunConfigurationRunnerParameters params;
    private EmbeddedLinuxJVMConsoleView consoleView;
    private EmbeddedSSHClient ssh;

    /**
     * Uploads Java application output folders
     *
     * @param compileOutput Output directory folder where to store the java application
     * @param cmd           The command to execute on the java files
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws RuntimeConfigurationException
     */
    public void uploadAndRunJavaApp(@NotNull final File compileOutput, @NotNull final String cmd)
            throws IOException, ClassNotFoundException, RuntimeConfigurationException {
        final String remoteDir = FileUtilities.SEPARATOR + EMBEDDED_LINUX_HOME + FileUtilities.SEPARATOR
                + params.getUsername() + FileUtilities.SEPARATOR + OUTPUT_LOCATION;
        String deploymentPath = remoteDir + FileUtilities.SEPARATOR + consoleView.getProject().getName();
        genericUpload(deploymentPath, compileOutput);
        runJavaApp(deploymentPath, cmd);
    }

    /**
     * Generic SSh Ftp uploader
     *
     * @param deploymentPath the remote location storing the compressed file
     * @param fileToUpload   files to upload
     * @throws IOException
     * @throws RuntimeConfigurationException
     */
    public void genericUpload(@NotNull final String deploymentPath, @NotNull final File fileToUpload) throws IOException, RuntimeConfigurationException {
        forceCreateDirectories(deploymentPath);
        Session session = connect(ssh.get());
        try {
            SFTPHandler sftpHandler = new SFTPHandler(consoleView);
            sftpHandler.upload(session, fileToUpload, deploymentPath);
        } catch (Exception e) {
            setErrorOnUI(e.getMessage());
        }
    }


    /**
     * Force create directories, if it exists it won't do anything
     *
     * @param path
     * @throws IOException
     * @throws RuntimeConfigurationException
     */
    private void forceCreateDirectories(@NotNull final String path) throws IOException, RuntimeConfigurationException {
        Session session = connect(ssh.get());
        try {
            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
            List<String> commands = Arrays.asList(
                    String.format("mkdir -p %s", path),
                    String.format("cd %s", path),
                    String.format("mkdir -p %s", FileUtilities.CLASSES),
                    String.format("mkdir -p %s", FileUtilities.LIB),
                    String.format("cd %s", path + FileUtilities.SEPARATOR + FileUtilities.CLASSES),
                    "rm -rf *",
                    String.format("cd %s", path + FileUtilities.SEPARATOR + FileUtilities.LIB),
                    "rm -rf *"
            );
            for (String command : commands) {
                consoleView.print(EmbeddedLinuxJVMBundle.getString("pi.deployment.command") + command + NEW_LINE, ConsoleViewContentType.SYSTEM_OUTPUT);
            }
            channelExec.setCommand(LinuxCommand.builder().commands(commands).build().toString());
            channelExec.connect();
            channelExec.disconnect();
        } catch (JSchException e) {
            setErrorOnUI(e.getMessage());
        }
    }

    /**
{'fraction_non_alphanumeric': 0.08474228916897224, 'fraction_numerical': 0.0, 'mean_word_length': 3.428312159709619, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7663888', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2529, 'n_words': 556}