Skip to content

Commit 502b4a7

Browse files
committed
Initial implementation
1 parent 3e9a58a commit 502b4a7

File tree

9 files changed

+493
-11
lines changed

9 files changed

+493
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ bin/
99
*.json
1010
export.*
1111
test.*
12+
*.jar
1213

1314
*~

build.gradle

+20
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ plugins {
1111
id 'java-library'
1212
}
1313

14+
plugins {
15+
id 'com.palantir.git-version' version '0.12.3'
16+
}
17+
1418
repositories {
1519
// Use jcenter for resolving dependencies.
1620
// You can declare any Maven/Ivy/file repository here.
1721
jcenter()
1822
}
1923

24+
//version gitVersion()
25+
2026
dependencies {
2127
// This dependency is exported to consumers, that is to say found on their compile classpath.
2228
api 'org.apache.commons:commons-math3:3.6.1'
@@ -26,4 +32,18 @@ dependencies {
2632

2733
// Use JUnit test framework
2834
testImplementation 'junit:junit:4.12'
35+
implementation 'org.json:json:20190722'
2936
}
37+
task fatJar(type: Jar) {
38+
manifest {
39+
attributes 'Implementation-Title': project.name,
40+
'Implementation-Version': versionDetails().lastTag,
41+
'Main-Class': 'de.neuwirthinformatik.Alexander.GitJarUpdate.Update'
42+
}
43+
baseName = project.name + '-all'
44+
from {
45+
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }{
46+
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
47+
}
48+
with jar
49+
}

src/main/java/GitJarUpdate/Library.java

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package de.neuwirthinformatik.Alexander.GitJarUpdate;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.URL;
6+
import java.util.Enumeration;
7+
import java.util.jar.Attributes;
8+
import java.util.jar.JarFile;
9+
import java.util.jar.Manifest;
10+
11+
public class Info {
12+
13+
public static enum OS {LINUX,WINDOWS};
14+
15+
public static OS os = detectOS();
16+
public static String VERSION = detectVersions();
17+
18+
private static OS detectOS() {
19+
//detect os
20+
String OpS = System.getProperty("os.name").toLowerCase();
21+
22+
if (OpS.indexOf("win") >= 0) {
23+
return OS.WINDOWS;
24+
//System.out.println("This is Windows");
25+
} else if (OpS.indexOf("mac") >= 0) {
26+
return OS.LINUX;
27+
//System.out.println("This is Mac");
28+
} else if (OpS.indexOf("nix") >= 0 || OpS.indexOf("nux") >= 0 || OpS.indexOf("aix") > 0 ) {
29+
return OS.LINUX;
30+
//System.out.println("This is Unix or Linux");
31+
} else if (OpS.indexOf("sunos") >= 0) {
32+
return OS.LINUX;
33+
//System.out.println("This is Solaris");
34+
} else {
35+
return OS.WINDOWS;
36+
//System.out.println("Your OS is not support!! => Falling back to Windows");
37+
}
38+
}
39+
40+
public static String detectVersions() {
41+
String VERSION = getManifestInfo();
42+
if(VERSION==null)VERSION="DirtyDebug";
43+
return VERSION;
44+
}
45+
46+
public static String getManifestInfo() {
47+
Enumeration resEnum;
48+
try {
49+
resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
50+
while (resEnum.hasMoreElements()) {
51+
try {
52+
URL url = (URL)resEnum.nextElement();
53+
InputStream is = url.openStream();
54+
if (is != null) {
55+
Manifest manifest = new Manifest(is);
56+
Attributes mainAttribs = manifest.getMainAttributes();
57+
String version = mainAttribs.getValue("Implementation-Version");
58+
if(version != null) {
59+
return version;
60+
}
61+
}
62+
}
63+
catch (Exception e) {
64+
// Silently ignore wrong manifests on classpath?
65+
}
66+
}
67+
} catch (IOException e1) {
68+
// Silently ignore wrong manifests on classpath?
69+
}
70+
return null;
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.neuwirthinformatik.Alexander.GitJarUpdate;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.charset.Charset;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardCopyOption;
11+
12+
public class StreamUtil {
13+
14+
15+
public static String readFullyAsString(InputStream inputStream, String encoding) throws IOException {
16+
return readFully(inputStream).toString(encoding);
17+
}
18+
19+
private static ByteArrayOutputStream readFully(InputStream inputStream) throws IOException {
20+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
21+
byte[] buffer = new byte[1024];
22+
int length = 0;
23+
while ((length = inputStream.read(buffer)) != -1) {
24+
baos.write(buffer, 0, length);
25+
}
26+
return baos;
27+
}
28+
public static String readFile(String path)
29+
{
30+
byte[] encoded;
31+
try {
32+
encoded = Files.readAllBytes(Paths.get(path));
33+
return new String(encoded, Charset.defaultCharset());
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
37+
return "";
38+
}
39+
public static void copyFile(String src, String dst)
40+
{
41+
try {
42+
Files.copy(new File(src).toPath(), new File(dst).toPath(),StandardCopyOption.REPLACE_EXISTING);
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package de.neuwirthinformatik.Alexander.GitJarUpdate;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.ArrayList;
5+
import java.util.Calendar;
6+
7+
public class Task {
8+
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.0'0000Z'");
9+
private static int running_tasks = 0;
10+
//public static final Object lockTask = new Object();
11+
public static final Object lockTimer = new Object();
12+
private static final ArrayList<Timer> runs = new ArrayList<Timer>();
13+
14+
static {
15+
start(() -> {
16+
long old_last_time = System.currentTimeMillis();
17+
long last_time = System.currentTimeMillis();
18+
incThreads(-1);//this is not a task
19+
while(true)
20+
{
21+
try {
22+
long sleep = 1000 - (System.currentTimeMillis()-last_time);
23+
if(sleep>0)Thread.sleep(sleep);
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();}
26+
old_last_time = last_time;
27+
last_time = System.currentTimeMillis();
28+
synchronized(lockTimer)
29+
{
30+
int i =0;
31+
while(i < runs.size())
32+
{
33+
if(!runs.get(i).run(last_time - old_last_time))
34+
{
35+
runs.remove(i);
36+
i--;
37+
}
38+
i++;
39+
}
40+
}
41+
}
42+
});
43+
}
44+
45+
46+
public static String time()
47+
{
48+
return sdf.format(Calendar.getInstance().getTime());
49+
}
50+
51+
public static interface Timer
52+
{
53+
public boolean run(long millis);
54+
}
55+
56+
public static synchronized void start(Runnable run)
57+
{
58+
incThreads(1);
59+
new Thread(() -> {run.run();incThreads(-1);}).start();
60+
}
61+
62+
public static void tick(Timer run)
63+
{
64+
synchronized(lockTimer)
65+
{
66+
runs.add(run);
67+
}
68+
}
69+
70+
public static synchronized void incThreads(int i) {
71+
running_tasks += i;
72+
//TUM.log.d("Current Threads: " + running_tasks,"Task");
73+
}
74+
75+
public static synchronized int getThreads() {
76+
return running_tasks;
77+
}
78+
79+
public static void sleep(long millis)
80+
{
81+
try {
82+
Thread.sleep(millis);
83+
} catch (InterruptedException e) {
84+
// TODO Auto-generated catch block
85+
e.printStackTrace();
86+
}
87+
}
88+
89+
90+
public static void sleepForAll()
91+
{
92+
while(getThreads()>0)
93+
try {
94+
Thread.sleep(1000);
95+
} catch (InterruptedException e) {
96+
// TODO Auto-generated catch block
97+
e.printStackTrace();
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)