From 33969c9c0388ca31b2e49ec4b3fde71a5629e0b5 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 23 Feb 2019 09:35:51 -0800 Subject: [PATCH] Use cleaner path finding code, misc. cleanup Instead of a bunch of nested if-elses, use a list of strings to try iterating over to fins python(.exe). Replace 1-line if-else with ternary operator --- src/ESP8266FS.java | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/ESP8266FS.java b/src/ESP8266FS.java index 7a3811a..95a51e7 100644 --- a/src/ESP8266FS.java +++ b/src/ESP8266FS.java @@ -208,11 +208,7 @@ private void createAndUpload(){ File espota = new File(platform.getFolder()+"/tools"); File esptool = new File(platform.getFolder()+"/tools"); String serialPort = PreferencesData.get("serial.port"); - String pythonCmd; - if(PreferencesData.get("runtime.os").contentEquals("windows")) - pythonCmd = "python.exe"; - else - pythonCmd = "python"; + String pythonCmd = PreferencesData.get("runtime.os").contentEquals("windows") ? "python.exe" : "python"; String uploadCmd = ""; //make sure the serial port or IP is defined @@ -228,21 +224,12 @@ private void createAndUpload(){ uploadCmd = uploadPyFile.getAbsolutePath(); } // Find python.exe if present, don't fail if not found for backwards compat - String toolPyCmd = pythonCmd; - if ((toolPyCmd != null ) && !toolPyCmd.isEmpty()) { - File toolPyFile = new File(platform.getFolder()+"/tools", toolPyCmd); + String[] paths = { platform.getFolder()+"/tools", platform.getFolder()+"/tools/python", PreferencesData.get("runtime.tools.python.path") }; + for (String s: paths) { + File toolPyFile = new File(s, pythonCmd); if (toolPyFile.exists() && toolPyFile.isFile() && toolPyFile.canExecute()) { pythonCmd = toolPyFile.getAbsolutePath(); - } else { - toolPyFile = new File(platform.getFolder()+"/tools/python", toolPyCmd); - if (toolPyFile.exists() && toolPyFile.isFile() && toolPyFile.canExecute()) { - pythonCmd = toolPyFile.getAbsolutePath(); - } else { - toolPyFile = new File(PreferencesData.get("runtime.tools.python.path"), toolPyCmd); - if (toolPyFile.exists() && toolPyFile.isFile() && toolPyFile.canExecute()) { - pythonCmd = toolPyFile.getAbsolutePath(); - } - } + break; } } // pythonCmd now points to either an installed exe with full path or just plain "python(.exe)" @@ -338,12 +325,12 @@ private void createAndUpload(){ System.out.println("[SPIFFS] reset : "+resetMethod); System.out.println("[SPIFFS] port : "+serialPort); System.out.println("[SPIFFS] speed : "+uploadSpeed); - if (uploadCmd != null && !uploadCmd.isEmpty()) { + if (!uploadCmd.isEmpty()) { System.out.println("[SPIFFS] python : "+pythonCmd); System.out.println("[SPIFFS] uploader : "+uploadCmd); } System.out.println(); - if (uploadCmd != null && !uploadCmd.isEmpty()) { + if (!uploadCmd.isEmpty()) { sysExec(new String[]{pythonCmd, uploadCmd, "--chip", "esp8266", "--port", serialPort, "--baud", uploadSpeed, "write_flash", uploadAddress, imagePath, "--end"}); } else { sysExec(new String[]{esptool.getAbsolutePath(), "-cd", resetMethod, "-cb", uploadSpeed, "-cp", serialPort, "-ca", uploadAddress, "-cf", imagePath});