package javarepl.commands;

import javarepl.Evaluator;
import jline.console.completer.StringsCompleter;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

import static com.googlecode.totallylazy.Streams.copyAndClose;
import static com.googlecode.totallylazy.Strings.startsWith;
import static java.lang.String.format;
import static javarepl.Utils.*;

public class AddToClasspath extends Command {
    private static final String COMMAND = ":cp";

    public AddToClasspath() {
        super(COMMAND + " <path> - includes given file or directory in the classpath", startsWith(COMMAND), new StringsCompleter(COMMAND));
    }

    public Void call(final Evaluator evaluator, String expression) throws Exception {
        String path = parseStringCommand(expression).second().getOrNull();
        try {
            URL url = resolveClasspath(path);

            if (isWebUrl(url)) {
                String outputFileName = randomIdentifier("external");
                System.out.println(format("Downloading %s...", path, outputFileName));

                File outputFile = new File(evaluator.outputDirectory(), outputFileName);
                copyAndClose(url.openStream(), new FileOutputStream(outputFile));

                evaluator.addClasspathUrl(outputFile.toURI().toURL());
            } else {
                evaluator.addClasspathUrl(url);
            }

            System.out.println(format("Added %s to classpath.", path));
        } catch (Exception e) {
            System.err.println(format("Could not add %s to classpath. %s", path, e.getLocalizedMessage()));
        }

        return null;
    }
}

@@ -902,78 +902,8 @@
 ) %7B%0A
-                String outputFileName = randomIdentifier(%22external%22);%0A
     
@@ -965,32 +965,16 @@
 .%22, path
-, outputFileName
 ));%0A%0A   
@@ -1046,22 +1046,36 @@
 (), 
-outputFileName
+randomIdentifier(%22external%22)
 );%0A 

shortened the jar name