diff --git a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/DownloadManager.java b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/DownloadManager.java index 7874c8787..a2ee29d2a 100644 --- a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/DownloadManager.java +++ b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/DownloadManager.java @@ -1,5 +1,7 @@ package com.neuronrobotics.bowlerstudio.scripting.external; +import static com.neuronrobotics.bowlerstudio.scripting.external.DownloadManager.getEnvironment; + import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; @@ -25,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; @@ -48,6 +51,7 @@ import javafx.application.Platform; import javafx.scene.control.Alert; import javafx.scene.control.Button; +import javafx.scene.control.ButtonType; import javafx.scene.control.ProgressBar; import javafx.scene.control.Alert.AlertType; import javafx.scene.image.Image; @@ -56,7 +60,11 @@ public class DownloadManager { private static String editorsURL= "https://github.com/CommonWealthRobotics/ExternalEditorsBowlerStudio.git"; private static String bindir = System.getProperty("user.home") + "/bin/BowlerStudioInstall/"; private static int ev=0; + private static ButtonType buttonType=null; public static Thread run(IExternalEditor editor,File dir, PrintStream out,String... finalCommand) { + return run(new HashMap(),editor,dir,out,finalCommand); + } + public static Thread run(Map env,IExternalEditor editor,File dir, PrintStream out,String... finalCommand) { List tnp = Arrays.asList(finalCommand); String command =""; out.println("Running:\n\n"); @@ -79,6 +87,12 @@ public static Thread run(IExternalEditor editor,File dir, PrintStream out,String // creating the process ProcessBuilder pb = new ProcessBuilder(asList); + Map envir = pb.environment(); + // set environment variable u + envir.putAll(env); + for (String s : envir.keySet()) { + // System.out.println("Environment var set: "+s+" to "+envir.get(s)); + } // setting the directory pb.directory(dir); // startinf the process @@ -136,8 +150,43 @@ public static Thread run(IExternalEditor editor,File dir, PrintStream out,String thread.start(); return thread; } + public static Map getEnvironment(String exeType) { + String key = discoverKey(); + + try { + for(String f:ScriptingEngine.filesInGit(editorsURL)) { + File file = ScriptingEngine.fileFromGit(editorsURL, f); + if( file.getName().toLowerCase().startsWith(exeType.toLowerCase())&& + file.getName().toLowerCase().endsWith(".json")) { + String jsonText = new String(Files.readAllBytes(file.toPath())); + Type TT_mapStringString = new TypeToken>() { + }.getType(); + Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); + HashMap database = gson.fromJson(jsonText, TT_mapStringString); + Map vm = (Map) database.get(key); + if(vm!=null) { + String baseURL = vm.get("url").toString(); + String type = vm.get("type").toString(); + String name = vm.get("name").toString(); + String exeInZip=vm.get("executable").toString(); + String jvmURL = baseURL + name + "." + type; + Map environment; + Object o = vm.get("environment"); + if(o!=null) { + System.out.println("Environment found for "+exeType+" on "+key); - public static File getExecutable(String exeType,ProgressBar progress,IExternalEditor editor) { + return (Map) o; + } + } + } + } + }catch(Throwable t) { + t.printStackTrace(); + + } + return new HashMap<>(); + } + public static File getExecutable(String exeType ,IExternalEditor editor) { String key = discoverKey(); try { @@ -158,7 +207,14 @@ public static File getExecutable(String exeType,ProgressBar progress,IExternalEd String name = vm.get("name").toString(); String exeInZip=vm.get("executable").toString(); String jvmURL = baseURL + name + "." + type; - File jvmArchive = download("", jvmURL, 400000000, progress, bindir, name + "." + type); + Map environment; + Object o = vm.get("environment"); + if(o!=null) { + environment=(Map) o; + }else + environment= new HashMap<>(); + + File jvmArchive = download("", jvmURL, 400000000, bindir, name + "." + type,exeType); File dest = new File(bindir + name); String cmd = bindir + name + "/"+exeInZip; if (!dest.exists()) { @@ -377,8 +433,9 @@ public static String discoverKey() { } - public static File download(String version, String downloadJsonURL, long sizeOfJson, ProgressBar progress, - String bindir, String filename) throws MalformedURLException, IOException, FileNotFoundException { + public static File download(String version, String downloadJsonURL, long sizeOfJson,String bindir, String filename, String downloadName) throws MalformedURLException, IOException, FileNotFoundException, InterruptedException { + + URL url = new URL(downloadJsonURL); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); @@ -387,17 +444,40 @@ public static File download(String version, String downloadJsonURL, long sizeOfJ @Override public void process(double percent) { System.out.println("Download percent " + percent); - if(progress!=null) - Platform.runLater(() -> { - progress.setProgress(percent); - }); +// if(progress!=null) +// Platform.runLater(() -> { +// progress.setProgress(percent); +// }); } }); File folder = new File(bindir + version + "/"); File exe = new File(bindir + version + "/" + filename); if (!folder.exists() || !exe.exists()) { - System.out.println("Start Downloading " + filename); + buttonType=null; + + BowlerStudio.runLater(()->{ + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Message"); + alert.setHeaderText("Would you like to download: "+downloadName+"\nfrom:\n"+downloadJsonURL); + Optional result = alert.showAndWait(); + buttonType = result.get(); + alert.close(); + }); + + while(buttonType==null) { + Thread.sleep(100); + + } + + if (buttonType.equals(ButtonType.OK)) { + System.out.println("Start Downloading " + filename); + + }else { + pis.close(); + throw new RuntimeException("No Eclipse insalled"); + } + folder.mkdirs(); exe.createNewFile(); byte dataBuffer[] = new byte[1024]; @@ -440,11 +520,11 @@ public static void main(String[] args) { // TODO Auto-generated catch block e.printStackTrace(); } - File f = getExecutable("eclipse",null,null); + File f = getExecutable("eclipse",null); String ws = EclipseExternalEditor.getEclipseWorkspace(); if(f.exists()) { System.out.println("Executable retrived:\n"+f.getAbsolutePath()); - run(null,f.getParentFile(), System.err,f.getAbsolutePath(),"-data", ws); + run(getEnvironment("eclipse"),null,f.getParentFile(), System.err,f.getAbsolutePath(),"-data", ws); } else System.out.println("Failed to load file!\n"+f.getAbsolutePath()); diff --git a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/EclipseExternalEditor.java b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/EclipseExternalEditor.java index bed78e43a..c49aa3046 100644 --- a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/EclipseExternalEditor.java +++ b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/external/EclipseExternalEditor.java @@ -67,7 +67,7 @@ public void launch(File file, Button advanced) { this.advanced = advanced; EclipseExternalEditor ee=this; new Thread(() -> { - File exeFile = getExecutable("eclipse",null,null); + File exeFile = getExecutable("eclipse",null); String eclipseEXE = exeFile.getAbsolutePath(); try { @@ -167,7 +167,7 @@ public void launch(File file, Button advanced) { System.out.println("Opening workspace "+ws); if(!isEclipseOpen( ws)) { - run(this,ScriptingEngine.getWorkspace() ,System.out, eclipseEXE, "-data", ws); + run(getEnvironment("eclipse"),this,ScriptingEngine.getWorkspace() ,System.out, eclipseEXE, "-data", ws); while (!isEclipseOpen( ws)) { try { Thread.sleep(5000); @@ -195,7 +195,7 @@ public void launch(File file, Button advanced) { } } } - run(this,dir,System.err, eclipseEXE, dir.getAbsolutePath() + delim()); + run(getEnvironment("eclipse"),this,dir,System.err, eclipseEXE, dir.getAbsolutePath() + delim()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace();