|
1 | 1 | package com.espressif.idf.core.tools; |
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
3 | 5 | import java.io.FileInputStream; |
| 6 | +import java.io.FileNotFoundException; |
4 | 7 | import java.io.IOException; |
5 | 8 | import java.io.InputStream; |
6 | 9 | import java.io.InputStreamReader; |
|
11 | 14 | import java.nio.file.Path; |
12 | 15 | import java.nio.file.Paths; |
13 | 16 | import java.nio.file.StandardCopyOption; |
| 17 | +import java.util.List; |
14 | 18 | import java.util.Optional; |
15 | 19 | import java.util.zip.ZipEntry; |
16 | 20 | import java.util.zip.ZipInputStream; |
17 | 21 |
|
18 | 22 | import org.eclipse.core.runtime.IProgressMonitor; |
19 | 23 | import org.eclipse.core.runtime.Platform; |
| 24 | +import org.eclipse.swt.widgets.Display; |
| 25 | +import org.eclipse.ui.console.MessageConsoleStream; |
20 | 26 |
|
| 27 | +import com.espressif.idf.core.IDFEnvironmentVariables; |
| 28 | +import com.espressif.idf.core.logging.Logger; |
21 | 29 | import com.espressif.idf.core.util.StringUtil; |
22 | 30 | import com.google.gson.JsonArray; |
23 | 31 | import com.google.gson.JsonObject; |
24 | 32 | import com.google.gson.JsonParser; |
25 | 33 |
|
26 | | -public class EimDownloader |
| 34 | +public class EimLoader |
27 | 35 | { |
28 | 36 | private static final String URL_JSON = "https://dl.espressif.com/dl/eim/eim_unified_release.json"; //$NON-NLS-1$ |
29 | 37 | private static final Path DOWNLOAD_DIR = Paths.get(System.getProperty("java.io.tmpdir"), "eim_gui"); //$NON-NLS-1$ //$NON-NLS-2$ |
30 | 38 |
|
31 | 39 | private String os; |
32 | 40 | private String arch; |
33 | | - DownloadListener listener; |
| 41 | + private DownloadListener listener; |
| 42 | + private MessageConsoleStream standardConsoleStream; |
| 43 | + private MessageConsoleStream errorConsoleStream; |
| 44 | + private Display display; |
34 | 45 |
|
35 | | - public EimDownloader(DownloadListener listener) |
| 46 | + public EimLoader(DownloadListener listener, MessageConsoleStream standardConsoleStream, MessageConsoleStream errorConsoleStream, Display display) |
36 | 47 | { |
37 | 48 | os = Platform.getOS(); |
38 | 49 | arch = Platform.getOSArch(); |
39 | 50 | this.listener = listener; |
| 51 | + this.standardConsoleStream = standardConsoleStream; |
| 52 | + this.errorConsoleStream = errorConsoleStream; |
| 53 | + this.display = display; |
40 | 54 | } |
| 55 | + |
| 56 | + private void logMessage(String message) |
| 57 | + { |
| 58 | + display.asyncExec(()->{ |
| 59 | + try |
| 60 | + { |
| 61 | + standardConsoleStream.write(message); |
| 62 | + } |
| 63 | + catch (IOException e) |
| 64 | + { |
| 65 | + Logger.log(e); |
| 66 | + logError(e.getMessage()); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + Logger.log(message); |
| 71 | + } |
| 72 | + |
| 73 | + private void logError(String message) |
| 74 | + { |
| 75 | + display.asyncExec(()->{ |
| 76 | + try |
| 77 | + { |
| 78 | + errorConsoleStream.write(message); |
| 79 | + } |
| 80 | + catch (IOException e) |
| 81 | + { |
| 82 | + Logger.log(e); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + Logger.log(message); |
| 87 | + } |
| 88 | + |
| 89 | + public Process launchEim(String eimPath) throws IOException |
| 90 | + { |
| 91 | + if (!Files.exists(Paths.get(eimPath))) |
| 92 | + throw new FileNotFoundException("EIM path not found: " + eimPath); //$NON-NLS-1$ |
| 93 | + |
| 94 | + String os = Platform.getOS(); |
| 95 | + List<String> command; |
| 96 | + |
| 97 | + if (os.equals(Platform.OS_WIN32)) |
| 98 | + { |
| 99 | + command = List.of("cmd.exe", "/c", eimPath.toString()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 100 | + } |
| 101 | + else if (os.equals(Platform.OS_MACOSX)) |
| 102 | + { |
| 103 | + command = List.of("open", "-a", eimPath.toString()); //$NON-NLS-1$//$NON-NLS-2$ |
| 104 | + } |
| 105 | + else if (os.equals(Platform.OS_LINUX)) |
| 106 | + { |
| 107 | + command = List.of("bash", "-c", "\"" + eimPath.toString() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw new UnsupportedOperationException("Unsupported OS: " + os); //$NON-NLS-1$ |
| 112 | + } |
| 113 | + |
| 114 | + Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); |
| 115 | + |
| 116 | + logMessage("Launched EIM application: " + eimPath + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 117 | + |
| 118 | + return process; |
| 119 | + } |
| 120 | + |
| 121 | + public Process installAndLaunchDmg(Path dmgPath) throws IOException, InterruptedException |
| 122 | + { |
| 123 | + logMessage("Mounting DMG...\n"); //$NON-NLS-1$ |
| 124 | + ProcessBuilder mountBuilder = new ProcessBuilder("hdiutil", "attach", dmgPath.toString()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 125 | + Process mountProcess = mountBuilder.start(); |
| 126 | + |
| 127 | + String volumePath = null; |
| 128 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(mountProcess.getInputStream()))) |
| 129 | + { |
| 130 | + String line; |
| 131 | + while ((line = reader.readLine()) != null) |
| 132 | + { |
| 133 | + if (line.contains("/Volumes/")) //$NON-NLS-1$ |
| 134 | + { |
| 135 | + String[] parts = line.split("\t"); //$NON-NLS-1$ |
| 136 | + for (String part : parts) |
| 137 | + { |
| 138 | + if (part.startsWith("/Volumes/")) //$NON-NLS-1$ |
| 139 | + { |
| 140 | + volumePath = part.trim(); |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (volumePath == null) |
| 149 | + throw new IOException("Failed to mount DMG: Volume path not found."); //$NON-NLS-1$ |
| 150 | + |
| 151 | + File[] apps = new File(volumePath).listFiles((dir, name) -> name.endsWith(".app")); //$NON-NLS-1$ |
| 152 | + if (apps == null || apps.length == 0) |
| 153 | + throw new FileNotFoundException("No .app found inside DMG."); //$NON-NLS-1$ |
| 154 | + |
| 155 | + File appBundle = apps[0]; |
| 156 | + Path targetAppPath = Paths.get("/Applications", appBundle.getName()); //$NON-NLS-1$ |
| 157 | + |
| 158 | + logMessage("Copying app to /Applications...\n"); //$NON-NLS-1$ |
| 159 | + |
| 160 | + // Copy to /Applications |
| 161 | + ProcessBuilder copyBuilder = new ProcessBuilder("cp", "-R", appBundle.getAbsolutePath(), //$NON-NLS-1$ //$NON-NLS-2$ |
| 162 | + targetAppPath.toString()); |
| 163 | + copyBuilder.inheritIO().start().waitFor(); |
| 164 | + |
| 165 | + logMessage("Unmounting DMG...\n"); //$NON-NLS-1$ |
| 166 | + new ProcessBuilder("hdiutil", "detach", volumePath).start().waitFor(); //$NON-NLS-1$ //$NON-NLS-2$ |
| 167 | + |
| 168 | + logMessage("Launching app from /Applications...\n"); //$NON-NLS-1$ |
| 169 | + |
| 170 | + Process openProcess = new ProcessBuilder("open", "-W", "-a", targetAppPath.toString()).start(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 171 | + new IDFEnvironmentVariables().addEnvVariable(IDFEnvironmentVariables.EIM_PATH, targetAppPath.toString()); |
| 172 | + return openProcess; |
| 173 | + } |
| 174 | + |
41 | 175 |
|
42 | 176 | public void downloadEim(IProgressMonitor monitor) |
43 | 177 | { |
|
0 commit comments