|
| 1 | +/* |
| 2 | + * This Java source file was generated by the Gradle 'init' task. |
| 3 | + */ |
| 4 | +package de.neuwirthinformatik.Alexander.GitJarUpdate; |
| 5 | + |
| 6 | +import java.awt.Image; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import javax.swing.JOptionPane; |
| 10 | + |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONObject; |
| 13 | + |
| 14 | + |
| 15 | +public class Update { |
| 16 | + |
| 17 | + |
| 18 | + public static void main(String[] args) |
| 19 | + { |
| 20 | + loadUpdate("TeXCalc-all.jar","APN-Pucky","TeXCalc"); |
| 21 | + } |
| 22 | + |
| 23 | + public boolean someLibraryMethod() { |
| 24 | + return true; |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + public static void loadUpdate(String fname, String github_user,String github_project) |
| 29 | + { |
| 30 | + StreamUtil.copyFile("new-" + fname,fname); |
| 31 | + String up = checkUpdates(github_user,github_project); |
| 32 | + if(!up.equals("")) |
| 33 | + { |
| 34 | + int selection = JOptionPane.showConfirmDialog(null, up,"Update",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE); |
| 35 | + if(selection == JOptionPane.OK_OPTION)update(fname,github_user,github_project); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static String checkUpdates(String github_user,String github_project) |
| 40 | + { |
| 41 | + String ret = ""; |
| 42 | + String jsf = "git_jar_version.json"; |
| 43 | + Wget.wGet(jsf, "https://api.github.com/repos/" + github_user + "/" + github_project + "/releases/latest"); |
| 44 | + String tumjson=StreamUtil.readFile(jsf).replaceAll("\n", ""); |
| 45 | + JSONObject tum = new JSONObject(tumjson); |
| 46 | + String tum_tag_name = tum.getString("tag_name"); |
| 47 | + if(!tum_tag_name.equals(Info.VERSION)) |
| 48 | + { |
| 49 | + ret += "New version: " + tum_tag_name + " available:\n"; |
| 50 | + ret += " - " + tum.getString("name") + "\n"; |
| 51 | + ret += "Current version: " + Info.VERSION; |
| 52 | + } |
| 53 | + |
| 54 | + return ret; |
| 55 | + } |
| 56 | + |
| 57 | + public static void update(String fname, String github_user, String github_project) |
| 58 | + { |
| 59 | + //TUM |
| 60 | + System.out.println("Downloading new version ..."); |
| 61 | + //Task.start(() -> Wget.wGet("TUM-new.jar", "https://drive.google.com/uc?export=download&id=0BxMgxg5B0xsDYWhMamxQR0hTZjQ")); |
| 62 | + String jsf = "git_jar_version.json"; |
| 63 | + Wget.wGet(jsf, "https://api.github.com/repos/" + github_user + "/" + github_project + "/releases/latest"); |
| 64 | + String tumjson=StreamUtil.readFile(jsf).replaceAll("\n", ""); |
| 65 | + JSONArray jsa= (new JSONObject(tumjson)).getJSONArray("assets"); |
| 66 | + String name=""; |
| 67 | + for(int i = 0; i < jsa.length(); ++i) { |
| 68 | + JSONObject jso = jsa.getJSONObject(i); |
| 69 | + name = jso.getString("name"); |
| 70 | + if(name.startsWith(fname)) { |
| 71 | + final String nname = name; |
| 72 | + Task.start(() -> { |
| 73 | + Wget.wGet("new-" + nname, jso.getString("browser_download_url")); |
| 74 | + //decryptFile("TUM-enc.jar", "TUM-new.jar"); |
| 75 | + }); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + //Restart |
| 81 | + ProcessBuilder builder = new ProcessBuilder("java", "-jar", "new-" + name); |
| 82 | + builder.redirectError(ProcessBuilder.Redirect.INHERIT); |
| 83 | + builder.redirectOutput(ProcessBuilder.Redirect.INHERIT); |
| 84 | + System.out.println("Waiting for all Processes to end..."); |
| 85 | + Task.sleepForAll(); |
| 86 | + System.out.println("Everything updated. Restarting"); |
| 87 | + try { |
| 88 | + Process p = builder.start(); |
| 89 | + } catch (IOException e) { |
| 90 | + // TODO Auto-generated catch block |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + System.exit(0); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments